﻿//Define global variables
var xmlDoc;
if (window.ActiveXObject) {
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
} else {
	xmlDoc=document.implementation.createDocument("","",null);
}
var xmlObj;
var xmlLoaded = false;
var fontsLoaded = false;

var previousSection = 0;
var currentSection = 0;
var vidTimelineID = 0;
var sectionNum = 0;
var sectionPageNum = 0;
var autoPlay = true;
var xamlRoot;
var mainNavTimerID;
var mainNavItemOn;
var sections = ["1","1_1", "2", "2_1", "2_2", "2_3", "3", "3_1", "3_2", "3_3", "4", "4_1", "4_2", "4_3", "5", "5b", "5_1"];
var sectionTitles = ["","The Ultimate Server Solution:", "", "Virtualization:", "Virtualization:", "Virtualization:", "", "Web Serving:", "Web Serving:", "Web Serving:", "", "Data Management:", "Data Management:", "Data Management:", "", "Powerful Results:", "Powerful Results:"];
var subSectionTitles = ["","A History of Innovation", "", "Consolidation ", "Centralization", "Compatibility", "", "Workload-Ready", "Flexible Resource Utilization", "Easily Managed", "", "Enhanced Performance", "Secure Control", "Readily Scalable", "", "Conclusion", "Resource Center"];
var mainsectionTitles = ["cvsMainNav1Title","cvsMainNav2Title","cvsMainNav3Title","cvsMainNav4Title","cvsMainNav5Title"];
var contentSectionTitles = ["subMenu1Item1Title","subMenu2Item1Title","subMenu2Item2Title","subMenu2Item3Title","subMenu3Item1Title","subMenu3Item2Title","subMenu3Item3Title","subMenu4Item1Title","subMenu4Item2Title","subMenu4Item3Title","subMenu5Item1Title"];

//Tracking arrays
var topNavTrackingCodes = ["ultimateserversolution_top_nav","historyofinnovation_top_nav", "virtualization_top_nav", "v_consolidation_top_nav", "v_centralization_top_nav", "v_compatibility_top_nav", "webserving_top_nav", "w_workload_top_nav", "w_flex_resouces_top_nav", "w_easily_managed_top_nav", "datamanagement_top_nav", "dm_enhanced_perfom_top_nav", "dm_secure_control_top_nav", "dm_readily_scale_top_nav", "powerful_results_top_nav", "", "resource_center_top_nav"];
var pageTrackingCodes = ["ultimateserversolution_page","historyofinnovation_page", "virtualization_page", "v_consolidation_page", "v_centralization_page", "v_compatibility_page", "webserving_page", "w_workload_page", "w_flex_resouces_page", "w_easily_managed_page", "datamanagement_page", "dm_enhanced_perfom_page", "dm_secure_control_page", "dm_readily_scale_page", "powerful_results_page", "", "resource_center_page"];

//For resources page (URL array, resourceLinkCodes, below it, correlates to this
var resourceLinks = ["http://www.microsoft.com/windowsserver2008/default.mspx","http://www.amd.com/opteron","http://www.amd.com/amplify_business","http://www.microsoft.com/silverlight/why-compelling.aspx","http://www.microsoft.com/windowsserver2008/default.mspx","http://wheretobuy.amd.com/servers.html","http://www.microsoft.com/silverlight/why-compelling.aspx","http://multicore.amd.com/us-en/benchmarks/","http://multicore.amd.com/us-en/quadcore/","http://www.amd.com/opteronserverpowermgmt/","http://www.microsoft.com","http://www.amd.com"];
var resourceLinkCodes = ["rc_msft_server_link","rc_opteron_link","rc_amd_lh_pages_link","rc_silverlight_link","rc_shop_msft_server_link","rc_shop_opteron_link","rc_shop_silverlight_link","rc_benchmark_elearn_link","rc_qc_elearn_link","rc_power_elearn_link","rc_microsoft_corp_link","rc_amd_corp_link"];

var navMenuVisible = false;

//video global elements
var downloader;

//global var for video
var vidElement;
var vidVolume = 0.5;
var vidToStart = -1;
var currentPlayingVid = -1;
var token;
var vidBuffered = false;
var playHiddenLastSection = false;

//Global states
var ccOn = false;
var inTransition = false;
var mainAnimationPlaying = true;
var mouseOverItem = "";

var titleTextAreaWidth = 342;

if (!window.Longhorn_latest)
	window.Longhorn_latest = {};

Longhorn_latest.Page = function() 
{
}

Longhorn_latest.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{	    
            
		//Load XML
		loadXML('xml/playlist.xml');

	    xamlRoot = rootElement;
	    
		this.control = control;
		
        initiateControls(rootElement);
		
		//Download FONTs here
	    var downloader = xamlRoot.getHost().createObject("downloader");
	    downloader.addEventListener("downloadfailed", downloader_DownloadFailed);
	    downloader.addEventListener("completed",downloader_Completed);
	    downloader.open("GET", "fonts/fonts.zip");
	    downloader.send();
	    
		// Sample event hookup:	
		//rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	},
	
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("Storyboard1").Begin();
	}
}



    loadXML = function(xmlFile) 
    {
        if (window.ActiveXObject) {
            xmlDoc.onreadystatechange = verifyXMLIE;
            xmlDoc.async = "false";
            xmlDoc.load(xmlFile);
        } else {
            xmlDoc.onload = verifyXMLFF;
            xmlDoc.load(xmlFile);
	    }
	    
	    
	    if (fontsLoaded)
	    {
	        startOpeningAnimation();
	    }
    }
    
    verifyXMLIE = function() 
    {
         xmlLoaded = true;
         //alert("Ready state? " + xmlDoc.readyState);
         if (xmlDoc.readyState == 4){
  	        xmlObj = xmlDoc.documentElement;
  	        //createMainNav();
		    //typeSetup();
         }
    }
    verifyXMLFF = function() 
    {
        //alert("loaded?");
        xmlLoaded = true;
	    xmlObj = xmlDoc.documentElement;
	    //createMainNav();
	    //typeSetup();
    }

    //Initiate the controls at the bottom left.
    function initiateControls(rootElement)
    {
        
	   
        //Hide the nav
        rootElement.findName("cvsNav").visibility = "collapsed"; //"visible";
        navfly = rootElement.findName("cvsMainNavItems");
        navfly["Canvas.Left"] = -906.0;
        navfly["IsHitTestVisible"] = false;
        
        arrow = xamlRoot.findName("subnavArrow");
        arrow.visibility = "Collapsed";
        
        disableButton(rootElement.findName("cvsBackButton"));
        rootElement.findName("cvsPauseButton").visibility = "collapsed";
        //rootElement.findName("cvsSoundOn").visibility = "visible";
        
        //Guides!!!! Remove these before final
   		//rootElement.findName("imgGuide").visibility = "collapsed";
   		//rootElement.findName("imgGuide2").visibility = "collapsed";
   		
   		/*
		rootElement.findName("cvsSoundOn").visibility = "visible";
		rootElement.findName("cvsBackButtonOn").visibility = "collapsed";
		rootElement.findName("cvsForwardButtonOn").visibility = "collapsed";
		rootElement.findName("cvsPlayButtonOn").visibility = "collapsed";
		*/
		disableAllButtons();
		//rootElement.findName("cvsSection1").visibility = "collapsed";
		
		//Hide all other sections besides home
		for (i=1;i<sections.length;++i)
		{
		    var sectionName = "cvsSection"+sections[i];
		    thisSection = rootElement.findName(sectionName);
		    thisSection["Visibility"] = "Collapsed";
		}
		
		//video downloader
		downloader = rootElement.GetHost().createObject("downloader");
	
	    //Set the first section
	    currentSection = rootElement.findName("cvsSection"+sections[sectionNum]);	
	    
	    //Video stuff
	    xamlRoot.findName("headerVid").stop();
		    
    }
    
    function startOpeningAnimation()
    {
        
        clearCCText();
        xamlRoot.findName("PageContent")["Visibility"] = "Visible";
        xamlRoot.findName("cvsLoading")["Visibility"] = "Collapsed";
        //
        var sbsn = xamlRoot.FindName("Home");
	    sbsn.Stop();
	    sbsn.Begin();
    }
    
    function openingAnimationDone(sender, args)
	{
	    //Play GuyFlicker
	    //alert("Flicker guy");
	    var guyImage = sender.findName("homeGuy"+(sections[sectionNum]));
	    guyImage.visibility = "Visible";
	    
	    var sbsn = sender.FindName("GuyFlicker"+(sections[sectionNum]));
	    sbsn.Stop();
	    sbsn.Begin();
	}
	
	function GuyFlickerComplete(sender, args)
	{
	    
	    clearCCText();
	    vidElement = sender.findName("vid"+sections[sectionNum]);
        //alert("guy done, starting video: " + vidElement.name);
        
        var startVidIndex = -1;
        for(var x=0; x<xmlObj.childNodes.length; x++)
            {
                if (xmlObj.childNodes[x].nodeType == 1)
                {
                    if (xmlObj.childNodes[x].getAttribute("id") == sections[sectionNum])
                    {
                        //alert("hit! " + xmlObj.childNodes[x].getAttribute("id"));
                        startVidIndex = x;               
                    }
                }
            }
        if (startVidIndex >= 0)
        {
           
            startVid(startVidIndex);
            clearInterval(vidTimelineID);
	        vidTimelineID = setInterval(vidTimeline,250);
	    }
        
	    var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		greenVid.play();
	    
	    enableAllButtons();
	    inTransition = false;
	    
	}
	
	function mainIntroAnimationedCompleted(sender, args)
	{
	    mainAnimationPlaying = false;
	}
	
    
   
    
    //MAIN NAV FUNCTIONS
    
    function resetNavTimeout(sender, args)
    {
        clearTimeout(mainNavTimerID);
    }
    
    function startNavTimeout(sender, args)
    {
       
        var sname = sender.name.toString();
        if (sname.indexOf("Submenu") != -1)
        {
            turnOffMainMenuItem(mainNavItemOn);
            //alert("hey!");
            //Handle the dropdown menu
	        subNav = sender.findName("SubNavMenuAnimation");
	        subNav.Stop();
        }
        mainNavTimerID = setTimeout("hideMainNav()",1000);
    }

    function hideMainNav()
    {
        if (navMenuVisible)
        {
            navcvs = xamlRoot.findName("cvsMainNavItems");
            navcvs["IsHitTestVisible"] = false;
            navMenuVisible = false;
            
            navfly = xamlRoot.findName("MainNavFlyin");
            navfly.Stop();
            navfly.Begin();
            
            turnOffMainMenuItem(mainNavItemOn);
            
            subNav = xamlRoot.findName("SubNavMenuAnimation");
	        subNav.Stop();
	    }
    }
    
    
    function animateMainNav(sender, args)
    {
        nav = xamlRoot.findName("cvsMainNavItems");
        nav["Canvas.Left"] = 0;
        
        navfly = sender.findName("MainNavFlyout");
        
        
        navfly.Stop();
        navfly.Begin();
        
        
        
    }
    
    function animateMainNavComplete(sender, args)
    {
        navcvs = xamlRoot.findName("cvsMainNavItems");
        navcvs["IsHitTestVisible"] = true;
        navMenuVisible = true;
    }
    
    
    //Sub nav functions
    function handleSubNavOver(sender, args)
    {
        turnOnMainMenuItem(mainNavItemOn);
        
        //hilight item
        textName = sender.name+"Title";
        xamlRoot.findName(textName)["Foreground"] = "#000000";
        
        arrow = xamlRoot.findName("subnavArrow");
        arrow.visibility = "Visible";
        
        //Show arrow
        //alert(menu.name);
	    arrow["Canvas.Left"] = sender.getParent()["Canvas.Left"] + 7;
	    arrow["Canvas.Top"] = sender.getParent().getParent()["Canvas.Top"] + sender["Canvas.Top"] - 17;
	    
    }
    
     //Sub nav functions
    function handleSubNavLeave(sender, args)
    {
        //hilight item
        textName = sender.name+"Title";
        xamlRoot.findName(textName)["Foreground"] = "#FFFFFF";
        
        arrow = xamlRoot.findName("subnavArrow");
        arrow.visibility = "Collapsed";
    }
    
    function handleNavClick(sender, args)
    {
        clearCCText();
               
        //alert(sender.Tag);
        if (sender.Tag != sections[sectionNum])
	    {
            strSenderID = sender.Tag;
            var newSection = ""
            for (s=0;s < sections.length;++s)
            {
                if (strSenderID == sections[s])
                {
                    newSection = s;
                }
            }
            
            handleTracking("topnav", newSection);
            jumpToPage(newSection);
        }
        hideMainNav();
    }
    
    
    
    //**********************
    //Mouse handlers section
    //**********************

    
     
    //Hover function for main menu at top
	function MainNavHover(sender, mouseEventArgs)
	{
	    var subNav;
	    
	    turnOffMainMenuItem(mainNavItemOn);
	    
	    //Set the main nav global var for using in submenus
	    mainNavItemOn = sender.name;
	    turnOnMainMenuItem(mainNavItemOn);
	    
	    
	    //Handle the dropdown menu
	    subNav = sender.findName("SubNavMenuAnimation");
	    subNav.Stop();
	    subNav["Storyboard.TargetName"] = sender.name + "Submenu";
        subNav.Begin();
	
	}
	
	function turnOnMainMenuItem(strName)
	{
	    
	    //xamlRoot.findName(strName)["Background"] = "#42446d";
	    
	    txtBlock = xamlRoot.findName(strName+"Title");
	    txtBlock.Foreground = "#949ed3";
	    
	    arrow = xamlRoot.findName("mainnavArrow");
	    arrow["Canvas.Left"] = xamlRoot.findName(strName)["Canvas.Left"] + txtBlock["Canvas.Left"] - 7;
	}
	
	//Mouseoff  function for bottom controls
	function MainNavOff(sender, mouseEventArgs)
	{
		turnOffMainMenuItem(mainNavItemOn);
	}
	
	function turnOffMainMenuItem(strName)
	{
	    if (strName != null)
	    {
	        //xamlRoot
	        //xamlRoot.findName(strName+"On")["Visibility"] = "Collapsed";
	        //alert(sender.name+"Title");
	        txtBlock = xamlRoot.findName(strName+"Title");
	        txtBlock.Foreground = "#FFFFFF";
	    }
	    arrow = xamlRoot.findName("mainnavArrow");
	    arrow["Canvas.Left"] = -900;
	}
	
	function MainNavClick(sender, mouseEventArgs)
	{
	        //First hide the main nav
	        resetNavTimeout(sender, mouseEventArgs);
	        hideMainNav();
    	    
	        handleNavClick(sender, mouseEventArgs);
	}
	
	function TrademarkHover(sender, mouseEventArgs)
	{
	    sender.textDecorations = "Underline";

	    
	}
	
	function TrademarkOff(sender, mouseEventArgs)
	{
	    sender.textDecorations = "None";

	}
	
	function trademarkClicked(sender, mouseEventArgs)
	{
	    window.open (sender.Tag,"newwin");
	}
	
	////////////////////////////////////////
	// Bottom controls code START
	// Main Nav buttons
	////////////////////////////////////////

    function disableAllButtons()
    {
        disableButton(xamlRoot.findName("cvsCC"));
        disableButton(xamlRoot.findName("cvsSound"));
        disableButton(xamlRoot.findName("cvsBackButton"));
        disableButton(xamlRoot.findName("cvsForwardButton"));
        disableButton(xamlRoot.findName("cvsPlayButton"));
        disableButton(xamlRoot.findName("cvsPauseButton"));
    }
    
    function enableAllButtons()
    {
        enableButton(xamlRoot.findName("cvsCC"));
        enableButton(xamlRoot.findName("cvsSound"));
        if (sectionNum > 0)
            enableButton(xamlRoot.findName("cvsBackButton"));
        else
            disableButton(xamlRoot.findName("cvsBackButton"));
        xamlRoot.findName("cvsBackButtonOn").visibility = "collapsed";
        enableButton(xamlRoot.findName("cvsForwardButton"));
        xamlRoot.findName("cvsForwardButtonOn").visibility = "collapsed";
        enableButton(xamlRoot.findName("cvsPlayButton"));
        xamlRoot.findName("cvsPlayButtonOn").visibility = "collapsed";
        enableButton(xamlRoot.findName("cvsPauseButton"));
        xamlRoot.findName("cvsPauseButtonOn").visibility = "collapsed";
        
        if (mouseOverItem != "")
        {
            xamlRoot.findName(mouseOverItem+"On").visibility = "visible";
        }
        
        
    }
    
    function setPlayButton(strPlayState)
    {
        if (strPlayState == "playing")
        {
            xamlRoot.findName("cvsPauseButton")["visibility"] = "Visible";
            xamlRoot.findName("cvsPlayButton")["visibility"] = "Collapsed";
        }
        else
        {
            xamlRoot.findName("cvsPauseButton")["visibility"] = "Collapsed";
            xamlRoot.findName("cvsPlayButton")["visibility"] = "Visible";
        }
    }
    
    function disableButton(objButton)
    {
       objButton.tag = "disabled";
       objButton.Cursor = "arrow";
       objButton.Opacity = .5;
    }
    
    function enableButton(objButton)
    {
       objButton.tag = "enabled";
       objButton.Cursor = "hand";
       objButton.Opacity = 1;
    }
    
    //Hover function for bottom controls
	function FooterControlHover(sender, mouseEventArgs)
	{
	    if (sender.tag != "disabled")
	        mouseOverItem = sender.name;
	        sender.findName((sender.Name+"On")).visibility = "visible";
	}
	
	//Mouseoff  function for bottom controls
	function FooterControlOff(sender, mouseEventArgs)
	{
	    mouseOverItem = "";
		sender.findName((sender.Name+"On")).visibility = "collapsed";
	}
	


	///////////////////////
	//Sound controls
	///////////////////////
	
	//Hover function for bottom controls
	function SoundControlHover(sender, mouseEventArgs)
	{
	    if (sender.tag != "disabled")
	        sender.findName((sender.Name+"On")).visibility = "visible";
	}
	
	//Mouseoff  function for bottom controls
	function SoundControlOff(sender, mouseEventArgs)
	{
	    if (vidVolume == 0)
	    {
	        sender.findName((sender.Name+"On")).visibility = "collapsed";
	    }
	    else
	    {
	        sender.findName((sender.Name+"On")).visibility = "visible";
	    }
		
	}
	
	function SoundClicked(sender, args)
	{
	    if (sender.tag != "disabled")
	    {
	        if (vidVolume == 0)
	        {
	            vidVolume = 0.5;
	           
	        }
	        else
	        {
	            vidVolume = 0.0;
	            sender.findName((sender.Name+"On")).visibility = "Collapsed";
	            
	            //Turn CC on!
	            ccOn = true;
	            sender.findName("cvsCCOn").visibility = "visible";
	            sender.findName("CCText")["Visibility"] = "Visible";
	        }
    	    
	        if (vidElement != null)
	            vidElement.Volume = vidVolume;
	    }
	}
	
	///////////////////////
	//CC controls
	///////////////////////
	
	//Hover function for bottom controls
	function CCControlHover(sender, mouseEventArgs)
	{
	    if (sender.tag != "disabled")
	        sender.findName((sender.Name+"On")).visibility = "visible";
	}
	
	//Mouseoff  function for bottom controls
	function CCControlOff(sender, mouseEventArgs)
	{
	    if (!ccOn)
	    {
	        sender.findName((sender.Name+"On")).visibility = "collapsed";
	    }
	    else
	    {
	        sender.findName((sender.Name+"On")).visibility = "visible";
	    }
		
	}
	
	function CCControlClicked(sender, args)
	{
	    if (sender.tag != "disabled")
	    {
	        if (!ccOn)
	        {
	            ccOn = true;
	            sender.findName("CCText")["Visibility"] = "Visible";
	            
	        }
	        else
	        {
	            ccOn = false;
	            sender.findName("CCText")["Visibility"] = "Collapsed";
	            sender.findName((sender.Name+"On")).visibility = "collapsed";
	        }
    	    
	       
	    }
	}	
	
	////////////////////////////////////////
	// Bottom controls code END
	// Main Nav buttons
	////////////////////////////////////////


	function jumpToPage(newSection)
	{
	    //Reset subtitle to blank when going to a new section
	    clearCCText();
	    clearSectionTitles();
        
	    
	    
	    if (mainAnimationPlaying)
	    {
	        sbObj = xamlRoot.findName("TextDropping"+sections[sectionNum]);
	        //alert("TextDropping"+sections[sectionNum]);
	        sbObj.Stop();
	        var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		    greenVid.stop();
		    
            sbsn = sender.FindName("MainPageIn");
            sbsn.Stop();
            mainAnimationPlaying = false;
	    }
	    
        if (vidElement != null)
            vidElement.Stop();
        
        previousSection = currentSection;
        //previousSection.visibility = "Collapsed";
        
        fade("out", previousSection.name);
        
	    
        //This might be where we handle all main section
        //storyboard animations
        if ((previousSection.name.indexOf("_") < 0 || sections[newSection].indexOf("_") < 0) && sections[sectionNum] != "5b")
        {
            disableAllButtons();
            
            if (previousSection.name.indexOf("_") < 0)
            {	                
                sbsn = sender.FindName("HomeOut");
                sbsn.Stop();
                sbsn.Begin();
               
                var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		        greenVid.stop();
            }
            else
            {
                mainAnimationPlaying = true;
                
                xamlRoot.findName("cvsNav").visibility = "collapsed"; 
                sbsn = xamlRoot.FindName("MainPageIn");
                sbsn.Stop();
                sbsn.Begin();
                
                var sbsn = xamlRoot.FindName("TextDropping"+(sections[newSection]));
	            sbsn.Stop();
	            
	            
            }
            
            sectionNum = newSection;
            
        }
        else
        {
            sectionNum = newSection;
    	    
    	    setCurrentSection(xamlRoot);
    	    playCurrentSection(xamlRoot);
        }

        //Set subtitle
	    setSubTitle();

            
        if (sectionNum == sections.length-1)
        {
            disableButton(xamlRoot.findName("cvsForwardButton"));
            xamlRoot.findName("cvsForwardButton").Tag = "disabled";
            enableButton(xamlRoot.findName("cvsBackButton"));
        }
        else if (sectionNum == 0)
        {
            disableButton(xamlRoot.findName("cvsBackButton"));
            xamlRoot.findName("cvsBackButton").Tag = "disabled";
            enableButton(xamlRoot.findName("cvsForwardButton"));
        }
        else
        {
           xamlRoot.findName("cvsForwardButton").Tag = "";
           xamlRoot.findName("cvsBackButton").Tag = "";
           enableButton(xamlRoot.findName("cvsBackButton"));	
           enableButton(xamlRoot.findName("cvsForwardButton"));	
        }	
	}
	
	function PageForward(sender, mouseEventArgs)
	{
	   
	    
	    if (sender.Tag != "disabled" && !inTransition)
	    {
	        clearCCText();
	        clearSectionTitles();
	    
	        //Turn off main animation if playing
	        if (mainAnimationPlaying)
	        {
	            //Turn off TextDropping
	            var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum]));
	            sbsn.Stop();
	            var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		        greenVid.stop();
	            
	            sbsn = sender.FindName("MainPageIn");
	            sbsn.Stop();
	            mainAnimationPlaying = false;
	        }
	        
	        //Reset subtitle to blank when going to a new section
	        var subTitleObj = xamlRoot.findName("sectionTitle");	
	        subTitleObj.Text = "";
	    
	        inTransition = true;
	        
	        if (vidElement != null)
                vidElement.Stop();
            
            if (navMenuVisible)
                hideMainNav();
           
	        previousSection = currentSection;
	        //previousSection.visibility = "Collapsed";
	        
	        fade("out", previousSection.name);
    	    
            //This might be where we handle all main section
            //storyboard animations
            if ((sections[sectionNum].indexOf("_") < 0 || sections[sectionNum+1].indexOf("_") < 0) && sections[sectionNum] != "5b")
	        {
	            disableAllButtons();
	            if (sections[sectionNum].indexOf("_") < 0)
	            {	                
	                sbsn = sender.FindName("HomeOut");
	                sbsn.Stop();
	                sbsn.Begin();
	                
	                var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		            greenVid.stop();
	            }
	            else
	            {
	                mainAnimationPlaying = true;
	                
	                sender.findName("cvsNav").visibility = "collapsed"; 
	                sbsn = sender.FindName("MainPageIn");
	                sbsn.Stop();
	                sbsn.Begin();
	                
	                var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum+1]));
	                sbsn.Stop();
	            }
	            
	            if (sectionNum < sections.length-1)
	            {
	                if ((playHiddenLastSection == false) && (sections[sectionNum] == "5"))
	                    ++sectionNum;
	                else
	                    playHiddenLastSection = false;
	                ++sectionNum;
	            }
	        }
	        else
	        {
        	    
        	    
	            if (sectionNum < sections.length-1)
	                ++sectionNum;
        	    
        	    clearCCText();
        	    setCurrentSection(sender);
        	    playCurrentSection(sender);
	        }

	        //Set subtitle
    	    setSubTitle();

            //tracking
            handleTracking("page", sectionNum)
            
            if (sectionNum == sections.length-1)
            {
                disableButton(sender.findName("cvsForwardButton"));
                sender.findName("cvsForwardButton").Tag = "disabled";
            }
            else
            {
               sender.findName("cvsForwardButton").Tag = "";
            }
            enableButton(sender.findName("cvsBackButton"));	
            
        }        
	}
	
	function PageBack(sender, mouseEventArgs)
	{
	    
	    
	    if (sender.Tag != "disabled" && !inTransition)
	    {
	        clearCCText();
	        clearSectionTitles();
	    
	        //Turn off main animation if playing
	        if (mainAnimationPlaying)
	        {
	            //Turn off TextDropping
	            var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum]));
	            sbsn.Stop();
	            var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		        greenVid.stop();
		        
	            sbsn = sender.FindName("MainPageIn");
	            sbsn.Stop();
	            mainAnimationPlaying = false;
	        }
	        
	        //Reset subtitle to blank when going to a new section
	        var subTitleObj = xamlRoot.findName("sectionTitle");	
	        subTitleObj.Text = "";
	    
	        inTransition = true;
	        
	        if (vidElement != null)
                vidElement.Stop();
            
	        previousSection = currentSection;
	        fade("out", previousSection.name);

            if (sections[sectionNum].indexOf("_") < 0 || sections[sectionNum-1].indexOf("_") < 0 || (sections[sectionNum] == "5_1"))
	        {
	            disableAllButtons();
	            if (sections[sectionNum].indexOf("_") < 0 && sections[sectionNum] != "5b")
	            {   	                
	                sbsn = sender.FindName("HomeOut");
	                sbsn.Stop();
	                sbsn.Begin();
	                
	                var greenVid = sender.findName("greenVideo"+sections[sectionNum]);
		            greenVid.stop();
	            }
	            else
	            {
	                sender.findName("cvsNav").visibility = "collapsed"; 
	                sbsn = sender.FindName("MainPageIn");
	                sbsn.Stop();
	                sbsn.Begin();
	                mainAnimationPlaying = true;
	                
	                if (sections[sectionNum] == "5_1")
	                {
	                    //For skipping over hidden section
	                    var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum-2]));
	                    sbsn.Stop();
	                }
	                else
	                {
	                    var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum-1]));
	                    sbsn.Stop();
	                }
	            }
	            
	            if (sectionNum > 0)
	            {
	                if (sections[sectionNum] == "5_1")
	                    --sectionNum;
	                --sectionNum;
	            }
	        }
	        else
	        {
        	    
	            if (sectionNum > 0)
	                --sectionNum;
        	    
        	    clearCCText();
        	    setCurrentSection(sender);
        	    playCurrentSection(sender);
	            
	            if (sectionNum == sections.length-1)
                {
                    disableButton(sender.findName("cvsForwardButton"));
                    sender.findName("cvsForwardButton").Tag = "disabled";
                }
                else
                {
                   sender.findName("cvsForwardButton").Tag = "";
                }
                enableButton(sender.findName("cvsBackButton"));	
            }
	        
	        //Set subtitle
    	    setSubTitle();
    	    
	        

	        if (sectionNum == 0)
            {
                disableButton(sender.findName("cvsBackButton"));
                sender.Tag = "disabled";
            }
            else
            {
               sender.Tag = "";
            }
            enableButton(sender.findName("cvsForwardButton"));
        }        
	}
	
	/* on storyboard completed functions */

	function playNewSection(sender, args)
	{
	    clearCCText();
	    enableAllButtons();
	    	    
	    if (sender.Name == "HomeOut")
	    {
	        mainAnimationPlaying = false;
	        
	        setCurrentSection(sender);
	        playCurrentSection(sender);
	        
            if (sectionNum == sections.length-1)
            {
                disableButton(sender.findName("cvsForwardButton"));
                sender.findName("cvsForwardButton").Tag = "disabled";
            }
            else
            {
               sender.findName("cvsForwardButton").Tag = "";
            }
            enableButton(sender.findName("cvsBackButton"));	
                        
            //Show the nav
            sender.findName("cvsNav").visibility = "visible"; //"visible";
            navfly = sender.findName("cvsMainNavItems");
            navfly["Canvas.Left"] = -906.0;
            navfly["IsHitTestVisible"] = false;
	    }
	    
	    inTransition = false;
	}
	
	function fadeInCompleted(sender, args)
	{
	    inTransition = false;
	}
	
	function mainPageInComplete(sender, args)
	{
	    
        enableAllButtons();
	    
        setCurrentSection(sender);
	    
        //Play GuyFlicker
        var guyImage = sender.findName("homeGuy"+(sections[sectionNum]));
        guyImage.visibility = "Visible";
	    
        var sbsn = sender.FindName("GuyFlicker"+(sections[sectionNum]));
        sbsn.Stop();
        sbsn.Begin();
	        
        if (sectionNum == sections.length-1)
        {
            disableButton(sender.findName("cvsForwardButton"));
            sender.findName("cvsForwardButton").Tag = "disabled";
        }
        else
        {
           sender.findName("cvsForwardButton").Tag = "";
        }
        enableButton(sender.findName("cvsBackButton"));	
        
       
	}

	/* on sotryboard completed functions END */

	
	//**********************
    //Video handlers section
    //**********************
	
	function stopAnimation(sender, args)
	{
	    
	    animatedObj = xamlRoot.findName(sender["Storyboard.TargetName"]);
	    currentYValue = animatedObj["Canvas.Top"];
	    animatedObj["Canvas.Top"] = currentYValue + 47;
	    sender.Stop();
	    
	}
	
	function onContentVideoEnded(sender, args)
	{
	    clearCCText();
	    if (autoPlay)
	    {
	        if (sections[sectionNum] == "5")
	        {
	            playHiddenLastSection = true;
	        }
	        else
	        {
	           playHiddenLastSection = false;
	        }
	        PageForward(sender, args);
	    }
	}
	
	function startVid(x)
	{
	    
	    clearCCText();
	    vidBuffered = false;
	    
        currentPlayingVid = parseInt(x);	    
	    vidElement.Source = xmlObj.childNodes[x].getAttribute("src");
	    token = vidElement.addEventListener("BufferingProgressChanged", loadVidDownload);
	    
	    vidElement.Volume = vidVolume;
	    loadVid();
    }
    

    loadVid = function()
    {
        
	    vidPlay();
    }
    
    vidPlay = function()
    {
        //alert(mainAnimationPlaying);
        if (mainAnimationPlaying)
        {
            var sbsn = xamlRoot.FindName("TextDropping"+(sections[sectionNum]));
	        sbsn.Stop();
	        sbsn.Begin();
	        xamlRoot.findName("section"+sections[sectionNum]+"header").visibility = "Visible";
	    }
        setPlayButton("playing");  
        //alert("playing!")  
        if (vidElement.CurrentState != "Playing")
	    {
	        vidElement.play();	
	    }
    }
    
    loadVidDownload = function(sender, args)
    {
        clearCCText();
        //vidName = "vid"+sections[sectionNum];
        if (vidElement != null && !vidBuffered)
        {
            
	        if (sender.bufferingProgress >= 1 || sender.bufferingProgress <= 0){
	            vidElement.RemoveEventListener("BufferingProgressChanged", token);
	            vidBuffered = true;
	            
		        vidElement.visibility = "Visible";
		        //alert("collapsing!");
		        xamlRoot.findName("cvsLoading").visibility = "Collapsed";
		        
		        vidPlay();
		        
	        } else {
	            //For some reason this keeps getting fired! Why doesn't it ever reach 1????
		        vidElement.visibility = "Collapsed";
		        xamlRoot.findName("cvsLoading").visibility = "Visible";
		        //xamlRoot.findName("LoadingText").Text = "Buffer: " + sender.bufferingProgress;
	        }
	    }
    }
	
	
	function video_DownloadFailed(sender, args)
	{
	    var pos = sender.position;
	    sender.Source = xmlObj.childNodes[currentPlayingVid].getAttribute("src");
	    sender.position = pos;
	}
		
	function onPlayPause(sender, args)
	{
	    if (sender.tag != "disabled")
	    {
	        if (vidElement.CurrentState == "Playing")
	        {
	            setPlayButton("paused"); 
	            vidElement.Pause();
	            //autoPlay = false;
	            
	            if (sections[sectionNum].indexOf("_") < 0 && mainAnimationPlaying)
	            {
	                //Stop Storyboard on main sections
	                var introAnimObj = sender.findName("TextDropping"+(sections[sectionNum]));
	                introAnimObj.Pause()
	            }
	        }
	        else if (vidElement.CurrentState == "Paused")
	        {
	            setPlayButton("playing"); 
	            vidElement.Play();
	            //autoPlay = true;
	            
	            if (sections[sectionNum].indexOf("_") < 0 && mainAnimationPlaying)
	            {
	                //Stop Storyboard on main sections
	                var introAnimObj = sender.findName("TextDropping"+(sections[sectionNum]));
	                introAnimObj.Resume()
	            }
	        }
	    }
	}
	
	function onLoopingVideoLoaded(sender, args)
	{
	    //sender.play();
	}
	
	function onLoopingMediaEnded(sender, args) 
	{
	    if (sender.Name == "headerVid")
	    {
	        if (sections[sectionNum].indexOf("_") >= 0)
	        {
	            sender.Position = "0"; 
                sender.play();
	        }
	    }
	    else
	    {
	        if (sections[sectionNum].indexOf("_") < 0)
	        {
	            sender.Position = "0"; 
                sender.play();
	        }
	    }
            
    }

	function turnOff(sender, args)
	{
	    pageCanvas = sender.findName(sender["Storyboard.TargetName"]);
	    pageCanvas.visibility = "collapsed";
	}
	
	vidTimeline = function()
	{
	    
	    var p = vidElement.position.seconds;
	    var d = vidElement.naturalDuration.seconds;
	    var ccTextObj = xamlRoot.findName("CCText");
	    var passTxt = "";
	    //loop through xml children for current vid - this code loops through them all - 
	    // every time the vidTimeline calls - not very efficient
	    for (var t=0; t<xmlObj.childNodes[currentPlayingVid].childNodes.length; t++) {		
		    //if nodetype = 1 (element) then check the data
		    if (xmlObj.childNodes[currentPlayingVid].childNodes[t].nodeType == 1) {
			    //check each node for the start attribute to get the ticker
			    var syncStart = xmlObj.childNodes[currentPlayingVid].childNodes[t].getAttribute("start");
			    
			    //this piece resets the text for every node that is less than P - also very inefficient - moving out of this loop
			    if((syncStart < p)){
				    passTxt = xmlObj.childNodes[currentPlayingVid].childNodes[t].firstChild.nodeValue;
				    //ccTextObj.Text = passTxt + " " + t + "::" + xmlObj.childNodes[currentPlayingVid].childNodes.length;
				    //ccTextObj["Canvas.Left"] = (375 - ccTextObj.ActualWidth)/2;
				    //var w = silverlightControl.content.findName("CCText").actualWidth;
	  		        //silverlightControl.content.findName("CCText")["Canvas.Left"] = ((silverlightControl.content.findName("CCText").width-w)/2);
			    
			        //step out if you find a match
			        
			    }
		    }		
	    }
	    
	    //now set the text one time after you've done your loop and figured out which text to display
	    ccTextObj.Text = passTxt;
		ccTextObj["Canvas.Left"] = (375 - ccTextObj.ActualWidth)/2;
	    //alert("vidTimeline");
    }
	
	//added on 3/12 to handle residual text when switching from one file to the next
	function clearCCText(){
	    //alert("clearCCText");
	    var ccTextObj = xamlRoot.findName("CCText");
	    ccTextObj.Text = "";
	    ccTextObj = null;
	}

	function setCurrentSection(sender)
	{
	    //alert("start first sec!");
	    if (previousSection == 0)
	        previousSection = currentSection;
        currentSection = sender.findName("cvsSection"+sections[sectionNum]);
        
        if (currentSection.name.indexOf("_") >= 0 || sections[sectionNum] == "5b")
        {
            currentSection.Opacity = 0;
        }
        else
        {
            sender.findName("section"+sections[sectionNum]+"header").visibility = "Collapsed";
        }
        
        currentSection.visibility = "Visible";
        
	}
	
	function playCurrentSection(sender)
	{
	    
	    clearCCText();
	    
	    if (vidElement != null)
            vidElement.Stop();
        
        
        if (sections[sectionNum] != "5_1") 
        {            
            vidElement = sender.findName("vid"+sections[sectionNum]);
            //alert(vidElement.name);
            var videoIndexToStart = -1;
            for(var x=0; x<xmlObj.childNodes.length; x++)
            {
                if (xmlObj.childNodes[x].nodeType == 1)
                {
                    if (xmlObj.childNodes[x].getAttribute("id") == sections[sectionNum])
                    {
                        //alert("hit! " + xmlObj.childNodes[x].getAttribute("id"));
                        videoIndexToStart = x;
                    }
                }
            }
            startVid(videoIndexToStart);
            
        }
        else
        {
            //alert(sections[sectionNum]);
            var ccTextObj = xamlRoot.findName("CCText");
            ccTextObj.Text = "";
            //alert(ccTextObj.Text);
        }
        
        if (currentSection.name.indexOf("_") >= 0 || sections[sectionNum] == "5b")
        {
            fade("in", currentSection.name);         
            xamlRoot.findName("headerVid").play();               
        }
        
	}
	
	
	function setSubTitle()
	{
	    
	    var strTitle = sectionTitles[sectionNum];
	    var strSubTitle = subSectionTitles[sectionNum];
	    var titleObj = xamlRoot.findName("sectionTitle");
	    var subTitleObj = xamlRoot.findName("subSectionTitle");	
	    titleObj["Canvas.Left"] = 2000.0;
	    subTitleObj["Canvas.Left"] = 2000.0;
	        
	    titleObj.Text = strTitle;
	    subTitleObj.Text = strSubTitle;
	    if (strTitle != "")
	    {
	        subTitleObj["Canvas.Top"] = titleObj["Canvas.Top"] + titleObj["ActualHeight"];
	        intCanvasLeft = (xamlRoot["Width"] - titleTextAreaWidth) + ((titleTextAreaWidth - titleObj["ActualWidth"])/2);
	        titleObj["Canvas.Left"] = intCanvasLeft; //xamlRoot.findName("Page").Width - (titleObj.ActualWidth + 15);
	        subTitleObj["Canvas.Left"] = titleObj["Canvas.Left"];
	        //alert(titleObj["Canvas.Left"]);
	    }
	}
	
	/* Font downloading */
	function downloader_DownloadFailed()
	{
	    alert("failed?");
	}
	
	function downloader_Completed(sender, args)
	{
	    
	    if (sender.status = 200)
	    {
	        // Call helper function to set the TextBlocks.
            setFont(sender, "greenBoxTitle", "AlexandraMD", 22);
            setFont(sender, "sectionTitle", "AlexandraMD", 22);
            setFont(sender, "subSectionTitle", "AlexandraMD", 18);
            setFont(sender, "cvsSection1Header", "AlexandraMD", 24);
            setFont(sender, "cvsSection1HeaderA", "AlexandraMD", 24);
            setFont(sender, "cvsSection2Header", "AlexandraMD", 24);            
            setFont(sender, "cvsSection3Header", "AlexandraMD", 24);
            setFont(sender, "cvsSection4Header", "AlexandraMD", 24);
            setFont(sender, "cvsSection5Header", "AlexandraMD", 24);
            
            sender.findName("sectionTitle")["Canvas.Left"] = 2000.0;
            sender.findName("subSectionTitle")["Canvas.Left"] = 2000.0;
            sender.findName("sectionTitle")["Foreground"] = "#8D8EBC";
            
            for (var i=0;i<mainsectionTitles.length;++i)
		    {
		        setFont(sender, mainsectionTitles[i], "Paralucent Light", 16);
		    }
		    
		    //contentSectionTitles
		    //for (var i=0;i<contentSectionTitles.length;++i)
		    //{
		    //    setFont(sender, contentSectionTitles[i], "Paralucent Light", 13);
		    //}
		    
		    //Bullets on each main screen
            var mainSectionBullets = ["cvsSection1ABullet1Text","cvsSection1ABullet2Text","cvsSection1ABullet3Text","cvsSection1Bullet1Text","cvsSection1Bullet2Text","cvsSection1Bullet3Text","cvsSection2Bullet1Text","cvsSection2Bullet2Text","cvsSection2Bullet3Text","cvsSection3Bullet1Text","cvsSection3Bullet2Text","cvsSection4Bullet1Text","cvsSection4Bullet2Text","cvsSection5Bullet1Text"];		   
		    for (var i=0;i<mainSectionBullets.length;++i)
		    {
		        setFont(sender, mainSectionBullets[i], "Paralucent Light", 24);
		    }
		    
		    
		    //Set fonts for Resources page
		    setFont(sender, "txtSection5_1_1_title", "AlexandraMD", 24);
		    setFont(sender, "txtSection5_1_2_title", "AlexandraMD", 24);
		    setFont(sender, "txtSection5_1_3_title", "AlexandraMD", 24);
		    setFont(sender, "txtSection5_1_4_title", "AlexandraMD", 24);
		    
		    //cvsSection5_1_1_Bullet1_Text
		    
		    setFont(sender, "cvsSection5_1_1_Bullet1_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_1_Bullet2_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_1_Bullet3_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_1_Bullet4_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_2_Bullet1_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_2_Bullet2_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_2_Bullet3_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_3_Bullet1_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_3_Bullet2_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_3_Bullet3_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_4_Bullet1_Text", "Paralucent Light", 22);
		    setFont(sender, "cvsSection5_1_4_Bullet2_Text", "Paralucent Light", 22);
		    
		    
		    
		    
	    }
	    else
	    {
	        //textBlk.setFontSource(sender);
	        //textBlk.fontSize = 24;
	        //textBlk.fontFamily = "Times New Roman";
	    }
	    
	    fontsLoaded = true;
	    if (xmlLoaded)
	    {
	        startOpeningAnimation();
	    }
	}
	
    // Set the downloaded font to the TextBlock.
    function setFont(downloader, textElement, fontFamily, fontSize)
    {
        // Retrieve a reference to the specified TextBlock.
        var myTextBlock = downloader.findName(textElement);

        // Add the fonts to the typeface collection.
        myTextBlock.setFontSource(downloader);

        // Specify the desired font.
        myTextBlock.fontFamily = fontFamily;

        // Set the size
        myTextBlock.fontSize = fontSize;
        
    }


	/* font downloading END */
	
	function fade(strDirection, strObj)
	{
        
        clearCCText();
        var fadeSB;
	    if (strDirection == "out")
	    {
	        fadeSB = xamlRoot.findName("FadeOut");
	    }
	    else
	    {
	        fadeSB = xamlRoot.findName("FadeIn");
	    }	    	    
        fadeSB.Stop();
        fadeSB["Storyboard.TargetName"] = strObj;
        fadeSB.Stop();
        fadeSB.Begin();
	}
	
	function resourceClicked(sender, args)
	{
	    
	    index = parseInt(sender.Tag);
	    handleTracking("resourcelink",index);
	    window.open (resourceLinks[index],"newwin");
	}
	
	function clearSectionTitles()
	{
	    var titleObj = xamlRoot.findName("sectionTitle");	
	    var subTitleObj = xamlRoot.findName("subSectionTitle");	
	    titleObj.Text = "";
	    subTitleObj.Text = "";
	    titleObj["Canvas.Left"] = 2000;
	    subTitleObj["Canvas.Left"] = 2000;
	}
	
	//Tracking code
	function handleTracking(trackingType, index)
	{
	    if (trackingType == "topnav")
	    {
	        if (topNavTrackingCodes[index] != "")
	        {
    	        _hbPageView(topNavTrackingCodes[index], '/theultimateserversolution+topnav');
    	    }
	    }
	    else if (trackingType == "page")
	    {
	        if (pageTrackingCodes[index] != "")
	        {
	            _hbPageView(pageTrackingCodes[index], '/theultimateserversolution+page');
	        }
	    }
	    else
	    {
	        //Resources
	        _hbLink(resourceLinkCodes[index]);
	    }
	}