var tabArr = new Array();
var barInt;

var img0o = new Image(88, 41);
img0o.src = "/images/b800HomeBtn_ovr.gif";
var img0a = new Image(88, 41);
imag0a = "/images/b800HomeBtn_act.gif";
var img0n = new Image(88, 41);
imag0n = "/images/b800HomeBtn_nor.gif";
//
var img1o = new Image(88, 41);
img1o.src = "/images/optionsBtn_ovr.gif";
var img1a = new Image(88, 41);
imag1a = "/images/optionsBtn_act.gif";
var img1n = new Image(88, 41);
imag1n = "/images/optionsBtn_nor.gif";
//
var img2o = new Image(88, 41);
img2o.src = "/images/featBtn_ovr.gif";
var img2a = new Image(88, 41);
imag2a = "/images/featBtn_act.gif";
var img2n = new Image(88, 41);
imag2n = "/images/featBtn_nor.gif";

$(document).ready(function () {
    $('body').supersleight();

    $(".mouseOver").hover(function () {
        if ($(this).attr("src").indexOf('-ovr') == -1) {
            $(this).attr("src", $(this).attr("src").split(".").join("-ovr."));
        }
    }, function () {
        if (!$(this).hasClass('muActive')) {
            $(this).attr("src", $(this).attr("src").split("-ovr.").join("."));
        }
    });

    //Remove the Raceway and Trolley logos from the left column on all the international busway pages.
    var thisURL = window.location.href;
    if (thisURL.indexOf("/intl/") == -1) {
        $('.nonIntlLogo').css('display', 'block');
    }

    bind_culture_dropdown();
    $.metadata.setType("attr", "validate");
    $("ul.sf-menu").superfish({ autoArrows: false, dropShadows: false });

    $('#frmRegistration').validate();

    // Datepicker
    $('.datepicker').datepicker({
        inline: true
    });

    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
		function () { $(this).addClass('ui-state-hover'); },
		function () { $(this).removeClass('ui-state-hover'); }
	);

    //stretch the bar at the top of the page
    barInt = setInterval(checkBar, 50);

    //handle gloabal image rollovers
    $(".hover").hover(function () {
        $(this).attr("src", $(this).attr("src").split("_nor.").join("_ovr."));
    }, function () {
        $(this).attr("src", $(this).attr("src").split("_ovr.").join("_nor."));
    });

    //create the tab array 
    tabArr.push("tabBtnCPMHome");
    tabArr.push("tabBtnOptions");
    tabArr.push("tabBtnSpecs");

    //hide tab content initially
    for (var i = 1; i < tabArr.length; i++) {
        $("#tab" + i).hide();
    }

    //set active content tab
    setActiveTab(0);

    //youtube embed
    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/pagiMi5HS4M&enablejsapi=1&playerapiid=ytplayer",
                       "ytapiplayer", "575", "383", "8", null, null, params, atts);


    //divider padding size repair
    if ($.browser.msie && $.browser.version == "6.0") {
        $("#tabBlueDivider").width(298);
    }

    var DCbnr_attributes = {};
    var DCbnr_params = {
        wmode: "transparent",
        base: "swf/"
    };
    swfobject.embedSWF("/busway/swf/ue_dcPulse_390x90.swf", "DCbnr", 390, 90, "8.0.0", "", DCbnr_params, DCbnr_attributes);

    $("#province").hide();
    toggle_action();
    $("#country_list").change(function () {
        toggle_action();
    });

    //Change tab images in busway to match languages
    if (currentLang.length > 0) {
        $('.eventsHeader').css('background-image', 'url(/busway/intl/' + currentLang + '/images/hdrEvents.png)');
        $('.linksHeader').css('background-image', 'url(/busway/intl/' + currentLang + '/images/hdrQuickLinks.png)');
        $('.newsHeader').css('background-image', 'url(/busway/intl/' + currentLang + '/images/hdrRecentNews.png)');
    }
});

function toggle_action() {
	if ($("#country_list").val() == "US") {
		$("#province").hide()
		$("#state_list").slideDown()
	} else {
		$("#province").slideDown()
		$("#state_list").hide()
	}
}

function closeAndThank(myURL) {
	//setTimeout(function() { parent.$.fn.colorbox.close(); }, 6000);
	setTimeout(function () { $.fn.colorbox({ width: "250px", height: "190px", open: true, iframe: true, href: myURL }); }, 700);
	setTimeout(function () { parent.$.fn.colorbox.close(); }, 6000);
}

function openCB(myText) {
	setTimeout(function () { $.fn.colorbox({ width: "290", height: "300", open: true, html: myText }); }, 900);
	setTimeout(function () { $.fn.colorbox.close(); }, 6000);
}

function bind_culture_dropdown() {
	var s = $("#flag img").attr("src");

	$("#flags img").each(function () {

		if ($(this).attr("src") == s) {
			$(this).parent().parent().hide()
		}
	})


	$("#culture_area").hoverIntent(
		function () {
			$("#flags").slideDown("fast");
		},
		function () {
			$("#flags").slideUp("fast");
		}
	);
}

$(window).resize(function () {
    checkBar();
});

function checkBar() {
    if ($('#mainContent').offset() != null || $('#mainContent').offset() != undefined) {
        $("#bgTrackBarImage").width($("#mainContent").offset().left);
    }
}

function setActiveTab(n) {
    if ($("#" + tabArr[n]).attr("src") != null) {
        $("img[id*='tabBtn']").each(function (index) {
            $(this).attr("src", $(this).attr("src").split("_act").join("_nor"));
        });

        for (var i = 0; i < tabArr.length; i++) {
            if (i != n) {
                $("#tab" + i).hide();
            }
        }

        $("#" + tabArr[n]).attr("src", $("#" + tabArr[n]).attr("src").split("_nor").join("_act"));
        $("#" + tabArr[n]).attr("src", $("#" + tabArr[n]).attr("src").split("_ovr").join("_act"));

        $("#tab" + n).show();
    }
}

function verlicon(data) {
	if (window.console && window.console.firebug) {
		//Firebug is enabled
		console.log(data);
	} else if (window.console) {
		console.log(data);
	}
}
