var BY_HATZOT = 0;
var BY_TIME = 1;

function MedTermometr (term_date, term_month, term_year, term_hour, term_minute, cdate, cmonth, cyear, chour, cminute, city, env, calendar_url, change_time, change_type) {
	this.change_time = change_time;
	this.change_type = change_type; 
	
	/* Time */
	var termDayDate = new Date (term_year, term_month - 1, term_date, 0, 0, 0);
	var termDayStamp = termDayDate.getTime();
		
	this.cdate = cdate;
	this.cmonth = cmonth;
	this.cyear = cyear;
	this.chour = chour;
	this.cminute = cminute;
	this.city = city;
	var cDayDate =  new Date (cyear, cmonth - 1, cdate, chour, cminute, 0);
	var cStamp = cDayDate.getTime();
	zmanim.latitude = city.latitude;
	zmanim.longitude = city.longitude;
	zmanim.timezone = city.gmt + city.swtime;
	zmanim.set_knissat(city.shabatAdj);
	var doys = getDoys (term_date, term_month, term_year);
	zmanim.day_of_year = doys[0];
	zmanim.day_of_year_prev = doys[1];
	zmanim.day_of_year_next = doys[2];
	zmanim.set_zmanim();
		
	// Change by hatzot
	if (this.change_type == BY_HATZOT) {
		var hatzot2 = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot2")];
		var hatzot1 = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot1")];
			
		if (hatzot2.time < 24) {
			if (cStamp > termDayStamp + Math.ceil(hatzot2.time*3600000)) {
					termDayStamp += 24*60*60*1000;
					var newTermDate = new Date (termDayStamp);
					term_date = newTermDate.getDate();
					term_month = newTermDate.getMonth() + 1;
					term_year = newTermDate.getFullYear();
					term_hour -= 24; 
					doys = getDoys (term_date, term_month, term_year);
					zmanim.day_of_year = doys[0];
					zmanim.day_of_year_prev = doys[1];
					zmanim.day_of_year_next = doys[2];
					zmanim.set_zmanim();
				}
		} else if (hatzot1.time < 12) {
			if (cStamp < termDayStamp + Math.ceil(hatzot1.time*3600000)) {
					termDayStamp -= 24*60*60*1000;
					var newTermDate = new Date (termDayStamp);
					term_date = newTermDate.getDate();
					term_month = newTermDate.getMonth() + 1;
					term_year = newTermDate.getFullYear();
					term_hour += 24; 
					doys = getDoys (term_date, term_month, term_year);
					zmanim.day_of_year = doys[0];
					zmanim.day_of_year_prev = doys[1];
					zmanim.day_of_year_next = doys[2];
					zmanim.set_zmanim();
				}
		}
	//CHANGE BY TIME
	} else {
		var changeStampDate = new Date (term_year, term_month - 1, term_date, this.change_time.hour, this.change_time.minute, 0);
		var changeStamp = changeStampDate.getTime();
		if (this.change_time.current_day == true) {
			if (cStamp > changeStamp) {
				termDayStamp += 24*60*60*1000;
				var newTermDate = new Date (termDayStamp);
				term_date = newTermDate.getDate();
				term_month = newTermDate.getMonth() + 1;
				term_year = newTermDate.getFullYear();
				term_hour -= 24; 
				doys = getDoys (term_date, term_month, term_year);
				zmanim.day_of_year = doys[0];
				zmanim.day_of_year_prev = doys[1];
				zmanim.day_of_year_next = doys[2];
				zmanim.set_zmanim();				}
		} else if (this.change_time.current_day == false) {
			if (cStamp < changeStamp) {
				termDayStamp -= 24*60*60*1000;
				var newTermDate = new Date (termDayStamp);
				term_date = newTermDate.getDate();
				term_month = newTermDate.getMonth() + 1;
				term_year = newTermDate.getFullYear();
				term_hour += 24; 
				doys = getDoys (term_date, term_month, term_year);
				zmanim.day_of_year = doys[0];
				zmanim.day_of_year_prev = doys[1];
				zmanim.day_of_year_next = doys[2];
				zmanim.set_zmanim();
			}
		}
	}
	
	this.term_date = term_date;
	this.term_month = term_month;
	this.term_year = term_year;
	this.term_hour = term_hour;
	this.term_minute = term_minute;
	var	hDate = getHebFromCiv(this.term_date, this.term_month, this.term_year);
	this.hdate = hDate.date;
	this.hmonth = hDate.month;
	this.hyear = hDate.year;
	
	var termDate = new Date (term_year, term_month - 1, term_date, 1, 1, 1);
	var termStamp = termDate.getTime();
		
	this.term_dow =  termDate.getDay();
		
	var prevTermDate = new Date(termStamp - 24*60*60*1000);
	this.prev_term_date = prevTermDate.getDate();
	this.prev_term_month = prevTermDate.getMonth() + 1;
	this.prev_term_year = prevTermDate.getFullYear();
	this.prev_term_dow = this.term_dow - 1 < 0 ? 6 : this.term_dow - 1;
	var	prevhDate = getHebFromCiv(this.prev_term_date, this.prev_term_month, this.prev_term_year);
	this.prevhdate = prevhDate.date;
	this.prevhmonth = prevhDate.month;
	this.prevhyear = prevhDate.year;
		
	var nextTermDate = new Date(termStamp + 24*60*60*1000);
	this.next_term_date = nextTermDate.getDate();
	this.next_term_month = nextTermDate.getMonth() + 1;
	this.next_term_year = nextTermDate.getFullYear();
	this.next_term_dow = this.term_dow + 1 > 6 ? 0 : this.term_dow + 1;
	var	nexthDate = getHebFromCiv(this.next_term_date, this.next_term_month, this.next_term_year);
	this.nexthdate = nexthDate.date;
	this.nexthmonth = nexthDate.month;
	this.nexthyear = nexthDate.year;
			
	this.mercury_pos = this.term_hour + this.term_minute/60;
	this.mercury_width = 0;
	this.beforeMercuryZman = null;
	this.afterMercuryZman = null;
		
	/* URLS of links */
	this.calendar_url = calendar_url;
	
	this.yesterday_moadim = new Array();
	this.tomorrow_moadim = new Array();
	this.today_moadim = new Array();
		
	this.today_moadim = moadim.get_current_moadim (this.term_date, this.term_month, this.term_year, city.galut);
	this.tomorrow_moadim = moadim.get_current_moadim (this.next_term_date, this.next_term_month, this.next_term_year, city.galut);
	this.yesterday_moadim = moadim.get_current_moadim (this.prev_term_date, this.prev_term_month, this.prev_term_year, city.galut);
	this.setup_zmanim ();	
}

function MedTermometr_setup_zmanim () {
	today_moadim_is_jom_tov = moadim.moadim_is_jom_tov (this.today_moadim, this.term_date, this.term_month, this.term_year);
	tomorrow_moadim_is_jom_tov = moadim.moadim_is_jom_tov (this.tomorrow_moadim, this.next_term_date, this.next_term_month, this.next_term_year);
	zmanim.zmanim[zmanim.get_zman_key_by_keyword ("knissat")].visible = false;
	zmanim.zmanim[zmanim.get_zman_key_by_keyword ("motzei_tov")].visible = false;
	zmanim.zmanim[zmanim.get_zman_key_by_keyword ("motzei")].visible = false;
	zmanim.zmanim[zmanim.get_zman_key_by_keyword ("tzeit")].visible = true;
	$('#tzeit img').attr('src','/img/ter_line_short.gif');
	$('#tzeit').removeClass('nereg');
	
	if (this.term_dow == 5 || (today_moadim_is_jom_tov  == false && tomorrow_moadim_is_jom_tov  == true)) {
		var knissat = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("knissat")];
		knissat.visible = true;
		knissat.offset = -1;
		var sunset = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("sunset")];
		sunset.offset = 1;
		var tzeit = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("tzeit")];
		tzeit.offset = 0;
	} 
	
	if (this.term_dow == 6) {
		var motzei =  zmanim.zmanim[zmanim.get_zman_key_by_keyword ("motzei")];
		motzei.visible = true;
		motzei.offset = 2;
		var tzeit = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("tzeit")];
		tzeit.visible = false;
		var sunset = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("sunset")];
		sunset.offset = 0;			
	} else if (today_moadim_is_jom_tov == true && tomorrow_moadim_is_jom_tov == false && this.term_dow != 5) {
		var motzei =  zmanim.zmanim[zmanim.get_zman_key_by_keyword ("motzei_tov")];
		motzei.visible = true;
		motzei.offset = 2;
		var tzeit = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("tzeit")];
		tzeit.visible = false;
		var sunset = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("sunset")];
		sunset.offset = 0;			
	} else if (today_moadim_is_jom_tov == true && tomorrow_moadim_is_jom_tov == true && this.term_dow != 5) {
		var tzeit = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("tzeit")];
		tzeit.nereg = true;
		$('#tzeit img').attr('src','/img/ter_line_short_nereg.gif');
		$('#tzeit').addClass('nereg');
	}
}
MedTermometr.prototype.setup_zmanim = MedTermometr_setup_zmanim;

function MedTermometr_setup_zmanim_offsets () {
	var hatzot1 = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot1")];
	var timeoffset = -hatzot1.time;
	var zpos = 0;
	var prevzpos = 0;
	var zwidth = 0;
	var prevzwidth = 0;
	/* Zmanim will be moved forward for not intercept each other, then we should move them all back to first hatzot */
	//var zlastoffset = 0;
	var zoffset = 0;
	
	var firstZmanWidth = 0;
	var lastZmanWidth = 0;
	for (index in zmanim.zmanim) {
		if (zmanim.zmanim[index].visible == true && zmanim.zmanim[index].keyword != 'hatzot1' && zmanim.zmanim[index].keyword != 'hatzot2') {
			if (firstZmanWidth == 0)
				firstZmanWidth = $('#'+zmanim.zmanim[index].keyword).outerWidth();
			lastZmanWidth = $('#'+zmanim.zmanim[index].keyword).outerWidth();
		}
	}
	for (index in zmanim.zmanim) {
		
		zoffset = (zmanim.zmanim[index].offset/100)*MTscaleWidth;
		if (zmanim.zmanim[index].keyword == 'hatzot1') {
			zwidth = $('#hatzot1').outerWidth();
			zpos = -zwidth/2;
		} else if (zmanim.zmanim[index].keyword == 'hatzot2') {
			zpos = MTscaleWidth - $('#hatzot2').outerWidth()/2;
			zwidth = $('#hatzot2').outerWidth();
		} else if (zmanim.zmanim[index].visible == true) {
			zwidth = $('#'+zmanim.zmanim[index].keyword).outerWidth();
			var k = (zmanim.zmanim[index].time + timeoffset)/24;
			scaleWidthAdj = $('#hatzot1').outerWidth()/2;
			scaleWidthReal = MTscaleWidth - scaleWidthAdj - $('#hatzot2').outerWidth()/2 - lastZmanWidth;
			zpos = scaleWidthAdj + scaleWidthReal*k - zwidth/2 + zoffset;
			if (zpos - prevzpos < prevzwidth + 3) {
				zoffset += prevzpos + prevzwidth + 3 - zpos;
				if (zmanim.zmanim[index].keyword == 'tzeit' || zmanim.zmanim[index].keyword == 'motzei' || zmanim.zmanim[index].keyword == 'motzei_tov') {
					zlastoffset = prevzpos + prevzwidth + 3 - zpos;
				}
				zpos = prevzpos + prevzwidth + 3;
			}
		} else {
			zpos = prevzpos;
			zwidth = prevzwidth;
		}
		zmanim.zmanim[index].width = zwidth;
		zmanim.zmanim[index].pos = Math.ceil(zpos);
		zmanim.zmanim[index].pip = false;
		prevzpos = zpos;
		prevzwidth = zwidth;
	}
	
	/*for (index in zmanim.zmanim) {
		if (zmanim.zmanim[index].keyword != 'hatzot1' && zmanim.zmanim[index].keyword != 'hatzot2') {
			zmanim.zmanim[index].pos -= Math.ceil(zlastoffset/2);
		}
	}*/
}
MedTermometr.prototype.setup_zmanim_offsets = MedTermometr_setup_zmanim_offsets;

function MedTermometr_show_zmanim () {
	for (index in zmanim.zmanim) {
		var zmandiv = $('#'+zmanim.zmanim[index].keyword);
		zmandiv.children('.ter_time').text(timeadj(zmanim.zmanim[index].time, this.city.ampm, zmanim.zmanim[index].roundup));
		if (zmanim.zmanim[index].visible == true && zmanim.zmanim[index].exist) {
			$('#'+zmanim.zmanim[index].keyword).show();
		} else {
			$('#'+zmanim.zmanim[index].keyword).hide();
		}
		//alert (parseInt(zmanim.zmanim[index].pos) + (zmanim.zmanim[index].pip == false ? 'px' : '%'))
		//$('#'+zmanim.zmanim[index].keyword).offset({left: parseInt(parseInt(MTscaleLeft) + parseInt(zmanim.zmanim[index].pos))});
		$('#'+zmanim.zmanim[index].keyword).css('left', parseInt(zmanim.zmanim[index].pos) + (zmanim.zmanim[index].pip == false ? 'px' : '%'))
		//alert ((MTscaleLeft + parseInt(zmanim.zmanim[index].pos)).toString() + $('#'+zmanim.zmanim[index].keyword).offset().left)
	}
}
MedTermometr.prototype.show_zmanim = MedTermometr_show_zmanim;

function MedTermometr_adjustMercury () {
	var flag_mercurySetup = false;
	this.afterMercuryZman = null;
	this.beforeMercuryZman = null;
	for (index in zmanim.zmanim) {
		if (zmanim.zmanim[index].visible == true) {
			if (zmanim.zmanim[index].time < this.mercury_pos) {
				this.beforeMercuryZman = zmanim.zmanim[index];
			} else {
				this.afterMercuryZman = zmanim.zmanim[index];
				break;
			}
		}
	}
	
	if (flag_mercurySetup == false) {
		var k1 = this.afterMercuryZman.pos - this.beforeMercuryZman.pos - this.beforeMercuryZman.width/2 + this.afterMercuryZman.width/2;
		var k2 = (this.mercury_pos - this.beforeMercuryZman.time)/(this.afterMercuryZman.time - this.beforeMercuryZman.time);
		this.mercury_width = Math.ceil(this.beforeMercuryZman.pos + this.beforeMercuryZman.width/2 + k1*k2 +3);
	}
	
	//FIX IE BUG
	//$('#mercury').css('left', '-' + parseInt($('#mercury').offset().left - MTscaleLeft) + 'px');
	
	$('#mercury').css('width', this.mercury_width + 'px');
}
MedTermometr.prototype.adjustMercury = MedTermometr_adjustMercury;

function MedTermometr_moveMercury() {
	this.mercury_pos = this.term_hour + this.term_minute/60;
	var h1time = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot1")].time;
	var h2time = zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot2")].time;
	if (mercuryFix == false)
		if (this.checkChangeParam() == true) 
			return;
	
	$('#mercury').css('background-image', 'url(' + env.subfolder + '/img/ter_animation_line.gif)');
	if (h1time > this.mercury_pos) {
		this.mercury_width = 0;
		$('#mercury').css('width', '0px');
	} else if (h2time < this.mercury_pos) {
		this.mercury_width = MTscaleWidth;
		$('#mercury').css('background-image', 'url(' + env.subfolder + '/img/ter_animation_line2.gif)');
		$('#mercury').css('width', this.mercury_width + 'px');
	} else {
		this.adjustMercury();
	}
}
MedTermometr.prototype.moveMercury = MedTermometr_moveMercury;

function MedTermometr_checkChangeParam() {
	if (this.change_type == BY_HATZOT && this.mercury_pos > zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot2")].time || 
		(this.change_type == BY_TIME && this.change_time.current_day == true && this.mercury_pos > this.change_time.hour + this.change_time.minute/60) || (this.change_type == BY_TIME && this.change_time.current_day == false && this.mercury_pos > this.change_time.hour + this.change_time.minute/60 + 24)) {
			//alert (this.mercury_pos +'/'+ zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot2")].time)
			var TermDate = new Date (this.term_year, this.term_month - 1, this.term_date, 1, 1, 1);
			var NewDate = new Date (TermDate.getTime() + 24*60*60*1000);
			this.term_hour -=24;
			RenewTerm(this.city, NewDate.getDate(), NewDate.getMonth() + 1, NewDate.getFullYear());
			return true;
	} else if (this.change_type == BY_HATZOT && this.mercury_pos < zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot1")].time || 
		(this.change_type == BY_TIME && this.change_time.current_day == true && this.mercury_pos < this.change_time.hour + this.change_time.minute/60) || (this.change_type == BY_TIME && this.change_time.current_day == false && this.mercury_pos < this.change_time.hour + this.change_time.minute/60 + 24)) {
			//alert (this.mercury_pos +'/'+ zmanim.zmanim[zmanim.get_zman_key_by_keyword ("hatzot1")].time)
			var TermDate = new Date (this.term_year, this.term_month - 1, this.term_date, 1, 1, 1);
			var NewDate = new Date (TermDate.getTime() - 24*60*60*1000);
			this.term_hour +=24;
			RenewTerm(this.city, NewDate.getDate(), NewDate.getMonth() + 1, NewDate.getFullYear());
			return true;
	}
	return false;
}
MedTermometr.prototype.checkChangeParam = MedTermometr_checkChangeParam;

function MedTermometr_changeHebTitles () {
	var	hDate = getHebFromCiv(this.term_date, this.term_month, this.term_year);
	this.hdate = hDate.date;
	this.hmonth = hDate.month;
	this.hyear = hDate.year;
	
	var termDate = new Date (this.term_year, this.term_month - 1, this.term_date, 1, 1, 1);
	var termStamp = termDate.getTime();
		
	this.term_dow =  termDate.getDay();
		
	var prevTermDate = new Date(termStamp - 24*60*60*1000);
	this.prev_term_date = prevTermDate.getDate();
	this.prev_term_month = prevTermDate.getMonth() + 1;
	this.prev_term_year = prevTermDate.getFullYear();
	this.prev_term_dow = this.term_dow - 1 < 0 ? 6 : this.term_dow - 1;
	var	prevhDate = getHebFromCiv(this.prev_term_date, this.prev_term_month, this.prev_term_year);
	this.prevhdate = prevhDate.date;
	this.prevhmonth = prevhDate.month;
	this.prevhyear = prevhDate.year;
		
	var nextTermDate = new Date(termStamp + 24*60*60*1000);
	this.next_term_date = nextTermDate.getDate();
	this.next_term_month = nextTermDate.getMonth() + 1;
	this.next_term_year = nextTermDate.getFullYear();
	this.next_term_dow = this.term_dow + 1 > 6 ? 0 : this.term_dow + 1;
	var	nexthDate = getHebFromCiv(this.next_term_date, this.next_term_month, this.next_term_year);
	this.nexthdate = nexthDate.date;
	this.nexthmonth = nexthDate.month;
	this.nexthyear = nexthDate.year;
	
	this.today_moadim = moadim.get_current_moadim (this.term_date, this.term_month, this.term_year, this.city.galut);
	this.tomorrow_moadim = moadim.get_current_moadim (this.next_term_date, this.next_term_month, this.next_term_year, this.city.galut);
	this.yesterday_moadim = moadim.get_current_moadim (this.prev_term_date, this.prev_term_month, this.prev_term_year, this.city.galut);
	
	var today_url = this.calendar_url;
	var tomorrow_url = this.calendar_url;
		
	var today_moadim_str = moadim.get_term_moadim_str (this.today_moadim, this.term_date, this.term_month, this.term_year);
	var tomorrow_moadim_str = moadim.get_term_moadim_str (this.tomorrow_moadim, this.next_term_date, this.next_term_month, this.next_term_year);
	
	var leftLink = "<a href='" + today_url + "'><img src='" + env.subfolder + "/img/sm_arr_1.gif' border='0' /></a>" + "<a href='" + today_url + "'>" + this.hdate + "&nbsp;" + env.HebMonthNamesRp[this.hmonth] + "</a>&nbsp;" +	today_moadim_str + "<a href='" + today_url + "'><img src='" + env.subfolder + "/img/sm_arr_2.gif' border='0' /></a>";
	
	var rightLink = "<a href='" + tomorrow_url + "'><img src='" + env.subfolder + "/img/sm_arr_2.gif' border='0' /></a>" + "<a href='" + tomorrow_url + "'>" + this.nexthdate + "&nbsp;" + env.HebMonthNamesRp[this.nexthmonth] + "</a>&nbsp;" +	tomorrow_moadim_str + "<a href='" + tomorrow_url + "'><img src='" + env.subfolder + "/img/sm_arr_2.gif' border='0' /></a>";
	
	$('#termPrevLink').html(leftLink);
	$('#termNextLink').html(rightLink);
}
MedTermometr.prototype.changeHebTitles = MedTermometr_changeHebTitles;
	
