﻿if (!window.sPlayer)
	window.sPlayer = {};

var v_ClipInfo = null;

sPlayer.sPlayer = function() 
{
}

sPlayer.sPlayer.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		//Get information about clip
		var cmd = (control == null || control.parentNode == null ? "" : control.parentNode.mediaInfo);
		//alert(cmd);
		try { v_ClipInfo = eval(cmd); } catch(e) {alert(e.message + "\n\r" + cmd);}
		//v_ClipInfo = new f_MediaInfo();	
		//alert(v_ClipInfo.toString());
	
		//Set objects
		this.plugIn = control;
		control.sPlayer = this;
		
		this.playerUI = rootElement;
		this.mediaElement = this.playerUI.findName("Media");
		this.canvasMedia = this.playerUI.findName("canvasMedia");				
		this.isDragging = false;		
		
		rootElement.Width = control.width;
		rootElement.Height = control.height;
		
		this.mediaElement.Width = rootElement.Width;
		this.mediaElement.Height = rootElement.Height;
		
		this.mediaElement.source = v_ClipInfo.URL;
		this.mediaElement.AutoPlay = v_ClipInfo.AutoPlay;
				
		this.onPositionChange = v_ClipInfo.onPositionChange;
		
		var fsb = rootElement.findName("FullScreenButtonSmall");
		fsb["Canvas.Top"] = this.mediaElement.Height - fsb.Height;
		fsb["Canvas.Left"] = this.mediaElement.Width - fsb.Width;
		
		fsb.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.fullScreenBtnClicked));
		fsb.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.fullScreenBtnEnter));
		fsb.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.fullScreenBtnLeave));

		//Event handlers
		this.mediaElement.addEventListener("mediaFailed", this.mediaFailed);	
		this.mediaElement.addEventListener("mediaEnded", this.mediaEnded);		
		this.mediaElement.addEventListener("currentStateChanged", Silverlight.createDelegate(this, this.currentStateChanged));
		this.mediaElement.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.downloadProgressChanged));
		
		//Dont remove this comment
		this.plugIn.content.onFullScreenChange = Silverlight.createDelegate(this, this.fullScreenChanged);
	},
		
	mediaEnded: function(sender, eventArgs) {		
  },
    
	mediaFailed: function(sender, eventArgs) {
		alert("Error loading media");
	}, 
	
	fullScreenBtnEnter: function(sender, eventArgs){
		this.fullScreenBtnHL(true);
	},
	
	fullScreenBtnLeave: function(sender, eventArgs){
		this.fullScreenBtnHL(false);
	},
	
	fullScreenBtnHL: function(on) {
		this.playerUI.findName("FullScreenButtonSmallImg").visibility = (on ? "collapsed" : "visible");
		this.playerUI.findName("FullScreenButtonSmallImg_hl").visibility = (on ? "visible" : "collapsed");
	},
	
	currentStateChanged: function(sender, eventArgs){	
	}, 
			
	fullScreenBtnClicked: function(sender, eventArgs){
		this.plugIn.content.fullScreen = !this.plugIn.content.fullScreen;		
	},
	
	fullScreenChanged: function(sender, eventArgs){
		if(this.plugIn.content.fullScreen){
			var xaml = "<Rectangle><Rectangle.Fill><VideoBrush SourceName='Media' /></Rectangle.Fill></Rectangle>";
			var fullScreenRect = this.plugIn.content.createFromXaml(xaml);
			fullScreenRect.width = this.plugIn.content.actualWidth;
			fullScreenRect.height = this.plugIn.content.actualHeight;
			this.playerUI.children.add(fullScreenRect);
		} 
		else{
			this.playerUI.children.removeAt(this.playerUI.children.count - 1);
		}
	},
	
	downloadProgressChanged: function(sender, eventArgs){
	}
}
	
function f_GetQueryVariable(v_Variable) {
	var v_Query = window.location.search.substring(1);
	var v_Vars = v_Query.split("&");
	  
	for (var i=0; i<v_Vars.length; i++) {
		var v_Pair = v_Vars[i].split("=");
		if (v_Pair[0] == v_Variable) {return v_Pair[1];}
	}	  
	return ""; 
}

function f_GetCurrentPosition(curTimeSpan, type){
	var retval = null;
	
	if(type != "str"){type = "num";}	

	switch(type){
	
		case "str":
		    var datetime = new Date(0, 0, 0, 0, 0, curTimeSpan)
		    var hours = datetime.getHours();
		    var minutes = datetime.getMinutes();
		    var seconds = datetime.getSeconds();
			
		    if (seconds < 10) {seconds = "0" + seconds;}	    
		    if (minutes < 10) {minutes = "0" + minutes;}
		    if (hours < 10) {hours = "0" + hours;}	    
			
			retval = hours + ":" + minutes + ":" + seconds;					
			break;
		case "num":
			if(!isNaN(curTimeSpan)){
				retval = curTimeSpan.toFixed(1);
			}
			else{
				retval = 0;
			}
			break;
	}
	return retval;
}

if (!window.Silverlight) 
  window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
  return function() {
    return method.apply(instance, arguments);
  }
}
