/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorDateEntry.js
//
// DESCRIPTION: Implements the date entry component
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2006.05.22
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////



//
// DEPENDENCY CHECKING:
//
if(typeof GLADIATOR_CORE_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorDateEntry.js" REQUIRES "gladiatorCore.js"!');
if(typeof GLADIATOR_DATE_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorDateEntry.js" REQUIRES "gladiatorDate.js"!');
if(typeof GLADIATOR_CALENDAR_INCLUDED == 'undefined') alert('NOTE BENE: "gladiatorDateEntry.js" REQUIRES "gladiatorCalendar.js"!');

var GLADIATOR_DATE_ENTRY_INCLUDED=true;

//////////////////////////////////////////
//
// gDateEntry: For entering dates
//
//////////////////////////////////////////
function gDateEntry(label,id,parentNode){
	
	var _parentComponent = new gFormComponent(label,id,parentNode);
	
	var _thisComponent = _parentComponent.getNode();
	var _textBox = document.createElementNS(NS.xhtml,"input");
	_textBox.setAttribute("class","dI");
	_textBox.id=id;
	_textBox.name=id;
	
	var _todayButton = document.createElementNS(NS.xhtml,"div");
	_todayButton.setAttribute("class","dTI");
	_todayButton.addEventListener("click",setToday,false);
	var _calendarButton = document.createElementNS(NS.xhtml,"div");
	_calendarButton.setAttribute("class","dCI");
	_calendarButton.addEventListener("click",showCalendar,false);
	
	_thisComponent.appendChild(_calendarButton);
	_thisComponent.appendChild(_todayButton);
	_thisComponent.appendChild(_textBox);
	
	var _thisDate     = new gDate();
	var _thisCalendar = new gCalendar("cal_"+id,false,false,_thisDate.getYear(),_thisDate.getMonth(),_thisDate.getDay(),false,true,false,"xmlFeeds/all.xml",_textBox);
	
	////////////////////////
	//
	// PRIVATE METHODS
	//
	////////////////////////
	
	//
	// setToday()
	//
	function setToday(){
		
		_thisDate.setToday();
		_textBox.value=_thisDate.s();
	}
	
	//
	// showCalendar()
	//
	function showCalendar(){
		
		//
		// Show the calendar relative to the current gDateEntry object's position:
		//
		// ROUGH SPOT : The following is very dependent on dimensions specified in
		//              the CSS ...
		//
		// --> a gFormComponent is, by default, 255 px wide.  The default CSS has
		//     border width of 2 px, so 255+2+2=259. Then we add an extra 5 px for
		//     visual spacing aesthetics ... = 264.
		//
		// --> The calendar itself is 296px + 2px border left + 2px border right.
		//     However, this includes a 12px shadow which we want to subtract off.
		//     Finally, add a little (5 px) for aesthetic spacing ... = 293
		//
		var posXY = findPosition(_thisComponent);
		var x=posXY[0];
		var y=posXY[1];
		// For aesthetics, raise the calendar up a little:
		y -= 42;
		
		if(LC && LC.getDirection()=="rtl"){
			// Right-to-left environment:
			x -= 293;
		}else{
			// Left-to-right environment:
			x += 265;
		}
		// Fix up if necessary to keep on screen:
		if(x<DIMENSIONS.padding) x=DIMENSIONS.padding;
		if(y<DIMENSIONS.padding) y=DIMENSIONS.padding;
		if(x>DIMENSIONS.width-293) x=DIMENSIONS.width-293;
		if(y>DIMENSIONS.height-237) y=DIMENSIONS.height-237;
		
		_thisCalendar.setPosition(x,y); 
		_thisCalendar.show(true);
		
	}
	
	////////////////////////
	//
	// PUBLIC METHODS
	//
	////////////////////////
	this.getNode = function(){
		
		return _thisComponent;
		
	}
	
}
