﻿if (!window.wqln_v2)
	window.wqln_v2 = {};
var tmr;
wqln_v2.Scene = function() 
{
}

wqln_v2.Scene.prototype =
{
	handleLoad: function(plugIn, userContext, rootElement) 
	{
		this.plugIn = plugIn;		
	    this.canvas = rootElement.children.getItem(0).findName("VideoBox");	    
	    this.origLeft = rootElement.children.getItem(0).findName("VideoLocation").getValue("Canvas.Left");
	    this.canvas.addEventListener("MouseLeftButtonUp",Silverlight.createDelegate(this,this.handleLoadButtonUp));
		this.video = rootElement.children.getItem(0).findName("Video");	
		this.bgImage = rootElement.children.getItem(0).findName("BackgroundImage");
		this.prevState = "Building";
		this.play = rootElement.children.getItem(0).findName("Play");	
		this.pause = rootElement.children.getItem(0).findName("Pause");	
		this.pauseable = true;		
		this.canvas.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
		this.canvas.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
		this.video.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
		this.video.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
		this.video.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleMediaOpened));
		//this.video.addEventListener("MediaClosed", Silverlight.createDelegate(this, this.handleMediaClosed));
		this.video.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEnded));
		this.pause.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handlePauseMouseUp));
		this.play.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handlePlayMouseUp));
		
		this.video.addEventListener("BufferingProgressChanged", Silverlight.createDelegate(this, this.handleBufferingProgressChanged));		
		this.video.addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.handleCurrentStateChanged));		
		//this.setVideoSource("mms://66.211.213.13/WQLNVideo/promo/carrier_30_tease.wmv");
		//this.video.Source="mms://66.211.213.13/WQLNVideo/promo/carrier_30_tease.wmv";
	},
	handleLoadButtonUp: function(sender,eventArgs)
	{
	    //alert("here");
	    //var media = sender.findName("Video"); //rootElement.children.getItem(0).findName("Video");
		//media.Source="mms://66.211.213.13/zurn/Siphonic.wmv";
		//self.status += media;
	    //media.play();
	},
	handlePlayMouseUp: function(sender,eventArgs)
	{
	    var media = sender.findName("Video"); //rootElement.children.getItem(0).findName("Video");
		//media.Source="mms://66.211.213.13/zurn/Siphonic.wmv";
		
		media.play();
		sender.setValue("Canvas.ZIndex", 0);
		var pause = sender.findName("Pause");
		pause.setValue("Canvas.ZIndex", 3);
	},
	handlePauseMouseUp: function(sender,eventArgs)
	{
	    
	    var media = sender.findName("Video"); //rootElement.children.getItem(0).findName("Video");
		//media.Source="mms://66.211.213.13/zurn/Siphonic.wmv";
		//self.status=this.pauseable;
		if(this.pauseable){
			media.pause();
		}else{
		    var tmpsrc = this.video.Source;
			media.stop();
			this.video.Source=tmpsrc;
		}
		sender.setValue("Canvas.ZIndex", 0);
		var pause = sender.findName("Play");
		pause.setValue("Canvas.ZIndex", 3);
	},
	// Sample event handlers
	handleMouseEnter: function(sender, eventArgs) 
	{
		// The following code shows how to find an element by name and call a method on it.
		var mouseEnterAnimation 
		var media = sender.findName("Video")
		try{
		
		
		if (media.CurrentState=="Playing" || media.CurrentState=="Buffering"){
		    if(this.pause.Opacity!=1){
		        mouseEnterAnimation= sender.findName("showStatsPlaying");
		        mouseEnterAnimation.begin(); 
		    }		    
		}else{
		    if(this.play.Opacity!=1){
		        mouseEnterAnimation= sender.findName("showStats");
		        mouseEnterAnimation.begin(); 
		    }
		}
		}catch(e){mouseEnterAnimation= sender.findName("showStats");}
		//mouseEnterAnimation.begin(); 
		//self.status="Over Video";
	},
	
	handleMouseDown: function(sender, eventArgs) 
	{
		//var mouseDownAnimation = sender.findName("mouseDown");
		//mouseDownAnimation.begin(); 
	    
	},
	
	handleMouseUp: function(sender, eventArgs) 
	{
	},
	
	handleMouseLeave: function(sender, eventArgs) 
	{
	    var mouseEnterAnimation 
	    var media = sender.findName("Video")
	    switch(media.CurrentState){
	        case "Buffering":
	            mouseEnterAnimation= sender.findName("hideStatsBuffering");
	            mouseEnterAnimation.begin(); 
	        break;
	        case "Playing":
	            if(this.pause.Opacity!=0){
	            mouseEnterAnimation= sender.findName("hideStatsPlaying");
	            mouseEnterAnimation.begin(); 
	            }
	        break;
	        case "Paused":
	        break;
	        
	    }	    
	   
		
		//.status="Video Out";
	},
	handlePropertyChanged: function(eventArgs)
	{
	},
	handleBufferingProgressChanged: function(sender,eventArgs)
	{
	    //self.status = sender.BufferingProgress;
	   var mediaStateTextBlock = sender.findName("VideoBuffer");
	   mediaStateTextBlock.Text="Buffered " + parseInt(sender.BufferingProgress * 100) + "%";	   
//	   if(parseInt(sender.BufferingProgress * 100)>=100 && sender.CurrentState!="Playing" && sender.CurrentState!="Paused"){
//	    var mouseExitAnimiation = sender.findName("hideStats");
//	    mouseExitAnimiation.begin()
//	    //sender.pause()
//	   }
	   
	},
	handleCurrentStateChanged: function(sender,eventArgs)
	{
	
	   var mediaStateTextBlock = sender.findName("VideoState");
	   
	   mediaStateTextBlock.Text=sender.CurrentState;
	   if(sender.CurrentState=="Playing"){
	     var mouseExitAnimiation = sender.findName("hideStatsPlaying");
	     mouseExitAnimiation.begin()
	     var pause = sender.findName("Pause");
	     pause.setValue("Canvas.ZIndex", 3);
		 var play = sender.findName("Play");
		 play.setValue("Canvas.ZIndex", 0);	
		 
	   }
	   if((sender.CurrentState=="Paused" || sender.CurrentState=="Stopped") && this.prevState!=sender.CurrentState){
	     var play = sender.findName("Play");
	     var pause = sender.findName("Pause");
	     var mouseExitAnimiation;
	     if(pause.Opacity==0){
	     mouseExitAnimiation = sender.findName("showPlay");
	     }else{
	     mouseExitAnimiation = sender.findName("pause2play");
	     }
	     
	     mouseExitAnimiation.begin()
	     //var pause = sender.findName("Pause");
	     pause.setValue("Canvas.ZIndex", 0);
		 //var play = sender.findName("Play");
		 play.setValue("Canvas.ZIndex", 3);
		 
		 try{
		
		 if(this.duration.Seconds-2<=this.video.Position.Seconds){
		     try{
		        if(this.pauseable){
	                raiseVideoEnded(sender);
	            }
	        }catch(e){} 
		 }
		 }catch(e){}
	   }
	   try{
	   this.prevState = sender.CurrentState;
	   }catch(e){}
	},
	handleMediaOpened: function(sender,eventArgs)
	{
	    var dur =sender.NaturalDuration;	  
	    this.duration = dur;  
	    var pos = sender.Position;
	    this.position=pos;
	    startTimer();
	},
	handleMediaEnded: function(sender,eventArgs)
	{
	    //alert("here");
	    sender.Stop();
	    var pos = sender.Position;
	    this.position=pos;
	    pos.Seconds=0;
	    sender.Pause();	   
	    try{
	        raiseVideoEnded(sender);
	    }catch(e){} 
	},
	setVideoSource: function(src)
	{
	    try{	    
	        this.video.Source=src;
	    }catch(e){}
	},
	getVideoSource: function(src)
	{
	    
	    return this.video.Source;
	},
	playVid: function()
	{
	    if(parseInt(this.video.BufferingProgress * 100)<100){
	        setTimeout("scene.playVid()",1000);
	    }else{
	        this.video.Play();
	    }
	},
	pauseVid: function()
	{
	    this.video.Pause();
	},
	stopVid: function()
	{
	    this.video.Stop();
	},
	status: function()
	{
	    var retval
	    try{
	        retval=this.video.CurrentState;
	    }catch(e){retval="err"}
	    return retval
	},
	setBgImage: function(src)
	{
	    this.bgImage.Source = src;
	},
	setPauseStop: function(tf)
	{
	    this.pauseable=tf;
	}
}
function timeSpan(sec){
    var retString = "";
    var tmpSecs = Math.floor(sec);
    retString = "";//new String(sec-tmpSecs).substr(0,4);
    if(sec>60){
        var remSecs = parseInt(sec)%60; 
        //self.status = remSecs;       
        retString=twoDigit(remSecs) + retString;
        var minutes = Math.floor(sec/60);
        retString=minutes + ":" + retString;
    }else{
        retString="00:" + twoDigit(parseInt(sec));
    }
    return retString;
}
function twoDigit(val){
    var retval = val;
    if(val<10){
        retval = "0" + retval
    }
    return retval;
}
function startTimer(){
    tmr=setInterval("updatePos()",500);
}
function stopTimer(){
    clearInterval(tmr);
}
function updatePos(){
    try{
    var vid = scene.video;
    var vidLocation = scene.video.findName("VideoLocation");
    vidLocation.Text = timeSpan(vid.Position.Seconds) + "/" + timeSpan(scene.duration.Seconds);
    rightAlign(vidLocation, scene.origLeft);
    }catch(e){}
}
function rightAlign(textBlock, origLeft){
    textBlock.setValue("Canvas.Left",origLeft+textBlock.Width-textBlock.ActualWidth);
}

