var player = null;
function playerReady(obj) {
    player = document.getElementById(obj['id']);
	addListeners();
};
function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "startTime");
	} else {
		setTimeout("addListeners()",100);
	}
};

function startTime()
      {
        var playlist = player.getPlaylist();
	    	
		if((playlist !== null) && (playlist !== undefined))
        {
          var currentTime = new Date();
          var currentMinute = (currentTime.getHours()*60) + currentTime.getMinutes();
		  for(var j in playlist)
          {
		    // expects the time to be strict 24:00 format, ie. hh:mm (08:05 fex or 23:01) 
			var startHour = parseInt((playlist[j].starttime.slice(0,2)),10) ; 
			var startMinute = parseInt((playlist[j].starttime.slice(3,6)),10) ;
			var scheduleMinute = ((startHour*60)+startMinute);
			//				
			//alert('Item = ' + playlist[j].location);
			//alert('Item = ' + j + ' Time ' + startHour + ':' + startMinute + ' Sched Min => ' + scheduleMinute + ' Current Minute = > ' + currentMinute);
			
			if( scheduleMinute > currentMinute)
            {
             // alert(' yyeeeee:haaaaaa ');
             player.removeModelListener("STATE", "startTime");
			 player.sendEvent('STOP');
			 // alert(playlist[0].location + " : " + playlist[1].location + " : " + playlist[2].location) 
			 // player.sendEvent('ITEM', 0);
			 player.sendEvent('ITEM', (j-1));
			  //var offset = hoursminutes - playlist[j - 1].starttime;
			  //offset = (offset*100*60) 
			  //alert('Offset = ' + offset );
			  //player.sendEvent('SEEK', offset);
			  // gid('start').innerHTML = 'START TIME: ' + playlist[j-1].starttime + '   CURRENT TIME: ' + hoursminutes + '   ITEM: ' + (j-1);
              
			  break;
            }
          }
        }
        else
        {
          setTimeout("startTime();", 50);
        }
};

function createPlayer(thePlayer, theFile, theHDFile, theWidth, theDepth) {
	//alert("The Player = " + thePlayer + "The file = " + theFile );
	var flashvars = {
		file:theFile, 
		autostart:"true", 
	//	playlist:"none",
	//	shuffle:"true",
		repeat:"always",
		controlbarsize: "20",
		abouttext: "Cumbria Live Player 3.1",
		aboutlink: "http://www.CumbriaLive.tv",
		quality: "true",
		backcolor: "000000", // Control Bar and playlist
		frontcolor: "999999", // Text and Icons 
		lightcolor: "666666", // Rollover
		screencolor: "000000",  //Background colour of Display  
		//plugins: "hd-1",
		//"hd.file": theHDFile,
		skin: "http://www.CumbriaLive.tv/Streaming/Player/Modieus-CLP-Pics.swf"
	}

	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always"
	}

	var attributes = {
		id:thePlayer,  
		name:thePlayer
	}
	var PlayerWidth = theWidth ; 
	var PlayerDepth = theDepth ;  
	
	switch (config) {
		case "1" : flashvars.playlist ="none" ; flashvars.autostart = "true"; break;  
		case "2" : flashvars.playlist = "none" ; flashvars.autostart = "false"; break ; 
		case "3" : flashvars.playlist = "right" ; flashvars.playlistsize = "250" ; PlayerWidth = PlayerWidth + 250 ; flashvars.autostart = "true"; break ; 
		case "4" : flashvars.playlist = "right" ; flashvars.playlistsize = "250" ; PlayerWidth = PlayerWidth + 250 ; flashvars.autostart = "false"; break; 
		default: flashvars.playlist = "none" ; flashvars.autostart = "true";
	} ; 
    swfobject.embedSWF("http://www.CumbriaLive.tv/Streaming/Player/player-4-5.swf", "placeholder1", PlayerWidth, PlayerDepth, "9.0.115", false, flashvars, params, attributes);
}

function GetTodaysPlaylist () {
var weekDayArray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var today = new Date();
var day = weekDayArray[today.getDay()+0];
TodaysPlaylist = "http://www.CumbriaLive.tv/Streaming/Playlists/" + day + ".xml";
TodaysPlaylist = encodeURIComponent( TodaysPlaylist + '?' + Math.round(1000 * Math.random()));
};

function LoadNewVideo(URL) {
	//alert (URL)
	player.sendEvent('STOP');	
	var obj = {file:URL, type:'highwinds'};
	player.sendEvent('LOAD', obj);
	player.sendEvent("PLAY",true);
};

function LoadNewPlaylist(URL) {
	//alert (URL)
	URL = encodeURIComponent( URL + '?' + Math.round(1000 * Math.random()));
	player.sendEvent('STOP');	
	var obj = {file:URL};
	player.sendEvent('LOAD', obj);
	player.sendEvent("PLAY",true);
};

function logoreload() {
		swfobject.embedSWF("http://www.CumbriaLive.tv/NewGraphics/CLTV_Final-Smaller.swf", "LogoLayer", "250", "120", "8.0.0", false);
}
function GetURL() {
	// Query string to this URL as below - note missing http:/ - this helps browser resolve the URL correctly. 
	// http://www.cumbrialive.tv/LoadPlayer-2.htm?config3=/www.cumbrialive.tv/Streaming/Playlists/Thursday.xml - format of call. 
	// Config 1 = No playlist displayed, autostarts
	// Config 2 = No playlist displayed, click to start
	// Config 3 = With playlist displayed, autostarts
	// Config 4 = With playlist displayed, click to start  
	//	
	fullURL = document.location.toString();
	video = fullURL.substring(fullURL.indexOf('?')+9, fullURL.length) ; 
	video = ('http:/' + video) ;
	config = fullURL.substring(fullURL.indexOf('?')+7, fullURL.indexOf('?')+8);
	//alert('Video URL = ' + video + 'Config Type = ' + config + "***");
}
function GetHURL() {
	// Query string to this URL as below - note missing http:/ - this helps browser resolve the URL correctly. 
	// http://www.cumbrialive.tv/LoadPlayer-2.htm?config3=/www.cumbrialive.tv/Streaming/Playlists/Thursday.xml - format of call. 
	// Config 1 = No playlist displayed, autostarts
	// Config 2 = No playlist displayed, click to start
	// Config 3 = With playlist displayed, autostarts
	// Config 4 = With playlist displayed, click to start  
	//	
	fullURL = document.location.toString();
	video = fullURL.substring(fullURL.indexOf('?')+9, fullURL.length) ; 
	video = ('http:/' + video) ;
	config = fullURL.substring(fullURL.indexOf('?')+7, fullURL.indexOf('?')+8);
	alert('Video URL = ' + video + 'Config Type = ' + config + "***");
	type:'highwinds';
	}

function GetBURL() {
	// Query string to this URL as below - note missing http:/ - this helps browser resolve the URL correctly. 
	// http://www.cumbrialive.tv/LoadPlayer-2.htm?config3=/www.cumbrialive.tv/Streaming/Playlists/Thursday.xml - format of call. 
	// Config 1 = No playlist displayed, autostarts
	// Config 2 = No playlist displayed, click to start
	// Config 3 = With playlist displayed, autostarts
	// Config 4 = With playlist displayed, click to start  
	//	
	fullURL = document.location.toString();
	video = fullURL.substring(fullURL.indexOf('?')+9, fullURL.length) ; 
	video = ('http://blip.tv/' + video) ;
	config = fullURL.substring(fullURL.indexOf('?')+7, fullURL.indexOf('?')+8);
	//alert('Video URL = ' + video + 'Config Type = ' + config + "***");
}




//-->
// JavaScript Document