//global variables used on AJAX postback to decide if we want to initialise the accordian or not
var postbackElement;
var buttonsToInitAccordianOn = "";
var AJAXPostbackButtonServerID = "";//server id of the postback button (so without all the naming containers at the front)
var disableOnPostbackButtonServerID = ""; //sets wich button to disable on ajax post back (so user can't keep clicking it)
var swfContainerServerID = ""; //server id of the div to contain the flash swf
var loadFlashSourceFileOnAJAXPostback = false; //used to check if we want to load a new source file into the flash swf
var xmlSourceURLToLoadOnAJAXPostback = ""; //the xml source url to load into the section home flash control when we have performed an AJAX postback
var flashSourceFile = ""; //variable used to hold the source of the swf to load
var serverIDAndControlToFadeInOnAJAXPostback = new Object(); //dictionary (Key:ServerID Of postback control, Value:Jquery id of elment to fade in).
var serverIDAndGoogleGoalToTrack = new Object;//dictionary (Key:ServerID Of postback control, Value:Google Goal name).
var serverIDAndGooglePageviewURLToTrack = new Object();//dictionary (Key:ServerID Of postback control, Value:Google Pageview URL).

jQuery(document).ready(function () {
    InitSearchBox();
    InitSitemapHover();
    InitTimeline();
    InitColumise();
    ShowAccordian();
});

function LoadPage()
{
    AddAJAXEndRequestHandler();
}

function InitSearchBox()
{
    // Search box value switching
	jQuery("#searchBox").focus(function() {
		var currentVal = jQuery("#searchBox").val();
		if (currentVal == "Enter a search term") {
			jQuery("#searchBox").val("");
			jQuery("#searchBox").css("color","#666666");
		}
	});
	jQuery("#searchBox").blur(function() {
		var currentVal = jQuery("#searchBox").val();
		if (currentVal == "") {
			jQuery("#searchBox").val("Enter a search term");
			jQuery("#searchBox").css("color","#cccccc");
		}
	});
}

function ShowAccordian()
{
    jQuery('#subnav_products').show();
}

function InitAccordian()
{
	//Subnav Accordians
	ShowAccordian();
	jQuery('.accordian ul').hide();
	jQuery('.accordian li a').click(
		function() {
		
		    if(this.href == "javascript:void(0);") {
		    
			    jQuery('.accordian li').removeClass("selected")
    			
			    var checkElement = jQuery(this).next();
			    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			        jQuery(this).parent("li").addClass("selected");
				    return false;
			    }
			    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    			    
				    jQuery('.accordian ul:visible').slideUp('normal');
				    checkElement.slideDown('normal');
				    jQuery(this).parent("li").addClass("selected");
				    return false;
    				
			    }
			}
			else {
			    return true;
			}
			
		}
	);	
}

function ExpandAccordianSection(accordianSectionID)
{
	jQuery('.accordian li').removeClass("selected")
	
	var elementJQuery = '.accordian li#' + accordianSectionID;
	var liClicked = jQuery(elementJQuery);
	var children = jQuery(elementJQuery).children('ul');
	var checkElement = jQuery(children[0]);
	
	if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		return false;
	}
	if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		jQuery('.accordian ul:visible').slideUp('normal');
		checkElement.slideDown('normal');
		checkElement.parent("li").addClass("selected");
		return false;	
	}
}

function InitColumise()
{
    // Columnise functions
	if (jQuery(".twocolumns")) {jQuery('.twocolumns').columnize({width:250});}

	if (jQuery(".threecolumns")) {jQuery('.threecolumns').columnize({width:166});}
}

function InitTimeline()
{
	jQuery("#timeline .row").mouseover(function() {
		jQuery(this).addClass("over");
	});
	jQuery("#timeline .row").mouseout(function() {
		jQuery(this).removeClass("over");
	});
}

function InitSitemapHover()
{
    // Sitemap hover
    /*
	jQuery("#sitemap .column").mouseover(function() {
		jQuery(this).addClass("current");
	});
	jQuery("#sitemap .column").mouseout(function() {
		jQuery(this).removeClass("current");
	});*/
}

function LoadHomeSectionFlashXML(swfPath, xmlSourceURL)
{
    //only reload the flash if we have set this flag in the serverside code (stops timers on the page reloading the flash)
    if(loadFlashSourceFileOnAJAXPostback)
    { 
        <!-- //
        var params = new Object();
        var flashvars = { 
            xmlImport:xmlSourceURL
        };

        if(xmlSourceURL != "")
        {
            swfobject.embedSWF(swfPath,swfContainerServerID,"539","357","8.0.0.0","/swf/expressInstall.swf",flashvars,params);
        }
        //-->
        //set the flag to false so other postbacks don't cause us to reload the flash
        loadFlashSourceFileOnAJAXPostback = false;
    }
}

function AddAJAXEndRequestHandler() 
{
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

function BeginRequestHandler(sender, args)
{//we have a problem that when a user goes to the homepage and clicks on the products extremly fast this "BeginRequestHandler" does not fire before "EndRequestHandler"
    postbackElement = args.get_postBackElement();
    var temparr = postbackElement.id.split("_");
    
    //ASP.net server id of button posted back
    AJAXPostbackButtonServerID = temparr[temparr.length -1]
    
    //disable submit button
    DiablePostbackButton(postbackElement, true);
}

function EndRequestHandler(sender, args)
{
    //initialise the accordian again after AJAX postback
    //buttonsToInitAccordianOn gives us the ability to decided if we want to call the initAccordian function for the AJAX postback or not
    if(buttonsToInitAccordianOn.indexOf("#" + AJAXPostbackButtonServerID + "#") != -1)
    {
        InitAccordian();
    }
    
    //enable submit button
    DiablePostbackButton(postbackElement, false);
    
    //load flash xml source
    LoadHomeSectionFlashXML(flashSourceFile, xmlSourceURLToLoadOnAJAXPostback);
        
    //fade in the elements set
    FadeIn(AJAXPostbackButtonServerID);
    
    //track goals with google
    GoogleGoalTracking(AJAXPostbackButtonServerID);
    
    //track pageview with google
    GooglePageviewURLTracking(AJAXPostbackButtonServerID);
}

function FadeIn(AJAXPostbackButtonServerID)
{
    //check if the piostback control is in our dictionary
    if(serverIDAndControlToFadeInOnAJAXPostback[AJAXPostbackButtonServerID] !== undefined)
    {
        var element = jQuery(serverIDAndControlToFadeInOnAJAXPostback[AJAXPostbackButtonServerID]);
    
        element.hide();
        element.fadeIn("slow");
    }
}


function DiablePostbackButton(button, bDisable)
{
    //check if we have chosen to disable the current postback control
    if(disableOnPostbackButtonServerID.indexOf("#" + AJAXPostbackButtonServerID + "#") != -1)
    {
        postbackElement.disabled = bDisable;
    }
} 

function GoogleGoalTracking(AJAXPostbackButtonServerID)
{  
    //check if the piostback control is in our dictionary
    if(serverIDAndGoogleGoalToTrack[AJAXPostbackButtonServerID] !== undefined)
    {
        pageTracker._trackEvent('Goals',serverIDAndGoogleGoalToTrack[AJAXPostbackButtonServerID]);
        //alert(serverIDAndGoogleGoalToTrack[AJAXPostbackButtonServerID]);
    }
}

function GooglePageviewURLTracking(AJAXPostbackButtonServerID)
{  
    //check if the piostback control is in our dictionary
    if(serverIDAndGooglePageviewURLToTrack[AJAXPostbackButtonServerID] !== undefined)
    {
        pageTracker._trackPageview(serverIDAndGooglePageviewURLToTrack[AJAXPostbackButtonServerID]);
        //alert(serverIDAndGooglePageviewURLToTrack[AJAXPostbackButtonServerID]);
    }
}