﻿// JScript File

/*Animation speeds in milliseconds*/
var backgroundSpeed = 750;
var backgroundSpeedIn = 1000;
var contentSpeed = 500;
var m_sender;

/*Adds click handlers to the main menu*/
function initMainMenu()
{
    $('#main-menu li a').click
    (
        function()
        {
	        handleMainMenuClick(this);
			return false;
        }
    )
    $('#additional-links a').click
    (
        function()
        {
	        handleMainMenuClick(this);
			return false;
        }
    )
}

/*handles a click on the main menu*/
function handleMainMenuClick(sender)
{
    var pageName = $(sender).text();
    
    debuglog("handleMainMenuClick");
    handleBackground($(sender));
    handleContent($(sender), pageName);
    handleNav($(sender));
    handleAnalytics('Main/' + pageName);
}

/*animates a change in background image when an item on the main menu is clicked */
function handleBackground(sender)
{
    var newclass = "background-bum";
    var selected = $(sender).parent().attr("id");
    //debuglog(selected);
    /*determined the new css class that should be used dependent on selected link*/
    if(selected == "main-menu-training")
    {
        newclass = "background-pecs";
    }
    else if(selected == "main-menu-quickfix")
    {
        newclass = "background-bum";
    }
    else if(selected == "main-menu-products")
    {
        newclass = "background-tummy";
    }
    else if(selected == "main-menu-press")
    {
        newclass = "background-legs";
    }
    else if(selected == "main-menu-resigym")
    {
        newclass = "background-arm";
    }

    /*ensures that background has 100% opacity before commencing animation*/
    $('#background').css("opacity", "1");
    /*commence fade out*/
    $('#background')
        .animate
        (
            {opacity: "0"},
            backgroundSpeed,
            "linear",
            function()
            {
                /*this is a callback function that is called when the fade out has completed*/

                /*remove class/background image*/
                $('#background').removeClass()
                /*pause*/
                .animate({opacity: "0"}, 200) 
                /*change class/background image*/
                .addClass(newclass)
                /*commence fade in (queued)*/
                .animate({opacity: "1"}, backgroundSpeedIn, "linear");
            }
        )
}

function handleContent(sender, pageName)
{
    m_sender = sender;
    /*animates the main content out, replaces the content using ajax, sets up the new content, animates the new content in*/
    
    if ($("#main-content-bg").css("display") == "none")
    {
        $('#main-content-bg').css({height: "0px"});
        handleAJAXCall();
    }
    else
    {
        /* animate out (shrink) */
        $('#main-content-bg')
            .animate
            (
                {height: "0px"},
                contentSpeed,
                "linear",
                handleAJAXCall
            )
    }
    
}

function handleAnalytics(pageName) 
{
	//TB: Apparently analytics requires the forward slash before page names... http://www.google.com/support/googleanalytics/bin/answer.py?answer=55520
    pageTracker._trackPageview('/' + pageName);
    //debuglog('Analytics - ' + pageName);
}


function handleAJAXCall()
{
    var sender = m_sender;

    /*hide container*/
    $('#main-content-bg').css("visibility", "hidden");
    
    /*change content of container using AJAX, we analyse the link that has been clicked to determine which url to load the new content from*/
    debuglog("sender=" + sender);
    debuglog("sender1=" + sender.attr("href"));
    $("#main-content-bg").load(sender.attr("href") + " #main-content", 
        function()
        {
            debuglog("sender2=" + sender.attr("href"));
            location.hash = convertToHash(sender.attr("href"));
            currentHash = location.hash;
            
            /*set up the tabs on the new content*/
            initTabs();
            /*set up the scrol bars on the new content*/
            initScrollBars();
            
            /*IE6 compatibility for PNGs on new content*/
            $("#main-content").pngFix();
            addFormListeners();
            
            /*ensure height is 0 before commencing animation */
            $('#main-content-bg').css("height", "0px");
            /*make element visible before commencing animation */
            $('#main-content-bg').css("visibility", "inherit");
            /*fade back in*/
            if ($("#main-content-bg").css("display") == "none")
            {
                $('#main-content-bg').css({height: "268px"});
            }
            else
            {
                $('#main-content-bg').animate({height: "268px"}, contentSpeed, "linear");
            }
        }
    );
}

function handleNav(sender)
{
	if(sender.parent().attr("id") == "additional-links") {
		/*handle underlining of elements when nav items are selected*/
		$("#main-menu .selected").removeClass("selected");
	} else {
		$(sender).parent().parent().children(".selected").removeClass("selected");
		$(sender).parent().addClass("selected");
	}
}
		
function debuglog(logtext)
{
	try
	{
		console.log(logtext);
	}
	catch(err)
	{
	}
}