var Calendar = function(elementID){
	this.yearRange = 2;
	
	this.currDate = new Date();
	this.userElement = document.getElementById(elementID);
	var name_before = this.userElement.name;
	this.userElement.id = "_" + elementID;
	this.userElement.name = "_" + elementID;
	this.userElement.setAttribute("readonly", null);
	this.userElement.removeAttribute("name");
	this.templateFile = "templates/calendar.html";
	this.templateSource = '';
	this.calendarLayer = null;
	
	if(typeof(window.__instance) == "undefined"){
		window.__instance = new Array();
	}
	window.__instance[this.userElement.id] = this;
	if(typeof(window.__instance["calendarTemplateSource"]) == "undefined"){
		var ajax = new ajax_request();
		ajax.async = false;
		ajax.call_back = "";
		ajax.file_path = "javascripts/calendar.html";
		var request = ajax.send();
		window.__instance["calendarTemplateSource"] = request.responseText;
	}
	this.templateSource = window.__instance["calendarTemplateSource"];
	Event.RegisterEventHandler(this.userElement, "onclick", Calendar.onClick);
	
	this.workElement = document.createElement("INPUT");
	this.workElement.setAttribute("id", elementID);
	this.workElement.setAttribute("name", name_before);
	this.workElement.setAttribute("type", "hidden");
	this.workElement.value = this.userElement.value;
	
	this.userElement.parentNode.appendChild(this.workElement);
	
	this.currDate.setMySQL(this.workElement.value);
	
	this.onClick = function(){
		calendar = document.getElementById("CalendarPopUp");
		
		if(calendar == null){
			var Body = document.getElementsByTagName("BODY")[0];
			var calendar = document.createElement("DIV");
			calendar.id = "CalendarPopUp";
			calendar.style.position = "absolute";
			coordinates = DocumentDOM.GetCoordinates(this.userElement);
			calendar.style.border = "solid 1px #cccccc";
			calendar.style.top = (coordinates[1] + this.userElement.offsetHeight) + "px";
			calendar.style.left = (coordinates[0]) + "px";
			Body.appendChild(calendar);
		}
		
		this.calendarLayer = document.getElementById("CalendarPopUp");
		calendar.innerHTML = this.templateSource;
		if(this.calendarLayer.style.display == "none"){
			this.calendarLayer.style.display = "";
		}
		
		//YEAR
		var yearElement = document.getElementById("calendar_year");
		var html = '';
		html += '<select onchange="Calendar.changeYear(this, \'' + this.userElement.id + '\');">';
		for(z = (this.currDate.getFullYear() - this.yearRange); z < (this.currDate.getFullYear() + this.yearRange); z++){
			if(this.currDate.getFullYear() == z){
				selected = ' selected';
			}else{
				selected = '';
			}
			html += '<option' + selected + ' value="' + z + '">' + z + '</option>'
		}
		html += '</select>';
		yearElement.innerHTML = html;
		
		//MONTH
		var monthElement = document.getElementById("calendar_month");
		var html = '';
		html += '<select onchange="Calendar.changeMonth(this, \'' + this.userElement.id + '\');">';
		for(z = 0; z <= 11; z++){
			if(this.currDate.getMonth() == z){
				selected = ' selected';
			}else{
				selected = '';
			}
			html += '<option' + selected + ' value="' + z + '">' + this.currDate.getTranslatedMonth(z) + '</option>'
		}
		html += '</select>';
		monthElement.innerHTML = html;
		
		//DAYS
		var currentDay = 0;
		var week = 1;
		var day = 1;
		var dayElement = document.getElementById("week" + week + "_day" + day);
		
		sourceDate = new Date();
		sourceDate.setMySQL(this.workElement.value);
		
		for(var x = 1; x <= 7; x++){
			curr = document.getElementById("day" + x);
			if(curr){
				curr.innerHTML = this.currDate.getTranslatedDayOfWeek(x - 1);
			}
		}
		
		while(dayElement != null){
			
			if(this.currDate.compareWeekDays(currentDay, (day - 1)) && currentDay < this.currDate.getNumberOfDays()){
				currentDay++;
				if( this.currDate.getDate() == currentDay && 
					sourceDate.getMonth() == this.currDate.getMonth() && 
					sourceDate.getFullYear() == this.currDate.getFullYear() ){
					dayElement.className += "Selected";
				}
				dayElement.innerHTML = '<div onclick="Calendar.setDate(this, \'' + this.userElement.id + '\');" onmouseout="this.parentNode.className=this.parentNode.className.replace(\'Over\', \'\')" onmouseover="this.parentNode.className=this.parentNode.className + \'Over\'">' + currentDay + '</div>';
			}
			
			day++;
			if(day > 7){
				week++;
				day = 1;
			}
			dayElement = document.getElementById("week" + week + "_day" + day);
		}
		
		//HOURS
		var hourElement = document.getElementById("calendar_hour");
		html = '';
		html += '<input onkeyup="this.value = (isNaN(parseInt(this.value))) ? 0 : parseInt(this.value);" maxlength="2" type="text" value="' + this.currDate.getHours() + '">';
		hourElement.innerHTML = html;
		
		//MINUTES
		var minuteElement = document.getElementById("calendar_minute");
		html = '';
		html += '<input onkeyup="this.value = (isNaN(parseInt(this.value))) ? 0 : parseInt(this.value);" maxlength="2" type="text" value="' + this.currDate.getMinutes() + '">';
		minuteElement.innerHTML = html;
	}
	
	this.redraw = function(){
		this.onClick();
	}
	
	this.setDate = function(){
		this.workElement.value = this.currDate.getMySQL();
		this.userElement.value = this.currDate.getTranslatedDate();
		this.calendarLayer.style.display = 'none';
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////

Calendar.setDate = function(obj, instanceID){
	window.__instance[instanceID].currDate.setDate(obj.innerHTML);
	window.__instance[instanceID].setDate();
}

Calendar.changeMonth = function(obj, instanceID){
	window.__instance[instanceID].currDate.setMonth(obj.value);
	window.__instance[instanceID].redraw();
}


Calendar.changeYear = function(obj, instanceID){
	window.__instance[instanceID].currDate.setFullYear(obj.value);
	window.__instance[instanceID].redraw();
}

Calendar.onClick = function(e){
	obj = this;
	
	if(typeof(this.tagName) == "undefined"){
		obj = e.srcElement;
	}
	
	window.__instance[obj.id].onClick();
}
