var silverlightControl;
var xmlDoc;
var forLocalUse;
var mainControl;
var xmlDoc;
var movieArray = new Array();
var currentMovie;
var movieTimelineID;
var callToActionID;
var isMenuOpen = false;
var isMouseDragging = false;
var inPause = false;
var isCCOn = false;
var isBackgroundShowing = true;
var isCallToActionShowing = false;
var isXMLInitialized = false;


if (!window.Silverlight) {
	window.Silverlight = {};
}
if (!window.Project_1)
	Project_1 = {};
createSilverlight = function(sourceXAML, versionNumber) {
	var scene = new Project_1.Page();
	Silverlight.createObjectEx({source: sourceXAML, parentElement: document.getElementById("silverlightControlHost"), id: "SilverlightControl", properties: { width: "700px", height: "550px", version: versionNumber }, events: { onLoad: Silverlight.createDelegate( scene, scene.handleLoad )}});
};
Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}
Project_1.Page = function() {
}
Project_1.Page.prototype = {
	handleLoad: function(control, userContext, rootElement) 
	{
		mainControl = control;
		
		modifyControlSize();
		toggleMenu();
		//mainControl.content.findName("backgroundMusic").Volume = (mainControl.content.findName("mediaPlayer").Volume)*backgroundMusicVolumeLevel;
		//mainControl.content.findName("backgroundMusic").Source = backgroundMusic;
		loadXMLDoc();
		clearInterval(movieTimelineID);
		currentMovie = -1;
	    movieTimelineID = setInterval(updateMovieTimeline, 1);
	    //currentMovie = -1;
		mainControl.content.findName("Demo").addEventListener("KeyUp", onKeyPress);	
		mainControl.content.onResize = modifyControlSize;
		setVolume(mainControl.content.findName("mediaPlayer").Volume);
		//highlights the first nav item
		//if(mainControl.content.findName("Nav" + (currentMovie + 1) + "Over")){
		    //mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
		//}
	}
}
//*************************************************************************************
//Begin Resizing functions
//*************************************************************************************
resizeBody = function() {
//	
//	var w = screen.width;
//	var h = screen.height;
//	//alert(w);
//	var silverlightObj = document.getElementById('silverlightControlHost');
//	silverlightObj.style.minWidth = silverlightObjWidth+'px';
//	silverlightObj.style.minHeight = silverlightObjHeight+'px';
//	if(w < (silverlightObjWidth)) {
//		silverlightObj.style.width = silverlightObjWidth+'px';
//	} else {
//		silverlightObj.style.width = "100%";
//	}
//	if(h < (silverlightObjHeight)) {
//		silverlightObj.style.height = silverlightObjHeight+'px';
//	} else {
//		silverlightObj.style.height = "100%";
//	}
//	
//	mainControl.content.findName('Demo')['Canvas.Left'] = Math.round((w-(silverlightObjWidth))/2);
//	mainControl.content.findName('Demo')['Canvas.Top'] = Math.round((h-(silverlightObjHeight))/2);
}
modifyControlSize = function() {
  
//    var w = document.body.clientWidth;
//	var h = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
//	var silverlightObj = document.getElementById('silverlightControlHost');
//	silverlightObj.style.minWidth = silverlightObjWidth+'px';
//	silverlightObj.style.minHeight = (silverlightObjHeight) +'px';
//    if(w <= silverlightObjWidth){
//        silverlightObj.style.width = silverlightObjWidth + "px";
//        mainControl.content.findName('Demo')['Canvas.Left'] = 0;
//    }
//    else{
//        silverlightObj.style.width = "100%";
//	    mainControl.content.findName('Demo')['Canvas.Left'] = Math.round((w-(silverlightObjWidth))/2);
//	}
//	if (h <= silverlightObjHeight){
//	    //alert("height");
//	    silverlightObj.style.height = (silverlightObjHeight + 40) + "px";
//	    mainControl.content.findName('Demo')['Canvas.Top'] = 40;
//	   
//    }
//    else{
//        silverlightObj.style.height = "100%";
//	    mainControl.content.findName('Demo')['Canvas.Top'] = 40;
//	    mainControl.content.findName('Demo')['Canvas.Top'] = Math.round(((h+40)-(silverlightObjHeight))/2);
//	}
	
}
//******************************************************************************************
//End Resizing Functions
//******************************************************************************************

//******************************************************************************************
//Begin XML Functions
//******************************************************************************************
loadXMLDoc = function() {
	if (location.protocol == "http:") {
		forLocalUse = false;
	} else {
		forLocalUse = true;
	}
	if (forLocalUse) {
		if (document.implementation && document.implementation.createDocument) {
			xmlDoc = document.implementation.createDocument('', '', null);
			xmlDoc.onload = parseXML;
		} else if (window.ActiveXObject) {
		    
			xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
			xmlDoc.async = "false";
			
			xmlDoc.onreadystatechange = xmlLoadStatus;
	 	} else {
			alert('This browser does not support some or all the technology this page uses. Please use another browser.');
			return;
		}
		xmlDoc.load(xmlURL);
	} else {
		if(window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
				xmlDoc = new XMLHttpRequest();
			} catch(e) {
				xmlDoc = false;
			}
		} else if(window.ActiveXObject) {
			try {
				xmlDoc = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try {
					xmlDoc = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					xmlDoc = false;
				}
			}
		}
		if(xmlDoc) {
			xmlDoc.onreadystatechange = xmlLoadStatus;
			xmlDoc.open('GET', xmlURL, true);
			xmlDoc.send('');
		}
		
	}	
}
xmlLoadStatus = function() {
	if (xmlDoc.readyState == 4) {
		parseXML();
	}
}
parseXML = function() {
	var movieCount = 0;
	var ccItems = 0;
	
	if (forLocalUse) {
	    for (var x=0; x<xmlDoc.documentElement.childNodes.length; x++) {
    	    
		    if (xmlDoc.documentElement.childNodes[x].nodeType == 1) {
			    movieArray[movieCount] = new Array(4);
    			
			    movieArray[movieCount][0] = xmlDoc.documentElement.childNodes[x].getAttribute("title");
			    if(xmlDoc.documentElement.childNodes[x].getAttribute("src") != null){
			        movieArray[movieCount][1] = xmlDoc.documentElement.childNodes[x].getAttribute("src");
			        ccItems = 0
			        movieArray[movieCount][2] = new Array();
			        for (var i=0; i < xmlDoc.documentElement.childNodes[x].childNodes.length; i++) {
				        if (xmlDoc.documentElement.childNodes[x].childNodes[i].nodeType == 1) {
					       movieArray[movieCount][2][ccItems] = new Array();
					       movieArray[movieCount][2][ccItems][0] = xmlDoc.documentElement.childNodes[x].childNodes[i].getAttribute("start");
					       movieArray[movieCount][2][ccItems][1] = xmlDoc.documentElement.childNodes[x].childNodes[i].firstChild.nodeValue;
					        ccItems++;
				        }
			        }
			    }
			    movieCount++;
		    }
    		
	    }
	}
	else{
	    
	    for (var x=0; x<xmlDoc.responseXML.documentElement.childNodes.length; x++) {
    	    
		    if (xmlDoc.responseXML.documentElement.childNodes[x].nodeType == 1) {
			    movieArray[movieCount] = new Array(4);
    			
			    movieArray[movieCount][0] = xmlDoc.responseXML.documentElement.childNodes[x].getAttribute("title");
			    if(xmlDoc.responseXML.documentElement.childNodes[x].getAttribute("src") != null){
			        movieArray[movieCount][1] = xmlDoc.responseXML.documentElement.childNodes[x].getAttribute("src");
			        ccItems = 0
			        movieArray[movieCount][2] = new Array();
			        for (var i=0; i < xmlDoc.responseXML.documentElement.childNodes[x].childNodes.length; i++) {
				        if (xmlDoc.responseXML.documentElement.childNodes[x].childNodes[i].nodeType == 1) {
					       movieArray[movieCount][2][ccItems] = new Array();
					       movieArray[movieCount][2][ccItems][0] = xmlDoc.responseXML.documentElement.childNodes[x].childNodes[i].getAttribute("start");
					       movieArray[movieCount][2][ccItems][1] = xmlDoc.responseXML.documentElement.childNodes[x].childNodes[i].firstChild.nodeValue;
					        ccItems++;
				        }
			        }
			    }
			    movieCount++;
		    }
    		
	    }
	}
	isXMLInitialized = true;
	currentMovie = -1;
}

//******************************************************************************************
//End XML Functions
//******************************************************************************************
//******************************************************************************************
//Begin Movie Playback functions
//******************************************************************************************
playCurrentMovie = function(){
    if(inPause == false){
        mainControl.content.findName("mediaPlayer").AutoPlay = "False"; 
        //alert("test");
        mainControl.content.findName("mediaPlayer").Source = movieArray[currentMovie][1];
         
    }
    else{
        inPause = false;
    }
    mainControl.content.findName("mediaPlayer").Play();
    mainControl.content.findName("mediaPlayer").AutoPlay = "True"; 
    mainControl.content.findName("mediaPlayer").Visibility = "Visible"; 
    
}

playNextMovie = function(){
   
    if(currentMovie < (movieArray.length - 1) && currentMovie >= 0){
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
        currentMovie++;
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
        inPause = false;
        
        playCurrentMovie();
        //hideBackground();
    }
    else if(currentMovie == (movieArray.length - 1)){
    //if last movie is done, display nav items
        mainControl.content.findName("CallToActionImage").Visibility = "Visible";
        if(!isMenuOpen){
            
            toggleMenu();
        }
        displayBackground();
        mainControl.content.findName("mediaPlayer").stop();
        //highlight first nav item
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
        //mainControl.content.findName("Nav1Over").Opacity = 1;
        currentMovie = -1;
        pauseMovie();
    }
    else if(currentMovie == -1){
        currentMovie++;
        mainControl.content.findName("CallToActionImage").Visibility = "Collapsed";
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
        inPause = false;
        
        playCurrentMovie();
        if(isMenuOpen){
		    toggleMenu();
		    hideBackground();
		}
	    if (mainControl.content.findName("Play")) {
		    mainControl.content.findName("Play").Visibility = "Collapsed";
	    }
    	
	    if (mainControl.content.findName("Pause")) {
		    mainControl.content.findName("Pause").Visibility = "Visible";
	    }
    }
    
}

playPreviousMovie = function(){
    
    if(currentMovie > 0){
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
        currentMovie--;
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
        playCurrentMovie();
        hideBackground();
    }
    else if(currentMovie == 0){
        mainControl.content.findName("CallToActionImage").Visibility = "Visible";
        //if first movie, display nav items
         if(!isMenuOpen){
            toggleMenu();
           }
            displayBackground();
            mainControl.content.findName("mediaPlayer").stop();
            //highlight first nav item
            mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
            //mainControl.content.findName("Nav1Over").Opacity = 1;
            currentMovie = -1;
            pauseMovie();
    }
    else if(currentMovie == -1){
        currentMovie++;
        mainControl.content.findName("CallToActionImage").Visibility = "Collapsed";
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
        inPause = false;
        
        playCurrentMovie();
        if(isMenuOpen){
		    toggleMenu();
		    hideBackground();
		}
	    if (mainControl.content.findName("Play")) {
		    mainControl.content.findName("Play").Visibility = "Collapsed";
	    }
    	
	    if (mainControl.content.findName("Pause")) {
		    mainControl.content.findName("Pause").Visibility = "Visible";
	    }
    }
}
pauseMovie = function(){
	mainControl.content.findName("mediaPlayer").pause();
	inPause = true;
	if (mainControl.content.findName("MusicLoop")) {
		mainControl.content.findName("MusicLoop").pause();
	}
	if (mainControl.content.findName("Pause")) {
		mainControl.content.findName("Pause").Visibility = "Collapsed";
	}
	
	if (mainControl.content.findName("Play")) {
		mainControl.content.findName("Play").Visibility = "Visible";
	}
	
}
playMovie = function(){
    if(currentMovie == -1){
        currentMovie = 0;
    }
    mainControl.content.findName("CallToActionImage").Visibility = "Collapsed";
	mainControl.content.findName("CallToActionButton").Visibility = "Collapsed";
	if(mainControl.content.findName("mediaPlayer").Visibility == "Collapsed"){
        mainControl.content.findName("mediaPlayer").Visibility = "Visible";
        
    }
    if(mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity < 1){
        mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 1;
    }
	playCurrentMovie();
	
	//if (mainControl.content.findName("MusicLoop")) {
	//	mainControl.content.findName("MusicLoop").play();
	//}
	if (mainControl.content.findName("Play")) {
		mainControl.content.findName("Play").Visibility = "Collapsed";
	}
	
	if (mainControl.content.findName("Pause")) {
		mainControl.content.findName("Pause").Visibility = "Visible";
	}
	
}
//******************************************************************************************
//End Movie Playback functions
//******************************************************************************************

//***********************************************************************************
//Begin Timing functions
//***********************************************************************************
updateMovieTimeline = function() {
    var noneSelected = 0;
    if(isXMLInitialized == true){
        if(currentMovie == -1){
            noneSelected = 1;
            currentMovie = 0;
        }
	    var p = mainControl.content.findName("mediaPlayer").position.seconds;
	    var totalTime = mainControl.content.findName("mediaPlayer").naturalDuration.seconds;
	    var timeRemaining = totalTime - p;
	    var indicatorWidth = mainControl.content.findName("TimelineEmptyBar").Width - mainControl.content.findName("TimelineHandle").Width;
	    if (isMouseDragging == false && mainControl.content.findName("TimelineEmptyBar")) {
		    var d = mainControl.content.findName("mediaPlayer").naturalDuration.seconds;
		    var w = mainControl.content.findName("TimelineEmptyBar").Width-mainControl.content.findName("TimelineHandle").Width;
		    if(d != 0 && p < d){
			    mainControl.content.findName("TimelineHandle")["Canvas.Left"] = ((p*w)/d)+(mainControl.content.findName("TimelineEmptyBar")["Canvas.Left"]);
			    mainControl.content.findName("TimelineFullBar").Width = ((p*w)/d);
		    }
	    }
    	
	    for (var cc=0; cc<movieArray[currentMovie][2].length; cc++) {	
		        var syncStart = movieArray[currentMovie][2][cc][0];
		        if(syncStart <= p){
			        var ccTxt = movieArray[currentMovie][2][cc][1];
			        setTextElement("CCText", ccTxt, "center");
		        }
	        }
	    if(mainControl.content.findName("TimeRemaining")){
		    mainControl.content.findName("TimeRemaining").Text = getTimeMinutes(timeRemaining)+":"+getTimeSeconds(timeRemaining);
	    }
	    if(noneSelected == 1){
            currentMovie = -1;
            noneSelected = 0;
        }
    }
}

getTimeMinutes = function (t) {
	var timeString = String(Math.floor((t/60))%60);
	var formattedTime = formatTimeString(timeString);
	return formattedTime;
}
getTimeSeconds = function (t) {
	var timeString = String(Math.floor(t)%60);
	var formattedTime = formatTimeString(timeString);
	return formattedTime;
}
formatTimeString = function (t) {
	if (t == "NaN") {
		t = "0";
	}
	if (t.length < 2) {
		t = "0"+t;
	}
	return t;
}
//*********************************************************************************
//End Timing Functions
//*********************************************************************************

//*********************************************************************************
//Start Misc Functions
//*********************************************************************************
setTextElement = function(textElement, txt, align) {
	if (mainControl.content.findName(textElement)) {
		mainControl.content.findName(textElement).Text = txt;
		var w = mainControl.content.findName(textElement).actualWidth;
		if (align == "right") {
			mainControl.content.findName(textElement)["Canvas.Left"] = mainControl.content.findName(textElement).width-w;
		} else if (align == "center") {
			mainControl.content.findName(textElement)["Canvas.Left"] = (mainControl.content.findName(textElement).width-w)/2;
		}
	}
}
toggleMenu = function(){
    if(isMenuOpen){
        mainControl.content.findName("Close").Begin();
        isMenuOpen = false;
    }
    else{
        mainControl.content.findName("Open").Begin();
        isMenuOpen =true;
    }
}

//hides the video player and displays the background image
displayBackground = function(){
    if(!isBackgroundShowing){
        
        mainControl.content.findName("HideMedia").begin();
        isBackgroundShowing = true;
    }
    if(isCCOn){
        isCCOn = false;
		mainControl.content.findName("HideCC").begin();
	}
}

//hides the video player and displays the call to action image
displayCallToAction = function(){
    if(!isCallToActionShowing){
        
        mainControl.content.findName("HideMedia").begin();
        isCallToActionShowing = true;
    }
    if(isCCOn){
        isCCOn = false;
		mainControl.content.findName("HideCC").begin();
	}
}

//hides the background image and displays the video player
hideBackground = function(){
    if(isBackgroundShowing){
        mainControl.content.findName("ShowMedia").begin();
        isBackgroundShowing = false;
    }
}
//******************************************************************************************
//End Misc Functions
//******************************************************************************************

//**********************************************************************************
//Begin volume control functions
//**********************************************************************************
mute = function() {
	mainControl.content.findName("mediaPlayer").IsMuted = "True";
	//mainControl.content.findName("backgroundMusic").IsMuted = "True";
	
	if (mainControl.content.findName("UnMuteButton")) {
		mainControl.content.findName("UnMuteButton").visibility = "Visible";
	}
}
unmute = function() {
	mainControl.content.findName("mediaPlayer").IsMuted = "False";
	//mainControl.content.findName("backgroundMusic").IsMuted = "False";
	if (mainControl.content.findName("UnMuteButton")) {
		mainControl.content.findName("UnMuteButton").visibility = "Collapsed";
	}
}
musicMute = function() {
	mainControl.content.findName("backgroundMusic").IsMuted = "True";
	
	if (mainControl.content.findName("MusicMute")) {
		mainControl.content.findName("MusicUnMute").visibility = "Collapsed";
	}
}
musicUnmute = function() {
	mainControl.content.findName("backgroundMusic").IsMuted = "False";
	
	if (mainControl.content.findName("MusicMute")) {
		mainControl.content.findName("MusicUnMute").visibility = "Visible";
	}
}
increaseVolume = function () {
	var v = mainControl.content.findName("mediaPlayer").Volume;
	v += .1;
	if (v > 1) {
		v = 1;
	}
	setVolume(v);
}
decreaseVolume = function () {
	var v = mainControl.content.findName("mediaPlayer").Volume;
	v -= .1;
	if (v < 0) {
		v = 0;
	}
	setVolume(v);
}
setVolume = function(v) {
	mainControl.content.findName("mediaPlayer").Volume = v;
	if (mainControl.content.findName("backgroundMusic")) {
		mainControl.content.findName("backgroundMusic").Volume = (v)*backgroundMusicVolumeLevel;
	}
	if (mainControl.content.findName("VolumeEmpty")) {
		var posSet = (((mainControl.content.findName("VolumeEmpty").Width-mainControl.content.findName("VolumeHandle").Width)*v)/1);
		mainControl.content.findName("VolumeHandle")["Canvas.Left"] = posSet;
		mainControl.content.findName("VolumeFullBar").Width = posSet;
	}
};
//**************************************************************************************************
//End Volume Control functions
//**************************************************************************************************

//**************************************************************************************************
//Begin Event Handlers--referenced in xaml
//**************************************************************************************************

//handles mouse rollover
onMouseEnter = function(sender, mouseEventArgs) {
    
	if(mainControl.content.findName(sender.name + "Over")){
	    clearInterval(window[sender.name+"Over"]);
	    window[sender.name+"Over"] = setInterval( function () {
	        var theName = sender.Name;
	        mainControl.content.findName(sender.name+"Over").Opacity += 0.05;
		    if (mainControl.content.findName(sender.name+"Over").Opacity >= 1) {
			    mainControl.content.findName(sender.name+"Over").Opacity = 1;
			    clearInterval(window[sender.name+"Over"]);
		    }
	    }, 1);
	}
}

//handles mouse rollout
onMouseLeave = function(sender, mouseEventArgs) {
	
	if(sender.name == "TimelineEmpty"){
		isMouseDragging = false;
	} else if(sender.name == "VolumeEmpty") {
		isMouseDragging = false;
	} else{
	
	    if(mainControl.content.findName(sender.name + "Over")){
	        clearInterval(window[sender.name+"Over"]);
	        window[sender.name+"Over"] = setInterval( function () {
	            var theName = sender.Name;
	            //don't change opacity for the playing nav item
	            if(sender.name.substr(3,1) != currentMovie + 1){
		            mainControl.content.findName(sender.name+"Over").Opacity += -(0.05);
		            if (mainControl.content.findName(sender.name+"Over").Opacity <= 0) {
			            mainControl.content.findName(sender.name+"Over").Opacity = 0;
			            clearInterval(window[sender.name+"Over"]);
		            }
		        }
	        }, 1);
	    }
	}
}

//handles various click events
onMouseLeftButtonUp = function(sender, mouseEventArgs) {
	if (sender.name == "Previous") {
		inPause = false;
		playPreviousMovie();
		
	} else if (sender.name == "Next") {
	    //mainControl.content.findName("mediaPlayer").pause();
	    //mainControl.content.findName("mediaPlayer").Visibility = "Collapsed";
	    //alert("here");
	     //mainControl.content.findName("mediaPlayer").stop();
	    
		inPause = false;
		playNextMovie();
		
	} else if (sender.name == "Play") {
		playMovie();
		if(isMenuOpen){
		    toggleMenu();
		    hideBackground();
		}
	} else if (sender.name == "Pause") {
		pauseMovie();
	} else if (sender.name.substr(0,3) == "Nav"){
	    
	    var navItem = sender.name.substr(3,1);
	    if(currentMovie >= 0){
	        if(mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity > 0){
                mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
            }
        }
	    currentMovie = navItem - 1;
	    inPause = false;
	    playMovie();
	    toggleMenu();
	    hideBackground();
	} else if (sender.name == "MenuButton"){
	    toggleMenu();
	} else if (sender.name == "TimelineEmpty") {
		isMouseDragging = false;
	} else if (sender.name == "VolumeEmpty") {
		isMouseDragging = false;
	} else if (sender.name == "MuteButton") {
		mute();
	} else if (sender.name == "UnMuteButton") {
		unmute();
	} else if (sender.name == "MusicMute") {
	
		musicUnmute();
	} else if (sender.name == "MusicUnMute") {
	    
		musicMute();
	} else if (sender.name == "CCButton") {
		if(isCCOn == false) {
			isCCOn = true;
			mainControl.content.findName("ShowCC").begin();
		} else {
			isCCOn = false;
			mainControl.content.findName("HideCC").begin();
		}
	} else if (sender.name == "FullScreenButton") {
		mainControl.content.fullScreen = !mainControl.content.fullScreen;
		resizeBody();
	} else if (sender.name == "IncreaseVolume") { 
		increaseVolume();
	} else if (sender.name == "DecreaseVolume") {
		decreaseVolume();
	}
}
onMouseMove = function(sender, mouseEventArgs) {
    
	if (sender.name == "TimelineEmpty" && isMouseDragging == true) {
		
		var coordinate = mouseEventArgs.getPosition(sender).x;	
		
		var movieDuration = mainControl.content.findName("mediaPlayer").naturalDuration.seconds;
		var indicatorWidth = mainControl.content.findName("TimelineEmptyBar").Width - mainControl.content.findName("TimelineHandle").Width;
		
		if (movieDuration != 0) {
			var dragPos = coordinate+(mainControl.content.findName("TimelineEmptyBar")["Canvas.Left"]);
             //alert(coordinate + ":" + (mainControl.content.findName("TimelineEmptyBar")["Canvas.Left"] + mainControl.content.findName("TimelineEmptyBar").Width));
			if (coordinate <= (indicatorWidth + mainControl.content.findName("TimelineEmptyBar")["Canvas.Left"])){
				mainControl.content.findName("TimelineHandle")["Canvas.Left"] = dragPos;
				mainControl.content.findName("TimelineFullBar").Width = coordinate;
				var seconds = ((movieDuration*coordinate)/indicatorWidth);
				var pos = mainControl.content.findName("mediaPlayer").position;
				pos.seconds = seconds;
				mainControl.content.findName("mediaPlayer").position = pos;
			}
		}
	} else if(sender.name == "VolumeEmpty" && isMouseDragging == true) {
	    
		var coordinate = mouseEventArgs.getPosition(sender).x + mainControl.content.findName("VolumeEmpty")["Canvas.Left"];
		var dragVolLeftStart = mainControl.content.findName("VolumeEmpty")["Canvas.Left"];
		var dragVolLeftEnd = mainControl.content.findName("VolumeEmpty")["Canvas.Left"]+mainControl.content.findName("VolumeEmpty").Width;
		//alert(coordinate + ":" + dragVolLeftStart + ":" + dragVolLeftEnd);
		if (coordinate >= dragVolLeftStart && coordinate <= dragVolLeftEnd) {
			var v = Math.round(((coordinate-dragVolLeftStart)*100)/mainControl.content.findName("VolumeEmpty").Width);
			setVolume((v/100));
		}
	} 
}
onKeyPress = function(sender, keyEventArgs) {
	if (keyEventArgs.key == 64) {
		mainControl.content.fullScreen = !mainControl.content.fullScreen;
	}
}
onMouseLeftButtonDown = function(sender, mouseEventArgs) {
	if(sender.name == "TimelineEmpty"){
		isMouseDragging = true;
	} else if(sender.name == "VolumeEmpty") {
		isMouseDragging = true;
	}
}
onMediaFailed = function(sender, eventArgs) {
	var pos = sender.position;
	sender.Source = movieArray[currentMovie][1];
	sender.position = pos;
}

onWindowResized = function(sender, eventArgs) {
	modifyControlSize();
}

onMediaEnded = function(){
    mainControl.content.findName("CallToActionImage").Visibility = "Visible";
    mainControl.content.findName("CallToActionButton").Visibility = "Visible";
    mainControl.content.findName("Nav" + (currentMovie + 1) + "Over").Opacity = 0;
    if(!isMenuOpen){
		    toggleMenu();
		    //hideBackground();
	}
	if (mainControl.content.findName("Pause")) {
		mainControl.content.findName("Pause").Visibility = "Collapsed";
	}
	
	if (mainControl.content.findName("Play")) {
		mainControl.content.findName("Play").Visibility = "Visible";
		mainControl.content.findName("PlayOver").Opacity = 0;
	}
	currentMovie = -1;
    //if the last movie has finished, make sure no menu items are highlighted
    
    
}

hideCallToAction = function(){
    mainControl.content.findName("CallToActionImage").Visibility = "Collapsed";
    mainControl.content.findName("CallToActionButton").Visibility = "Collapsed";
    playNextMovie();
}

actionbutton = function() {
	open("http://www.microsoft.com/ocs","_blank");
}

//*******************************************************************************************
//End Event Handlers
//*******************************************************************************************

function media_state_changed(sender, args)
{
   // Get the TextBlock object to display the current status.
   var mediaStateTextBlock = sender.findName("mediaStateTextBlock");
   // Get the MediaElement object
   var media = sender.findName("mediaPlayer");

   mediaStateTextBlock.Text = media.CurrentState;
}

