/// <summary>The ScopeMenu class adds the javascript logic for creating our scope lightbox selectors.
var scopeImageCrossFader;

$(document).ready(function() { 
    //If franchise level banners exist, then hide group level banner container.  
    //Otherwise hide franchise level banner container and show available group level banner.
    OverrideFranchiseLevelBanners();

	//First turn off all our images.
	$("div#scopeLogoContainer > img").hide();
	
	//Turn off all dealer link panels and other dealer panel items.
	//$("div#scopeDealershipContainer > div").hide();
	$("a#scopeCloseDealerList").hide();
	
	
	if($("div#homePageScopeMenuContainer").length > 0)
	{
		//If we are on the hompage then start the animation straight away.
		$("div#scopeLogoContainer > img:first").fadeIn("slow", startCrossFader());
		//If we are on the homepage then hide the select manufacturer link.
		$("a#aManufacturerScopeDisplay").parent("li").hide();
	}
	
	$("a#aManufacturerScopeDisplay").click(
	//$("div#TB_window").show(
		function () {
			//First turn off all our images.
			$("div#scopeLogoContainer > img").hide();
			//Turn the first logo image on and animate through them periodically.
			$("div#scopeLogoContainer > img:first").fadeIn("slow", startCrossFader());
			//$("div#scopeLogoContainer > img:first").show(null, startCrossFader());
		}
	);
	
	
	$("a#TB_closeWindowButton").click(
	//$("div#TB_window").hide(
		function () {
			//Clear our cross fade animation.
			clearInterval(scopeImageCrossFader);
		}
	);


	//Add a rollover script to each of the manufacturer links, to display it's associated logo.
	$("ul#scopeLinkContainer > li > a").hover(
		function () {
			if (isDealershipContainerActive()) return;
			var mfrKey = $(this).attr("id").replace("scopeFranchiseLink","");
			//Clear our cross fade animation.
			clearInterval(scopeImageCrossFader);
			//hide all our logo images.
			$("div#scopeLogoContainer > img").hide();
			//Show the logo associated with the selected manufacturer.
			$("div#scopeLogoContainer > img[alt=" + mfrKey + "]").show();
		}, 
		function () {
			if (	(!hasDealershipContainer()) ||
					(!isDealershipContainerActive())) startCrossFader();
		}
	); 
	
	//At the group level make the click load the dealer info rather than going direct to the website.
	$("ul#scopeLinkContainer > li > a").click(
		function () {
			//If we are at the franchise level we have no dealer list so just link sraight to the website.
			//Porsche guidlines force us to load the porsche holing page from any link so we hack that fix in here.
			if (	(!hasDealershipContainer()) || 
					($(this).attr("id") == "scopeFranchiseLinkporsche") ||
					($(this).attr("id") == "scopeFranchiseLinkalpina")) return true;
			
			var mfrKey = "scopeDealers" + $(this).attr("id").replace("scopeFranchiseLink","");
			//hide all our logo images.
			$("div#scopeLogoContainer > img:visible").hide();
			
			//Hide any open dealer details.
			$("div#scopeDealershipContainer > div:visible").hide();
			
			//Show the dealers associated with the selected manufacturer.
			$("div#scopeDealershipContainer > div#" + mfrKey).show();
			
			$("a#scopeCloseDealerList").show();
			return false;
		}
	); 
	
	//At the group level allow the user to close the active dealer list manually.
	$("a#scopeCloseDealerList").click(
		function () {
			//Hide our active dealer list.
			$("div#scopeDealershipContainer > div:visible").hide();
			$("a#scopeCloseDealerList").hide();		
			//Now turn the first logo image on and animate through them periodically.
			$("div#scopeLogoContainer > img:first").fadeIn("slow", startCrossFader());
			
			//Reset backgroundImage on container
			
			return false;
		}
	); 
});

function hasDealershipContainer()
{
	return ($("div#scopeDealershipContainer").attr("id"));
}

function isDealershipContainerActive()
{
	return ($("div#scopeDealershipContainer > div:visible").attr("id"));
}

function startCrossFader()
{
	clearInterval(scopeImageCrossFader);
	scopeImageCrossFader = setInterval("doCrossFade()", 3000);
}

function doCrossFade()
{
	//if(!$("div#scopeLogoContainer > img")) return;
	var activeImage = $("div#scopeLogoContainer > img:visible");
	var nextImage = (!activeImage.next().attr("id")) ? $("div#scopeLogoContainer > img:first") : (activeImage.next());
	activeImage.fadeOut("slow");
	nextImage.fadeIn("slow");
	//activeImage.hide();
	//nextImage.show();
} 

function OverrideFranchiseLevelBanners()
{
    var siteId = $("input#hdnSiteType").val();
    if (siteId == "Franchise")
    {      
        var groupOverrideBanner = $("div.siteMenuColumn div.groupoverridebanner");        
        if (groupOverrideBanner)
        {
            var panelCount = 0; 
            $("div.siteMenuColumn").children("div.swfContainer").each(function() {
                var swfContainer = $(this);
                if (!swfContainer.hasClass("groupoverridebanner")) panelCount++;                
            });
            if (panelCount == 0) groupOverrideBanner.show();
        }
    }
}
 
