var SatCalendar = function (sat, obj) {
	this.sat = sat;
	this.schedules = new Array();
	this._schedules = obj.schedules;
	SatCalendar.clearTable();
	var table = $('scheduleTable');
	if (this._schedules) {
		for (var i=0, l=this._schedules.length; i<l; i++)
			this.schedules.push(new SatCalendarSchedule(this._schedules[i].schedule,i,this));
		for (var i=0, l=this._schedules.length; i<l; i++)
			$('scheduleTable').appendChild(this.schedules[i].row);
	}
}
SatCalendar.clearTable = function () {
	var table = $('scheduleTable');
	var rows = table.getElementsByTagName('TR');
	var rmList = new Array();
	for (var i=0, l=rows.length; i<l; i++) {
		if (rows[i].className!='header')
			rmList.push(rows[i]);
	}
	for (var i=0, l=rmList.length; i<l; i++)
		rmList[i].parentNode.removeChild(rmList[i]);
};
SatCalendar.showErrorNotAvailable = function () {
	this.clearTable();
}
SatCalendar.prototype.checkSelectedSchedule = function (comparison) {
	for (var i=0, l=this.schedules.length; i<l; i++) {
		if (this.schedules[i].equals(comparison)) this.schedules[i].setSelectedStyle();
		else this.schedules[i].refreshStyle();
	}
	
};
SatCalendar.prototype.refreshStyles = function () {
		for (var i=0, l=this.schedules.length; i<l; i++)
			this.schedules[i].refreshStyle();
}

var SatCalendarSchedule = function (obj, index, calendar) {
	this.calendar = calendar;
	this.index = index;
	this.nakedEyeOk = ("OK"==obj.nakedEye);
	var icon = document.createElement('IMG');
	icon.src = 'images/fam/' + ((this.nakedEyeOk)?'star.png':'star_gray.png');
	
	var visibilityCell = document.createElement('TD');
	visibilityCell.appendChild(icon);

	this.row = document.createElement('TR');
	this.row.appendChild(visibilityCell);
	
	this.time = Util.parseDateAsGmt(obj.since.sinceTime);
	
	var dateString = Util.formatDate(Util.parseDateAsGmt(obj.peek.peekTime),"MM/DD");
	var sinceString = Util.formatDate(Util.parseDateAsGmt(obj.since.sinceTime),"HH:mm:ss");
	var peekString = Util.formatDate(Util.parseDateAsGmt(obj.peek.peekTime),"HH:mm:ss");
	var untilString = Util.formatDate(Util.parseDateAsGmt(obj.until.untilTime),"HH:mm:ss");
	
	
	Event.observe(this.row,'click', this.getSetDateClickAction(obj));
	
	this.row.appendChild(SatCalendarSchedule._getCell(dateString));
		
	this.row.appendChild(SatCalendarSchedule._getCell(sinceString));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.since.sinceAzimuth + "("+Util.getAzimuthString(obj.since.sinceAzimuthStr)+")"));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.since.sinceElevation));
	
	this.row.appendChild(SatCalendarSchedule._getCell(peekString));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.peek.peekAzimuth + "("+Util.getAzimuthString(obj.peek.peekAzimuthStr)+")"));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.peek.peekElevation));
	
	this.row.appendChild(SatCalendarSchedule._getCell(untilString));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.until.untilAzimuth + "("+Util.getAzimuthString(obj.until.untilAzimuthStr)+")"));
	this.row.appendChild(SatCalendarSchedule._getCell(obj.until.untilElevation));
	this.refreshStyle();
}
SatCalendarSchedule.prototype.equals = function(comparison){
	return Util.isTimeEqual(this.time, comparison.time);
};
SatCalendarSchedule.prototype.refreshStyle = function () {
	this.row.className = (this.index%2==0)?'odd':'even';
};
SatCalendarSchedule.prototype.setSelectedStyle = function () {
	this.row.className = (this.index%2==0)?'odd-focus':'even-focus';
};
SatCalendarSchedule.prototype.getSetDateClickAction = function (obj) {
	var self = this;
	return function () {
		self.calendar.refreshStyles();
	  	urlInfo.targetDate = Util.parseDateAsGmt(obj.since.sinceTime);
  		urlInfo.refresh(false);
	}
}
SatCalendarSchedule._getCell = function (message) {
	var cell = document.createElement('TD');
	cell.innerHTML = message;
	return cell;	
}
