$(document).ready(function()
{
	//$.loadRandomBackgroundImage(2); // Argument is the number of background images to choose from.  File naming convention is bg.1250x980.<index>.jpg
    $.preloadImages(
		'/chicagotrolley/SiteBase/wrap/theme0/images/nav01_on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/nav02_on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/nav03_on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/nav04_on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/nav05_on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/caret.leftnav.secondary.on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/caret.leftnav.tertiary.on.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/arrow_white.gif',
		'/chicagotrolley/SiteBase/wrap/theme0/images/flyout.buyticketsonline.on.png',
		'/chicagotrolley/SiteBase/wrap/theme0/images/flyout.getaquote.on.png'
	);
	$.initFrmEmailSignup();
	$.initLeftnav();
	$.initHomepageTabs();
	$.initActionButtons();
	$.fixupMissingCssSupportInIE();
});

jQuery.fixupMissingCssSupportInIE = function()
{
    var userAgent = navigator.userAgent.toLowerCase();

    var browser = {
        version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
        safari: /webkit/.test( userAgent ),
        opera: /opera/.test( userAgent ),
        msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
        mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	}	
	if (browser.msie && browser.version < 8)
	{
		// Go through and set alternate styles for anything we were using :before and :after pseudo classes on
		$(".txtTestimonial").each(function()
		{
			$(this).html('"' + $(this).html() + '"');
		});
		$(".requiredField").each(function()
		{
			$(this).append($("<img>").attr("src", "/chicagotrolley/SiteBase/wrap/theme0/images/red.star.gif"));
		});
		$(".requiredFieldLegend").each(function()
		{
			$(this).prepend($("<img>").attr("src", "/chicagotrolley/SiteBase/wrap/theme0/images/red.star.gif"));
		});
	}
}

jQuery.initHomepageTabs = function()
{
	$("#boxLeftColumnHomeTopData").tabs();
	$("#boxLeftColumnHomeBottomData").tabs();
}

/*
 * Preload all navigation images
 */
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

/*
 * Select and display random background image
 */
jQuery.loadRandomBackgroundImage = function()
{
	var iNumImages = arguments[0]; // argv[0]=number of images to choose from
	var iRndImageIndex = Math.floor(Math.random() * iNumImages) + 1;
	$("#boxContainerBodyBackground").css("background-image", "url(/chicagotrolley/SiteBase/wrap/theme0/images/bg.1250x980." + iRndImageIndex + ".jpg)");
}

/*
 * Initialize the wrapper utility bar email signup form
 */
jQuery.initFrmEmailSignup = function()
{
	var sDefaultText = "enter email address";
	$("#txtEmailAddress").val(sDefaultText);
	$("#txtEmailAddress").focus(
        function()
        {
			if (this.value == sDefaultText)
			{
				this.value = '';
			}
        }).blur(
        function()
        {
			if (this.value == "")
			{
				this.value = sDefaultText;
			}
        });
    $("#frmEmailSignup").submit(
    function()
    {
		if (this.txtEmailAddress.value == sDefaultText)
		{
			this.txtEmailAddress.value = "";
		}
    });
}

/*
 * Initialize functions for the left nav
 */
jQuery.initLeftnav = function() {
    $("li.leftNavItemSecondaryOff").each(function()
    {   
        $(this).click(
        function()
        {
            var sHref = $(this).find("a").attr("href");
            if (sHref != null && sHref != "")
                location.href = sHref;
        });
        $(this).hover(
        function()
        {   
            $(this).removeClass("leftNavItemSecondaryOff");
            $(this).addClass("leftNavItemSecondaryOver");
        },    
        function()
        {   
            $(this).removeClass("leftNavItemSecondaryOver");
            $(this).addClass("leftNavItemSecondaryOff");
        }); 
    }); 
	$("li.leftNavItemTertiaryOff").each(function()
    {   
        $(this).click(
        function()
        {
            var sHref = $(this).find("a").attr("href");
            if (sHref != null && sHref != "")
                location.href = sHref;
        });
        $(this).hover(
        function()
        {   
            $(this).removeClass("leftNavItemTertiaryOff");
            $(this).addClass("leftNavItemTertiaryOn");
        },    
        function()
        {   
            $(this).removeClass("leftNavItemTertiaryOn");
            $(this).addClass("leftNavItemTertiaryOff");
        }); 
    }); 
}

/*
 * Initialize functions for the left nav
 */
jQuery.initActionButtons = function() {
	// Set up alias for some bug on booking step 1 in FF
	try
	{
		prdBooking = document.prdBooking;
	}
	catch (ex) {}

    // Booking engine button replacement
    $("#boxLegacyApp input[type='image']").each(function()
    {
		// Alt tag will become the text on our replacement button.
		// This assumes that the booking engine image buttons all have alt tags.
		// This also assumes that all action buttons are in the following location.
		if (this.src.toLowerCase().indexOf("/coachusabase/wrap/theme0/images") > 0)
		{
			var sBtnText = this.alt;
			var sOnClick = $(this).attr("onclick");
			var sNewInputHtml = $('<input type="submit" value="'+sBtnText+'" class="btnActionButtonOff"/>');
			$(sNewInputHtml).click(sOnClick);
			$(this).after(sNewInputHtml).remove();
		}
    });
    
    $("#boxLegacyApp img").each(function()
    {
		// Alt tag will become the text on our replacement button.
		// This assumes that the booking engine image buttons all have alt tags.
		// This also assumes that all action buttons are in the following location.
		if (this.src.toLowerCase().indexOf("/coachusabase/wrap/theme0/images") > 0)
		{
            if (this.src.toLowerCase().indexOf("icon_gift.gif") > 0 || this.src.toLowerCase().indexOf("btn_exclamation.gif") > 0)
            {
                return false;
            }
			var elParentAnchor = $(this).parents("a");
			$(elParentAnchor).attr("class", "txtActionButton");
			var sBtnText = this.alt;
			var sNewInputHtml = '<span>'+sBtnText+'</span>';
			$(this).after(sNewInputHtml).remove();
		}
    });

    $("input.btnActionButtonOff").each(function()
    {   
        $(this).hover(
        function()
        {   
            $(this).removeClass("btnActionButtonOff");
            $(this).addClass("btnActionButtonOn");
        },    
        function()
        {   
            $(this).removeClass("btnActionButtonOn");
            $(this).addClass("btnActionButtonOff");
        }); 
    }); 
}



