﻿/*
-----------------------------------------------------------------------------------------------------------------------
Main
Libreria que gestiona el comportamiento general de la pantalla principal.
-----------------------------------------------------------------------------------------------------------------------
*/
if(typeof jeroenwijering == "undefined") {
	var jeroenwijering = new Object();
	jeroenwijering.utils = new Object();
}


var plugin
var main
var statusbox
//Carrusel - Variables de trabajo
var CarruselHolder //enlace al carrusel
var ConPrecarga = true;
var objsArr=new Array()
var videoControl;

var num_imgs=11 //42

//Estos deberian ser dinamicos y en funcion del tamaño del canvas "contenedor" del carrusel.
var radiusX=320 //480
var radiusY=110 //110
var centerX=450
var centerY=260


var speed=-0.02
var main_down=false
var sFontName = "Segoe Semibold";

//Loaded="mainCanvasLoaded"
function mainCanvasLoaded(s) {
	main=s.findName("mainCanvas");
	plugin = s.getHost();
	imagesHolder=s.findName("imagesHolder");
	CarruselHolder = s.findName("CarruselHolder");
	

	//statusbox = s.findName("tbstatus")
	
	if (ConPrecarga == false) {
	    BuildItems();
    }
	
	//Asocio el evento de resize con el manejador
	plugin.content.onResize = onResizeStatus ; //resize;
	
	//Aqui podriamos lanzar el loader.. más claro? y todo estará cargado... vamos...
	setTimeout(GestionarDescarga, 600);
}


/*
-----------------------------------------------------------------------------------------------------------------------
    Gestión de la descarga de los assets
-----------------------------------------------------------------------------------------------------------------------
*/


var oPctDownloaded;
var Offset;
var oCercleLoader;
var iNumTimesProgressChanged;

var videoconfiguration = {
		backgroundcolor:'FFFFFF',
		file:'afraid.wmv',
		height:'200',
		image:'',
		backcolor:'FFFFFF',
		frontcolor:'000000',
		lightcolor:'000000',
		screencolor:'000000',
		width:'400',
		logo:'',
		overstretch:'false',
		showicons:'true',
		shownavigation:'true',
		showstop:'false',
		showdigits:'true',
		usefullscreen:'true',
		usemute:'false',
		autostart:'false',
		bufferlength:'3',
		duration:'0',
		repeat:'false',
		sender:'',
		volume:'90',
		link:'',
		linkfromdisplay:'false',
		linktarget:'_self'
	};

//Loaded="mainCanvasLoaded" 
function handleLoad(control, userContext, rootElement) {
    if (ConPrecarga == true) {
	    //this.control = control;
       oPctDownloaded = main.findName("PercentDownloaded"); 
       Offset = main.findName("Offset1");      
	    
	    // Create a Downloader object.
	    var downloader = control.createObject("downloader");
	    downloader.addEventListener("DownloadProgressChanged", onDownloadProgressChanged);
	    downloader.addEventListener("completed", onCompleted);
        downloader.Open("GET", "innovadls.zip?" + new Date()); // The querystring is there to avoid caching
	    downloader.send();
		


		
	}
}
	
////Gestiono la descarga de los assets 
function GestionarDescarga(){
  if (ConPrecarga == true) {
     oPctDownloaded = main.findName("PercentDownloaded"); 
     Offset = main.findName("Offset1");      
     oCercleLoader = main.findName("Cercle_Loader");
     oPctDownloaded.Visibility = "Visible"; //Visibility="Collapsed"
     oCercleLoader.Visibility = "Visible";
     iNumTimesProgressChanged=0;
     // Retrieve a reference to the plug-in.
     var slPlugin = main.getHost();
     // Create a Downloader object.
     downloader = slPlugin.createObject("downloader");  
     //var control = document.getElementById("SilverlightControl");
     //var downloader = control.CreateObject("downloader");
     downloader.AddEventListener("DownloadProgressChanged", onDownloadProgressChanged);
     downloader.AddEventListener("Completed", onCompleted);
     downloader.Open("GET", "innovadls.zip?" + new Date()); // The querystring is there to avoid caching
     downloader.Send();
   }
}

// Gestión del progreso de la descarga..
function onDownloadProgressChanged(sender, eventArgs)
{
    var percentComplete = sender.DownloadProgress * 100;  
    iNumTimesProgressChanged +=1;
    oPctDownloaded.Text = Math.floor(percentComplete) + "%"; 
    Offset.Offset= percentComplete/100;      
}

//Variables globales a las que asigno los elementos descargados
var downloader;
var img0;
var img1;
var img2;
var img3;
var img4;
var img5;
var img6;
var img7;
var fontSegoeUI;
var fontSegoeUIBold;

// Handler for successful completion of download
function onCompleted(sender, eventArgs)
{

  var control = sender.GetHost();
  var root = control.Content.Root;
  iNumTimesProgressChanged = iNumTimesProgressChanged + 1;
 
  sender.findName("Logo_fade_in").begin();
  sender.findName("banner").begin();
    oPctDownloaded.Visibility = "Collapsed"; //Visibility="Collapsed"
    oCercleLoader.Visibility = "Collapsed";

  main=sender.findName("mainCanvas");
  plugin = sender.getHost();
  imagesHolder=sender.findName("imagesHolder");
  CarruselHolder = sender.findName("CarruselHolder");
  
  plugin.content.onResize = onResizeStatus ; //resize;

	//plugin.content.findName("viteze").Visibility = "Visible";
	plugin.content.findName("icons").Visibility = "Visible";

	
  BuildItems(); //BuildItems, segun "ConPrecarga" cargara del objeto downloader o de los assets para su descarga..

   var oTxtBlockPopUp = sender.findName("ItemTitulo");
   oTxtBlockPopUp.setFontSource(sender);
   //var sFontName = "Segoe Condensed"; //"Segoe UI"
   oTxtBlockPopUp.fontFamily = sFontName;  
   // Del contenido (texto)
   var oTextePopUp = sender.findName("Texte");
   oTextePopUp.setFontSource(sender);
   oTextePopUp.fontFamily = sFontName;  
   
  // var oCanvasMLSelector = sender.findName("CanvasMultiLenguaje");
   //var AnimCanvasMLAppear = plugin.content.findName( "CanvasMLAppear" );
  //AnimCanvasMLAppear.Begin();

  videoconfiguration['sender'] = sender;
} 

function FullScreen(s,e)
{
	var plugin = s.getHost();
    plugin.content.fullScreen = !plugin.content.fullScreen;
	onResizeStatus(s,e);
}

function LogoMoveDown(s,e){

    main.findName("LogoMoveDown").Begin();
}

/*
-----------------------------------------------------------------------------------------------------------------------
    Gestión del resizing del navegador y contenedor de Silverlight
-----------------------------------------------------------------------------------------------------------------------
*/

//Gestiono el resizing del control de javascript
function onResizeStatus(sender, eventArgs)
{
    //Control del estado de cambio de tamaño
    //	var targetWidth = plugin.content.actualWidth
    //	var targetHeight = plugin.content.actualHeight
    //	var msgString =  " ActualWidth: " + plugin.content.actualWidth;
    //		msgString += " ActualHeight: " + plugin.content.actualHeight;

	//var s=sender.findName("mainCanvas");
	var s = main.findName("mainCanvas")
	var sourceWidth = s.width
	var sourceHeight = s.height

//	msgString += " Canvas.Width: " + sourceWidth;
//	msgString += " Canvas.Height: " + sourceHeight;
 	
 	//Establecemos la nueva escala en base al tamaño del host 
	//var content = sender.GetHost().Content;
	var content = main.GetHost().Content;
	var escala = Math.min(content.ActualWidth / content.Root.Width,
	                      content.ActualHeight / content.Root.Height);
    //var transform = sender.findName("ST");
    var transform = main.findName("ST");
   	transform.scaleX = escala;
   	transform.scaleY = escala;
    content.Root["Canvas.Left"] = (content.ActualWidth - content.Root.Width * escala) /2 ;
    content.Root["Canvas.Top"] = (content.ActualHeight - content.Root.Height * escala) /2 ;
	
	//var mesa = sender.findName("InnovaTable");
	//var mesa = main.findName("InnovaTable");
//var mesaHeightEscalada = mesa.height * escala;
	//var mainCanvas = sender.findName("mainCanvas");
	var mainCanvas = main.findName("mainCanvas");
	var mainCanvasHeight = mainCanvas.height;
	var mainCanvasHeightEscalado = mainCanvasHeight * escala;
	
//    mesa["Canvas.Top"] = plugin.content.actualHeight - mesaHeightEscalada; //+++
//    mesa["Canvas.Top"] = content.Root.Height - mesa.height;  //+++++++ funciona OK!
	//var textBlock = sender.findName("tbstatus");
//	var textBlock = main.findName("tbstatus");
//	textBlock.text = msgString;
}

function ResizeToContainer(){
	var s = main.findName("mainCanvas")
	var sourceWidth = s.width
	var sourceHeight = s.height
	
	//Establecemos la nueva escala en base al tamaño del host 
	var content = main.GetHost().Content;
	var escala = Math.min(content.ActualWidth / content.Root.Width,
	                      content.ActualHeight / content.Root.Height);
    var transform = main.findName("ST");
   	transform.scaleX = escala;
   	transform.scaleY = escala;
    content.Root["Canvas.Left"] = (content.ActualWidth - content.Root.Width * escala) /2 ;
    content.Root["Canvas.Top"] = (content.ActualHeight - content.Root.Height * escala) /2 ;
	
	//var mesa = main.findName("InnovaTable");
//	var mesaHeightEscalada = mesa.height * escala;
	var mainCanvas = main.findName("mainCanvas");
	var mainCanvasHeight = mainCanvas.height;
	var mainCanvasHeightEscalado = mainCanvasHeight * escala;
}

function mainCanvasMouseLeave(s) {
	main_down=false
}

//Enlaces
function GoToMSIPCWebsite(sender, args){
    self.location = "http://www.productivitycenter.org";
    
}




















/****************************************************************************
* The controller of the player MVC triad, which processes all user input.
****************************************************************************/
jeroenwijering.Controller = function(cfg) {
	this.configuration = cfg;
}

jeroenwijering.Controller.prototype = {
	startMVC: function(vie,mdl) {
		this.view = vie;
		this.model = mdl;
		if(this.configuration['usemute'] == 'true') {
			this.view.onVolume(0);
			this.view.onMute(true);
			this.model.goVolume(0);
		} else {
			this.view.onVolume(this.configuration['volume']);
			this.model.goVolume(this.configuration['volume']);
		}
		if(this.configuration['autostart'] == 'true') {
			this.model.goStart();
		} else { 
			this.model.goPause();
		}
	},

	setState: function(old,stt) {
		this.state = stt;
	},

	setLink: function() {
		if (this.configuration['linktarget'].indexOf('javascript:') == 0) {
			return Function(this.configuration['linktarget']).apply();
		} else if (this.configuration['linktarget'] == '_blank') {
			window.open(this.configuration['link']);
		} else if (this.configuration['linktarget'] != '') {
			window.location = this.configuration['link'];
		}
	},

	setMute: function() {
		if(this.configuration['usemute'] == 'true') {
			this.configuration['usemute'] = 'false';
			this.model.goVolume(this.configuration['volume']);
			this.view.onMute(false);
		} else {
			this.configuration['usemute'] = 'true';
			this.model.goVolume(0);
			this.view.onMute(true);
		}
	},

	setPlay: function() {
		if(this.state == 'Buffering' || this.state == 'Playing') {
			this.model.goPause();
		} else {
			this.model.goStart();
		}
	},

	setScrub: function(sec) {
		if(sec < 2) { 
			sec = 0;
		} else if (sec > this.configuration['duration']-4) {
			sec = this.configuration['duration']-4;
		}
		if(this.state == 'Buffering' || this.state == 'Playing') {
			this.model.goStart(sec);
		} else {
			this.model.goPause(sec);
		}
	},

	setStop: function() {
		this.model.goStop();
	},

	setVolume: function(pct) {
		if(pct < 0) { pct = 0; } else if(pct > 100) { pct = 100; }
		this.configuration['volume'] = Math.round(pct);
		this.model.goVolume(pct);
		this.view.onVolume(pct);
		if(this.configuration['usemute'] == 'true') {
			this.configuration['usemute'] = 'false';
			this.view.onMute(false);
		} 
	},

	setFullscreen: function() {
		var fss = !this.configuration['sender'].getHost().content.FullScreen;
		this.configuration['sender'].getHost().content.FullScreen = fss;
		jeroenwijering.utils.delegate(this.view,this.view.onFullscreen);
	}
}










/****************************************************************************
* The view of the player MVC triad, which manages the graphics.
****************************************************************************/
jeroenwijering.View = function(cfg,ctr) {
	this.configuration = cfg;
	this.controller = ctr;
	this.fstimeout;
	this.fslistener;
	this.display = this.configuration['sender'].findName("PlayerDisplay");
	this.controlbar = this.configuration['sender'].findName("PlayerControls");
	this.configuration['sender'].getHost().content.onResize = 
		jeroenwijering.utils.delegate(this,this.resizePlayer);
	this.configuration['sender'].getHost().content.onFullScreenChange = 
		jeroenwijering.utils.delegate(this,this.onFullscreen);
	this.assignColorsClicks();
	this.resizePlayer();
}

jeroenwijering.View.prototype = {
	onBuffer: function(pct) {
		var snd = this.configuration['sender'];
		if(pct == 0) { 
			snd.findName("BufferText").Text = null;
		} else { 
			pct < 10 ? pct = "0"+pct: pct = ""+pct;
			snd.findName("BufferText").Text = pct;
		}
	},

	onFullscreen: function(fss) {
		var snd = this.configuration['sender'];
		var fst = snd.getHost().content.FullScreen;
		if(fst) { 
			this.fstimeout = setTimeout(jeroenwijering.utils.delegate(this,
				this.hideFSControls),2000);
			this.fslistener = this.display.addEventListener('MouseMove',
				jeroenwijering.utils.delegate(this,this.showFSControls));
			snd.findName("FullscreenSymbol").Visibility = "Collapsed";
			snd.findName("FullscreenOffSymbol").Visibility = "Visible";
		} else {
			clearTimeout(this.fstimeout);
			this.display.removeEventListener("MouseMove",this.fslistener);
			this.controlbar.Visibility = "Visible";
			this.display.Cursor = "Hand";
			snd.findName("FullscreenSymbol").Visibility = "Visible";
			snd.findName("FullscreenOffSymbol").Visibility = "Collapsed";
		}
		this.resizePlayer();
	},

	showFSControls: function(sdr,arg) {
		var vbt = sdr.findName('PlayerControls');
		var yps = arg.GetPosition(vbt).Y;
		clearTimeout(this.fstimeout);
		this.controlbar.Visibility = "Visible";
		this.display.Cursor = "Hand";
		if(yps < 0) { 
			this.fstimeout = setTimeout(jeroenwijering.utils.delegate(this,
				this.hideFSControls),2000);
		}
	},

	hideFSControls: function() {
		this.controlbar.Visibility = "Collapsed";
		this.display.Cursor = "None";
	},

	onLoad: function(pct) {
		var snd = this.configuration['sender'];
		var max = snd.findName("TimeSlider").Width;
		snd.findName("DownloadProgress").Width = Math.round(max*pct/100);
	},

	onMute: function(mut) {
		var snd = this.configuration['sender'];
		this.configuration['usemute'] = ''+mut;
		if(mut) {
			snd.findName("VolumeHighlight").Visibility = "Collapsed";
			snd.findName("MuteSymbol").Visibility = "Visible";
			snd.findName("MuteOffSymbol").Visibility = "Collapsed";
			if(this.state == 'Playing') {
				snd.findName("MuteIcon").Visibility = "Visible";
			}
		} else {
			snd.findName("VolumeHighlight").Visibility = "Visible";
			snd.findName("MuteSymbol").Visibility = "Collapsed";
			snd.findName("MuteOffSymbol").Visibility = "Visible";
			snd.findName("MuteIcon").Visibility = "Collapsed";
		}
	},

	onState: function(old,stt) {
		var snd = this.configuration['sender'];
		this.state = stt;
		if(stt == 'Buffering' || stt == 'Playing' || stt == 'Opening') {
			snd.findName("PlayIcon").Visibility = "Collapsed";
			snd.findName("PlaySymbol").Visibility = "Collapsed";
			snd.findName("PlayOffSymbol").Visibility = "Visible";
			if (stt=='Playing' || this.configuration['showicons']=='false') {
				snd.findName("BufferIcon").Visibility = "Collapsed";
				snd.findName("BufferText").Visibility = "Collapsed";
				if(this.configuration['usemute'] == 'true') {
					snd.findName("MuteIcon").Visibility = "Visible";
				}
			} else if(this.configuration['showicons'] == 'true') {
				snd.findName("BufferIcon").Visibility = "Visible";
				snd.findName("BufferText").Visibility = "Visible";
			} else {
				snd.findName("BufferIcon").Visibility = "Collapsed";
				snd.findName("BufferText").Visibility = "Collapsed";
			}
		} else { 
			snd.findName("MuteIcon").Visibility = "Collapsed";
			snd.findName("BufferIcon").Visibility = "Collapsed";
			snd.findName("BufferText").Visibility = "Collapsed";
			snd.findName("PlaySymbol").Visibility = "Visible";
			snd.findName("PlayOffSymbol").Visibility = "Collapsed";
			if(this.configuration['showicons'] == 'true') {
				snd.findName("PlayIcon").Visibility = "Visible";
			} else {
				snd.findName("PlayIcon").Visibility = "Collapsed";
			}
		}
	},

	onTime: function(elp,dur) {
		var snd = this.configuration['sender'];
		var snd = this.configuration['sender'];
		var max = snd.findName("TimeSlider").Width;
		var pos = Math.round(max*elp/dur);
		if(isNaN(pos)) { pos = 0; }
		this.configuration['duration'] = dur;
		snd.findName("ElapsedText").Text = 
			jeroenwijering.utils.timestring(elp);
		snd.findName("RemainingText").Text = 
			jeroenwijering.utils.timestring(dur-elp);
		snd.findName("TimeSymbol")['Canvas.Left'] = pos+4;
		snd.findName("TimeHighlight").Width = pos-2;
	},

	onVolume: function(pct) {
		var snd = this.configuration['sender'];
		snd.findName("VolumeHighlight").Width = Math.round(pct/5);
	},

	assignColorsClicks: function() {
		this.display.Cursor = "Hand";
		this.display.Background = "#FFFFFFFF";
		if(this.configuration['linkfromdisplay'] == 'false') { 
			this.display.addEventListener('MouseLeftButtonUp',
				jeroenwijering.utils.delegate(this.controller,
				this.controller.setPlay));
		} else { 
			this.display.addEventListener('MouseLeftButtonUp',
				jeroenwijering.utils.delegate(this.controller,
				this.controller.setLink));
		}
		if(this.configuration['logo'] != '') {
			this.display.findName('OverlayCanvas').Visibility = "Visible";
			this.display.findName('OverlayLogo').ImageSource = 
				this.configuration['logo'];
		}
		this.controlbar.findName("ControlbarBack").Fill = 
			"#FF"+this.configuration['backcolor'];
		this.assignButton('Play',this.controller.setPlay);
		this.assignButton('Stop',this.controller.setStop);
		this.configuration['sender'].findName('ElapsedText').Foreground = 
			"#FF"+this.configuration['frontcolor'];
		this.assignSlider('Time',this.changeTime);
		this.configuration['sender'].findName('DownloadProgress').Fill = 
			"#FF"+this.configuration['frontcolor'];
		this.configuration['sender'].findName('RemainingText').Foreground = 
			"#FF"+this.configuration['frontcolor'];
		this.assignButton('Link',this.controller.setLink);
		this.assignButton('Fullscreen',this.controller.setFullscreen);
		this.assignButton('Mute',this.controller.setMute);
		this.assignSlider('Volume',this.changeVolume);
	},

	assignButton: function(btn,act) {
		var el1 = this.configuration['sender'].findName(btn+'Button');
		el1.Cursor = "Hand";
		el1.addEventListener('MouseLeftButtonUp',
			jeroenwijering.utils.delegate(this.controller,act));
		el1.addEventListener('MouseEnter',
			jeroenwijering.utils.delegate(this,this.rollOver));
		el1.addEventListener('MouseLeave',
			jeroenwijering.utils.delegate(this,this.rollOut));
		this.configuration['sender'].findName(btn+'Symbol').Fill = 
			"#FF"+this.configuration['frontcolor'];
		try {
			this.configuration['sender'].findName(btn+'OffSymbol').Fill = 
				"#FF"+this.configuration['frontcolor'];
		} catch(e) {}
	},

	assignSlider: function(sld,act) {
		var el1 = this.configuration['sender'].findName(sld+'Button');
		el1.Cursor = "Hand";
		el1.addEventListener('MouseLeftButtonUp',
			jeroenwijering.utils.delegate(this,act));
		el1.addEventListener('MouseEnter',
			jeroenwijering.utils.delegate(this,this.rollOver));
		el1.addEventListener('MouseLeave',
			jeroenwijering.utils.delegate(this,this.rollOut));
		this.configuration['sender'].findName(sld+'Slider').Fill = 
			"#FF"+this.configuration['frontcolor'];
		this.configuration['sender'].findName(sld+'Highlight').Fill = 
			"#FF"+this.configuration['frontcolor'];
		this.configuration['sender'].findName(sld+'Symbol').Fill = 
			"#FF"+this.configuration['frontcolor'];
	},

	rollOver: function(sdr) {
		var str = sdr.Name.substr(0,sdr.Name.length-6);
		this.configuration['sender'].findName(str+'Symbol').Fill = 
			"#FF"+this.configuration['lightcolor'];
		try {
			this.configuration['sender'].findName(str+'OffSymbol').Fill = 
				"#FF"+this.configuration['lightcolor'];
		} catch(e) {}
	},

	rollOut: function(sdr) {
		var str = sdr.Name.substr(0,sdr.Name.length-6);
		this.configuration['sender'].findName(str+'Symbol').Fill = 
			"#FF"+this.configuration['frontcolor'];
		try {
			this.configuration['sender'].findName(str+'OffSymbol').Fill = 
				"#FF"+this.configuration['frontcolor'];
		} catch(e) {}
	},

	changeTime: function(sdr,arg) {
		var tbt = sdr.findName('TimeSlider');
		var xps = arg.GetPosition(tbt).X;
		var sec = Math.floor(xps/tbt.Width*this.configuration['duration']);
		this.controller.setScrub(sec);
	},

	changeVolume: function(sdr,arg) {
		var vbt = sdr.findName('VolumeButton');
		var xps = arg.GetPosition(vbt).X;
		this.controller.setVolume(xps*5);
	},

	resizePlayer: function() {},

	resizeDisplay: function(wid,hei) {},

	resizeControlbar: function(wid,yps,alp) {},

	centerElement: function(nam,wid,hei) {},

	stretchElement: function(nam,wid,hei) {},

	placeElement: function(nam,xps,yps) {}
}










/****************************************************************************
* The model of the player MVC triad, which stores all playback logic.
****************************************************************************/
jeroenwijering.Model = function(cfg,ctr,vie) {
	this.configuration = cfg;
	this.controller = ctr;
	this.view = vie;
	this.video = this.configuration['sender'].findName("VideoWindow");
	this.preview = this.configuration['sender'].findName("PlaceholderImage");
	var str = {
		'true':'UniformToFill',
		'false':'Uniform',
		'fit':'Fill',
		'none':'None'
	}
	this.state = this.video.CurrentState;
	this.timeint;
	this.video.Stretch = str[this.configuration['overstretch']];
	this.preview.Stretch = str[this.configuration['overstretch']];
	this.video.BufferingTime = 
		jeroenwijering.utils.spanstring(this.configuration['bufferlength']);
	this.video.AutoPlay = true;
	this.video.AddEventListener("CurrentStateChanged",
		jeroenwijering.utils.delegate(this,this.stateChanged));
	this.video.AddEventListener("MediaEnded",
		jeroenwijering.utils.delegate(this,this.mediaEnded));
	this.video.AddEventListener("BufferingProgressChanged",
		jeroenwijering.utils.delegate(this,this.bufferChanged));
	this.video.AddEventListener("DownloadProgressChanged",
		jeroenwijering.utils.delegate(this,this.downloadChanged));
	if(this.configuration['image'] != '') {
		this.preview.Source = this.configuration['image'];
	}
}

jeroenwijering.Model.prototype = {
	goPause: function(sec) {
	try
			{
				this.video.pause();
				if(!isNaN(sec)) 
				{
							this.video.Position = jeroenwijering.utils.spanstring(sec);
				}
				this.timeChanged();
			}
	catch(err){}
	},

	goStart: function(sec) {
		this.video.Visibility = 'Visible';
		this.preview.Visibility = 'Collapsed';
		if(this.state == "Closed") {
			this.video.Source = this.configuration['file'];
		} else {
			this.video.play();
		}
		if(!isNaN(sec)) {
			this.video.Position = jeroenwijering.utils.spanstring(sec);
		}
	},

	goStop: function() {
		//this.video.Visibility = 'Collapsed';
		//this.preview.Visibility = 'Visible';
		this.goPause(0);
		//this.video.Source = 'null';
	},

	goVolume: function(pct) {
		this.video.Volume = pct/100;
	},

	stateChanged: function() {
		var stt = this.video.CurrentState;
		if(stt != this.state) {
			this.controller.setState(this.state,stt);
			this.view.onState(this.state,stt);
			this.state = stt;
			this.configuration['duration'] = 
				Math.round(this.video.NaturalDuration.Seconds*10)/10;
			if(stt != "Playing" && stt != "Buffering" && stt != "Opening") {
				clearInterval(this.timeint);
			} else {
				this.timeint = setInterval(jeroenwijering.utils.delegate(
					this,this.timeChanged),100);
			}
		}
	},

	mediaEnded: function() {
		if(this.configuration['repeat'] == 'true') {
			this.goStart(0);
		} else {
			this.video.Visibility = 'Collapsed';
			this.preview.Visibility = 'Visible';
			this.goPause(0);
		}
	},

	bufferChanged: function() {
		var bfr = Math.round(this.video.BufferingProgress*100);
		this.view.onBuffer(bfr);
	},

	downloadChanged: function() {
		var dld = Math.round(this.video.DownloadProgress*100);
		this.view.onLoad(dld);
	},

	timeChanged: function() {
		var pos = Math.round(this.video.Position.Seconds*10)/10;
		this.view.onTime(pos,this.configuration['duration']);
	}
}










/****************************************************************************
* Some utility functions.
****************************************************************************/
jeroenwijering.utils.delegate = function(obj,fcn) {
	return function() {
		return fcn.apply(obj,arguments);
	}
}
jeroenwijering.utils.timestring = function(stp) {
	var hrs = Math.floor(stp/3600);
	var min = Math.floor(stp%3600/60);
	var sec = Math.round(stp%60);
	var str = "";
	sec > 9 ? str += sec: str +='0'+sec;
	min > 9 ? str = min+":"+str: str='0'+min+":"+str;
	hrs > 0 ? str = hrs+":"+str: null;
	return str;
}
jeroenwijering.utils.spanstring = function(stp) {
	var hrs = Math.floor(stp/3600);
	var min = Math.floor(stp%3600/60);
	var sec = Math.round(stp%60*10)/10;
	var str = hrs+':'+min+':'+sec;
	return str;
}