var SatSchedule = function (obj, sat) {
	this.jsonObj = obj;
	this.positions = obj.schedule.plots;
	this.sat = sat;
	this.nakedEyeOk = ('OK'==obj.schedule.nakedEye); 
	this.tab = this.getTab();
	$('scheduleTabContainer').appendChild(this.tab);
}
SatSchedule.prototype.getTab = function () {
	var obj = this.jsonObj;
	var timeTab = document.createElement('LI');
	this.tabDiv = document.createElement('DIV');
	this.tabLink = document.createElement('A');
	
	var peekTime = Util.parseDateAsGmt(obj.schedule.since.sinceTime);
	
	this.tabLink.appendChild(SatSchedule.getEyeIcon(this.nakedEyeOk));
	this.tabLink.appendChild(document.createTextNode(Util.formatDate(peekTime, "MM/DD HH:mm")));
	
	this.tabLink.appendChild(this.getTooltip());
	timeTab.className = (this.nakedEyeOk)?'tag':'tag';
	
	this.tabLink.href = 'javascript:void(0)';
	Event.observe(this.tabLink,'click', this.getSelectedAction());
	
	this.tabDiv.appendChild(this.tabLink);
	timeTab.appendChild(this.tabDiv);
	return timeTab;
}
SatSchedule.getEyeIcon = function (nakedEyeOk) {
	var icon = document.createElement('IMG');
	icon.src = './images/fam/' + ((nakedEyeOk)?'star.png':'star_gray.png');
	return icon;
};
SatSchedule._getTooltipTitle = function (message) {
	var div = document.createElement('DIV');
	div.innerHTML = message;
	div.className = 'title';
	return div;
};
SatSchedule._getTooltipContent = function (message) {
	var div = document.createElement('DIV');
	div.innerHTML = message;
	div.className = 'content';
	return div;
};
SatSchedule.prototype.getTooltip = function () {
	var schedule = this.jsonObj.schedule;
	var tooltip = document.createElement('DIV');
	tooltip.appendChild(SatSchedule._getTooltipTitle(Dict.NAKED_EYE));
	var msgNakedEye = ('OK'==schedule.nakedEye)? Dict.NAKED_EYE_OK:Dict.NAKED_EYE_NG;
	tooltip.appendChild(SatSchedule._getTooltipContent(msgNakedEye));	
	
	this.time = Util.parseDateAsGmt(schedule.since.sinceTime);
	
	var msgPeek = Util.formatDate(Util.parseDateAsGmt(schedule.peek.peekTime),'MM/DD HH:mm:ss ') + Util.getAzimuthString(schedule.peek.peekAzimuthStr) +
		"<br/>" +Dict.ELEVATION + " " + schedule.peek.peekElevation;
	var msgSince = Util.formatDate(Util.parseDateAsGmt(schedule.since.sinceTime),'MM/DD HH:mm:ss ') + Util.getAzimuthString(schedule.since.sinceAzimuthStr)+
		"<br/>" +Dict.ELEVATION + " " + schedule.since.sinceElevation;
	var msgUntil = Util.formatDate(Util.parseDateAsGmt(schedule.until.untilTime),'MM/DD HH:mm:ss ') + Util.getAzimuthString(schedule.until.untilAzimuthStr)+
		"<br/>" +Dict.ELEVATION + " " + schedule.until.untilElevation;
	tooltip.appendChild(SatSchedule._getTooltipTitle(Dict.PEAK));
	tooltip.appendChild(SatSchedule._getTooltipContent(msgPeek));
	tooltip.appendChild(SatSchedule._getTooltipTitle(Dict.SINCE));
	tooltip.appendChild(SatSchedule._getTooltipContent(msgSince));
	tooltip.appendChild(SatSchedule._getTooltipTitle(Dict.UNTIL));
	tooltip.appendChild(SatSchedule._getTooltipContent(msgUntil));
	tooltip.className = 'scheduleTip';
	return tooltip;
};
SatSchedule.prototype.showMessage = function () {
	var schedule = this.jsonObj.schedule;
	var msgNakedEye = ('OK'==schedule.nakedEye)? Dict.NAKED_EYE_OK:Dict.NAKED_EYE_NG;
	Message.showMessage(Message.SAT_SCHEDULE_NEXT, [Util.formatDate(Util.parseDateAsGmt(schedule.peek.peekTime),'MM/DD HH:mm:ss '), msgNakedEye]);
};
SatSchedule.prototype.getSelectedAction = function () {
	var self = this;
	return function(){self.select()};
};
SatSchedule.COLOR_X = 'black';
SatSchedule.COLOR_S = 'yellow';
SatSchedule.SIZE_X = 1;
SatSchedule.SIZE_S = 2;

SatSchedule.prototype.select = function () {
	Player.reset();
	Player.show();
	for (var i=0, l=this.sat.schedules.length; i<l; i++) {
		this.sat.schedules[i].unselect();
	}
	this.tabDiv.className = 'selected';
	this.sat.selectedSchedule = this;
	try {
		this.sat.calendar.checkSelectedSchedule(this);
	} catch (e){}
	this.draw();
};
SatSchedule.prototype.draw = function () {
	if (SatSchedule.polylines) {
		for (var i= 0; i<SatSchedule.polylines.length; i++)
			satMap.removeOverlay(SatSchedule.polylines[i].polyline);
	}
    var positions = this.positions;
    var latLngs = null;
	SatSchedule.polylines = new Array();
	var prevVisibility = null;
    for (var i = 0, l = positions.length; i < l; i++) {
		var requireDraw = false;
        var position = positions[i];
		if (positions[i].visibility!=prevVisibility) {
			var prevLatLngs = latLngs;
			if (prevLatLngs) SatSchedule.polylines.push({vis: prevVisibility,pos: prevLatLngs});
			latLngs = new Array();
			if (prevLatLngs) latLngs.push(prevLatLngs[prevLatLngs.length-1]);
		}
		prevVisibility = position.visibility;
        var longitude = parseFloat(position.longitude);
        var latitude = parseFloat(position.latitude);
        latLngs.push(new GLatLng(latitude, longitude));
    }
	SatSchedule.polylines.push({vis: prevVisibility,pos: latLngs});
	for (var i = 0; i < SatSchedule.polylines.length; i++) {
		var line = SatSchedule.polylines[i];
		var size = ("S"==line.vis)?SatSchedule.SIZE_S:SatSchedule.SIZE_X;
		var color = ("S"==line.vis)?SatSchedule.COLOR_S:SatSchedule.COLOR_X;
		line.polyline = new GPolyline(line.pos, color,size);
		satMap.addOverlay(line.polyline);
	}
	satMap.setCenter(urlInfo.currentPosition);
}
SatSchedule.prototype.unselect = function () {
	this.tabDiv.className = '';
}
