
var Schedule = {
	
	InitMainSchedule : function() {
		/*
		Schedule.BuildMainScheduleTabs(document.getElementById("menu"));
		Schedule.LoadMainSchedule(document.getElementById("menu").firstChild,document.getElementById('scheduleBody'));
	*/
	 },
	
	InitMainScheduleTab : function() {
		Schedule.BuildMainScheduleTabs(document.getElementById("menu"));
	},
	
	BuildMainScheduleTabs : function(menu, offset){
		/*
		if (typeof(offset) == "undefined"){
			offset = 0;
		}
		menu.innerHTML = "";
		for (var i=0; i < 7; i++)
		{
			var menuItem = new ScheduleTab();
			menuItem.date.setDate(new Date().getDate() + i + offset);
			
			// Pull main schedule html
			menu.innerHTML += menuItem.getHtml();
		}
		*/
	},
	
	
	BuildMiniScheduleTabs : function(menu)
	{
		for (var i=0; i < 7; i++)
		{
			var menuItem = new ScheduleTab();
			menuItem.date.setDate(new Date().getDate() + i);
			
			// Pull mini schedule html
			menu.innerHTML += menuItem.getHtml();
		}
	},
	
	BuildSelectedTab : function(tab) {
		// Find the last selected element, and unselect it
		var lastSelectedTab = document.getElementById("selected");
		if (lastSelectedTab != null)
		{
			lastSelectedTab.removeAttribute("id");
			lastSelectedTab.className = "scheduleTab";
		}
		
		// Set the current selected element 
		tab.setAttribute("id", "selected");
		tab.className = "scheduleTabSelected";
		
		Schedule.currentSelection = tab;
	},
	
	LoadMainSchedule : function(tab, schedule)
	{
		// Find the last selected element, and unselect it
		var lastSelectedTab = document.getElementById("selected");
		var tabCount = tab.parentNode.childNodes.length;
		
		for (var i=0; i < 7; i++) {
			
			if (document.getElementById("scheduleDate").getAttribute("numericaldate") == tab.parentNode.childNodes[i].getAttribute("numericdate")) {
				tab.parentNode.childNodes[i].setAttribute("id", "selected");
				tab.parentNode.childNodes[i].className = "scheduleTabSelected";
				 
			} 
			
		}
		
		if (lastSelectedTab != null)
		{
			lastSelectedTab.removeAttribute("id");
			lastSelectedTab.className = "scheduleTab";
		}
		
		// Set the current selected element 
		//alert("loadschedule " + tab.getAttribute("numericdate"));
		/*
		 * dbs
		tab.setAttribute("id", "selected");
		tab.className = "scheduleTabSelected";
		
		 */ 
		 /// Schedule Source URL 
		//var sourceUrl = "http://teg8fref1:9220/exclude/schedulefeed/";
		//var sourceUrl = "http://teg8fref1:9220/exclude/schedulefeed/0,,"+tab.numericdate+"||"+(new Date().getTimezoneOffset()/60-4)+",00.html";
		//this is the real url
		//var sourceUrl = "http://"+ window.location.host + "/exclude/schedulefeed/0,," + tab.getAttribute("numericdate") + "||"+(new Date().getTimezoneOffset()/60-4)+",00.html";
		var sourceUrl = "http://teg8fref1.turner.com:9220/exclude/schedulefeedNew.jsp?selectedDate=11/1/2007&timeZone=E";
		
		//alert("sourceUrl:" + sourceUrl);
		//alert("sourceUrl:" + sourceUrl + ", tab.numericdate:" + tab.numericdate + ", tab.date:" + tab.date);
		Schedule.currentSelection = tab;
		// Load the main schedule
		//x3 for tbs redesign dont load main schedule - DBS 10/26/2007
		 
		 
		/*
		advAJAX.get({
			url: sourceUrl,
			onSuccess : function(obj) {schedule.innerHTML = obj.responseText;Behaviour.apply()},
			onError : function(obj) { alert("Error: " + obj.status); }
		});
		 */
		
	},
	
	 
	
	SelectRowDetails : function(scheduleRow) {
		// Un-select the other line items
		oldItem = document.getElementById("scheduleRowSelected");
		if (oldItem != null)
			oldItem.removeAttribute("id");
		scheduleRow.setAttribute("id", "scheduleRowSelected");
		//var uniquevar= new animatedcollapse();
		//totalDescrDivs = 25;
		//var url = window.location.host;
		//var testInfo = "http://" + window.location.host + "/exclude/scheduleAdditionalInfo/0,,"+franchiseairingid+"|"+franchiseid+"|"+titleid+",00.html";
		//url = url + "/exclude/scheduleAdditionalInfoNew.jsp?titleId=" + titleId;
		//alert("ExpandScheduleDetails");
		// Load the main schedule
		//additionalInfo.innerHTML = "Derrick";
		
		//Expand the Details
		
		/*
		advAJAX.get({
			url: url,
			onSuccess : function(obj) {additionalInfo.innerHTML = obj.responseText; },
			onError : function(obj) { alert("Error: " + obj.status); }
		
		});
		*/
		
	},
	
	LoadAdditionalInfo : function(scheduleRow, franchiseairingid, franchiseid, titleid)
	{
		// Un-select the other line items
		oldItem = document.getElementById("scheduleRowSelected");
		if (oldItem != null)
			oldItem.removeAttribute("id");
		scheduleRow.setAttribute("id", "scheduleRowSelected");
		
		var url = window.location.host;
		var testInfo = "http://" + window.location.host + "/exclude/scheduleAdditionalInfo/0,,"+franchiseairingid+"|"+franchiseid+"|"+titleid+",00.html";
			
		// Load the main schedule
		advAJAX.get({
			url: "http://teg8fref1:9220/exclude/scheduleAdditionalInfo/0,,"+franchiseairingid+"|"+franchiseid+"|"+titleid+",00.html",
			onSuccess : function(obj) {document.getElementById('additionalInfo').innerHTML = obj.responseText; },
			onError : function(obj) { alert("Error: " + obj.status); }
		});
	},
	
	animateDetailItems : function(titleid)
	{
		// Load the main schedule
		/*
		advAJAX.get({
			url: "http://" + window.location.host + "/exclude/scheduleRelatedItems/0,,||"+titleid+",00.html",
			onSuccess : function(obj) {document.getElementById('relatedItems').innerHTML = obj.responseText; },
			onError : function(obj) { alert("Error: " + obj.status); }
		});
		*/
	},
	
	LoadRelatedItems : function(titleid)
	{
		// Load the main schedule
		advAJAX.get({
			url: "http://" + window.location.host + "/exclude/scheduleRelatedItems/0,,||"+titleid+",00.html",
			onSuccess : function(obj) {document.getElementById('relatedItems').innerHTML = obj.responseText; },
			onError : function(obj) { alert("Error: " + obj.status); }
		});
	},
	
	LoadMiniSchedule : function(tab, schedule)
	{
		// Find the last selected element, and unselect it
		var lastSelectedTab = document.getElementById("selected");
		if (lastSelectedTab != null)
		{
			lastSelectedTab.removeAttribute("id");
			lastSelectedTab.className = "scheduleTab";
		}
		
		// Set the current selected element 
		tab.setAttribute("id", "selected");
		tab.className = "scheduleTabSelected";
		
		// Schedule Source URL 
		var sourceUrl = "http://" + window.location.host + "/exclude/schedulefeed/";
		
		// Load the main schedule
		advAJAX.get({
			url: sourceUrl,
			onSuccess : function(obj) { schedule.innerHTML = obj.responseText; },
			onError : function(obj) { alert("Error: " + obj.status); }
		});
	},
	
	BuildMainScheduleTabsPreviousWeek : function(menu, schedule){
		if (typeof(Schedule.offset) == "undefined"){
			Schedule.offset = 0;
		}
		
		Schedule.offset-=7;
		var childCount = 0;
		var currentChild = Schedule.currentSelection;
		if (isValidDate(currentChild.getAttribute("numericdate"))) {
			while ((currentChild.previousSibling)!=null){
				childCount++;
				currentChild = currentChild.previousSibling;
			}
			
			Schedule.BuildMainScheduleTabs(menu, Schedule.offset);
			
			Schedule.LoadMainSchedule(menu.childNodes[childCount], schedule);
		}
	},
	BuildMainScheduleTabsNextWeek : function(menu, schedule){
		if (typeof(Schedule.offset) == "undefined"){
			Schedule.offset = 0;
		}

		Schedule.offset+=7;
		var childCount = 0;
		var currentChild = Schedule.currentSelection;
		if (isValidDate(currentChild.getAttribute("numericdate"))) {
			while ((currentChild.previousSibling)!=null){
				childCount++;
				currentChild = currentChild.previousSibling;
			}
			Schedule.BuildMainScheduleTabs(menu, Schedule.offset);
			Schedule.LoadMainSchedule(menu.childNodes[childCount], schedule);	
		}
	},
	
	cleanWhitespace : function(node) {
    	var notWhitespace = /\S/;
    	for (var i=0; i < node.childNodes.length; i++) {
			var childNode = node.childNodes[i];
    		if ((childNode.nodeType == 3)&&(!notWhitespace.test(childNode.nodeValue))) {
       			// that is, if it's a whitespace text node
       			node.removeChild(node.childNodes[i]);
       			i--;
			}
    		if (childNode.nodeType == 1) {
       			// elements can have text child nodes of their own
       			this.cleanWhitespace(childNode);
    		}
    	}
	},
	
	LoadScheduleFromCalendar : function(offset)
	{
		Schedule.BuildMainScheduleTabs(document.getElementById("menu"), offset);
		Schedule.LoadMainSchedule(document.getElementById("menu").firstChild,document.getElementById('scheduleBody'));
	}
}

