/****************************************************************************
* Hot TV Video Player version 2.0, Silverlight 1.0
* Deployment Date : February 22, 2010
* Added Functions: TV Channels 
****************************************************************************/
if(typeof hotvideoplayer == "undefined") {
	var hotvideoplayer = new Object();
	hotvideoplayer.utils = new Object();
	hotvideoplayer.xml = new Object();
	hotvideoplayer.xml.globalArray=new Array();
	hotvideoplayer.xml.linkArray=new Array();
	hotvideoplayer.xml.channels=new Array();
	hotvideoplayer.xml.programs=new Array();
	hotvideoplayer.xml.justNow=new Date();
}

/****************************************************************************
* The player wrapper; loads config variables and starts MVC cycle.
****************************************************************************/
hotvideoplayer.Player = function(cnt, src, cfg) {
    this.controller;
    this.model;
    this.view;
    this.configuration = {
        streamtype: 'vod', //'vod'=video player  'tv'=tv player 'videoads'= Ads Video Player
        catid: '',
        begintime: '',
        guide: 'off',
        reklamDIV: 'reklam',
        initialsize: 'mediumsize',
        widewidth: '960',
        wideheight: '540',
        width: '656',
        height: '369',
        backgroundcolor: 'FFFFFF',
        windowless: 'true',
        file: '',
        filearray: '',
        sourcefile: '',
        image: '',
        errorimage: 'img/broadcastError.jpg',
        backcolor: 'FFFFFF',
        frontcolor: '000000',
        lightcolor: '000000',
        screencolor: '000000',
        logo: '',
        overstretch: 'false',
        shownavigation: 'true',
        showstop: 'false',
        showdigits: 'true',
        usefullscreen: 'true',
        usemute: 'false',
        autostart: 'false',
        bufferlength: '3',
        duration: '0',
        repeat: 'false',
        sender: '',
        start: '0',
        volume: '50',
        link: '',
        linkfromdisplay: 'false',
        linktarget: '_self'
    };
    for (itm in this.configuration) {
        if (cfg[itm] != undefined) {
            if (itm.indexOf('color') > 0) {
                this.configuration[itm] = cfg[itm].substr(cfg[itm].length - 6);
            } else {
                this.configuration[itm] = cfg[itm];
            }
        }
    }
    if (this.configuration['streamtype'] == 'vod') {
        this.configuration['height'] = Math.round(this.configuration['height']) + 27; //27 is controlbar height
        this.configuration['wideheight'] = Math.round(this.configuration['wideheight']) + 27; //27 is controlbar height
    }
    this.configuration['streamtype'] = this.configuration['streamtype'].toLowerCase();
    this.configuration['filearray'] = this.configuration['file'].split("|"); 
    this.configuration['sourcefile'] = String(this.configuration['filearray'][0]);
    this.configuration['catid'] = String(this.configuration['catid']);
    hotvideoplayer.xml.justNow.setTime(this.configuration['begintime']);
    hotvideoplayer.utils.clock(hotvideoplayer.xml.justNow);
    Silverlight.createObjectEx({
        source: 'silver/hottvplayer_v_2_0.xaml',
        parentElement: cnt,
        properties: {
            width: this.configuration['width'],
            height: this.configuration['height'],
            version: '1.0',
            inplaceInstallPrompt: true,
            isWindowless: this.configuration['windowless'],
            background: '#' + this.configuration['backgroundcolor']
        },
        events: {
            onLoad: this.onLoadHandler,
            onError: null
        },
        context: this
    });

}


hotvideoplayer.Player.prototype = {
	addListener: function(typ,fcn) {
		this.view.listeners.push({type:typ,func:fcn});
	},

	getConfig: function() { 
		return this.configuration;
	},

	onLoadHandler: function(pid,tgt,sdr) {
		tgt.configuration['sender'] = sdr;
		tgt.controller = new hotvideoplayer.Controller(tgt.configuration);
		tgt.view = new hotvideoplayer.View(tgt.configuration,tgt.controller);
		tgt.model = new hotvideoplayer.Model(tgt.configuration,tgt.controller,tgt.view);
		tgt.controller.startMVC(tgt.view,tgt.model);
	},

	sendEvent: function(typ,prm) {
		switch(typ.toUpperCase()) {
			case 'LINK':
				this.controller.setLink();
				break;
			case 'LOAD':
				this.controller.setLoad(prm);
				break;
			case 'MUTE':
				this.controller.setMute();
				break;
			case 'PLAY':
				this.controller.setPlay();
				break;
			case 'SCRUB':
				this.controller.setScrub(prm);
				break;
			case 'STOP':
				this.controller.setStop();
				break;
			case 'VOLUME':
				this.controller.setVolume(prm);
				break;
		}
	}
}










/****************************************************************************
* The controller of the player MVC triad, which processes all user input.
****************************************************************************/
hotvideoplayer.Controller = function(cfg) {
	this.configuration = cfg;
}

hotvideoplayer.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);
			this.configuration['sender'].findName('MuteButton').Visibility='Collapsed';
		} else {
			this.view.onVolume(this.configuration['volume']);
			this.model.goVolume(this.configuration['volume']);
			this.configuration['sender'].findName('unMuteButton').Visibility='Collapsed';
		}
		if(this.configuration['autostart'] == 'true') {
			this.model.goStart();
		} else { 
			this.model.goPause();
			this.configuration['sender'].findName('PlayButton').Visibility='Visible';
			this.configuration['sender'].findName('PauseButton').Visibility='Collapsed';
		}
	},

	setState: function(old,stt) {
		this.state = stt;
		var pos = this.configuration['start'];
		if(old == 'Closed' && pos > 0) {
			setTimeout(hotvideoplayer.utils.delegate(this,this.setScrub),200,pos);
		} 
	},

	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'];
		}
	},

	setLoad: function(fil) {
		if(this.model.state != "Closed") {
			this.model.goStop(); 
		}
		this.configuration['sourcefile'] = fil;
		if(this.configuration['autostart'] == 'true') {
			setTimeout(hotvideoplayer.utils.delegate(this.model,this.model.goStart),100);
		}
	},

	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') {
			if(this.configuration['duration'] == 0) { 
				this.model.goStop();
			} else { 
				this.model.goPause();
			}
		} else {
			this.model.goStart();
			this.configuration['sender'].findName('videoOfferScreen')['Visibility']='Collapsed';
		}
	},

	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);
		} 
	},


	setWidescreen: function() {
		if (this.configuration['initialsize']=='mediumsize'){
			this.configuration['initialsize']='bigsize';
			this.configuration['sender'].getHost().width=this.configuration['widewidth'];
			this.configuration['sender'].getHost().height=this.configuration['wideheight'];
			if (this.configuration['streamtype']=='vod') {
				window.location ='#videotop';	
			} else {
				window.location ='#tvtop';
			}
			if(document.getElementById(this.configuration['reklamDIV'])) {		
				document.getElementById(this.configuration['reklamDIV']).style.display='none';
			}
		}else {
			this.configuration['initialsize']='mediumsize';
			this.configuration['sender'].getHost().width=this.configuration['width'];
			this.configuration['sender'].getHost().height=this.configuration['height'];
			if(document.getElementById(this.configuration['reklamDIV'])) {		
				document.getElementById(this.configuration['reklamDIV']).style.display='block';
			}
		}
	},
	
	setNothing: function() {
	},
	
	setPrev: function() {
		
		if(this.view.firstrelvid>0){
			this.view.firstrelvid=this.view.firstrelvid - this.view.vidperpage;
			if (this.view.firstrelvid<0) {this.view.firstrelvid=0;}
			this.view.putRelatedVideos();
		}
	},
	
	setNext: function() {
		if(this.view.firstrelvid + this.view.vidperpage<hotvideoplayer.xml.globalArray.length){
			this.view.firstrelvid=this.view.firstrelvid + this.view.vidperpage;
			this.view.putRelatedVideos();
		}
	},

	showChannels: function() {
		if(this.configuration['sender'].findName("Channels")['Canvas.Left']>=0) {
			this.configuration['sender'].findName('slideoutChannels').Begin();
			this.configuration['sender'].findName('fadeOutProgramInfo').Begin();
		} else {
			this.configuration['sender'].findName('slideinChannels').Begin();
		}
	},

	showProgramInfo: function() {
		if(this.configuration['sender'].findName('programInfoBox').Opacity<1) {
			this.configuration['sender'].findName('programInfoBox').Visibility="Visible";
			this.configuration['sender'].findName('fadeInProgramInfo').Begin();
		} else {
			this.configuration['sender'].findName('fadeOutProgramInfo').Begin();
		}
	},


	setFullscreen: function() {
		var fss = !this.configuration['sender'].getHost().content.FullScreen;
		this.configuration['sender'].getHost().content.FullScreen = fss;
		hotvideoplayer.utils.delegate(this.view,this.view.onFullscreen);
	}
}










/****************************************************************************
* The view of the player MVC triad, which manages the graphics.
****************************************************************************/
hotvideoplayer.View = function(cfg,ctr) {
	this.configuration = cfg;
	this.listeners = Array();
	this.controller = ctr;
	this.fstimeout;
	this.fslistener;
	this.beginX; 
	this.leftMax;
	this.rightMax;
	this.videostatus;
	this.isMouseDown = true; 
	this.firstrelvid=0;
	this.vidperpage=0;
	this.currentchannel=0;
	this.display = this.configuration['sender'].findName("PlayerDisplay");
	this.controlbar = this.configuration['sender'].findName("PlayerControls");
	this.configuration['sender'].getHost().content.onResize = 
		hotvideoplayer.utils.delegate(this,this.resizePlayer);
	this.configuration['sender'].getHost().content.onFullScreenChange = 
		hotvideoplayer.utils.delegate(this,this.onFullscreen);
	this.assignColorsClicks();
	this.resizePlayer();
}

hotvideoplayer.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;
		}
		this.delegate('BUFFER',[pct]);
	},

	onFullscreen: function(fss) { 
		var snd = this.configuration['sender'];
		var fst = snd.getHost().content.FullScreen;
		if(fst) { 
			this.fstimeout = setTimeout(hotvideoplayer.utils.delegate(this,
				this.hideFSControls),2000);
			this.fslistener = this.display.addEventListener('MouseMove',
				hotvideoplayer.utils.delegate(this,this.showFSControls));
		} else {
			clearTimeout(this.fstimeout);
			this.display.removeEventListener("MouseMove",this.fslistener);
			this.controlbar.Visibility = "Visible";
			this.display.Cursor = "Hand";
		}
		this.resizePlayer();
		this.delegate('FULLSCREEN');
	},

	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(hotvideoplayer.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);
		this.delegate('LOAD',[pct]);
	},

	onMute: function(mut) {
		var snd = this.configuration['sender'];
		this.configuration['usemute'] = ''+mut;
		if(mut) {
			this.configuration['sender'].findName('MuteButton').Visibility='Collapsed';
			this.configuration['sender'].findName('unMuteButton').Visibility='Visible';

			if(this.state == 'Playing') {
				//snd.findName("MuteIcon").Visibility = "Visible";
			}
		} else {
			this.configuration['sender'].findName('MuteButton').Visibility='Visible';
			this.configuration['sender'].findName('unMuteButton').Visibility='Collapsed';
			snd.findName("MuteIcon").Visibility = "Collapsed";
		}
		this.delegate('MUTE');
	},

	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('PlayButton').Visibility='Collapsed';
			snd.findName('PauseButton').Visibility='Visible';
			if (stt=='Playing') {
				snd.findName("BufferIcon").Visibility = "Collapsed";
				snd.findName("BufferText").Visibility = "Collapsed";
				if(this.configuration['usemute'] == 'true') {
					//snd.findName("MuteIcon").Visibility = "Visible";
				}
			} else{
				snd.findName("BufferIcon").Visibility = "Visible";
				snd.findName("BufferText").Visibility = "Visible";
			}
		} else { 
			snd.findName("MuteIcon").Visibility = "Collapsed";
			snd.findName("BufferIcon").Visibility = "Collapsed";
			snd.findName("BufferText").Visibility = "Collapsed";
			snd.findName("PlayButton").Visibility = "Visible";
			snd.findName("PauseButton").Visibility = "Collapsed";
			if(this.configuration['linkfromdisplay'] == 'true') {
				snd.findName("PlayIcon").Visibility = "Collapsed";
			} else { 
				//snd.findName("PlayIcon").Visibility = "Visible";
			}
		}
		try {
			if(!(old == 'Completed' && stt == 'Buffering') &&
				!(old == 'Buffering' && stt == 'Paused')) {
				playerStatusChange(old.toUpperCase(),stt.toUpperCase());
			}
		} catch (err) {}
		this.delegate('STATE',[old,stt]);
	},

	onTime: function(elp,dur) {
		var snd = this.configuration['sender'];
		var snd = this.configuration['sender'];
		var max = snd.findName("TimeSlider").Width;
		if(dur > 0) { 
			var pos = Math.round(max*elp/dur);
			this.configuration['duration'] = dur;
			snd.findName("ElapsedText").Text = hotvideoplayer.utils.timestring(elp);
			snd.findName("RemainingText").Text = hotvideoplayer.utils.timestring(dur-elp);
			//snd.findName("TimeSymbolPict").Visibility = "Visible";
			snd.findName("TimeSymbolPict")['Canvas.Left'] = pos+4;
			snd.findName("TimeHighlight").Width = pos-2;
		} else  { 
			//snd.findName("TimeSymbolPict").Visibility = "Collapsed";
		}
		this.delegate('TIME',[elp,dur]);
	},

	onVolume: function(pct) {
		var snd = this.configuration['sender'];
		snd.findName("VolumeSymbolPict")['Canvas.Left']=Math.round(pct/5);
		this.delegate('VOLUME',[pct]);
	},

	assignColorsClicks: function() {
		this.display.Cursor = "Hand";
		this.display.Background = "#FF"+this.configuration['screencolor'];
		if(this.configuration['linkfromdisplay'] == 'false') { 
			this.display.addEventListener('MouseLeftButtonUp',
				hotvideoplayer.utils.delegate(this.controller,
				this.controller.setPlay));
		} else { 
			this.display.addEventListener('MouseLeftButtonUp',
				hotvideoplayer.utils.delegate(this.controller,
				this.controller.setLink));
			this.display.findName("PlayIcon").Visibility = "Collapsed";
		}
		if(this.configuration['logo'] != '') {
			this.display.findName('OverlayCanvas').Visibility = "Visible";
			this.display.findName('OverlayLogo').ImageSource = 
				this.configuration['logo'];
		}
		this.configuration['sender'].findName('ElapsedText').Foreground = 
			"#FF"+this.configuration['frontcolor'];
		this.assignSlider('Time',this.changeTime);
		this.configuration['sender'].findName('DownloadProgress').Fill = '#FFFF0000'
		this.configuration['sender'].findName('RemainingText').Foreground = 
			"#FF"+this.configuration['frontcolor'];

		this.assignButton('FullscreenButton',this.controller.setFullscreen,true);
		this.assignButton('FullscreenoffButton',this.controller.setFullscreen,true);
		this.assignButton('WidescreenButton',this.controller.setWidescreen,true);
		this.assignButton('WidescreenoffButton',this.controller.setWidescreen,true);
		this.assignButton('MuteButton',this.controller.setMute,true);
		this.assignButton('unMuteButton',this.controller.setMute,true);
		this.assignButton('PlayButton',this.controller.setPlay,true);
		this.assignButton('PauseButton',this.controller.setPlay,true);
		this.assignButton('VolumeButton',this.controller.setNothing,true);
		
		this.assignButton('repeatVideo',this.controller.setPlay,true);
		this.assignButton('ForwardIcon',this.controller.setNext,true);
		this.assignButton('RewindIcon',this.controller.setPrev,true);
		
		this.assignButton('ChannelsButton',this.controller.showChannels,true);
		this.assignButton('closeChannels',this.controller.showChannels,false);
		this.assignButton('closeProgramInfoBox',this.controller.showProgramInfo,false);
		this.assignButton('openProgramInfoBox',this.controller.showProgramInfo,false);
		
		

	
		var ts=this.configuration['sender'].findName('VolumeSymbolPict');
		ts.addEventListener('MouseLeftButtonDown', hotvideoplayer.utils.delegate(this.controller,this.onMouseDown));
		ts.addEventListener('MouseLeftButtonUp', hotvideoplayer.utils.delegate(this.controller,this.onMouseUp));
		ts.addEventListener('MouseMove', hotvideoplayer.utils.delegate(this.controller,this.onMouseMove));

		var ts=this.configuration['sender'].findName('TimeSymbolPict');
		ts.addEventListener('MouseLeftButtonDown', hotvideoplayer.utils.delegate(this.controller,this.onMouseDown));
		ts.addEventListener('MouseLeftButtonUp', hotvideoplayer.utils.delegate(this.controller,this.onMouseUp));
		ts.addEventListener('MouseMove', hotvideoplayer.utils.delegate(this.controller,this.onMouseMove));
	},

	onMouseDownVideo: function(sender, mouseEventArgs){
		window.location = hotvideoplayer.xml.linkArray[Number(sender.Name.substr(7,5))];
	},

	onMouseDownChannel: function(sender, mouseEventArgs){
		var cc = Number(sender.Name.substr(8,5));
		var justnow = hotvideoplayer.xml.justNow;
		this.configuration['sender'].findName('CurrentChannelName').Text=hotvideoplayer.xml.channels[cc][1];
		this.controller.setLoad(hotvideoplayer.xml.channels[cc][2]);
		this.currentchannel = cc;
		
		for (var x=0; x<hotvideoplayer.xml.programs.length; x++) {
			if (hotvideoplayer.xml.programs[x][0]==hotvideoplayer.xml.channels[cc][0]){
				if (hotvideoplayer.xml.programs[x][3]<=justnow && hotvideoplayer.xml.programs[x][4]>=justnow) {
					break;
				}
			}
		}
		
		
		if (x<hotvideoplayer.xml.programs.length){
			var st = hotvideoplayer.utils.GetStrTime(hotvideoplayer.xml.programs[x][3]);
			var et = hotvideoplayer.utils.GetStrTime(hotvideoplayer.xml.programs[x][4]);
			var dur = Math.round(hotvideoplayer.xml.programs[x][7]/60);
			var rem = Math.round((hotvideoplayer.xml.programs[x][4]-justnow)/60000);
			var elp = Math.round((dur - rem)/dur*70);
		
			this.configuration['sender'].findName('CurrentProgramName').Text=hotvideoplayer.xml.programs[x][2];
			this.configuration['sender'].findName('CurrentProgramGenre').Text=hotvideoplayer.xml.programs[x][5];
			this.configuration['sender'].findName('CurrentProgramTime').Text=st+" - "+et;
			this.configuration['sender'].findName('CurrentProgramDuration').Text=String(dur)+" Dk";
			this.configuration['sender'].findName('CurrentProgramDurationLeft').Text=String(rem)+" Dk";
			this.configuration['sender'].findName('CurrentProgramDurationRect').Width=elp;
			this.configuration['sender'].findName('CurrentProgramInfo').Text=hotvideoplayer.xml.programs[x][6];
		} else {
			this.configuration['sender'].findName('CurrentProgramName').Text="Program Bilgisi Yok";
			this.configuration['sender'].findName('CurrentProgramGenre').Text="";
			this.configuration['sender'].findName('CurrentProgramTime').Text="";
			this.configuration['sender'].findName('CurrentProgramDuration').Text="";
			this.configuration['sender'].findName('CurrentProgramDurationLeft').Text="";
			this.configuration['sender'].findName('CurrentProgramDurationRect').Width=0;
			this.configuration['sender'].findName('CurrentProgramInfo').Text="Program Bilgisi Yok";
		}

		for (var row=0; row<10; row++) {
			this.configuration['sender'].findName('PTime_'+row).Text='';
			this.configuration['sender'].findName('PTitle_'+row).Text='';
			this.configuration['sender'].findName('PGenre_'+row).Text='';
			x++;
			if (hotvideoplayer.xml.programs.length>x){	
				if (hotvideoplayer.xml.programs[x][0]!=hotvideoplayer.xml.programs[x-1][0]) {
					for (var c=x+1;c<hotvideoplayer.xml.programs.length;c++){
						if (hotvideoplayer.xml.programs[c][0]==hotvideoplayer.xml.programs[x-1][0] && hotvideoplayer.xml.programs[c][1]!=hotvideoplayer.xml.programs[x-1][1]) {
							break;
						}	
					}
					x=c;	
				}
				if (hotvideoplayer.xml.programs.length>x){	
					this.configuration['sender'].findName('PTime_'+row).Text=hotvideoplayer.utils.GetStrTime(hotvideoplayer.xml.programs[x][3]) + ' - ' + hotvideoplayer.utils.GetStrTime(hotvideoplayer.xml.programs[x][4]);
					this.configuration['sender'].findName('PTitle_'+row).Text=hotvideoplayer.xml.programs[x][2];
					this.configuration['sender'].findName('PGenre_'+row).Text=hotvideoplayer.xml.programs[x][5];
				}
			
			}	 
		  
		}

	},
	
		
	onMouseEnterChannel: function(sender, mouseEventArgs){
		var cc = Number(sender.Name.substr(8,5));
		var pcn = this.configuration['sender'].findName('PointedChannelName');
		pcn.Text=hotvideoplayer.xml.channels[cc][1]; 
		pcn.Visibility = 'Visible';
		this.configuration['sender'].findName('PointedChannelRectangle').Visibility = 'Visible';
	},

	onMouseLeaveChannel: function(sender, mouseEventArgs){
		this.configuration['sender'].findName('PointedChannelName').Visibility = 'Collapsed';
		this.configuration['sender'].findName('PointedChannelRectangle').Visibility = 'Collapsed';
	},


	onMouseDown: function(sender, mouseEventArgs){
		this.beginX = mouseEventArgs.getPosition(null).x; 
		this.isMouseDown = true; 
		sender.captureMouse(); 
		if (sender.Name=='TimeSymbolPict') {			
				var dur = Math.round(this.model.video.NaturalDuration.Seconds*10)/10;
				var pos = Math.round(this.model.video.Position.Seconds*10)/10;
				var progressPercent = pos/dur;
				var len = this.configuration['sender'].findName('TimeSlider').Width;
				var completedPixel = Math.round(progressPercent*len);
				var uncompletedPixel = len-completedPixel;
				this.leftMax = this.beginX  -  completedPixel;
				this.rightMax = this.beginX + uncompletedPixel;
				this.videostatus = this.state;
				this.model.goPause();		
		}

	},
	
	
	onMouseUp: function(sender, mouseEventArgs) 
	{ 
		this.isMouseDown = false; 
		sender.releaseMouseCapture();
		if (sender.Name=='TimeSymbolPict' && this.videostatus!="Paused") {
			setTimeout(hotvideoplayer.utils.delegate(this.model,this.model.goStart),200);		
		}
	},

	
	onMouseMove: function(sender, mouseEventArgs) { 
	  if (this.isMouseDown == true) 
		{ 
			var currX = mouseEventArgs.getPosition(null).x; 
			if (sender.Name=='VolumeSymbolPict') {
				this.setVolume(Math.round(this.configuration['volume'])+(currX - this.beginX)*5);	
			}
			if (sender.Name=='TimeSymbolPict') {
				if (currX!=this.beginX) {
					var newpos=(this.configuration['duration']/(this.rightMax - this.leftMax))*(currX-this.leftMax);
					this.setScrub(newpos);
				} 
				if (currX<this.leftMax || currX>this.rightMax) {
					currX=this.beginX; 
				} 
			}	
			sender["Canvas.Left"] += currX - this.beginX; 
			this.beginX = currX; 
		} 
	},


	assignButton: function(btn,act,tiptext) {
		var el1 = this.configuration['sender'].findName(btn);
		el1.Cursor = "Hand";
		if (act!=''){ 
			el1.addEventListener('MouseLeftButtonUp',
				hotvideoplayer.utils.delegate(this.controller,act));
		}
		if (tiptext){
			el1.addEventListener('MouseEnter',
				hotvideoplayer.utils.delegate(this,this.rollOver));
			el1.addEventListener('MouseLeave',
				hotvideoplayer.utils.delegate(this,this.rollOut));
		}
	},
	
	rollOver: function(sdr) {
		var str = sdr.Name+'Text';
		var cbwdt = this.controlbar.Width;
		var cblft = this.controlbar['Canvas.Left'];
		var ilft = this.configuration['sender'].findName(sdr.Name)['Canvas.Left'];
		var nawdt = this.configuration['sender'].findName('TipArea').Width;
		if(ilft+nawdt<cbwdt){
			this.configuration['sender'].findName('Notes')['Canvas.Left']=cblft+ilft;
		} else {
			this.configuration['sender'].findName('Notes')['Canvas.Left']=cblft+cbwdt-nawdt-2;
		}
		this.configuration['sender'].findName('TipText').Text =this.configuration['sender'].findName(str).Text ;
		this.configuration['sender'].findName('fadeinNotes').Begin();	
	},

	rollOut: function(sdr) {
		this.configuration['sender'].findName('fadeoutNotes').Begin();	
	},
	
	assignSlider: function(sld,act) {
		var el1 = this.configuration['sender'].findName(sld+'Button');
		el1.Cursor = "Hand";
		el1.addEventListener('MouseLeftButtonUp',
			hotvideoplayer.utils.delegate(this,act));
		this.configuration['sender'].findName(sld+'Slider').Fill =
			"#FF"+this.configuration['frontcolor'];
		this.configuration['sender'].findName(sld+'Highlight').Fill = '#FFFF0000'
		this.configuration['sender'].findName(sld+'Symbol').Fill = 
			"#FF"+this.configuration['frontcolor'];
	},

	delegate: function(typ,arg) {
		for(var i=0; i<this.listeners.length; i++) {
			if(this.listeners[i]['type'].toUpperCase() == typ) {
				this.listeners[i]['func'].apply(null,arg);
			}
		}
	},


	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);
	},

	putRelatedVideos: function() {
		var wid = this.configuration['sender'].getHost().content.actualWidth;
		var hei = this.configuration['sender'].getHost().content.actualHeight;
		var cwd = this.configuration['width'];
		var ratio =wid/cwd;
		
		
		if (!isNaN(ratio)){
			this.configuration['sender'].findName('videoOfferScreen').Width=cwd;
			this.configuration['sender'].findName('videoOfferScreen')['Canvas.Top']=Math.round((hei-27-325*ratio)/2);
			this.configuration['sender'].findName('videoOfferScreen')['Canvas.Left']=Math.round((wid-cwd*ratio)/2);
			this.configuration['sender'].findName('relatedVideoScale').scaleX=ratio;
			this.configuration['sender'].findName('relatedVideoScale').scaleY=ratio;
		}
		
		this.vidperpage = 4;
		if (this.vidperpage>hotvideoplayer.xml.globalArray.length) {this.vidperpage=hotvideoplayer.xml.globalArray.length;}
		
		if(this.configuration['sender'].findName('relvid_0')){
			for (var z=0; z<hotvideoplayer.xml.globalArray.length; z++) {
				this.configuration['sender'].findName('relvid_'+z)['Canvas.Top']=(z*65)-(this.firstrelvid*65);
				if (z>=this.firstrelvid && z<this.vidperpage+this.firstrelvid){
					this.configuration['sender'].findName('relvid_'+z).Visibility="Visible";
				} else {
					this.configuration['sender'].findName('relvid_'+z).Visibility="Collapsed";			
				}
			}
			
		}
		if (this.firstrelvid==0) {this.configuration['sender'].findName('RewindIcon').Opacity=0.1;} 
							else {this.configuration['sender'].findName('RewindIcon').Opacity=1;}
		if (this.firstrelvid+this.vidperpage>=hotvideoplayer.xml.globalArray.length) 
								 {this.configuration['sender'].findName('ForwardIcon').Opacity=0.1;} 
							else {this.configuration['sender'].findName('ForwardIcon').Opacity=1;}
	},

	putChannels: function() {
		if (hotvideoplayer.xml.programs.length==0 ) {
			var tomorrow = new Date();
			var today = hotvideoplayer.xml.justNow;
			var hour = today.getHours();
			if (hour<7) {
				var t=Date.parse(today);
				today.setTime(t-(24*60*60*1000));
				tomorrow = hotvideoplayer.xml.justNow;
			} else {
				var t=Date.parse(today);
				tomorrow.setTime(t+(24*60*60*1000));
			}

			var monthstr;
			var daystr;
			var day = today.getDate();
			var month = today.getMonth()+1;
			var year = today.getFullYear(); 
			if (month<10) monthstr='0'+month; else monthstr=month;
			if (day<10) daystr='0'+day; else daystr=day;
			hotvideoplayer.xml.GetProgramInfo(year+monthstr+daystr);
			
			day = tomorrow.getDate();
			month = tomorrow.getMonth()+1;
			year = tomorrow.getFullYear(); 
			if (month<10) monthstr='0'+month; else monthstr=month;
			if (day<10) daystr='0'+day; else daystr=day;
			hotvideoplayer.xml.GetProgramInfo(year+monthstr+daystr);
		}

		var wid = this.configuration['sender'].getHost().content.actualWidth;
		var hei = this.configuration['sender'].getHost().content.actualHeight;
		var cwd = this.configuration['width'];
		var ratio =wid/cwd;

		if (!isNaN(ratio)){
			if (this.configuration['sender'].findName('Channels')['Canvas.Left']<0 && ratio!=0) {
				this.configuration['sender'].findName('Channels')['Canvas.Left']=Math.round(-154*ratio);
			}
			this.configuration['sender'].findName('DA_slideoutChannels').To=Math.round(-154*ratio);
			this.configuration['sender'].findName('ChannelsScale').scaleX=ratio;
			this.configuration['sender'].findName('ChannelsScale').scaleY=ratio;
		}

		if (!this.configuration['sender'].findName('channel_0')) {
			hotvideoplayer.xml.GetChannels();
			for (var x=0; hotvideoplayer.xml.globalArray.length>x; x++) {
				var mtag = this.configuration['sender'].getHost().content.createFromXaml(hotvideoplayer.xml.globalArray[x]);
				this.configuration['sender'].findName('Channels').children.add(mtag);
				this.configuration['sender'].findName('channel_'+x).Cursor = "Hand";
				this.configuration['sender'].findName('channel_'+x).addEventListener('MouseLeftButtonDown', hotvideoplayer.utils.delegate(this,this.onMouseDownChannel));
				this.configuration['sender'].findName('channel_'+x).addEventListener('MouseEnter', hotvideoplayer.utils.delegate(this,this.onMouseEnterChannel));
				this.configuration['sender'].findName('channel_'+x).addEventListener('MouseLeave', hotvideoplayer.utils.delegate(this,this.onMouseLeaveChannel));
			} // for

			for (var x=0; x<10; x++) {
				var ctop=x*21+126;
				var mstr = '<Canvas Canvas.Top="'+ctop+'" Canvas.Left="8" >';
				mstr += '<Rectangle Width="484" Height="1" Fill="black" Opacity="0.1" />';
				mstr += '<TextBlock Name="PTime_'+x+'" Text="" Canvas.Left="6" Canvas.Top="3" Foreground="darkblue" FontSize="12" />';
				mstr += '<TextBlock Name="PTitle_'+x+'" Text="" Canvas.Left="96" Canvas.Top="3" Foreground="white" FontSize="12" />';
				mstr += '<TextBlock Name="PGenre_'+x+'" Text="" Canvas.Left="370" Canvas.Top="3" Foreground="green" FontSize="12" />';
				mstr += '</Canvas>';
				var mtag = this.configuration['sender'].getHost().content.createFromXaml(mstr);
				this.configuration['sender'].findName('programInfoBox').children.add(mtag);
			} // for
			
		} 
	},



	resizePlayer: function() {
		var wid = this.configuration['sender'].getHost().content.actualWidth;
		var hei = this.configuration['sender'].getHost().content.actualHeight;
		var fss = this.configuration['sender'].getHost().content.FullScreen;
	
		if (this.configuration['streamtype']=='vod'){
			this.putRelatedVideos();
		}
		if (this.configuration['streamtype']=='tv' && this.configuration['guide']=='on'){
			this.putChannels();
		}

		this.configuration['sender'].findName('fadeoutNotes').Begin();	
		if(this.configuration['shownavigation'] == 'true') {
			if(fss == true) {
				this.resizeDisplay(wid,hei);
				if (this.configuration['streamtype']=='vod'){
					this.controlbar['Canvas.Left'] = Math.round(wid/2-250);
					this.resizeControlbar(500,hei-this.controlbar.Height-16);
				} else {
					if (this.configuration['guide']=='on') {
						this.controlbar['Canvas.Left'] = Math.round(wid/2-75);
						this.resizeControlbar(150,hei-this.controlbar.Height-16);
					} else { 
						this.controlbar['Canvas.Left'] = Math.round(wid/2-60);
						this.resizeControlbar(120,hei-this.controlbar.Height-16);
					}
				}
				this.controlbar.findName('ControlbarBack')['Opacity'] = 0.3;
				this.configuration['sender'].findName('FullscreenButton')['Visibility']='Collapsed';
				this.configuration['sender'].findName('FullscreenoffButton')['Visibility']='Visible';
				this.controlbar.findName('WidescreenButton')['Visibility'] = 'Collapsed';
				this.controlbar.findName('WidescreenoffButton')['Visibility'] = 'Collapsed';
				this.configuration['sender'].findName('Notes')['Canvas.Left'] = Math.round(wid/2-60);
				this.configuration['sender'].findName('Notes')['Canvas.Top'] = hei-this.controlbar.Height-35;
			} else { 
				this.controlbar['Canvas.Left'] = 0;
				this.resizeControlbar(wid,hei-this.controlbar.Height);
				this.configuration['sender'].findName('FullscreenButton').Visibility='Visible';
				this.configuration['sender'].findName('FullscreenoffButton').Visibility='Collapsed';
				if (this.configuration['streamtype']=='vod'){				
					this.resizeDisplay(wid,hei-27); //  (wid,hei-27) 27 is controlbar height
					this.controlbar.findName('ControlbarBack')['Opacity'] = 1;
					this.controlbar['Opacity'] = 1;
				} else {
					this.resizeDisplay(wid,hei); 
					this.controlbar.findName('ControlbarBack')['Opacity'] = 0.3;
					this.display.addEventListener('MouseEnter', hotvideoplayer.utils.delegate(this,this.rollOverControlbar));
					this.display.addEventListener('MouseLeave', hotvideoplayer.utils.delegate(this,this.rollOutControlbar));
					this.controlbar.addEventListener('MouseEnter', hotvideoplayer.utils.delegate(this,this.rollOverControlbar));
					this.controlbar.addEventListener('MouseLeave', hotvideoplayer.utils.delegate(this,this.rollOutControlbar));
				}
				if (this.configuration['initialsize']=='mediumsize'){
					this.controlbar.findName('WidescreenButton')['Visibility'] = 'Visible';
					this.controlbar.findName('WidescreenoffButton')['Visibility'] = 'Collapsed';
				} else {
					this.controlbar.findName('WidescreenButton')['Visibility'] = 'Collapsed';
					this.controlbar.findName('WidescreenoffButton')['Visibility'] = 'Visible';				
				}
				if(this.configuration['streamtype']=="videoads") {
					this.controlbar.findName('WidescreenButton')['Visibility'] = 'Collapsed';
					this.controlbar.findName('WidescreenoffButton')['Visibility'] = 'Collapsed';
					this.controlbar.findName('FullscreenButton')['Visibility'] = 'Collapsed';
					this.controlbar.findName('FullscreenoffButton')['Visibility'] = 'Collapsed';
				}		
				this.configuration['sender'].findName('Notes')['Canvas.Left'] = wid-125;
				this.configuration['sender'].findName('Notes')['Canvas.Top'] = hei-50;
			}
		} else {
			this.resizeDisplay(wid,hei);
		}
	},

	rollOverControlbar: function(sdr){
		this.configuration['sender'].findName('fadeinControlbar').Begin();	

	},

	rollOutControlbar: function(sdr){
		this.configuration['sender'].findName('fadeoutControlbar').Begin();	
	},


	resizeDisplay: function(wid,hei) {
		this.stretchElement('PlayerDisplay',wid,hei);
		this.stretchElement('VideoWindow',wid,hei);
		this.stretchElement('PlaceholderImage',wid,hei);
		this.centerElement('PlayIcon',wid,hei);
		this.centerElement('MuteIcon',wid,hei);
		this.centerElement('BufferIcon',wid,hei);
		this.centerElement('BufferText',wid,hei);
		this.display.findName('OverlayCanvas')['Canvas.Left'] = wid -
			this.display.findName('OverlayCanvas').Width - 10;
		this.display.Visibility = "Visible";
	},

	resizeControlbar: function(wid,yps,alp) {
		var fss = this.configuration['sender'].getHost().content.FullScreen;
		var imgwid=5;
			if (this.configuration['streamtype']=='tv' && this.configuration['guide']=='on'){
			this.controlbar.findName('ChannelsButton').Visibility="Visible";
		}
		if (this.configuration['streamtype']=='vod'){
			this.controlbar.findName('RemainingButton').Visibility="Visible";
			this.controlbar.findName('ElapsedButton').Visibility="Visible";
			this.controlbar.findName('TimeButton').Visibility="Visible";
		}
		if(!fss && this.configuration['streamtype']!="videoads") {
			imgwid = imgwid + this.configuration['sender'].findName('WidescreenButton').Width;
			this.placeElement('WidescreenButton',wid-imgwid);
			this.placeElement('WidescreenoffButton',wid-imgwid);		
		}
		if(this.configuration['streamtype']!="videoads") {		
			imgwid = imgwid + 6 + this.configuration['sender'].findName('FullscreenButton').Width;
			this.placeElement('FullscreenButton',wid-imgwid);
			this.placeElement('FullscreenoffButton',wid-imgwid);			
		}
		imgwid = imgwid + 3 + this.configuration['sender'].findName('VolumeButton').Width;
		this.placeElement('VolumeButton',wid-imgwid);
		imgwid = imgwid - 3 + this.configuration['sender'].findName('MuteButton').Width;
		this.placeElement('MuteButton',wid-imgwid);
		this.placeElement('unMuteButton',wid-imgwid);
		imgwid = imgwid + 5 + this.configuration['sender'].findName('RemainingButton').Width;
		this.placeElement('RemainingButton',wid-imgwid);
		
		var imgwidleft=5;
		if (this.configuration['streamtype']=='vod'){
			this.placeElement('PlayButton',imgwidleft);
			this.placeElement('PauseButton',imgwidleft);
		} else {
			this.placeElement('PlayButton',wid-imgwid + 15);
			this.placeElement('PauseButton',wid-imgwid + 15);
		}
		
		imgwidleft = imgwidleft + 10 + this.configuration['sender'].findName('PlayButton').Width;
		this.placeElement('ElapsedButton',imgwidleft);
		this.stretchElement('TimeButton',wid-imgwidleft-imgwid);
		this.stretchElement('TimeSlider',wid-imgwidleft-imgwid-65);
		this.stretchElement('DownloadProgress',wid-imgwidleft-imgwid-65);
		var tsb = this.configuration['sender'].findName('TimeSymbol');
		this.stretchElement('TimeHighlight',tsb['Canvas.Left']-5);
		

		this.controlbar['Canvas.Top'] = yps;
		this.stretchElement('PlayerControls',wid);
		this.stretchElement('ControlbarBack',wid);
		
		var pos = Math.round(this.configuration['sender'].findName("VideoWindow").Position.Seconds*10)/10;
		this.onTime(pos,this.configuration['duration']);
		
		this.controlbar.Visibility = "Visible";
	},

	centerElement: function(nam,wid,hei) {
		var elm = this.configuration['sender'].findName(nam);
		elm['Canvas.Left'] = Math.round(wid/2 - elm.Width/2);
		elm['Canvas.Top'] = Math.round(hei/2 - elm.Height/2);
	},

	stretchElement: function(nam,wid,hei) {
		var elm = this.configuration['sender'].findName(nam);
		elm.Width = wid;
		if (hei != undefined) { elm.Height = hei; }
	},

	placeElement: function(nam,xps,yps) {
		var elm = this.configuration['sender'].findName(nam);
		elm['Canvas.Left'] = xps;
		if(yps) { elm['Canvas.Top'] = yps; }
	}
}









/****************************************************************************
* The model of the player MVC triad, which stores all playback logic.
****************************************************************************/
hotvideoplayer.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.currentfile=0;
	this.video.Stretch = str[this.configuration['overstretch']];
	this.preview.Stretch = str[this.configuration['overstretch']];
	this.video.BufferingTime = 
		hotvideoplayer.utils.spanstring(this.configuration['bufferlength']);
	this.video.AutoPlay = true;
	this.video.AddEventListener("CurrentStateChanged",
		hotvideoplayer.utils.delegate(this,this.stateChanged));
	this.video.AddEventListener("MediaEnded",
		hotvideoplayer.utils.delegate(this,this.mediaEnded));
	this.video.AddEventListener("BufferingProgressChanged",
		hotvideoplayer.utils.delegate(this,this.bufferChanged));
	this.video.AddEventListener("DownloadProgressChanged",
		hotvideoplayer.utils.delegate(this,this.downloadChanged));
	if(this.configuration['image'] != '') {
		this.preview.Source = this.configuration['image'];
	}
}

hotvideoplayer.Model.prototype = {
	goPause: function(sec) {
		this.video.pause();
		if(!isNaN(sec)) {
			this.video.Position = hotvideoplayer.utils.spanstring(sec);
		}
		this.timeChanged();
	},
	
	goStart: function(sec) {
		this.video.Visibility = 'Visible';
		if(this.configuration['streamtype']!="videoads") {	
			this.preview.Visibility = 'Collapsed';
		}
		if(this.state == "Closed") {
			this.video.Source = this.configuration['sourcefile'];
		} else {
			this.video.play();
		}
		if(!isNaN(sec)) {
			this.video.Position = hotvideoplayer.utils.spanstring(sec);
		}
	},

	goStop: function() {
			this.video.Visibility = 'Collapsed';
			this.preview.Visibility = 'Visible';
			this.goPause(0);
			this.video.Source = 'null';
			this.view.onBuffer(0);
			clearInterval(this.timeint);
	},

	goVolume: function(pct) {
		this.video.Volume = pct/100;
	},

	stateChanged: function() {
		var stt = this.video.CurrentState;

		if(stt == 'Closed'  && this.video.Source != 'null' && this.configuration['streamtype'] == 'tv') {
			this.preview.Source = this.configuration['errorimage'];
			this.preview.Visibility='Visible';
			this.video.Visibility='Collapsed';
		} else {
			this.preview.Source = this.configuration['image'];
		}

		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(hotvideoplayer.utils.delegate(
					this,this.timeChanged),100);
			}
		}
	},

	mediaEnded: function() {
	//alert('movie ends');
		var playlistended = false; 
		if(this.configuration['repeat'] == 'true') {	
			this.goStart(0);
		} else {
			this.state = 'Completed';
			this.view.onState(this.state,'Completed');
			this.video.Visibility = 'Collapsed';
			this.preview.Visibility = 'Visible';
			this.goPause(0);
			playlistended = true;
			if (this.configuration['filearray'].length > 0){
				this.currentfile++;
				if (this.currentfile==this.configuration['filearray'].length){
					this.currentfile--;
					playlistended = true;
				} else {
					playlistended = false;
					this.configuration['sourcefile']=this.configuration['filearray'][this.currentfile];
					this.goStop();
					setTimeout(hotvideoplayer.utils.delegate(this,this.goStart),1000);
				} // if (this.currentfile==this.configuration['filearray'].length)
			} // if (this.configuration['filearray'].length > 0)
		} // if(this.configuration['repeat'] == 'true') 
	if (this.configuration['catid'].length>0 && playlistended){
			if (!this.configuration['sender'].findName('relvid_0')) {
				hotvideoplayer.xml.GetRelatedVideos(this.configuration['catid']);
				for (var x=0; hotvideoplayer.xml.globalArray.length>x; x++) {
					var mtag = this.configuration['sender'].getHost().content.createFromXaml(hotvideoplayer.xml.globalArray[x]);
					this.configuration['sender'].findName('relatedVideos').children.add(mtag);
					this.configuration['sender'].findName('relvid_'+x).Cursor = "Hand";
					this.configuration['sender'].findName('relvid_'+x).addEventListener('MouseLeftButtonDown', hotvideoplayer.utils.delegate(this.controller,this.view.onMouseDownVideo));
				} // for
			} // if (!this.configuration['sender'].findName('relvid_0'))
				this.view.putRelatedVideos();
				this.configuration['sender'].findName('videoOfferScreen')['Visibility']='Visible';
		} // if (this.configuration['catid'].length>0 && playlistended)

	},

	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() {
	//ilave play listte 2.item suresi icin , kaldirilacak
		this.configuration['duration'] = Math.round(this.video.NaturalDuration.Seconds*10)/10;
	//ilave son		
		var pos = Math.round(this.video.Position.Seconds*10)/10;
		this.view.onTime(pos,this.configuration['duration']);
	}
}










/****************************************************************************
* Some utility functions.
****************************************************************************/
hotvideoplayer.utils.delegate = function(obj,fcn) {
	return function() {
		return fcn.apply(obj,arguments);
	}
}
hotvideoplayer.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;
}
hotvideoplayer.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;
}

hotvideoplayer.utils.GetStrTime = function(dt) {
    var strTime = '';
    var hr = dt.getHours();
    var mn = dt.getMinutes();
    var strHr = '0';
    var strMn = '0';

    if (hr < 10) {
        strHr = '0' + hr;
    } else {
    strHr = hr;
    }
    if (mn < 10) {
        strMn = '0' + mn;
    } else {
    strMn = mn;
    }
    strTime = strHr + ':' + strMn;
    return strTime;
}


hotvideoplayer.utils.clock = function(begintime) {
	var datenumber=Date.parse(begintime);
	hotvideoplayer.xml.justNow.setTime(datenumber+1000);
	var timeoutstr='hotvideoplayer.utils.clock(hotvideoplayer.xml.justNow)';
	setTimeout(timeoutstr,1000);
	
}



/****************************************************************************
*XML functions.
****************************************************************************/

hotvideoplayer.xml.getHTTPRequestObject = function() {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
}


hotvideoplayer.xml.RelatedVideosCallBack = function() {
    var httpResults;
    try {
        if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
                eval('httpResults=' + xmlhttp.responseText);
                if (hotvideoplayer.xml.CreateRelatedVideos(httpResults)) {
                    xmlhttp = null;
                }
            } else {
                xmlhttp = null;
                getHTTPRequestObject();
            }
        } 
    } catch (e) { }
}


hotvideoplayer.xml.GetRelatedVideos = function(catid) {
	xmlhttp = null;
	hotvideoplayer.xml.getHTTPRequestObject();
	if (xmlhttp) { 
		try {
			xmlhttp.open('GET', "vidixhandler.aspx?catid=" + catid, false);
			xmlhttp.onreadystatechange = hotvideoplayer.xml.RelatedVideosCallBack;
			xmlhttp.send('');
		} catch (e) { }
	}
}


hotvideoplayer.xml.CreateRelatedVideos = function(br) {
		for (var x=0; br.RelatedVideos.length>x; x++) {
			var mlink = br.RelatedVideos[x].VideoLink
			var mname = br.RelatedVideos[x].VideoName
			var mpict = br.RelatedVideos[x].VideoPict
			var mown = br.RelatedVideos[x].VideoOwner
			var mview = br.RelatedVideos[x].VideoView
			var mrate = br.RelatedVideos[x].VideoRate
			if (mrate>100) mrate=100;

			var mstr = 	'<Canvas Name="relvid_' + x + '" Canvas.Top="' + (x*65) + '" Visibility="Collapsed">' ;
			mstr += 	'<Rectangle  Height="62" Width="400" Fill="#b4b4b4"  RadiusX="5" RadiusY="5"  />' ;
			mstr += 	'<Image  Source="' + mpict + '"   Canvas.Left="5" Canvas.Top="5"  Width="79" Height="52" />' ;
			mstr += 	'<TextBlock Text="' + mname + '" Foreground="Blue" FontSize="12" Canvas.Left="90" Canvas.Top="5"  Width="300"  Height="30" TextWrapping="Wrap" LineHeight="14"  LineStackingStrategy="BlockLineHeight" /> ' ;
			mstr += 	'<TextBlock Text="' + mown + '" Foreground="Green" FontSize="11" Canvas.Left="90" Canvas.Top="32" />' ;
			mstr += 	'<TextBlock Text="' + mview + ' kez izlendi" FontSize="9" Canvas.Left="90" Canvas.Top="46" />' ;
			mstr += 	'<Canvas  Width="' + Math.round(mrate/100*78) + '" Height="15" Canvas.Left="320" Canvas.Top="42" Background="Red">' ;
			mstr += 	'<Image Source="img/stars.png" Canvas.Left="-1" Canvas.Top="-1" />' ;
			mstr += 	'</Canvas>' ;
			mstr += 	'</Canvas>' ;
			hotvideoplayer.xml.globalArray[x]=mstr;
			hotvideoplayer.xml.linkArray[x]=mlink;
			
		}
	return true;		
	}	


hotvideoplayer.xml.ChannelsCallBack = function() {
    var httpResults;
    try {
        if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
                eval('httpResults=' + xmlhttp.responseText);
                if (hotvideoplayer.xml.CreateChannels(httpResults)) {
                    xmlhttp = null;
                }
            } else {
                xmlhttp = null;
                getHTTPRequestObject();
            }
        } 
    } catch (e) { }
}


hotvideoplayer.xml.GetChannels = function() {
	xmlhttp = null;
	hotvideoplayer.xml.getHTTPRequestObject();
	if (xmlhttp) { 
		try {
			xmlhttp.open('GET', "silver/channels.xml", false);
			xmlhttp.onreadystatechange = hotvideoplayer.xml.ChannelsCallBack;
			xmlhttp.send('');
		} catch (e) { }
	}
}


hotvideoplayer.xml.CreateChannels = function(br) {
	
		for (var x=0; br.Channels.length>x; x++) {
			var mlink = br.Channels[x].Link;
			var mname = br.Channels[x].Name;
			var mpict = br.Channels[x].Pict;
			var mcid   = br.Channels[x].CID;

			switch (x%2){
				case 0 :
					var canvasLeft=5;
					break;
				case 1 :
					var canvasLeft=79;
					break;
				case 2 :
					var canvasLeft=153;
					break;
			}
			
			var canvasTop= Math.floor(x/2)*40+99;
	
			var mstr = '<Canvas Name="channel_'+x+'" Canvas.Top="'+canvasTop+'" Canvas.Left="'+canvasLeft+'">';
			mstr +=    '<Rectangle Width="72" Height="38" Stroke="#a0a0a0" StrokeThickness="1" RadiusX="5" RadiusY="5" />';
			mstr +=    '<Image Source="'+mpict+'" Canvas.Left="3" Canvas.Top="4" />';
			mstr +=    '<TextBlock Name="cname_'+x+'" Text="' + mname + '" Visibility="Collapsed"/>';
			mstr +=    '</Canvas>';

			hotvideoplayer.xml.globalArray[x]=mstr;
			hotvideoplayer.xml.channels.push([,,,]);
			hotvideoplayer.xml.channels[x][0]=mcid;
			hotvideoplayer.xml.channels[x][1]=mname;
			hotvideoplayer.xml.channels[x][2]=mlink;
			hotvideoplayer.xml.channels[x][3]=mpict;
		}
		return true;
	}	


hotvideoplayer.xml.ProgramInfoCallBack = function() {
    var httpResults;
    try {
        if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
                eval('httpResults=' + xmlhttp.responseText);
                if (hotvideoplayer.xml.CreateProgramInfo(httpResults)) {
                    xmlhttp = null;
                }
            } else {
                xmlhttp = null;
                getHTTPRequestObject();
            }
        } 
    } catch (e) {}
}


hotvideoplayer.xml.GetProgramInfo = function(day) {
	xmlhttp = null;
	hotvideoplayer.xml.getHTTPRequestObject();
	if (xmlhttp) { 
		try {
			xmlhttp.open('GET', 'silver/broadcast/' + day + '.txt', false);
			xmlhttp.onreadystatechange = hotvideoplayer.xml.ProgramInfoCallBack;
			xmlhttp.send('');
		} catch (e) { }
	}
}


hotvideoplayer.xml.CreateProgramInfo = function(br) {
	var pl = hotvideoplayer.xml.programs.length;
	for (var x=0; br.Broadcast.length>x; x++) {
		hotvideoplayer.xml.programs.push([,,,,,,,]);	
		hotvideoplayer.xml.programs[pl][0]=br.Broadcast[x].Channel;
		hotvideoplayer.xml.programs[pl][1]=br.Broadcast[x].ID;
		hotvideoplayer.xml.programs[pl][2]=br.Broadcast[x].Name;
		var strPs = br.Broadcast[x].Begin.replace('/', '');
		eval('var ps = new ' + strPs.replace('/', ''));
		hotvideoplayer.xml.programs[pl][3]=ps;
		var strEs = br.Broadcast[x].End.replace('/', '');
		eval('var es = new ' + strEs.replace('/', ''));
		hotvideoplayer.xml.programs[pl][4]=es;
		hotvideoplayer.xml.programs[pl][5]=br.Broadcast[x].Genre;
		hotvideoplayer.xml.programs[pl][6]=br.Broadcast[x].Desc;
		hotvideoplayer.xml.programs[pl][7]=br.Broadcast[x].Duration;
		pl++;
	}
		return true;	
}	


