if(IMG_BUSTER == undefined){
	IMG_BUSTER = '';
}
var Calendar = {
	
	date: null,
	currentlySelectedDay:1,
	currentlySelectedMonth:0,
	months: ['January','February','March','April','May','June','July','August','September','October','November','December'],
	daysInMonth: [31,28,31,30,31,30,31,31,30,31,30,31],
	currentTimezone:Utils.TIMEZONE_ET,
	clickListeners:[],
	timezoneListener:null,
	
	init: function() {
		this.date = new Date();
		this.currentlySelectedDay = this.date.getDate();
		this.currentlySelectedMonth = this.date.getMonth();
		this.currentlySelectedYear = this.date.getFullYear();
		
		this.fixLeapYear();
		this.currentTimezone = Utils.getTimezone();
	},
	
	fixLeapYear: function() {
		//fix leap year:
		var year = this.date.getFullYear();
		this.daysInMonth[1] = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)? 29 : 28;
	},
	
	setMonthAndYear: function(month, year) {
		$('monthTitle').innerHTML = this.months[month]+' '+year;
	},
	
	forward: function() {
		var d = this.date;
		d.setMonth(d.getMonth(), 1);
		this.date.setMonth(d.getMonth() + 1);
		var calContent = this.getCalContent();
		var cal = $('scheduleCalendar');
		var complete = function() {
			cal.setStyle({left:'200px'});
			cal.innerHTML = calContent;
			new Effect.Move(cal, {mode:'relative', x:-200, duration:0.2});
		}
		new Effect.Move(cal, {mode:'relative', x: -200, duration:0.2, afterFinish:complete});
		//$('monthTitle').innerHTML = this.months[this.date.getMonth()]+' '+this.date.getFullYear();
		this.setMonthAndYear(this.date.getMonth(), this.date.getFullYear());
		this.fixLeapYear();
	},
	
	back: function() {
		var d = this.date;
		d.setMonth(d.getMonth(), 1);
		this.date.setMonth(d.getMonth() - 1);
		var calContent = this.getCalContent();
		var cal = $('scheduleCalendar');
		var complete = function() {
			cal.setStyle({left:'-200px'});
			cal.innerHTML = calContent;
			new Effect.Move(cal, {mode:'relative', x:200, duration:0.2});
		}
		new Effect.Move(cal, {mode:'relative', x: 200, duration:0.2, afterFinish:complete});
		//$('monthTitle').innerHTML = this.months[this.date.getMonth()]+' '+this.date.getFullYear();
		this.setMonthAndYear(this.date.getMonth(), this.date.getFullYear());
		this.fixLeapYear();
	},
	
	show: function() {
		$('scheduleCalendar').innerHTML = this.getCalContent();
		this.selectDay(this.date.getFullYear(), this.date.getMonth(), this.date.getDate());
		this.setMonthAndYear(this.date.getMonth(), this.date.getFullYear());
	},	
	
	getCalContent: function() {
		var d = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0, 0, 0, 0);
		var firstDay = d.getDay();
		
		var content = '<table cellpadding="0" cellspacing="1" border="0">'
				+ '<tr class="topRow">'
				+ '<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>'
				+ '</tr>';
		
		curDay = 1;
		monthLength = this.daysInMonth[this.date.getMonth()];
		for(var i = 0; i < 6; i++) {
			
			content += '<tr class="dayRow">';
			
			for(var j = 0; j <= 6; j++) {
				
				if(i == 0 && j < firstDay) {
					content += '<td class="empty">';
				} else if(this.currentlySelectedDay == curDay 
					&& this.currentlySelectedMonth == this.date.getMonth()
					&& this.currentlySelectedYear == this.date.getFullYear()) {
					content += '<td class="selected" id="calendarBox_'+curDay+'"><a href="javascript:Calendar.selectDay('
							+ this.date.getFullYear() +', '
							+ this.date.getMonth() +', '
							+ curDay+')">' + curDay + '</a>';
					curDay++;
				} else if(curDay <= monthLength) {
					content += '<td class="filled" id="calendarBox_'+curDay+'"><a href="javascript:Calendar.selectDay('
							+ this.date.getFullYear() +', '
							+ this.date.getMonth() +', '
							+ curDay+')">' + curDay + '</a>';
					curDay++;
				} else {
					content += '<td class="empty"><a href="">';
				}
				content += '</td>';
			}
			
			content += '</tr>';	
			
			if(curDay > monthLength) break;
		}
		
		return content;
		
	},
	
	selectDay: function(year, month, day) {
		var oldDay = this.currentlySelectedDay;
	
		this.date.setYear(year);
		this.date.setMonth(month);
		this.date.setDate(day);
		
		if($('calendarBox_' + oldDay)) $('calendarBox_' + oldDay).className = 'filled';
		$('calendarBox_' + day).className = 'selected';
		
		this.currentlySelectedDay = day;
		this.currentlySelectedMonth = month;
		this.currentlySelectedYear = year;
		
		var dateString = (this.months[month] + ' ' + day + ', ' + year).toUpperCase();
		var timezone = Utils.getTimezone();
		$('scheduleContent').update('<img src="/images/common/ajax-loader.gif?'+IMG_BUSTER+'">');
		new Ajax.Request('/scripts/loadDailySchedule.php', {
			method:'get',
			parameters: {date: year + '-' + (month + 1) + '-' + day, tz:timezone},
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON(true);

				if(json.message = 'success') {
					var len = json.schedule.length;
					var scheduleContent = $('scheduleContent');
					var content = '';
					if(len > 0) {
						$$('#schedulePopup .popupSchedule .title')[0].update(dateString);
						for(var i = 0; i < len; i++) {
							var thelink = '/details/?id='+json.schedule[i].movie_id;
							if (json.schedule[i].url) var thelink = json.schedule[i].url;

							content += '<table cellpadding="0" cellspacing="0" border="0"><tr>'
									+ '<td class="showTime">'+ json.schedule[i].showtime +'</td>'
								+ '<td class="showTitle"><a href="'+thelink+'">'
								+ json.schedule[i].title +'</a></td>'
								+ '</tr></table>';
						}
					} else {
						content = '<div class="error">No schedule information for this time.</div>';		
						//content = '<div class="error">HBO Canada Launches October 30.</div>';						
					}
					
					scheduleContent.innerHTML = content;
				} else {
					alert('No schedule information for this time.');
				}
			},
			onFailure: function(){ 
				alert('No schedule information for this time.');
			}
		});
	},
	
	refresh: function() {
		this.selectDay(this.date.getFullYear(), this.date.getMonth(), this.date.getDate());
	},
	
	isOpen: function() {
		return ($('schedulePopup').getStyle('display') == 'block');
	},
	
	open: function() {
		closeSearch();
		this.init();
		this.highlightTimezone();
		this.updateMonthlyScheduleButton();
		this.startClickListeners();
		this.setTimezoneChangeListener();
		var popup = $$('.schedulePopup')[0];
		$('popupContent').setStyle({opacity:0.0});
		var complete = function() {
			$('popupBottom').setStyle({display:'block'});
			$('popupTop').setStyle({display:'block'});
			$('popupContent').setStyle({display:'block'});
			new Effect.Opacity('popupContent', {from: 0.0, to: 1.0, duration: 0.2, afterFinish:function(){Calendar.show()}});
		};		
		popup.setStyle({display:'block'});
		new Effect.Morph(popup, {style:{width: '652px'}, duration:0.5, afterFinish:complete});
		//track that the schedule was opened:
		Utils.trackSchedule();
		//Utils.trackLink('Schedule Opened');	
	},
	
	close: function() {
		this.stopClickListeners();
		this.clearTimezoneChangeListener();
		var complete = function() {
			$('popupBottom').setStyle({display:'none'});
			$('popupTop').setStyle({display:'none'});
			$('popupContent').setStyle({display:'none'});
			var popup = $$('.schedulePopup')[0];
			//popup.morph('width:0px;');
			new Effect.Morph(popup, {style:{width: '0px'}, duration:0.5, afterFinish:function(){popup.setStyle({display:'none'})}});
			$('scheduleCalendar').innerHTML = '';
			$('scheduleContent').innerHTML = '';
		};
		new Effect.Opacity('popupContent', {from: 1.0, to: 0.0, duration: 0.2, afterFinish:complete});
	},
	
	/*************************************************
		Functions for the Timezone select:
	**************************************************/
	
	highlightTimezone: function() {
		var button1 = $('scheduleTimezoneETButton');
		var button2 = $('scheduleTimezoneMTButton');
		
		if(this.currentTimezone == Utils.TIMEZONE_ET) {
			button1.addClassName('active');
			button2.removeClassName('active');
		} else {
			button2.addClassName('active');
			button1.removeClassName('active');
		}
	},
	
	selectTimezone: function(timezone) {
		this.currentTimezone = Utils.getTimezone();
		this.highlightTimezone();
		this.refresh();
	},
	
	setTimezoneChangeListener: function() {
		this.timezoneListener = function() {
			if(this.isOpen()) {
				var tz = Utils.getTimezone();
				this.selectTimezone(tz);
				this.updateMonthlyScheduleButton();
			}
		}.bind(this);
		Event.observe(document.body, 'timezone:change', this.timezoneListener);
	},
	
	clearTimezoneChangeListener: function() {
		Event.stopObserving(document.body, 'timezone:change', this.timezoneListener);
	},
	
	startClickListeners: function() {
		this.clickListeners[0] = function() {
			Utils.setTimezone(Utils.TIMEZONE_ET);
		};
		this.clickListeners[1] = function() {
			Utils.setTimezone(Utils.TIMEZONE_MT);
		};
		
		$('scheduleTimezoneETButton').observe('click', this.clickListeners[0]);
		$('scheduleTimezoneMTButton').observe('click', this.clickListeners[1]);	
	},
	
	stopClickListeners: function() {
		$('scheduleTimezoneETButton').stopObserving('click', this.clickListeners[0]);
		$('scheduleTimezoneMTButton').stopObserving('click', this.clickListeners[1]);
	},
	
	updateMonthlyScheduleButton: function() {
		$('scheduleMonthlyButton').href = '/schedule/?tz=' + Utils.getTimezone();	
	}
};


var TonightsSchedule = {
	currentTimezone:Utils.TIMEZONE_ET,
	timezoneListener:null,
	
	init: function() {
		Event.observe(window, 'load', this.setListeners.bind(this));
		this.currentTimezone = Utils.getTimezone();
		this.highlightTimezone();
	},
	
	highlightTimezone: function() {
		var button1 = $('tonightScheduleTimezoneETButton');
		var button2 = $('tonightScheduleTimezoneMTButton');
		
		if(this.currentTimezone == Utils.TIMEZONE_ET) {
			button1.addClassName('active');
			button2.removeClassName('active');
		} else {
			button2.addClassName('active');
			button1.removeClassName('active');
		}
	},
	
	setListeners: function() {
		this.timezoneListener = function() {
			var tz = Utils.getTimezone();
			this.selectTimezone(tz);
		}.bind(this);
		Event.observe(document.body, 'timezone:change', this.timezoneListener);
		
		$('tonightScheduleTimezoneETButton').observe('click', function(){Utils.setTimezone(Utils.TIMEZONE_ET);});
		$('tonightScheduleTimezoneMTButton').observe('click', function(){Utils.setTimezone(Utils.TIMEZONE_MT);});
	},	
	
	selectTimezone: function(timezone) {
		this.highlightTimezone();
		var scheduleToHide, schedulToShow;
		if(timezone == Utils.TIMEZONE_ET && this.currentTimezone != Utils.TIMEZONE_ET) {
			scheduleToHide = $('MTScheduleInfo');
			schedulToShow = $('ETScheduleInfo');
		} else if(timezone == Utils.TIMEZONE_MT && this.currentTimezone != Utils.TIMEZONE_MT) {
			scheduleToHide = $('ETScheduleInfo');
			schedulToShow = $('MTScheduleInfo');
		} else {
			return;	
		}
		this.currentTimezone = Utils.getTimezone();
		var complete = function() {
			Effect.Appear(schedulToShow, {duration:0.3});
			this.highlightTimezone();
		}.bind(this);
		Effect.Fade(scheduleToHide, {afterFinish:complete, duration:0.3});
	}
};

Calendar.init();

