var overCalendar;
function getXpos(obj) {
 
	var elem = obj;
	var xPos = 0; 
	 
	while (elem) {
		xPos += elem.offsetLeft; 
		elem = elem.offsetParent;
	}
	 
	return xPos;
}
 
 
function getYpos(obj) {
 
	var elem = obj;
	var yPos = 0;
	 
	while (elem) {
		yPos += elem.offsetTop;
		elem = elem.offsetParent;
	}
	 
	return yPos;
 
}
 
function showHide(theCalObj)
{
	if(!theCalObj.vizibil)
	{
		theCalObj.vizibil = theCalObj.showCal(theCalObj.vizibil,theCalObj.containerId);
		$(theCalObj.inputId).focus();
	}
	else
	{
		theCalObj.vizibil = theCalObj.hideCal(theCalObj.vizibil,theCalObj.containerId);
	} 
}

var Calendar = Class.create({
	initialize:function(inputId,IframeSrc,CloseBtnTxt,position)
	{
		if($(inputId))
		{
			//id-ul containerului
			//this.containerId = calendarContainerId;
			this.containerId = "calendarContainer_"+inputId;
			calendarContainerId = this.containerId;
			
			//id-ul iframe-ului cu calendar
			//this.frameId = iframeId;
			this.frameId = "frameId_"+inputId;
			iframeId = this.frameId
						
			//id-ul inputului text
			this.inputId = inputId;
			
			//calendarul ii vizibil sau nu
			this.vizibil = false;
			
			//calea catre calendar_new.php (src in iframe)
			this.iframeSrc= IframeSrc;
			var Obj = this;
			var container = document.createElement("div");
			Element.extend(container);
			container.addClassName("calContainerClass").hide();
			container.id = calendarContainerId;
			
			// insert it in the document
			if($('mainContainer'))
			{
				$('mainContainer').appendChild(container);
			}
			else
			{
				document.body.appendChild(container);
			}
				
			var iframe = document.createElement("iframe");
			Element.extend(iframe);
			iframe.addClassName("actualCalClass");
			iframe.id = iframeId;
			
			var calType = $(inputId).readAttribute('calType');
			var lowerLimit = $(inputId).readAttribute('lowerLimit');
			iframe.src = IframeSrc+"?calType="+calType+"&lowerLimit="+lowerLimit+"&date="+$(inputId).value;
			container.appendChild(iframe);
			container.observe("mouseover",function(event){overCalendar = true;});
			container.observe("mouseout",function(event){overCalendar = false;});
			if(!position)
				this.positionCalendar(inputId);
			else
				this.positionCalendar(inputId,position);
			this.attachEventsAndHandlers();
			var closeBtn = document.createElement("div");
			Element.extend(closeBtn);
			closeBtn.addClassName("CloseBtn");
			container.appendChild(closeBtn);
			
			var closeLink = document.createElement("a");
			Element.extend(closeLink);
			closeBtn.appendChild(closeLink);
			
			closeLink.href = "javascript:void(0)";
			closeLink.update(CloseBtnTxt);
			closeLink.observe("click",function(){Obj.vizibil = Obj.hideCal(Obj.vizibil,Obj.containerId)});
			if($(inputId).onchange)
			{
				$(inputId).observe("change:custom",function(event){$(inputId).onchange();});
			}
			else
			{
				$(inputId).observe("change:custom",function(event){return false;});
			}
		}
	},
	showCal:function(vizibil,theCalId)
	{
		$(theCalId).show();
		return true;
	},
	hideCal:function(vizibil,theCalId)
	{
		$(theCalId).hide();
		return false;
	},
	positionCalendar:function(inputId,pos)
	{
		if(!pos)
			var pos = "bottomRight";
		var coords = $(inputId).viewportOffset().toString().split(",");
		var inputHeight = $(inputId).getHeight();
		var container = $(this.containerId);	
		switch(pos)
		{
			case "topRight":container.style.left = coords[0]+"px";
							container.style.top = (1*coords[1]-(inputHeight+container.getHeight()))+"px";
							break;
			case "topMiddle":
							var middleOfInput = parseInt($(inputId).getWidth()/2);
							var middleOfInputCoord = 1*coords[0]+middleOfInput;
							container.style.left = middleOfInputCoord-parseInt(container.getWidth()/2)+"px";
							container.style.top = (1*coords[1]-(inputHeight+container.getHeight()))+"px";
							break;
			case "topLeft":	var mostRightCorner = 1*coords[0]+$(inputId).getWidth();
							container.style.left = mostRightCorner-parseInt(container.getWidth())+"px";
							container.style.top = (1*coords[1]-(inputHeight+container.getHeight()))+"px";
							break;
			case "rightTop":container.style.left = (1*coords[0]+$(inputId).getWidth())+"px";
						  	container.style.top = coords[1]+"px";
						  	break;
			case "rightMiddle":	container.style.left = (1*coords[0]+$(inputId).getWidth())+"px";
								var middleOfInputCoord = 1*coords[1]+parseInt(inputHeight/2);
								container.style.top = middleOfInputCoord-parseInt(container.getHeight()/2)+"px";
						  		break;
			case "rightBottom":	container.style.left = (1*coords[0]+$(inputId).getWidth())+"px";
								var bottomfInputCoord = 1*coords[1];
								container.style.top = bottomfInputCoord-parseInt(container.getHeight())+"px";
						  		break;
			case "bottomRight": container.style.left = coords[0]+"px";
						   		container.style.top = (1*coords[1]+inputHeight)+"px";
						   		break;
			case "bottomMiddle": var middleOfInput = parseInt($(inputId).getWidth()/2);
								 var middleOfInputCoord = 1*coords[0]+middleOfInput;
							 	 container.style.left = middleOfInputCoord-parseInt(container.getWidth()/2)+"px";
						   		 container.style.top = (1*coords[1]+inputHeight)+"px";
						   		 break;
			case "bottomLeft":	 var mostRightCorner = 1*coords[0]+$(inputId).getWidth();
								 container.style.left = mostRightCorner-parseInt(container.getWidth())+"px";
								 container.style.top = (1*coords[1]+inputHeight)+"px";
						   		 break;
			case "leftTop" : 	container.style.left = (1*coords[0]-container.getWidth())+"px";
						  		container.style.top = coords[1]+"px";
						  		break;
	  		case "leftMiddle" : container.style.left = (1*coords[0]-container.getWidth())+"px";
						  		var middleOfInputCoord = 1*coords[1]+parseInt(inputHeight/2);
								container.style.top = middleOfInputCoord-parseInt(container.getHeight()/2)+"px";
						  		break;
			case "leftBottom" : container.style.left = (1*coords[0]-container.getWidth())+"px";
								var bottomfInputCoord = 1*coords[1];
								container.style.top = bottomfInputCoord-parseInt(container.getHeight())+"px";
						  		break;
		}
	},
	setDateForInput:function(){
		var frame;
		if(document.all)
			frame = $(this.frameId).contentWindow.document;
		else
			frame = $(this.frameId).contentDocument;
		if($(this.inputId).value!==frame.getElementById('date').value)
		{
			$(this.inputId).value = frame.getElementById('date').value;
			this.vizibil = this.hideCal(this.vizibil,this.containerId);
			$(this.inputId).fire("change:custom");
		}
	},
	updateSrc:function()
	{
		var calType = $(this.inputId).readAttribute('calType');
		var lowerLimit = $(this.inputId).readAttribute('lowerLimit');
		$(this.frameId).src = this.iframeSrc+"?calType="+calType+"&lowerLimit="+lowerLimit+"&date="+$(this.inputId).value;
	},
	attachEventsAndHandlers:function()
	{
		var theCalObj = this;
		//Click pe input sa apara si sa dispara iframe-ul;
		$(this.inputId).observe('click', function(event){if(!theCalObj.vizibil){theCalObj.vizibil = theCalObj.showCal(theCalObj.vizibil,theCalObj.containerId);}else{theCalObj.vizibil = theCalObj.hideCal(theCalObj.vizibil,theCalObj.containerId);}theCalObj.setDateForInput();});
		
		//la blur input daca nu ii deasupra calendarului sa dispara
		$(this.inputId).observe('blur', function(event){if(!overCalendar){theCalObj.vizibil = theCalObj.hideCal(theCalObj.vizibil,theCalObj.containerId);}});
		
		//transmiterea valorii din iframe la input
		$(this.frameId).observe('load', function(event){theCalObj.setDateForInput();});
		
		//bugFix
		$(this.frameId).observe('mouseout',function(event){if(theCalObj.vizibil)$(theCalObj.inputId).focus();})
	}
});

