﻿var silverlightControl;
var xmlDoc;
var forLocalUse;
var currentPlayingVid;
var vidTimelineID;
var objectFadingInID;
var objectFadingOutID;
var currentVidSource;
var vidInitialized = false;
var doDragTime = false;
var doDragVol = false;
var playerLoop;
var CCOn = false;
var ActionOn = false;
var _global = this;
var musicLoopInitialized = false;
var playlistXMLArray = new Array();
var bannerShowAnimationStarted = false;
var bannerHideAnimationStarted = false;
var playerFinished = false;
if (!window.Silverlight) {
	window.Silverlight = {};
}
if (!window.DemoPlayerEngine) {
	window.DemoPlayerEngine = {};
}
createSilverlight = function(sourceXAML, versionNumber) {
	var scene = new DemoPlayerEngine.Page();
	Silverlight.createObjectEx({source: sourceXAML, parentElement: document.getElementById("1f100660-da4a-4948-9096-6e48686bb170"), id: "SilverlightControl", properties: { width: "808", height: "560", version: versionNumber }, events: { onLoad: Silverlight.createDelegate( scene, scene.handleLoad )}});
};
Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}
DemoPlayerEngine.Page = function() {
}
DemoPlayerEngine.Page.prototype = {
	handleLoad: function(control, userContext, rootElement) 
	{	
		silverlightControl = control;
		
		silverlightControl.content.findName("DemoPlayer").addEventListener("KeyUp", keyboardEvents);
		
		silverlightControl.content.onResize = onResized; 
		silverlightControl.content.onFullScreenChange = onBodyResized;
		loadXMLDoc('/global/systemcenter/virtualmachinemanager/en/us/RichMedia/demo/XML/DemoPlaylist.xml');
		
	
	}
}
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 = compileXML;
		} 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) {
		compileXML();
	}
}
compileXML = function() {
	var totalItems = 0;
	var ccItems = 0;
	if (forLocalUse) {
		for (var x=0; x<xmlDoc.documentElement.childNodes.length; x++) {
			if (xmlDoc.documentElement.childNodes[x].nodeType == 1) {
				playlistXMLArray[totalItems] = new Array(3);
				playlistXMLArray[totalItems][0] = xmlDoc.documentElement.childNodes[x].getAttribute("name");
				playlistXMLArray[totalItems][1] = xmlDoc.documentElement.childNodes[x].getAttribute("src");
				playlistXMLArray[totalItems][2] = new Array();
				ccItems = 0;
				for (var c=0; c<xmlDoc.documentElement.childNodes[x].childNodes.length; c++) {
					if (xmlDoc.documentElement.childNodes[x].childNodes[c].nodeType == 1) {
						playlistXMLArray[totalItems][2][ccItems] = new Array();
						playlistXMLArray[totalItems][2][ccItems][0] = xmlDoc.documentElement.childNodes[x].childNodes[c].getAttribute("start");
						playlistXMLArray[totalItems][2][ccItems][1] = xmlDoc.documentElement.childNodes[x].childNodes[c].firstChild.nodeValue;
						ccItems++;
					}
				}
				totalItems++;
			}
		}
	} else {
		for (var x=0; x<xmlDoc.responseXML.documentElement.childNodes.length; x++) {
			if (xmlDoc.responseXML.documentElement.childNodes[x].nodeType == 1) {
				playlistXMLArray[totalItems] = new Array(3);
				playlistXMLArray[totalItems][0] = xmlDoc.responseXML.documentElement.childNodes[x].getAttribute("name");
				playlistXMLArray[totalItems][1] = xmlDoc.responseXML.documentElement.childNodes[x].getAttribute("src");
				playlistXMLArray[totalItems][2] = new Array();
				ccItems = 0;
				for (var c=0; c<xmlDoc.responseXML.documentElement.childNodes[x].childNodes.length; c++) {
					if (xmlDoc.responseXML.documentElement.childNodes[x].childNodes[c].nodeType == 1) {
						playlistXMLArray[totalItems][2][ccItems] = new Array();
						playlistXMLArray[totalItems][2][ccItems][0] = xmlDoc.responseXML.documentElement.childNodes[x].childNodes[c].getAttribute("start");
						playlistXMLArray[totalItems][2][ccItems][1] = xmlDoc.responseXML.documentElement.childNodes[x].childNodes[c].firstChild.nodeValue;
						ccItems++;
					}
				}
				totalItems++;
			}
		}
	}
	if (musicLoopURL != null && musicLoopURL != "" && silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").Source = musicLoopURL;
	}
	musicLoopInitialized = true;
	startSilverlightDemo();
}
startSilverlightDemo = function() {
    
	currentPlayingVid = 0;
	clearInterval(vidTimelineID);
	vidTimelineID = setInterval(vidTimeline, 1);
	setVolumeManual(.5);
	unmuteAll();
	unmuteMusic();
//	createDemoNavigation();
	startVid(currentPlayingVid);
}
createDemoNavigation = function() {
	if (silverlightControl.content.findName("NavigationWindow")) {
		var w = Math.round(silverlightControl.content.findName("NavigationWindow").Width/playlistXMLArray.length);
		var h = Math.round(silverlightControl.content.findName("NavigationWindow").Height/playlistXMLArray.length);
		for (var x=0; x<playlistXMLArray.length; x++) {
			var txt = playlistXMLArray[x][0];
			var xamlString = eval(navigationButtonXAML);
			xamlAdd = silverlightControl.content.createFromXaml(xamlString);
		  silverlightControl.content.findName("NavigationWindow").children.insert(x, xamlAdd);
		  setFont("NavigationTxt"+x, txt, navigationButtonTextAlignment);
		}
	}
}
demoNavigationReturnID = function(sender) {
	var senderName = sender.name;
	var buttonID = senderName.replace(/NavigationButton/,"");
	return buttonID;
}
demoNavigationClick = function(sender, mouseEventArgs) {
	startVid(demoNavigationReturnID(sender));
}
demoNavigationRollOver = function(sender, mouseEventArgs) {
	if (currentPlayingVid != demoNavigationReturnID(sender)) {
		rollOver(sender, mouseEventArgs);
	}
}
demoNavigationRollOut = function(sender, mouseEventArgs) {
	if (currentPlayingVid != demoNavigationReturnID(sender)) {
		rollOut(sender, mouseEventArgs);
	}
}
onBodyResized = function() {
	var w = document.body.clientWidth;
	var h = 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';
	} else {
		silverlightObj.style.width = "100%";
	}
	if(h < (silverlightObjHeight)) {
		silverlightObj.style.height = silverlightObjHeight+'px';
	} else {
		silverlightObj.style.height = "100%";
	}
	updateLayoutSize(silverlightControl.content.actualWidth, silverlightControl.content.actualHeight);
}
onResized = function(sender, eventArgs) {

    //1f100660-da4a-4948-9096-6e48686bb170
	var silverlightObj = document.getElementById('1f100660-da4a-4948-9096-6e48686bb170');//('SilverlightControlHost');//adding SL Id removes JS errors but causes video to be off centered
	var w = silverlightObj.style.width;
	var h = silverlightObj.style.height;
	
	 
	 if(w.indexOf('px') != -1){
		w = w.slice(0,-2);
	}
	if(h.indexOf('px') != -1){
		h = h.slice(0,-2);
	}  
	if(w == "100%"){
		w = document.body.clientWidth;
	}
	if(h == "100%"){
		h = document.body.clientHeight;
	}
	 
	
	//updateLayoutSize(w, h);
	
	
}
updateLayoutSize = function(w, h) {
	silverlightControl.content.findName('DemoPlayer').Width = w;
	silverlightControl.content.findName('DemoPlayer').Height = h;
	silverlightControl.content.findName('DemoPlayer')['Canvas.Left'] = Math.round((w-(silverlightObjWidth))/2);
	silverlightControl.content.findName('DemoPlayer')['Canvas.Top'] = Math.round((h-(silverlightObjHeight))/2);
	if (silverlightControl.content.findName('DemoBackground')) {
		silverlightControl.content.findName('DemoBackground').Width = w;
		silverlightControl.content.findName('DemoBackground').Height = h;
	}
}
startVid = function(vid) {
	vidInitialized = true;
	if (silverlightControl.content.findName("PlayerEndButton")) {
		silverlightControl.content.findName("PlayerEndButton").Visibility = "Collapsed";
	}
	clearInterval(window["NavigationButton"+vid+"Roll"]);
	currentPlayingVid = vid;
	if (silverlightControl.content.findName("NavigationWindow")) {
		for (var x=0; x<playlistXMLArray.length; x++) {
		    silverlightControl.content.findName("NavigationButton"+x+"Roll").Opacity = 0;			
		}
		silverlightControl.content.findName("NavigationButton"+vid+"Roll").Opacity = 1;
	}
	var vidSource = playlistXMLArray[vid][1];
	if (vidSource != null && vidSource != "") {
		currentVidSource = vidSource;
		silverlightControl.content.findName("PlayerWindow").Source = vidSource;
		vidPlay();
	}
	//alert(silverlightControl.content.findName("PlayerWindow").naturalDuration.seconds);
	
}
prevVid = function(){
	if(currentPlayingVid != 0) {
		currentPlayingVid--;
		startVid(currentPlayingVid);
	}
}
nextVid = function(){
	if(currentPlayingVid < playlistXMLArray.length-1) {
		currentPlayingVid++;
		startVid(currentPlayingVid);
	}
}
vidPlay = function(){
	silverlightControl.content.findName("PlayerWindow").play();
	if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").play();
	}
	if (silverlightControl.content.findName("PauseButton")) {
		silverlightControl.content.findName("PauseButton").Visibility = "Visible";
	}
	if (silverlightControl.content.findName("PauseIcon")) {
		silverlightControl.content.findName("PauseIcon").Visibility = "Visible";
	}
	if (silverlightControl.content.findName("PlayButton")) {
		silverlightControl.content.findName("PlayButton").Visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("PlayIcon")) {
		silverlightControl.content.findName("PlayIcon").Visibility = "Collapsed";
	}
    //hideBanner(5);
}
vidPause = function(){
	silverlightControl.content.findName("PlayerWindow").pause();
	if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").pause();
	}
	if (silverlightControl.content.findName("PauseButton")) {
		silverlightControl.content.findName("PauseButton").Visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("PauseIcon")) {
		silverlightControl.content.findName("PauseIcon").Visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("PlayButton")) {
		silverlightControl.content.findName("PlayButton").Visibility = "Visible";
	}
	if (silverlightControl.content.findName("PlayIcon")) {
		silverlightControl.content.findName("PlayIcon").Visibility = "Visible";
	}
}
vidEnd = function(){
	if(vidInitialized){
		if(currentPlayingVid == playlistXMLArray.length-1) {
			if (playerLoop == true) {
				startVid(0);
			} else {
			    playerFinished = true;
				if (silverlightControl.content.findName("PlayerEndButton")) {
					silverlightControl.content.findName("PlayerEndButton").Visibility = "Visible";
				}
				if (silverlightControl.content.findName("PauseButton")) {
            		silverlightControl.content.findName("PauseButton").Visibility = "Collapsed";
	            }
	            if (silverlightControl.content.findName("PauseIcon")) {
		            silverlightControl.content.findName("PauseIcon").Visibility = "Collapsed";
	            }
	            if (silverlightControl.content.findName("PlayButton")) {
		            silverlightControl.content.findName("PlayButton").Visibility = "Visible";
	            }
	            if (silverlightControl.content.findName("PlayIcon")) {
		            silverlightControl.content.findName("PlayIcon").Visibility = "Visible";
	            }
			}
		} else {
			nextVid();
		}
	}
}
vidTimeline = function() {
	var p = silverlightControl.content.findName("PlayerWindow").position.seconds;
	if (doDragTime == false && silverlightControl.content.findName("TimeIndicatorEmpty")) {
		var d = silverlightControl.content.findName("PlayerWindow").naturalDuration.seconds;
		var w = silverlightControl.content.findName("TimeIndicatorEmpty").Width-silverlightControl.content.findName("TimeSliderGripper").Width;
		if(d != 0 && p < d){
			silverlightControl.content.findName("TimeSliderGripper")["Canvas.Left"] = ((p*w)/d)+(silverlightControl.content.findName("TimeIndicatorEmpty")["Canvas.Left"]);
			silverlightControl.content.findName("TimeIndicatorFull").Width = ((p*w)/d);
			//this section hides and displays the banner
			/*if(p > (d-redisplayBannerTimeInSeconds) && bannerShowAnimationStarted == false){
			    beginBannerShowAnimation();
			     bannerShowAnimationStarted = true;
			}
			else if(p > hideBannerTimeInSeconds && p < (d-redisplayBannerTimeInSeconds) && bannerShowAnimationStarted == true){
			    bannerShowAnimationStarted = false;
			    beginBannerHideAnimation();
			}
			else if(p > hideBannerTimeInSeconds && bannerHideAnimationStarted == false){
			    bannerHideAnimationStarted = true;
			    beginBannerHideAnimation();
			}
			else if(p<hideBannerTimeInSeconds && bannerHideAnimationStarted){
			    bannerHideAnimationStarted = false;
			   
			    beginBannerShowAnimation();
			}*/
		}
	}
	else if(doDragTime==false){
	    silverlightControl.content.findName("PlayerWindow").play();
	}
	else if(playerFinished){
	    playerFinished = false;
	    if (silverlightControl.content.findName("PlayIcon")) {
		 if(silverlightControl.content.findName("PlayIcon").Visibility == "Collapsed"){
		    silverlightControl.content.findName("PlayerWindow").play();
		 }
	    }
	    
	}
	
	for (var cc=0; cc<playlistXMLArray[currentPlayingVid][2].length; cc++) {	
		var syncStart = playlistXMLArray[currentPlayingVid][2][cc][0];
		if(syncStart <= p){
			var passTxt = playlistXMLArray[currentPlayingVid][2][cc][1];
			setFont("CCText", passTxt, "center");
		}
	}
	if(silverlightControl.content.findName("PlayerCurrentTime")){
		silverlightControl.content.findName("PlayerCurrentTime").Text = returnTimeMinutes(p)+":"+returnTimeSeconds(p);
	}
	if(silverlightControl.content.findName("PlayerTotalTime")){
		silverlightControl.content.findName("PlayerTotalTime").Text = returnTimeMinutes(d)+":"+returnTimeSeconds(d);
		totalSeconds = d;
	}
}
returnTimeMinutes = function (t) {
	var timeString = String(Math.floor((t/60))%60);
	var formattedTime = formatTimeString(timeString);
	return formattedTime;
}
returnTimeSeconds = 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;
}
musicLoopRepeat = function(sender, args){
	if (musicLoopInitialized == true) {
		var pos = sender.position;
		pos.seconds = 0;
		sender.position = pos;
		sender.play();
	}
}
muteMusic = function() {

	//silverlightControl.content.findName("PlayerWindow").IsMuted = "True";
	if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").IsMuted = "True";
	}
	if (silverlightControl.content.findName("MusicMuteIcon")) {
		silverlightControl.content.findName("MusicMuteIcon").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("MusicMuteButton")) {
		silverlightControl.content.findName("MusicMuteButton").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("MusicUnmuteIcon")) {
		silverlightControl.content.findName("MusicUnmuteIcon").visibility = "Visible";
	}
	 
	if (silverlightControl.content.findName("MusicUnmuteButton")) {
		silverlightControl.content.findName("MusicUnmuteButton").visibility = "Visible";
	}
	 
	
}
unmuteMusic = function() {
	//silverlightControl.content.findName("PlayerWindow").IsMuted = "False";
	//this only plays music when mute all is not selected
	if (silverlightControl.content.findName("MuteButton")) {
		if(silverlightControl.content.findName("MuteButton").visibility == "Visible"){
		    if (silverlightControl.content.findName("MusicLoop")) {
		        silverlightControl.content.findName("MusicLoop").IsMuted = "False";
	        }
		}
	}
	
	/*if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").IsMuted = "False";
	}*/
	if (silverlightControl.content.findName("MusicMuteIcon")) {
		silverlightControl.content.findName("MusicMuteIcon").visibility = "Visible";
	}
	if (silverlightControl.content.findName("MusicMuteButton")) {
		silverlightControl.content.findName("MusicMuteButton").visibility = "Visible";
	}
	 
	if (silverlightControl.content.findName("MusicUnmuteIcon")) {
		silverlightControl.content.findName("MusicUnmuteIcon").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("MusicUnmuteButton")) {
		silverlightControl.content.findName("MusicUnmuteButton").visibility = "Collapsed";
	}
}
muteAll = function() {

	silverlightControl.content.findName("PlayerWindow").IsMuted = "True";
	
	if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").IsMuted = "True";
	}
	if (silverlightControl.content.findName("MuteIcon")) {
		silverlightControl.content.findName("MuteIcon").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("MuteButton")) {
		silverlightControl.content.findName("MuteButton").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("UnmuteIcon")) {
		silverlightControl.content.findName("UnmuteIcon").visibility = "Visible";
	}
	if (silverlightControl.content.findName("UnmuteButton")) {
		silverlightControl.content.findName("UnmuteButton").visibility = "Visible";
	}
}
unmuteAll = function() {
	silverlightControl.content.findName("PlayerWindow").IsMuted = "False";
	//this keeps the music muted if the user clicked this prior to muting everything
	
	 
	if (silverlightControl.content.findName("MusicUnmuteButton")) {
		if(silverlightControl.content.findName("MusicUnmuteButton").visibility == "Visible"){
		    if (silverlightControl.content.findName("MusicUnmuteLoop")) {
		        silverlightControl.content.findName("MusicUnmuteLoop").IsMuted = "True";
		        
	        }
		}
		else{
		    if (silverlightControl.content.findName("MusicLoop")) {
		        silverlightControl.content.findName("MusicLoop").IsMuted = "False";
	        }
		}
	}
	else{
		if (silverlightControl.content.findName("MusicLoop")) {
		    silverlightControl.content.findName("MusicLoop").IsMuted = "False";
	    }
	}
	/*if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").IsMuted = "False";
	}*/
	if (silverlightControl.content.findName("MuteIcon")) {
		silverlightControl.content.findName("MuteIcon").visibility = "Visible";
	}
	if (silverlightControl.content.findName("MuteButton")) {
		silverlightControl.content.findName("MuteButton").visibility = "Visible";
	}
	if (silverlightControl.content.findName("UnmuteIcon")) {
		silverlightControl.content.findName("UnmuteIcon").visibility = "Collapsed";
	}
	if (silverlightControl.content.findName("UnmuteButton")) {
		silverlightControl.content.findName("UnmuteButton").visibility = "Collapsed";
	}
}
setVolumeUp = function () {
	var v = silverlightControl.content.findName("PlayerWindow").Volume;
	v += .1;
	if (v > 1) {
		v = 1;
	}
	setVolumeManual(v);
}
setVolumeDown = function () {
	var v = silverlightControl.content.findName("PlayerWindow").Volume;
	v -= .1;
	if (v < 0) {
		v = 0;
	}
	setVolumeManual(v);
}
setVolumeManual = function(v) {
	silverlightControl.content.findName("PlayerWindow").Volume = v;
	if (silverlightControl.content.findName("MusicLoop")) {
		silverlightControl.content.findName("MusicLoop").Volume = (v)*musicLoopVolumeMultiplier;
	}
	if (silverlightControl.content.findName("VolumeIndicatorEmpty")) {
		var posSet = (((silverlightControl.content.findName("VolumeIndicatorEmpty").Width-silverlightControl.content.findName("VolumeSliderGripper").Width)*v)/1);
		silverlightControl.content.findName("VolumeSliderGripper")["Canvas.Left"] = posSet+silverlightControl.content.findName("VolumeIndicatorEmpty")["Canvas.Left"]
		silverlightControl.content.findName("VolumeIndicatorFull").Width = posSet;
	}
};
setFont = function(textElement, txt, align) {
	if (silverlightControl.content.findName(textElement)) {
		silverlightControl.content.findName(textElement).Text = txt;
		var w = silverlightControl.content.findName(textElement).actualWidth;
		if (align == "right") {
			silverlightControl.content.findName(textElement)["Canvas.Left"] = silverlightControl.content.findName(textElement).width-w;
		} else if (align == "center") {
			silverlightControl.content.findName(textElement)["Canvas.Left"] = (silverlightControl.content.findName(textElement).width-w)/2;
		}
	}
}
keyboardEvents = function(sender, keyEventArgs) {
	if (keyEventArgs.key == 64) {
		silverlightControl.content.fullScreen = !silverlightControl.content.fullScreen;
	}
}
clickEvents = function(sender, mouseEventArgs) {
	if (sender.name == "PrevButton") {
		prevVid();
	} else if (sender.name == "NextButton") {
		nextVid();
	} else if (sender.name == "PlayButton") {
		vidPlay();
	} else if (sender.name == "PauseButton") {
		vidPause();
	} else if (sender.name == "TimeSliderButton") {
		doDragTime = false;
	} else if (sender.name == "VolumeSliderButton") {
		doDragVol = false;
	} else if (sender.name == "MuteButton") {
		muteAll();
	} else if (sender.name == "UnmuteButton") {
		unmuteAll();
	} else if (sender.name == "MusicMuteButton") {
		muteMusic();
	} else if (sender.name == "MusicUnmuteButton") {
		unmuteMusic();
	} else if (sender.name == "CCButton") {
		if(CCOn == false) {
			CCOn = true;
			silverlightControl.content.findName("TIMELINE_ShowCC").begin();
		} else {
			CCOn = false;
			silverlightControl.content.findName("TIMELINE_HideCC").begin();
		}
	} else if (sender.name == "LearnMoreButton") {
		if(ActionOn == false) {
			ActionOn = true;
			silverlightControl.content.findName("TIMELINE_HideAction").begin();
		} else {
			ActionOn = false;
			silverlightControl.content.findName("TIMELINE_ShowAction").begin();
		}	
	} else if (sender.name == "ToggleFullScreenButton") {
		silverlightControl.content.fullScreen = !silverlightControl.content.fullScreen;
	} else if (sender.name == "ToggleOnLoopPlaybackButton") {
		setLoop(true);
	} else if (sender.name == "ToggleOffLoopPlaybackButton") {
		setLoop(false);
	} else if (sender.name == "VolumeUpButton") {
		setVolumeUp();
	} else if (sender.name == "VolumeDownButton") {
		setVolumeDown();
	}
}
setLoop = function(looping) {
	if (looping == true) {
		if (silverlightControl.content.findName("ToggleOnLoopPlaybackButton")) {
			silverlightControl.content.findName("ToggleOnLoopPlaybackButton").Visibility = "Collapsed";
		}
		if (silverlightControl.content.findName("ToggleOnLoopPlaybackIcon")) {
			silverlightControl.content.findName("ToggleOnLoopPlaybackIcon").Visibility = "Collapsed";
		}
		if (silverlightControl.content.findName("ToggleOffLoopPlaybackButton")) {
			silverlightControl.content.findName("ToggleOffLoopPlaybackButton").Visibility = "Visible";
		}
		if (silverlightControl.content.findName("ToggleOffLoopPlaybackIcon")) {
			silverlightControl.content.findName("ToggleOffLoopPlaybackIcon").Visibility = "Visible";
		}
		playerLoop = true;
	} else {
		if (silverlightControl.content.findName("ToggleOnLoopPlaybackButton")) {
			silverlightControl.content.findName("ToggleOnLoopPlaybackButton").Visibility = "Visible";
		}
		if (silverlightControl.content.findName("ToggleOnLoopPlaybackIcon")) {
			silverlightControl.content.findName("ToggleOnLoopPlaybackIcon").Visibility = "Visible";
		}
		if (silverlightControl.content.findName("ToggleOffLoopPlaybackButton")) {
			silverlightControl.content.findName("ToggleOffLoopPlaybackButton").Visibility = "Collapsed";
		}
		if (silverlightControl.content.findName("ToggleOffLoopPlaybackIcon")) {
			silverlightControl.content.findName("ToggleOffLoopPlaybackIcon").Visibility = "Collapsed";
		}
		playerLoop = false;
	}	
}
rollOver = function(sender, mouseEventArgs) {
	clearInterval(window[sender.name+"Roll"]);
	window[sender.name+"Roll"] = setInterval( function () {
		silverlightControl.content.findName(sender.name+"Roll").Opacity += 0.05;
		if (silverlightControl.content.findName(sender.name+"Roll").Opacity >= 1) {
			silverlightControl.content.findName(sender.name+"Roll").Opacity = 1;
			clearInterval(window[sender.name+"Roll"]);
		}
	}, 1);
}
rollOut = function(sender, mouseEventArgs) {
	clearInterval(window[sender.name+"Roll"]);
	window[sender.name+"Roll"] = setInterval( function () {
		silverlightControl.content.findName(sender.name+"Roll").Opacity += -(0.01);
		if (silverlightControl.content.findName(sender.name+"Roll").Opacity <= 0) {
			silverlightControl.content.findName(sender.name+"Roll").Opacity = 0;
			clearInterval(window[sender.name+"Roll"]);
		}
	}, 1);
}
leaveEvents = function(sender, mouseEventArgs) {
	if(sender.name == "TimeSliderButton"){
		doDragTime = false;
	} else if(sender.name == "VolumeSliderButton") {
		doDragVol = false;
	}
	rollOut(sender, mouseEventArgs);
}
outEvents = function(sender, mouseEventArgs){
	if(sender.name == "TimeSliderButton"){
		doDragTime = false;
	} else if(sender.name == "VolumeSliderButton") {
		doDragVol = false;
	}
	rollOut(sender, mouseEventArgs);
}
pressEvents = function(sender, mouseEventArgs) {
	if(sender.name == "TimeSliderButton"){
		doDragTime = true;
	} else if(sender.name == "VolumeSliderButton") {
		doDragVol = true;
	}
}
dragEvents = function(sender, mouseEventArgs) {
	if (sender.name == "TimeSliderButton" && doDragTime == true) {
		var coordinate = mouseEventArgs.getPosition(sender).x;		
		var d = silverlightControl.content.findName("PlayerWindow").naturalDuration.seconds;
		var w = silverlightControl.content.findName("TimeIndicatorEmpty").Width-silverlightControl.content.findName("TimeSliderGripper").Width;
		if (d != 0) {
			var dragPos = coordinate+(silverlightControl.content.findName("TimeIndicatorEmpty")["Canvas.Left"]);
			if (dragPos <= (w+(silverlightControl.content.findName("TimeIndicatorEmpty")["Canvas.Left"]))) {
				silverlightControl.content.findName("TimeSliderGripper")["Canvas.Left"] = dragPos;
				silverlightControl.content.findName("TimeIndicatorFull").Width = coordinate;
				var seconds = ((d*coordinate)/w);
				var pos = silverlightControl.content.findName("PlayerWindow").position;
				pos.seconds = seconds;
				silverlightControl.content.findName("PlayerWindow").position = pos;
			}
		}
	} else if(sender.name == "VolumeSliderButton" && doDragVol == true) {
		var coordinate = mouseEventArgs.getPosition(sender).x+silverlightControl.content.findName("VolumeSliderButton")["Canvas.Left"];
		var dragVolLeftStart = silverlightControl.content.findName("VolumeIndicatorEmpty")["Canvas.Left"];
		var dragVolLeftEnd = silverlightControl.content.findName("VolumeIndicatorEmpty")["Canvas.Left"]+silverlightControl.content.findName("VolumeIndicatorEmpty").Width;
		if (coordinate >= dragVolLeftStart && coordinate <= dragVolLeftEnd) {
			var v = Math.round(((coordinate-dragVolLeftStart)*100)/silverlightControl.content.findName("VolumeIndicatorEmpty").Width);
			setVolumeManual((v/100));
		}
	} 
}
retrySourceMediaElement = function(sender, eventArgs) {
	var pos = sender.position;
	sender.Source = currentVidSource;
	sender.position = pos;
}
endURL = function() {
	open(actionURL,"_blank");
}
/*ReadyURL = function() {
	open(ReadyURL2,"_blank");
}
ReadyURLBottom = function() {
	open(ReadyURLBase,"_blank");
}*/

beginBannerHideAnimation = function(){
    silverlightControl.content.findName("TIMELINE_HideAction").begin();
}

beginBannerShowAnimation = function(){
    silverlightControl.content.findName("TIMELINE_ShowAction").begin();
}