classWriteSchedule = new Class.create();

classWriteSchedule.prototype = {
	
	initialize: function(){
	},
	
	setChannel: function(channelId){
		this.channel = channelId;
		this.lastStartTimeItem = "";
		this.lastTitleItem = "";
	},
	
	fetchSchedule: function(fStreamTime){
		try {
			var obj = getObject("movieObj");
			obj.request247Schedule(this.channel);
			
			this.streamTime = fStreamTime;

		}catch(e){}
	},
	
	cutStr: function(fString, fLength){
		if(fString.length >= fLength){
			var tmp = fString.substr(0, (fLength - 3))+"...";
			return tmp;
		}else{
			return fString;
		}
	},
	
	createItem: function(fItem, fItemNumber){
		var clipArray = {};
		clipArray.starttime		= fItem.starttime;
		clipArray.time 			= fItem.starttime == "TBA" ? "TBA" : this.create_time(fItem.starttime);
		clipArray.title 		= this.cutStr(fItem.title, 48);
		clipArray.description 	= fItem.description;
		
		this.responseArray.push(clipArray);
	},
	
	getDuration: function(fDuration){
		var tmp = fDuration.split(":");
		return (tmp[1]+":"+tmp[2]);
	},	
	
	create_time: function(time){
		//time = sched_array[0]["starttime"];
			t_ar = time.split(":");
			addH2 = t_ar[0]*1;
			addM2 = t_ar[1]*1;
			addS2 = t_ar[2]*1;
			
			off_arr = this.get_offset();
			//alert(off_arr);
			f_hr =  off_arr[0];
			f_mn = off_arr[1];
			f_sc =  off_arr[2];
	
			time = (this.add_times(addH2,addM2,addS2,f_hr,f_mn,f_sc,"-"));
			
			t_p = time.split(":");
			
			if(t_p[0] == 12){
				h = 12;
				ampm = "pm";
			}else if(t_p[0] == 0){
				h = 12;
				ampm = "am";
			}else if(t_p[0] > 12 ){
				h = t_p[0] - 12 ;
				ampm = "pm";
			}else{
				h = t_p[0] ;
				ampm = "am";	
			}
			return(h + ":"+ t_p[1] + "" + ampm );
	},	
	
	get_offset: function(){
		offset = this.streamTime;
		//subtract the users clock from the stream
		var ar = offset.split(":");
		addH = ar[0]*1;
		addM = ar[1]*1;
		addS = ar[2]*1;
		
		//alert(offset);
		var odate = new Date();
		uhrs = odate.getHours();
		umins = odate.getMinutes();
		usecs = odate.getSeconds();
		
		//alert(parseInt(addM) +":"+ parseInt(umins));
		var hDiff = parseInt(addH,10) - parseInt(uhrs,10);
		var mDiff = parseInt(addM,10) - parseInt(umins,10);
		var sDiff = parseInt(addS,10) - parseInt(usecs,10);
		return( [hDiff,mDiff,sDiff] );
	},
	
	resetTitle: function(){
		this.lastStartTimeItem 	= "";
		this.lastTitleItem 		= "";
	},
	
	write_schedule: function(req){	
		//alert("write_schedule" + req);
		this.responseArray = new Array();
		try {
			var rHtml = "";
			var sched_array = req;
			var oLen = sched_array.length;
			//this.targetDiv = parent.document.getElementById(this.ElementPlayList);
			for(i=0; i < oLen; i++){
			
				if(this.lastStartTimeItem == sched_array[0].starttime && this.lastTitleItem == sched_array[0].title) {
					return;
				}
			
				this.createItem(sched_array[i], i);
			}
			
			this.lastStartTimeItem 	= sched_array[0].starttime;
			this.lastTitleItem 		= sched_array[0].title;				
			publish247Schedule(this.responseArray);
		}
		catch(e){}
	},
	
	itemDetails: function(){
		try {
			this.detailDiv = parent.document.getElementById(this.ElementNowPlaying);
	        var rHtml = "<div id=\"nowPlayingLeft\" class=\"textSmallBlack\">";
	        rHtml += "<div id=\"nowPlayingHeading\"><img style=\"vertical-align:baseline;\" src=\"/web/images/heading_Now_Playing.gif\" alt=\"\" width=\"102\" height=\"10\" /></div>";
	        rHtml += "<div id=\"nowPlayingContent\"><span style=\"text-transform: uppercase;\">"+this.firstItem.title+" ("+this.getDuration(this.firstItem.duration)+")</span><br /><br />";
	        rHtml += this.firstItem.description+"<br /><br />";
	        rHtml += "<a href=\""+this.firstItem.link_url+"\" class=\"linkNowPlaying\" target=\"_blank\">"+this.firstItem.link_text+"</a></div>";
	        rHtml += "</div>";
	        rHtml += "</div>";
	
			this.detailDiv.innerHTML = rHtml;
		}catch(e){}
	},
	
	itemRelated: function(){
		try {
			this.relatedDiv = parent.document.getElementById(this.ElementRelatedContent);
			var rHtml = "<div><img style=\"vertical-align:baseline;\" src=\"web/images/heading_you_might.gif\" alt=\"\" width=\"114\" height=\"8\" /></div>";
			rHtml += "<div class=\"relVidContent\" style=\"margin-top: 9px\">";
			for(var i=0;i < this.firstItem.related.length;i++){
	              rHtml += "<div class=\"relVidText\"><a href=\"#\" onclick=\"setFlashVariables('sendmovie','rservice=Controller&fparams=channelId**" + this.firstItem.related[i].channel + "::event**loadRsFrom3RdLevNav&fname=getRulesXML');setFlashVariables('sendmovie','rservice=Controller&fparams=channelId**" + this.firstItem.related[i].channel + "::contentId**" + this.firstItem.related[i].contentid + "::event**PLAY_CONTENT_ASX&fname=getRulesXML'); currentContentId = " + this.firstItem.related[i].contentid + "; setClipsInactive();\" class=\"linkNowPlaying\">" + this.cutStr(this.firstItem.related[i].title, 50) + "</a> </div>";
			}              
	        rHtml += "</div>";
	        this.relatedDiv.innerHTML = rHtml;
		}catch(e){}
	},
	
	cutStr: function(fString, fLength){
		if(fString.length >= fLength){
			var tmp = fString.substr(0, (fLength - 3))+"...";
			return tmp;
		}else{
			return fString;
		}
	},
	
	time_compare: function(t1,t2){
		if(t1 =="TBA"|| t2=="TBA")return true;
		//alert("x"+t1+" " + t2);
		tp1 = t1.split(":");
		tp2 = t2.split(":");
		if(parseInt(tp1[0],10) > parseInt(tp2[0],10)){
	
			return true;
		}else if(parseInt(tp1[0],10) < parseInt(tp2[0],10)){
			return false;	
		}
	
		if(parseInt(tp1[1],10) > parseInt(tp2[1],10)){
	
			return true;
		}else if(parseInt(tp1[1],10) < parseInt(tp2[1],10)){
			return false;	
		}
		if(parseInt(tp1[2],10) > parseInt(tp2[2],10)){return true;}
		
		return(false);
	},

	get_utc_time: function(offset ,stime) {
		try {
			var ar = offset.split(":");
			addH = Math.abs(ar[0]*1);
			addM = Math.abs(ar[1]*1);
			addS = Math.abs(ar[2]*1);
			
			//calculate the stream time into pieces
			var spieces = stime.split("]");
			var t_pieces = spieces[1].split(":");
			var sec_frames = t_pieces[2].split(".");
			
			if(offset.substring(0,1) == "+"){
				var mathop = "+";
			}else{
				var mathop = "-";
			}
			
			 utc_time = this.add_times(t_pieces[0],t_pieces[1],sec_frames[0],addH ,addM,addS,mathop);
			 //add 10 hours(sydney time)
			 utc_pieces = utc_time.split(":");
		
			 /*
			 FOR DAYLIGHT SAVING YOU NEED TO CHANGE THE FOLLOWING NUMBER AFTER THE + 
			 IN WINTER PUT IT TO 10
			 IN SUMMER PUT IT TO 11
			 */
			 utc_h = ( (parseInt(utc_pieces[0],10) + 10) %24);
			 if (hour3<=9) { hour3="0"+hour3; }
			 return(utc_h+":"+utc_pieces[1] +":"+utc_pieces[2]);
		} catch (e){}
	},

	add_times: function(h1,m1,s1,h2,m2,s2,math_op){
		if(math_op == "-"){
			hour3 =parseInt(h1,10) - parseInt(h2,10);
		    min3 = parseInt(m1,10) - parseInt(m2,10);
		    sec3 = parseInt(s1,10) - parseInt(s2,10);
			if(sec3 < 0){ sec3 = 60 + sec3; min3 -=1 ; }
			if(min3 < 0){ min3 = 60 + min3; hour3 -=1 ; }
			
			if (sec3>=60) { sec3 = sec3-60; min3 = parseInt(min3,10)+1; }
	  	   if (min3>=60) { min3 = min3 - 60; hour3 = parseInt(hour3,10)+1; }
		   
		}else{
		   hour3 = parseInt(h1,10) + parseInt(h2,10);
		   min3 = parseInt(m1,10) + parseInt(m2,10);
	
		   sec3 = parseInt(s1,10) + parseInt(s2,10);
		   if (sec3>=60) { sec3 = sec3-60; min3 = parseInt(min3,10)+1; }
	  	   if (min3>=60) { min3 = min3 - 60; hour3 = parseInt(hour3,10)+1; }
	
		}
		//get the remainder
	    hour3 = Math.abs(hour3 % 24);
	
	   if (sec3<=9) { sec3="0"+sec3; }
	   if (min3<=9) { min3="0"+min3; }
	   if (hour3<=9) { hour3="0"+hour3; }
	 
	   return(hour3+":"+min3+":"+sec3);	
	}
}
