/**
  *  @projectDescription
  *  <p>A collection of tv-schedule functionality</p>
  *
  *  @author			Rick Farmer
  *  @version			1.0
  *
  */
 

// The Weekly Schedule Object
function oWeeklySchedule(){

	this.seriesUrl = "series.html"; // base url to series.html
	this.seriesPaid = "";  // series url param. ex. 26.5262.116786.28593.8 (networkId.gridTitleId.propertyId.seriesId.episodeNumber)
	this.dailyUrl = "daily.html";  // base url to daily.html
	this.dailyDate = ""; // daily url param. ex. 20080203.034 (yyyyMMdd.DDD)
	this.dayOfWeek = ""; // day of week to display in the day row. ex Mon. (EEE)
	this.monthAndDay = ""; // month and year to display in day row.  ex. Jan 07 (MMM yy)
	this.showTitle = ""; // title of the show
	this.episodeTitle = ""; // title of the episode
	this.tvRating = ""; // tv rating of the episode
	this.slotLength = ""; // length of episode
	this.closedCaptioned = "" // the flag for whether this is closed captioned or not (ex. Y)
	this.reminderAddShow = "" // The reminder add show parameter. ex. 200801120100:200801112000:26:14691:120030:1:1 	
	this.gmtAiringDatetime = "" // The air date and time in gmt
	this.timeAreaDatetime = "" // 
	this.broadcastDate = "" //
	

	this.getCCText  = function(){ // get the additonal CC text
		
		var CCText = "";
		
		if(this.closedCaptioned.toLowerCase()=="y"){
			CCtext = ", CC";
		}
		
		return CCText;
	
	}
	
	this.getSlotLengthCSS = function(){ // slot length css classId to use to control the listing witdh
		
		var classId = "minSixZero";
		
		if(this.slot_length==3000){
			classId = "minThreeZero";
		}else if(this.slot_length==6000){
			classId = "minSixZero";
		}else if(this.slot_length==6000){
			classId = "minNineZero";
		}else if(this.slot_length==12000){
			classId = "minOneTwoZero";
		}
		
		return classId;
		
	
	}

	this.getReminderText = function(){
		
		var ouput = "";
		
		if(this.reminderAddShow.length>0){
		output = new String('<table cellpadding="0" cellspacing="0" border="0"><tr><td height="25" align="left" valign="bottom">\n'
		+'<input type="checkbox" id="addshow" name="addshow" value="' + this.reminderAddShow + '"\/>\n'
		+'<\/td><td align="left" valign="middle" class="remindText">Remind Me<\/td><\/tr><\/table>');	
		}else{
		output = new String('<br style="line-height: 12px;"\/>\n');
		}
	
		return output;
	}
	
	this.getWeekDayCell = function(){

		var output = new String('<div id="dayRows">\n'
		+'<table cellpadding="0" cellspacing="0" border="0"><tr><td id="weekDay" style="border-color: #b7cfe9;" align="center" valign="top"><div class="cellPad">\n'
		+'<a class="black" href="' + this.dailyUrl + '?date=' + this.dailyDate + '">\n'
		+'<strong>' + this.dayOfWeek + '<\/strong>\n'
		+'<br\/>' + this.monthAndDay + '<br\/>\n'
		+'<\/a><br\/><\/div><\/td>\n');
	
		return output;

	}

	this.getEpisodeCell = function(){

		var output = new String('<td id="' + this.getSlotLengthCSS() + '" style="border-color: #b7cfe9;" align="left" valign="top">\n'
		+'<div class="cellPad">\n'
		+'<strong>\n'
		+'<a href="' + this.seriesUrl + '?paid=' + this.seriesPaid + '">' + this.showTitle + '<\/a>\n'
		+'<\/strong>\n'
		+'<br\/>\n'
		+'<em>' + this.episodeTitle + '<\/em>\n'
		+'<br\/>\n'
		+'<span class="creditsText">\n'
		+'<a href="javascript: void(null);" onclick="tvRatingsDiv(\'tvRatings\');">' + this.tvRating + '<\/a>' + this.getCCText() + '\n'
		+'<\/span>\n'
		+'<br\/>\n'
		+this.getReminderText()
		+'<div\/>\n'
		+'<\/td>\n'
		+'<\/tr>\n'
		+'<\/table>\n'
		+'<\/div>');
		
		return output;

	}

}

// Load the JSON content
function loadWeeklyScheduleContent(dailyUrl, seriesUrl){
			
	if(content){
		if(content.ScheduleDetailedOutput.status.toLowerCase()=="success"){
			debug.out("WS status: " + content.ScheduleDetailedOutput.status);
		
			var output = "";
			var schedule = new Array(); 
			
			c = content.ScheduleDetailedOutput.scheduleDetailedArray.ScheduleDetail;
				debug.out("ws status: " + content.ScheduleDetailedOutput.status);
				debug.out("c = content.ScheduleDetailedOutput.scheduleDetailedArray.ScheduleDetail");
				debug.out("c.length: " + c.length);
				
				// Load the schedule array with weeklySchedule objects
				for(var i=0; i<c.length; i++){
				
					root = c[i];				
					schedule.push(new oWeeklySchedule());
					
					
					var seriesPaid = root.netProperty.networkId 
									+ "." + root.netSchedule.gridTitleId 
									+ "." + root.netProperty.propertyId 
									+ "." + root.netProperty.seriesId
									+ "." + root.netPropEpisode.episodeNumber;   
					
					//200801130000:200801121900:26:14019:110722:2:2
					var reminderAddShow = root.netSchedule.gmtAiringDatetime; // todo: need the correct data or no data if time is too close to now
					
					schedule[i].slotLength = root.netSchedule.slotLength;
					schedule[i].dailyDate = "20080107.007";
					schedule[i].dayOfWeek = "Mon.";
					schedule[i].monthAndDay = "Jan 07";
					schedule[i].dailyUrl = dailyUrl;
					schedule[i].seriesUrl = seriesUrl;
					schedule[i].seriesPaid = seriesPaid;
					schedule[i].showTitle = root.netProperty.title;
					schedule[i].episodeTitle = root.netPropEpisode.title;
					schedule[i].tvRating = root.versionAndCreditArray.netPropEpVersion.parentalRatingCode;
					schedule[i].closedCaptioned = root.versionAndCreditArray.netPropEpVersion.closedCaptionedFlag;
					schedule[i].reminderAddShow = reminderAddShow;
					schedule[i].gmtAiringDatetime = root.netSchedule.gmtAiringDatetime;
					schedule[i].broadcastDate = root.netSchedule.broadcastDate;
					schedule[i].timeAreaDatetime = root.netSchedule.timeAreaDatetime;
					
					debug.out(i + ": " + schedule[i].showTitle);
					debug.out(i + ": " + schedule[i].episodeTitle);
					debug.out(i + ": " + schedule[i].timeAreaDatetime);
					debug.out(i + ": " + schedule[i].slotLength);
				}
				
				
				output += schedule[0].getWeekDayCell();
				output += schedule[0].getEpisodeCell();
				
				return output;

			}else{
				debug.out("The data is currently unavailable");
				// There was a problem getting data from the web service
				// TODO: handle no data from the web service
		}
	} 		
		
}
