/**
 * @author ashley.gibson
 */



function NgaCalendar(textFieldId)
{
	var _this = this;
	
	this._months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	
	this._overcal = false;
	
	this._textFieldId = textFieldId;
	
	this._id = this._textFieldId + "cal";
	
	this._containerId = this._textFieldId + "ctnr";
	
	this._calcontainer = new YAHOO.widget.Overlay(this._containerId, { context:[this._textFieldId,"tl","bl"], visible:true } );
	
	this._calcontainer.render(document.body);
	
	this._cal = new YAHOO.widget.Calendar(this._id,this._containerId,{ title:"Select date:"});
	
	
	this._cal.cfg.setProperty("MDY_DAY_POSITION", 1);
	this._cal.cfg.setProperty("MDY_MONTH_POSITION", 2);
	this._cal.cfg.setProperty("MDY_YEAR_POSITION", 3);
	 
	this._cal.cfg.setProperty("MD_DAY_POSITION", 1);
	this._cal.cfg.setProperty("MD_MONTH_POSITION", 2);
	
	this._cal.selectEvent.subscribe(this.dateSelected, this, true);
	this._cal.renderEvent.subscribe(this.setupListeners, this, true);
	
	
	YAHOO.util.Event.addListener( this._textFieldId, 'focus', function() {_this.showCal() }  );
	YAHOO.util.Event.addListener( this._textFieldId, 'blur',  function() {_this.hideCal() } );
	
	this._cal.render();
	this.hideCal();
	
}

NgaCalendar.prototype.setupListeners = function()
{
	var _this = this;
	YAHOO.util.Event.addListener( this._cal.id, 'mouseover',  function() {_this.overCal() } );
	YAHOO.util.Event.addListener( this._cal.id, 'mouseout',  function() {_this.outCal() } );	
}

NgaCalendar.prototype.dateSelected = function()
{
	var calDate = this._cal.getSelectedDates()[0];
	test = new Date();
	calDate =  calDate.getDate() + '-' + this._months[calDate.getMonth()] + '-' + calDate.getFullYear(); 
	YAHOO.util.Dom.get(this._textFieldId).value = calDate;
	this._overcal = false;
	this.hideCal();
} 

NgaCalendar.prototype.showCal = function()
{
	var calDate = new String(YAHOO.util.Dom.get(this._textFieldId).value);
	
	//convert all the date delimeters to - as the standard
	calDate = calDate.replace("/","-","gi");
	calDate = calDate.replace(".","-","gi");
	calDate = calDate.replace("\\","-","gi");
	calDateArray = calDate.split("-");
	
	//check if the month value is a number or a word and if it a number, reformat for australian format.
	if (!isNaN(calDateArray[1]))
	{
		calDate = calDateArray[1] + "-" + calDateArray[0] + "-" + calDateArray[2];
	}
	
	//remove the - delimeter from the string
	calDate = calDate.replace("-"," ","gi");
	
	calDate = new Date(calDate);
	
	//if date is invalid use todays date
	if (isNaN(calDate))
	{
		calDate = new Date();
	}

	this._cal.cfg.setProperty('selected', calDate.getDate()+'/'+(calDate.getMonth()+1)+'/'+calDate.getFullYear());
	this._cal.cfg.setProperty('pagedate', (calDate.getMonth()+1)+'/'+calDate.getFullYear());
	
	// re-render the calendar after changing config
	this._cal.render();

	this._calcontainer.align("tl", "bl");
	this._calcontainer.show();
}

NgaCalendar.prototype.hideCal = function()
{
	if (!this._overcal)
	{
		this._calcontainer.hide();
	}
}
 
NgaCalendar.prototype.overCal = function() 
{ 
	this._overcal = true;
}

NgaCalendar.prototype.outCal = function() 
{
	this._overcal = false;
}  
