﻿/**************************************************************
** VECD Silverlight
** 2/2008
** by http://www.xcarab.com for Microsoft
****************************************************************/
var xmlDoc;
if (window.ActiveXObject) {
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
} else {
	xmlDoc=document.implementation.createDocument("","",null);
}
var xmlObj;
var doDragVol = false;
var doDragTime = false;
var vidTimelineID = 0;
var currentPlayingVid = -1;
var selectedMain = -1;
var selectedSub = -1;
var overSub = false;
var subCheck = -1;
var subCheckCount = -1;
var overVolume = false;
var volumeCount = -1;
var vidToStart = -1;
var startedFirstVid = 0;
var allVideos = new Array();
if (!window.VDIDemo) {
	window.VDIDemo= {};
}
VDIDemo.Page = function() {
}
VDIDemo.Page.prototype = {
	handleLoad: function(control, userContext, rootElement) {
		this.control = control;
		silverlightControl = this.control;		
		silverlightControl.content.findName("ShellCanvas").visibility = "Collapsed"
		silverlightControl.content.findName("BufferingArea_Buffering").begin();
		loadXML('playlist.xml');		
	}
}
loadXML = function(xmlFile) {
  if (window.ActiveXObject) {
  	xmlDoc.onreadystatechange = verifyXMLIE;
  	xmlDoc.async = "false";
  	xmlDoc.load(xmlFile);
  } else {
  	xmlDoc.onload = verifyXMLFF;
  	xmlDoc.load(xmlFile);
	}
}
verifyXMLIE = function() {
 if (xmlDoc.readyState == 4) {
  	xmlObj = xmlDoc.documentElement;
  	loadVideos();
  	createMainNav();
	startSilverlight();
 }
}
verifyXMLFF = function() {
	xmlObj = xmlDoc.documentElement;
	loadVideos();
	createMainNav();
	startSilverlight();
}
loadVideos = function() {
    var n = 0;
    for(var x=0; x<xmlObj.childNodes.length; x++){
  	    if (xmlObj.childNodes[x].nodeType == 1) {
  	        if (xmlObj.childNodes[x].tagName == "Menu") {
  	           var topNode = xmlObj.childNodes[x];		
		       for(var y=0; y<topNode.childNodes.length; y++) {	             
		            if (topNode.childNodes[y].nodeType == 1) {
		                allVideos[n] = new Object();
		                allVideos[n].MainID = x;
		                allVideos[n].SubID = y;
		                allVideos[n].Src = topNode.childNodes[y].getAttribute("src");
		                allVideos[n].XmlNode = topNode.childNodes[y];
		                n++;
		            }
		       }
  	        }
  	        else {
  	            allVideos[n] = new Object();
	            allVideos[n].MainID = x;
	            allVideos[n].SubID = -1;
	            allVideos[n].Src = xmlObj.childNodes[x].getAttribute("src");
	            allVideos[n].XmlNode = xmlObj.childNodes[x];
	            n++;	         
  	        }  	        
	    }
    }
}
onResized = function(sender, eventArgs) {
}
onRollOverURL = function(sender, mouseEventArgs) {	
	sender.foreground = "#FF173DA9";
}
onRollOutURL = function(sender, mouseEventArgs) {	
	sender.foreground = "#FF1D548B";
}
onRollOverPlayerControlsButton = function(sender, mouseEventArgs) {	
    if (sender.name == "ButtonPlayAgain") {
		sender.findName("PlayAgainOver").begin();
	}
}
onRollOutPlayerControlsButton = function(sender, mouseEventArgs) {	
    if (sender.name == "ButtonPlayAgain") {
		sender.findName("PlayAgainOut").begin();
	}
}
onVolumeEnter = function() {
    overVolume = true;
    volumeCount = -1;
}
onVolumeLeave = function() {
   overVolume = false;
   volumeCount = 1;
}
clickEvents = function(sender, mouseEventArgs) {    
	if (sender.name == "ButtonPlay") {
		vidPlay();
		sender.findName("VolumeCanvas").visibility = "Collapsed";
	} else if (sender.name == "ButtonPause") {
		vidPause();
		sender.findName("VolumeCanvas").visibility = "Collapsed";
	} else if (sender.name == "ButtonMute") {
	    sender.findName("VolumeCanvas").visibility = "Collapsed";
		if(sender.findName("VidWindow").IsMuted == false) {
			sender.findName("VidWindow").IsMuted = "True";
			sender.findName("MusicLoop").IsMuted = "True";
			sender.findName("IconMute").visibility = "Visible";
			sender.findName("IconUnmute").visibility = "Collapsed";
		} else {
			sender.findName("VidWindow").IsMuted = "False";
			sender.findName("MusicLoop").IsMuted = "False";
			sender.findName("IconMute").visibility = "Collapsed";
			sender.findName("IconUnmute").visibility = "Visible";
		}		
	} else if (sender.name == "ButtonPlayAgain") {	    
		pushVid(0);
	} else if (sender.name == "VolumeSliderCanvas") {	    
		doDragVol = false;
	} else if (sender.name == "TimelineSliderCanvas") {
	    sender.findName("VolumeCanvas").visibility = "Collapsed";
		doDragTime = false;	
	} else if (sender.name == "ButtonVolume") {
	    if (sender.findName("VolumeCanvas").visibility == "Collapsed") {
	        sender.findName("VolumeCanvas").visibility = "Visible";
	    } else {
	        sender.findName("VolumeCanvas").visibility = "Collapsed";
	    }
	} else if (sender.name == "InfoText") {	    
		window.open("http://www.windowsvista.com/vecd");
	}
}
setVolumeManual = function (v) {
	var currentV = silverlightControl.content.findName("VidWindow").Volume;
	if (v>0) {
		if (currentV<1) {
			currentV += v;
		} else {
			currentV = 1;
		}
		if (currentV>1) {
			currentV = 1;
		}
	} else {
		if (currentV>0) {
			currentV += v;
		} else {
			currentV = 0;
		}
		if (currentV<0) {
			currentV = 0;
		}
	}
	silverlightControl.content.findName("VidWindow").Volume = currentV;
	silverlightControl.content.findName("MusicLoop").Volume = (currentV)/5;			
	var posSet = ((silverlightControl.content.findName("VolumeSliderCanvas").height*(1-currentV))/1);
	silverlightControl.content.findName("VolumeSlider").height = posSet-1;	
	silverlightControl.content.findName("VolumeSliderGripper")["Canvas.Top"] = (silverlightControl.content.findName("VolumeSliderCanvas")["Canvas.Top"]
	+silverlightControl.content.findName("VolumeSlider").height)-silverlightControl.content.findName("VolumeSliderGripper").height;	
};
dragEvents = function(sender, mouseEventArgs) {
	if(sender.name == "VolumeSliderCanvas" && doDragVol == true) {
		var coordinate = mouseEventArgs.getPosition(sender).y;
		silverlightControl.content.findName("VolumeSlider").Height = coordinate;		
		var v = Math.round((silverlightControl.content.findName("VolumeSlider").Height*100)/silverlightControl.content.findName("VolumeSliderCanvas").Height);
		silverlightControl.content.findName("VidWindow").Volume = (1-(v/100));
		silverlightControl.content.findName("MusicLoop").Volume = silverlightControl.content.findName("VidWindow").Volume/5;
		silverlightControl.content.findName("VolumeSliderGripper")["Canvas.top"] = coordinate;
	} else if(sender.name == "TimelineSliderCanvas" && doDragTime == true) {
		var coordinate = mouseEventArgs.getPosition(sender).x;		
		var d = silverlightControl.content.findName("VidWindow").naturalDuration.seconds;
		var w = silverlightControl.content.findName("TimelineSliderCanvas").width;
		if(d != 0) {
			silverlightControl.content.findName("TimelineSlider")["Canvas.Left"] = coordinate-3;
			silverlightControl.content.findName("TimelineSliderFollow").width = coordinate+1;			
			var seconds = ((d*coordinate)/w);
			var pos = silverlightControl.content.findName("VidWindow").position;
			pos.seconds = seconds;
			silverlightControl.content.findName("VidWindow").position = pos;
		}
	}
}
outEvents = function(sender, mouseEventArts) {
	if(sender.name == "VolumeSliderCanvas") {
		doDragVol = false;
	} else if(sender.name == "TimelineSliderCanvas") {
		doDragTime = false;
	}
}
pressEvents = function(sender, mouseEventArgs) {
	if(sender.name == "VolumeSliderCanvas") {
		doDragVol = true;
	} else if(sender.name == "TimelineSliderCanvas") {
		doDragTime = true;
	}
}
setFont = function(textElement, fontFamily, txt, align) {
    var txtBlock = silverlightControl.content.findName(textElement);
    txtBlock["Canvas.Top"] = txtBlock["Canvas.Top"]+2;
    txtBlock.text = txt;
    if (align == "right") {
    	var w = txtBlock.actualWidth;
    	txtBlock["Canvas.Left"] = txtBlock["Canvas.Left"]+(txtBlock.width-w);
    } else if (align == "center") {
    	var w = txtBlock.actualWidth;
    	txtBlock["Canvas.Left"] = txtBlock["Canvas.Left"]+((txtBlock.width-w)/2);
    }
}
onMarkerReached = function(sender, args) {
	var m = args.marker;
	if (m.Type == "Link") {
	    silverlightControl.content.findName("LinkFlashUp").begin();
	} else if (m.Type == "MOver") {	    
	    if (m.text.length == 1) {
	       this["buttonRollOver"+m.text](silverlightControl.content.findName("buttonMainNav"+m.text));
	    } else {    
	       var ids = m.text.split('-');
           this["buttonRollOver"+ids[0]+"_"+ids[1]](silverlightControl.content.findName("buttonSubNav"+ids[0]+"-"+ids[1]));
        }
	} else if (m.Type == "MOut") {
	    if (m.text.length == 1) {
	       this["buttonRollOut"+m.text](silverlightControl.content.findName("buttonMainNav"+m.text));
	    } else {    
	       var ids = m.text.split('-');
           this["buttonRollOut"+ids[0]+"_"+ids[1]](silverlightControl.content.findName("buttonSubNav"+ids[0]+"-"+ids[1]));
        }
	}
}
makeMarker = function(mediaElement, name, type, text, positionInSecs) {	
	var mXaml = '<TimelineMarker Name="' + name + '" Type="' + type + '" Text="' + text + '" Time="' + positionInSecs +'" />';	
	var m = silverlightControl.content.CreateFromXAML(mXaml);
	mediaElement.markers.add(m);
}
startVid = function(vid){	
	currentPlayingVid = vid;	
	silverlightControl.content.findName("VidWindow").Source = allVideos[currentPlayingVid].Src;
	startedFirstVid = 1;
	loadVid();
}
findAndStartVid = function(main,sub) {	    
	for(var n=0;n<allVideos.length;n++) {
	    if ((allVideos[n].MainID == main) && (allVideos[n].SubID == sub)) {
	        startVid(n);
	    }
	}
}
loadVid = function() {
    silverlightControl.content.findName("PlayAgainCanvas").visibility = "Collapsed";
	silverlightControl.content.findName("VidWindow").visibility = "Visible";
	silverlightControl.content.findName("BufferingArea").visibility = "Collapsed";
	vidPlay();
}
vidOpened = function() {
    var vidWin = silverlightControl.content.findName("VidWindow");
    silverlightControl.content.findName("PlayAgainCanvas").visibility = "Collapsed";
    vidWin.visibility = "Visible";
	silverlightControl.content.findName("BufferingArea").visibility = "Collapsed";	
		
	for (var t=0; t<allVideos[currentPlayingVid].XmlNode.childNodes.length; t++) {		
	    var someNode = allVideos[currentPlayingVid].XmlNode.childNodes[t];
		if (someNode.nodeType == 1) {
		    var syncStart = someNode.getAttribute("start");
		    if (someNode.tagName == "Link") {
			   makeMarker(vidWin,"Link"+t,"Link","text1","00:0" + secondsDisplay(syncStart));
			} else if (someNode.tagName == "MouseOver") {
			   makeMarker(vidWin,"MOver"+t,"MOver",someNode.getAttribute("button"),"00:0" + secondsDisplay(syncStart));
			} else if (someNode.tagName == "MouseOut") {
			   makeMarker(vidWin,"MOut"+t,"MOut",someNode.getAttribute("button"),"00:0" + secondsDisplay(syncStart));
			}
		}		
	}
}
loadVidDownload = function(sender,eventArgs) {
	if(sender.BufferingProgress == 1) {
	    silverlightControl.content.findName("PlayAgainCanvas").visibility = "Collapsed";
		silverlightControl.content.findName("VidWindow").visibility = "Visible";
		silverlightControl.content.findName("BufferingArea").visibility = "Collapsed";
		vidPlay();
	} else {
	    silverlightControl.content.findName("PlayAgainCanvas").visibility = "Collapsed";
		silverlightControl.content.findName("VidWindow").visibility = "Collapsed";
		silverlightControl.content.findName("BufferingArea").visibility = "Visible";
	}
}
prevVid = function() {
	if(currentPlayingVid > 0) {
		var x = currentPlayingVid;				
		pushVid(--x);
	}
}
nextVid = function() {
	if(currentPlayingVid < allVideos.length-1) {	
		var x = currentPlayingVid;		
		pushVid(++x);
	}
}
vidEnd = function() {
	if(startedFirstVid == 1){
		if(currentPlayingVid == allVideos.length-1) {
		    // display some end message
		    silverlightControl.content.findName("PlayAgainCanvas").opacity = 0;
		    silverlightControl.content.findName("PlayAgainCanvas").visibility = "Visible";
		    silverlightControl.content.findName("VidWindow").visibility = "Collapsed";
		    silverlightControl.content.findName("PlayToAgainSam").begin();
		} else {
			nextVid();
		}
	}
}
vidPlay = function() {	
	silverlightControl.content.findName("VidWindow").play();	
	silverlightControl.content.findName("ButtonPlay").visibility = "Collapsed";
	silverlightControl.content.findName("ButtonPause").visibility = "Visible";
	
}
vidPause = function() {	
	silverlightControl.content.findName("VidWindow").pause();	
	silverlightControl.content.findName("ButtonPlay").visibility = "Visible";
	silverlightControl.content.findName("ButtonPause").visibility = "Collapsed";
}
roundDisplay = function(i){
    var v = Math.round(i);
    var diff = i-v;
    if (diff < 0) {
        v = v-1;
    }
    if (v > 9)
        return v;
    else
        return "0" + v.toString();
}
secondsDisplay = function(v){
    if (v > 60) {   
        return roundDisplay(v/60) + ":" + roundDisplay(v%60);
    } else {        
        return "0:" + roundDisplay(v);
    }
}
selectMain = function(m) {
    if ((selectedMain >= 0) && (selectedMain != m)) {
        if (selectedSub >= 0) {
            silverlightControl.content.findName("ButtonCanvasRollOut"+selectedMain+"-"+selectedSub).begin();
        }
        silverlightControl.content.findName("ButtonCanvasRollOut"+selectedMain).begin();
	    selectedSub = -1;
    }
    selectedMain = m;
}
selectSub = function(m,s) {
    if ((selectedSub >= 0) && (selectedSub != s)) {
        silverlightControl.content.findName("ButtonCanvasRollOut"+m+"-"+selectedSub).begin();
    }
    selectedSub = s;
}
doTimeChecks = function() {
    if ((subCheck >= 0) && (subCheck != selectedMain)) {
        if (subCheckCount++ > 200) {
            hideSubNav();
            subCheck = -1;
            overSub = false;
            subCheckCount = -1;
        }
    }
    if ((!overVolume) && (volumeCount > 0)) {
        if (volumeCount++ > 5) {
            silverlightControl.content.findName("VolumeCanvas").visibility = "Collapsed";
            volumeCount = -1;
        }
    }
}
setTimelineText = function(txt) {
    var txtBlock = silverlightControl.content.findName("TimelineText");    
    txtBlock.text = txt;    
	var w = txtBlock.actualWidth;
	txtBlock["Canvas.Left"] = ((txtBlock.width-w)/2);    
}
vidTimeline = function(){
	if(doDragTime == false){	
		var p = silverlightControl.content.findName("VidWindow").position.seconds;
		var d = silverlightControl.content.findName("VidWindow").naturalDuration.seconds;
		var w = silverlightControl.content.findName("TimelineSliderCanvas").width-silverlightControl.content.findName("TimelineSlider").width;
		setTimelineText(secondsDisplay(p) + "/" + secondsDisplay(d));
		doTimeChecks();
		if(d != 0 && p < d){
			silverlightControl.content.findName("TimelineSlider")["Canvas.Left"] = ((p*w)/d);
			silverlightControl.content.findName("TimelineSliderFollow").width = ((p*w)/d)+1;			
		}
		for (var t=0; t<allVideos[currentPlayingVid].XmlNode.childNodes.length; t++) {		
		    var someNode = allVideos[currentPlayingVid].XmlNode.childNodes[t];
			if (someNode.nodeType == 1) {
				var syncStart = someNode.getAttribute("start");
				if(syncStart <= p) {
				    if (someNode.tagName == "Sync") {				        
					    var passTxt = allVideos[currentPlayingVid].XmlNode.childNodes[t].firstChild.nodeValue;
					    silverlightControl.content.findName("CaptionText").Text = passTxt;					
					}
				}
			}		
		}
	}
}
displaySubNav = function(subID) {
  silverlightControl.content.findName("SubNav").Visibility = "Visible";
  silverlightControl.content.findName("subNav-"+subID).Visibility = "Visible";
}
hideSubNav = function() {
  silverlightControl.content.findName("SubNav").Visibility = "Collapsed";
}
createMainNav = function(){	
	var canvasX = 4;
	for(var x=0; x<xmlObj.childNodes.length; x++){
		if (xmlObj.childNodes[x].nodeType == 1) {			
			var w = 120;
			
			// create main nav button	
			var buttonText = xmlObj.childNodes[x].getAttribute("name");		
			var xamlButton = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="buttonMainNav'+x
								+'" Width="'+w+'" Height="25" Canvas.Left="'+canvasX+'" Canvas.Top="0" Cursor="Hand" MouseEnter="buttonRollOver'+x
								+'" MouseLeave="buttonRollOut'+x+'" MouseLeftButtonUp="buttonPress'+x+'">'
								+ '<Canvas x:Name="ButtonCanvas'+x+'" Width="'+w+'" Height="25" Opacity="0">'
								+ '<Rectangle Width="'+w+'" Height="25" Canvas.Top="2" RadiusX="2" RadiusY="2" x:Name="buttonRect0-'+x+'">'
			                    + '<Rectangle.Fill><LinearGradientBrush EndPoint="0,0" StartPoint="0,1"><GradientStop Color="#FFB8C5E5" Offset="1"/><GradientStop Color="#FF121415" Offset="0.438"/><GradientStop Color="#FF090B0C" Offset="0.473"/></LinearGradientBrush></Rectangle.Fill></Rectangle>'
			                    
		                        + '<Rectangle Width="'+(w-2)+'" Height="23" RadiusX="2" RadiusY="2" Canvas.Left="1" Canvas.Top="3" x:Name="buttonRect1-'+x+'">'
			                    + '<Rectangle.Stroke><LinearGradientBrush EndPoint="0,0" StartPoint="0,1"><GradientStop Color="#FFBBBCBC" Offset="1"/><GradientStop Color="#FF121415" Offset="0.438"/><GradientStop Color="#FF2b2d2e" Offset="0.473"/></LinearGradientBrush></Rectangle.Stroke></Rectangle>'
		                        + '<Rectangle Width="'+w+'" Height="25" RadiusX="2" RadiusY="2" Stroke="#FF4b4c4c" Canvas.Top="2" x:Name="buttonRect2-'+x+'"/></Canvas>'
		                        + '<Image x:Name="buttonSep-'+x+'" Height="25" Canvas.Left="190" Canvas.Top="2" Source="tabseptop.png" />'
								+ '<TextBlock x:Name="buttonText'+x+'" Width="'+w+'" Height="12" Canvas.Top="8" FontFamily="Arial" FontSize="11" Foreground="#FFFFFFFF" Text="' + buttonText + '" TextWrapping="Wrap" IsHitTestVisible="False"/></Canvas>';								
			var xamlButtonAdd = silverlightControl.content.createFromXaml(xamlButton);
			silverlightControl.content.findName("MainNav").children.insert(0,xamlButtonAdd);
			canvasX += w;
			
			if (xmlObj.childNodes[x].tagName == "Menu")
			{
			   // build the sub nav buttons
			   var xamlSubButtons = '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="subNav-'+x+'" Width="734" Height="25" Canvas.Top="0">';
			   var topNode = xmlObj.childNodes[x];
			   subCanvasX = 70;
			   for(var y=0; y<topNode.childNodes.length; y++)
			   {
		            if (topNode.childNodes[y].nodeType == 1) 
		            {		                
		                var subText = topNode.childNodes[y].getAttribute("name");		
		                xamlSubButtons += '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="buttonSubNav'+x+'-'+y
						+'" Width="200" Height="25" Canvas.Left="'+subCanvasX+'" Canvas.Top="0" Cursor="Hand" MouseEnter="buttonRollOver'+x+'_'+y+'" MouseLeave="buttonRollOut'+x+'_'+y+'" MouseLeftButtonUp="subButtonPress'+x+'_'+y+'">'
						+ '<Canvas x:Name="ButtonCanvas'+x+'-'+y+'" Width="200" Height="29" Canvas.Left="0" Canvas.Top="0" Opacity="0">'
		                + '<Rectangle x:Name="buttonRect0-'+x+'-'+y+'" Width="200" Height="25" Canvas.Left="5" Canvas.Top="2" RadiusX="2" RadiusY="2">'
			            + '<Rectangle.Fill><LinearGradientBrush EndPoint="0,0" StartPoint="0,1"><GradientStop Color="#FFFAFCFE" Offset="1"/><GradientStop Color="#FFB8C5E5" Offset="0.469"/><GradientStop Color="#FFB8C5E5" Offset="0"/></LinearGradientBrush></Rectangle.Fill></Rectangle>'
		                + '<Rectangle x:Name="buttonRect1-'+x+'-'+y+'" Width="198" Height="23" RadiusX="2" RadiusY="2" Stroke="#FFFFFFFF" Canvas.Left="6" Canvas.Top="3" />'
		                + '<Rectangle x:Name="buttonRect2-'+x+'-'+y+'" Width="200" Height="25" RadiusX="2" RadiusY="2" Stroke="#FFA3A4A5" Canvas.Left="5" Canvas.Top="2" /></Canvas>'
		                + '<Image x:Name="buttonSep-'+x+'-'+y+'" Height="23" Canvas.Left="190" Canvas.Top="2" Source="tabsepbottom.png" />'
						+ '<TextBlock x:Name="subButtonText'+x+'-'+y+'" Width="210" Height="12" Canvas.Top="8" FontFamily="Arial" FontSize="11" Foreground="#FF000000" Text="'+subText+'" TextWrapping="Wrap" IsHitTestVisible="False"/></Canvas>';
						
						this["subButtonPress"+x+"_"+y] = function(sender){
		                    var senderName = sender.name;
		                    var buttonID = sender.name.replace(/buttonSubNav/,"");
			                var ids = senderName.replace(/buttonSubNav/,"").split('-');
			                var storyboardNameID = "ButtonCanvasRollOver"+ids[0];
			                if(selectedMain != ids[0]){					
					            silverlightControl.content.findName(storyboardNameID).begin();					    
				            }			               
			                selectMain(ids[0]);
			                selectSub(ids[0],ids[1]);
		                    findAndStartVid(ids[0],ids[1]);
		               }
		               
		               subCanvasX += 200;		               
		               
		               this["buttonRollOver"+x+"_"+y] = function(sender){
				            var buttonID = sender.name.replace(/buttonSubNav/,"");				            
				            var storyboardNameID = "ButtonCanvasRollOver"+buttonID;
				            if ((selectedMain + "-" + selectedSub) != buttonID){
					            silverlightControl.content.findName(storyboardNameID).begin();
					            overSub = true;
					            subCheck = -1;
				            }
			            }
			            this["buttonRollOut"+x+"_"+y] = function(sender){
				            var buttonID = sender.name.replace(/buttonSubNav/,"");				            
				            var storyboardNameID = "ButtonCanvasRollOut"+buttonID;
				            if ((selectedMain + "-" + selectedSub) != buttonID){				                
					            silverlightControl.content.findName(storyboardNameID).begin();
					            overSub = false;
					            subCheck = buttonID.split('-')[0];
				            }
			            }
			            var xamlButtonStoryBoardsIn  = '<Storyboard xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ButtonCanvasRollOver'+x+'-'+y+'">'			                                 			                                             
			                                             +'<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ButtonCanvas'+x+'-'+y+'" Storyboard.TargetProperty="(UIElement.Opacity)">'
	                                                     +'<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>'				                                         
	                                                     +'<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="1"/>'
                                                         +'</DoubleAnimationUsingKeyFrames>'				                                             
			                                            + '</Storyboard>';
			            var xamlButtonStoryboardsInAdd = silverlightControl.content.createFromXaml(xamlButtonStoryBoardsIn);
			            silverlightControl.content.findName("Page").resources.add(xamlButtonStoryboardsInAdd);		
			            var xamlButtonStoryBoardsOut = '<Storyboard xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ButtonCanvasRollOut'+x+'-'+y+'">'			                                 
			                                             +'<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ButtonCanvas'+x+'-'+y+'" Storyboard.TargetProperty="(UIElement.Opacity)">'
	                                                     +'<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>'				                                         
	                                                     +'<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0"/>'
                                                         +'</DoubleAnimationUsingKeyFrames>'				                                             
			                                            + '</Storyboard>';
			            var xamlButtonStoryBoardsOutAdd = silverlightControl.content.createFromXaml(xamlButtonStoryBoardsOut);
			            silverlightControl.content.findName("Page").resources.add(xamlButtonStoryBoardsOutAdd);
		               
		            }
		       }
		       xamlSubButtons += "</Canvas>";		       
		       
		       var xamlSubButtonAdd = silverlightControl.content.createFromXaml(xamlSubButtons);
			   silverlightControl.content.findName("SubNav").children.insert(0,xamlSubButtonAdd);			   
			
			   this["buttonPress"+x] = function(sender){
			        var senderName = sender.name;
				    var buttonID = senderName.replace(/buttonMainNav/,"");				    
				    selectMain(buttonID);
			        selectSub(buttonID,0);
			        var storyboardNameID = "ButtonCanvasRollOver"+buttonID+"-0";		            
			        silverlightControl.content.findName(storyboardNameID).begin();		            
		            findAndStartVid(buttonID,0);
			   }
			   this["buttonRollOver"+x] = function(sender){
				    var buttonID = sender.name.replace(/buttonMainNav/,"");
				    var txtNameID = "buttonText"+buttonID;
				    var storyboardNameID = "ButtonCanvasRollOver"+buttonID;
				    if(selectedMain != buttonID){					
					    silverlightControl.content.findName(storyboardNameID).begin();
					    overSub = true;    
					    displaySubNav(buttonID);
					    subCheck = -1;
				    }				    
			    }
			    this["buttonRollOut"+x] = function(sender){
				    var buttonID = sender.name.replace(/buttonMainNav/,"");
				    var nameID = "buttonText"+buttonID;
				    var storyboardNameID = "ButtonCanvasRollOut"+buttonID;
				    if(selectedMain != buttonID){					
					    silverlightControl.content.findName(storyboardNameID).begin();
					    overSub = false;
					    subCheck = buttonID;
				    }
			    }
			}
			else
			{			
			    this["buttonPress"+x] = function(sender){
				    var senderName = sender.name;
				    var buttonID = senderName.replace(/buttonMainNav/,"");
				    hideSubNav();	    
				    selectMain(buttonID);
				    findAndStartVid(buttonID,-1);
			    }
			    
			    this["buttonRollOver"+x] = function(sender){
				    var buttonID = sender.name.replace(/buttonMainNav/,"");
				    var txtNameID = "buttonText"+buttonID;
				    var storyboardNameID = "ButtonCanvasRollOver"+buttonID;
				    if(selectedMain != buttonID){					
					    silverlightControl.content.findName(storyboardNameID).begin();
				    }
				    hideSubNav();
			    }
			    this["buttonRollOut"+x] = function(sender){
				    var buttonID = sender.name.replace(/buttonMainNav/,"");
				    var nameID = "buttonText"+buttonID;
				    var storyboardNameID = "ButtonCanvasRollOut"+buttonID;
				    if(selectedMain != buttonID){					
					    silverlightControl.content.findName(storyboardNameID).begin();
				    }
				    if (selectedSub >= 0) {
				        displaySubNav(selectedMain);
				    } else {
				        hideSubNav();
				    }
			    }
		    }
			
			var xamlButtonStoryBoardsIn  = '<Storyboard xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ButtonCanvasRollOver'+x+'">'		
			                                 +'<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ButtonCanvas'+x+'" Storyboard.TargetProperty="(UIElement.Opacity)">'
	                                         +'<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>'				                                         
	                                         +'<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="1"/>'
                                             +'</DoubleAnimationUsingKeyFrames>'			                                 
			                                + '</Storyboard>';
			var xamlButtonStoryboardsInAdd = silverlightControl.content.createFromXaml(xamlButtonStoryBoardsIn);
			silverlightControl.content.findName("Page").resources.add(xamlButtonStoryboardsInAdd);		
			var xamlButtonStoryBoardsOut = '<Storyboard xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ButtonCanvasRollOut'+x+'">'	
			                                 +'<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ButtonCanvas'+x+'" Storyboard.TargetProperty="(UIElement.Opacity)">'
	                                         +'<SplineDoubleKeyFrame KeyTime="00:00:00" Value="1"/>'				                                         
	                                         +'<SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="0"/>'
                                             +'</DoubleAnimationUsingKeyFrames>'			                                 
			                                + '</Storyboard>';
			var xamlButtonStoryBoardsOutAdd = silverlightControl.content.createFromXaml(xamlButtonStoryBoardsOut);
			silverlightControl.content.findName("Page").resources.add(xamlButtonStoryBoardsOutAdd);
		}
	}
	
	// second pass to layout buttons
	canvasX = 4;
	for(var x=0; x<xmlObj.childNodes.length; x++){
		if (xmlObj.childNodes[x].nodeType == 1) {
		    // determine button width
		    var txtBlock = silverlightControl.content.findName("buttonText"+x);
		    var w = txtBlock.actualWidth + 68;
		    txtBlock["Canvas.Left"] = 34;
		    
		    var button = silverlightControl.content.findName("buttonMainNav"+x);
		    button["Width"] = w;
		    button["Canvas.Left"] = canvasX;
		    button = silverlightControl.content.findName("ButtonCanvas"+x);
		    button["Width"] = w;
		    button = silverlightControl.content.findName("buttonRect0-"+x);
		    button["Width"] = w;
		    button = silverlightControl.content.findName("buttonRect1-"+x);
		    button["Width"] = w-2;
		    button = silverlightControl.content.findName("buttonRect2-"+x);
		    button["Width"] = w;
		    button = silverlightControl.content.findName("buttonSep-"+x);
		    button["Canvas.Left"] = w;
		    
		    if (xmlObj.childNodes[x].tagName == "Menu") {
			   topNode = xmlObj.childNodes[x]
			   var canvasSubX = 0;
			   for(var y=0; y<topNode.childNodes.length; y++) {
		            if (topNode.childNodes[y].nodeType == 1) {	
		                // determine button width
		                txtBlock = silverlightControl.content.findName("subButtonText"+x+"-"+y);
		                var subWidth = txtBlock.actualWidth + 68;
		                txtBlock["Canvas.Left"] = 34;
		                
		                button = silverlightControl.content.findName("buttonSubNav"+x+"-"+y);
		                button["Width"] = subWidth;
		                button["Canvas.Left"] = canvasSubX;
		                button = silverlightControl.content.findName("ButtonCanvas"+x+"-"+y);
		                button["Width"] = subWidth;
		                button = silverlightControl.content.findName("buttonRect0-"+x+"-"+y);
		                button["Width"] = subWidth
		                button = silverlightControl.content.findName("buttonRect1-"+x+"-"+y);
		                button["Width"] = subWidth-2;
		                button = silverlightControl.content.findName("buttonRect2-"+x+"-"+y);
		                button["Width"] = subWidth;
		                button = silverlightControl.content.findName("buttonSep-"+x+"-"+y);
		                button["Canvas.Left"] = subWidth+6;
		                if (y==(topNode.childNodes.length-1)) {		                
		                    button["Visibility"] = "Collapsed";
		                }
		                
		                canvasSubX += subWidth + 3;
		            }
		       }
		       
		       // resize and position subnav
		       var subNav = silverlightControl.content.findName("subNav-"+x);
		       subNav["Width"] = canvasSubX - 3;
		       if (((canvasX + w)-(canvasSubX/2)+canvasSubX) > silverlightControl.content.findName("SubNav")["Width"]) {
		           subNav["Canvas.Left"] = silverlightControl.content.findName("SubNav")["Width"] - (canvasSubX + 3);
		       } else {		       		           
		           subNav["Canvas.Left"] = (canvasX + w)-(canvasSubX/2);
		       }
		    }
		    
		    canvasX += w + 1;
		}
	}
}
startSilverlight = function(){
	setVolumeManual(.10);
	silverlightControl.content.findName("ShellCanvas").visibility = "Visible"
	silverlightControl.content.findName("ShellAnimation").begin();
}
pushVid = function(vid) {    
    var m = allVideos[vid].MainID;
    this["buttonRollOver"+m](silverlightControl.content.findName("buttonMainNav"+m));
    this["buttonPress"+m](silverlightControl.content.findName("buttonMainNav"+m));  	
    if (allVideos[vid].SubID >= 0) {
      var s = allVideos[vid].SubID;
      this["buttonRollOver"+m+"_"+s](silverlightControl.content.findName("buttonSubNav"+m+"-"+s));
      this["subButtonPress"+m+"_"+s](silverlightControl.content.findName("buttonSubNav"+m+"-"+s));
    }
}
silverlightReady = function() {
    if (allVideos.length > 0) {
        pushVid(0);
    }	
	clearInterval(vidTimelineID);
	vidTimelineID = setInterval(vidTimeline, 1);
	silverlightControl.content.findName("BufferingArea").visibility = "Visible"	
}
musicLoopRepeat = function(sender, args) {
	var pos = sender.position;
	pos.seconds = 0;
	sender.position = pos;
	sender.play();
}
retry = function (sender, args) {
   	var pos = sender.position;
	sender.Source = allVideos[currentPlayingVid].Src;
	sender.position = pos;
}
