SLOG.library("CalJ.misc", [
	"SLOG.Ajax",
	"CalJ.DatePicker"
], function() {


CalJ.Shabbat = {


	picker: null,
	popupChangeDate: function(language) {

		if(null == CalJ.Shabbat.picker) {
			var target = "/gregorianCalendar";
			if(language == "en") {
				target = "/en" + target;
			}

			CalJ.Shabbat.picker = new CalJ.Convert.DatePicker(
				"CalJ.Shabbat.picker", 
				document.getElementById("divGCalendar"), 
				null,
				target,
				function() { return document.getElementById("containerGDateText").getAttribute("gdate"); },
				CalJ.Shabbat.pick
			);
		}

		if(CalJ.Shabbat.picker.isOn())
			CalJ.Shabbat.picker.close();
		else
			CalJ.Shabbat.picker.open();
	},
	pick: function(picker, year, month, day) {
		document.forms["formChangeShabbatDate"].date.value = year + "-" + month + "-" + day;
		document.forms["formChangeShabbatDate"].submit();
	}
};

CalJ.Zmanim = {


		picker: null,
		popupChangeDate: function(language) {
			if(null == CalJ.Zmanim.picker) {
				var target = "/gregorianCalendar";
				if(language == "en") {
					target = "/en" + target;
				}

				CalJ.Zmanim.picker = new CalJ.Convert.DatePicker(
					"CalJ.Zmanim.picker", 
					document.getElementById("divGCalendarZmanim"), 
					null,
					target,
					function() { return document.getElementById("containerGDateZmanimText").getAttribute("gdate"); },
					CalJ.Zmanim.pick
				);
			}

			if(CalJ.Zmanim.picker.isOn())
				CalJ.Zmanim.picker.close();
			else
				CalJ.Zmanim.picker.open();
		},
		pick: function(picker, year, month, day) {
			zmanimChangeDate(year, month, day);
		}
	};



CalJ.Convert = {

	DatePicker: function(name, divContainer, imgButton, ajaxCommand, fnGetInitDate, callbackPick) {
		CalJ.DatePicker.call(this, name);
	
		this.divContainer = divContainer;
		this.imgButton = imgButton;
		this.ajaxCommand = ajaxCommand;
		this.fnGetInitDate = fnGetInitDate;
		this.callbackPick = callbackPick;

		//Useful closure for method override mechanism.
		var _this = this;

		//Override method: CalJ.DatePicker.close
		this._close = this.close;
		this.close = function() {
			_this._close();
			if(this.divContainer) {
				this.divContainer.style.display = "none";
			}
			if(this.imgButton)
				//Change the appearance of the button: 
				this.imgButton.src = this.imgButton.src.replace("-on\.", "-off.");
		};

		this.isOn = function() {
			if(this.divContainer)
				return (this.divContainer.style.display == "block");
			return true;
		};

		this.pushImage = function() {
			//Change the appearance of the button:
			if(this.imgButton)
				this.imgButton.src = this.imgButton.src.replace("-off\.", "-on.");
		};

		this._open = this.open;
		this.open = function(year, month, day) {
			this._open(year, month, day);
			var iniDate = "";
			if(this.fnGetInitDate) {
				iniDate = (this.fnGetInitDate)();
			}
			this.ajaxRequest(iniDate, "");
		};

		this._nextMonth = this.nextMonth;
		this.nextMonth = function(year, month) {
			this._nextMonth();
			this.ajaxRequest(year + "-" + month, "nextMonth");
		};

		this._prevMonth = this.prevMonth;
		this.prevMonth = function(year, month) {
			this._prevMonth();
			this.ajaxRequest(year + "-" + month, "prevMonth");
		};

		this._changeYear = this.changeYear;
		this.changeYear = function(year, month) {
			this._changeYear();
			this.ajaxRequest(year + "-" + month, "changeYear");
		};

		this.changeMonth = this.changeYear;

		this.ajaxRequest = function(date, move) {
			var selectionContainer = document.getElementById("tableCalendar." + this.name);
			var selection = "";
			if(selectionContainer) {
				selection = selectionContainer.getAttribute("selectionDate");
			}
			SLOG.Ajax.getHTML(
				this.ajaxCommand,
				{
					picker: this.name,
					date: date,
					selection: selection,
					move: move
				},
				function(params, response) { _this.displayCallback(params, response); }
			);			
		};

		this.displayCallback = function(params, response) {
			this.divContainer.innerHTML = response;
			this.divContainer.style.display = "block";
		};

		this._pick = this.pick;
		this.pick = function(year, month, day) {
			this._pick(year, month, day);
			if(this.callbackPick)
				(this.callbackPick)(this, year, month, day);
		}
	},




	g: null,

	popupGCalendar: function() {

		if(CalJ.Convert.g == null) {
			CalJ.Convert.g = new CalJ.Convert.DatePicker(
				"CalJ.Convert.g", 
				document.getElementById("divGCalendar"), 
				document.getElementById("imgGCalendar"),
				"/gregorianCalendar",
				function() { return document.getElementById("containerGDateText").getAttribute("gdate"); },
				CalJ.Convert.gPick
			);
		}

		//Toggle off if needed
		if(CalJ.Convert.g.isOn()) {
			CalJ.Convert.g.close();
			return;
		}

		CalJ.Convert.g.pushImage();

		if(CalJ.Convert.h)
			CalJ.Convert.h.close();
		CalJ.Convert.g.open();
	},


	gPick: function(datePicker, year, month, day) {
		datePicker.close();
		SLOG.Ajax.getJS(
			"/setGDate.do",
			{
				forwardSuccess: '/jsDate',
				year: year,
				month: month,
				day: day
			},
			function(params, response) {
				CalJ.Convert.refreshDisplay(response);
			}
		);
	},







	h: null,
	popupHCalendar: function() {

		if(CalJ.Convert.h == null) {
			CalJ.Convert.h = new CalJ.Convert.DatePicker(
				"CalJ.Convert.h", 
				document.getElementById("divHCalendar"), 
				document.getElementById("imgHCalendar"),
				"/hebrewCalendar",
				function() { return document.getElementById("containerHDateText").getAttribute("hdate"); },
				CalJ.Convert.hPick
			);
		}

		//Toggle off if needed
		if(CalJ.Convert.h.isOn()) {
			CalJ.Convert.h.close();
			return;
		}

		CalJ.Convert.h.pushImage();

		if(CalJ.Convert.g)
			CalJ.Convert.g.close();
		CalJ.Convert.h.open();
	},


	hPick: function(datePicker, year, month, day) {
		datePicker.close();

		SLOG.Ajax.getJS(
			"/setHDate.do",
			{
				forwardSuccess: '/jsDate',
				year: year,
				month: month,
				day: day
			},
			function(params, response) {
				CalJ.Convert.refreshDisplay(response);
			}
		);
	},

	refreshDisplay: function(data) {
		document.getElementById("containerGDateText").innerHTML = data.g;
		document.getElementById("containerGDateText").setAttribute("gdate", data.gdate);
		document.getElementById("containerHDateText").innerHTML = data.h;
		document.getElementById("containerHDateText").setAttribute("hdate", data.hdate);
		document.getElementById("containerHebrewStringText").innerHTML = data.heb;
		document.getElementById("containerParasha").innerHTML = data.parasha;
		document.getElementById("containerParashaHebrew").innerHTML = data.parashaHebrew;
		var table;
		if(table = document.getElementById("tableCalendar.CalJ.Convert.g"))
			table.setAttribute("selectionDate", "");
		if(table = document.getElementById("tableCalendar.CalJ.Convert.h"))
			table.setAttribute("selectionDate", "");
	},

	setIsrael: function(israel) {
		SLOG.Ajax.getJS(
			"/setObservanceIsrael.do",
			{
				forwardSuccess: "jsDate",
				israel: israel
			},
			function(params, response) {
				CalJ.Convert.refreshDisplay(response);
			}
		);
	}
};

//Create the inheritance.
CalJ.Convert.DatePicker.prototype = new CalJ.DatePicker();

});