/*-----------------------------------------------
	JavaScript Document
	Jquery Unobtrusive Datepicker
------------------------------------------------*/
jQuery.fn.datepicker = function(){
	return this.click(function(){
		jQuery.datepick.showDatePicker(this);
		return false;
	});
};
jQuery.datepick = {
	isVisible : false,
	curShowing : null,
	curDate : null,
	months : [JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER],
	days : [SUN,MON,TUE,WED,THU,FRI,SAT],
	uniqueCount : 0, 
	getUniqueId : function(){ this.uniqueCount++; return 'datePick_r4nd0m5tr1D'+this.uniqueCount; },
	getMonthName : function(key){
		key = parseFloat(key);
		while(key>11) key = key-12;
		return this.months[key];
	},
	getDayName : function(key){
		key = parseFloat(key);
		while(key>6) key = key-7;
		return this.days[key];
	},
	clearCals : function(){
		jQuery('.datepicker_container').slideUp('fast',function(){jQuery('.datepicker_container').remove();});
		this.isVisible = false;
		this.curShowing = null;
	},
	getHtmlObject : function(descrip){
		var el = document.createElement(descrip.tagName);
		for(var i in descrip) if(i!='tagName') el[i] = descrip[i];
		return el;
	},
	getCalTable : function(mo,yr,fldRef){
		var d = this.getHtmlObject({tagName:'div',className:'datepicker_calTblCont'});
		var tbl = this.getHtmlObject({tagName:'table',className:'datepicker_calTbl'});
		tbl.cellPadding = 0;
		tbl.cellSpacing = 1;
		var tBody = this.getHtmlObject({tagName:'tbody'});
		tbl.appendChild(tBody);
		var startRef = new Date(yr,mo,1);
		var endRef = new Date(yr,mo+1,0);
		while(startRef.getDay()!=0) startRef = new Date(startRef.getFullYear(),startRef.getMonth(),startRef.getDate()-1);
		while(endRef.getDay()!=6) endRef = new Date(endRef.getFullYear(),endRef.getMonth(),endRef.getDate()+1);
		var startDate = new Date(startRef.getFullYear(),startRef.getMonth(),startRef.getDate());
		endDate = endRef;
		
		var curRow = this.getHtmlObject({tagName:'tr'});
		for(var x=0;x<this.days.length;x++){
			var th = this.getHtmlObject({tagName:'th',innerHTML:this.days[x]});
			curRow.appendChild(th);
		}
		tBody.appendChild(curRow);
		curRow = this.getHtmlObject({tagName:'tr'});
		while(startDate<=endDate){
			var curDay = startDate.getDay();
			if(curDay%7==0){
				tBody.appendChild(curRow);
				curRow = this.getHtmlObject({tagName:'tr'});
			}
			var thisDate = (startDate.getMonth()+1)+'/'+startDate.getDate()+'/'+startDate.getFullYear();
			var td = this.getHtmlObject({tagName:'td',className:'datepicker_calCell '+this.getDayName(curDay)+' '+((mo==startDate.getMonth()) ? 'this_month':'other_month'),width:'14%'});
			var aLink = this.getHtmlObject({tagName:'a',innerHTML:startDate.getDate(),href:'javascript:void(0)'});
			jQuery(aLink).attr('dateRef',thisDate);
			jQuery(aLink).attr('fieldRef',fldRef);
			jQuery(aLink).click(function(){ jQuery.datepick.setCalValue(this); });
			td.appendChild(aLink);
			curRow.appendChild(td);
			startDate = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+1);
		}
		tBody.appendChild(curRow);
		d.appendChild(tbl);
		return d;
	},
	setCalendar : function(pNode,mo,yr){
		jQuery(pNode).empty();
		var moName = jQuery.datepick.getMonthName(mo);
		var hdr = jQuery.datepick.getHtmlObject({tagName:'div',className:'datepicker_hdr',innerHTML:moName+', '+yr});
		
		//Next Month Link
		var nxt = jQuery.datepick.getHtmlObject({tagName:'a',className:'datepicker_link datepicker_nextLink',innerHTML:'&raquo;',href:'javascript:void(0)'});
		var nxtDt = new Date(yr,mo+1,1);
		jQuery(nxt).attr('mo',nxtDt.getMonth());
		jQuery(nxt).attr('yr',nxtDt.getFullYear());
		jQuery(nxt).attr('fldRef',pNode.id);
		jQuery(nxt).click(function(){
			jQuery.datepick.updateCalendar(this);
			return false;		
		});
		
		//Previous Month Link
		var prev = jQuery.datepick.getHtmlObject({tagName:'a',className:'datepicker_link datepicker_prevLink',innerHTML:'&laquo;',href:'javascript:void(0)'});
		var prevDt = new Date(yr,mo-1,1);
		jQuery(prev).attr('mo',prevDt.getMonth());
		jQuery(prev).attr('yr',prevDt.getFullYear());
		jQuery(prev).attr('fldRef',pNode.id);
		jQuery(prev).click(function(){
			jQuery.datepick.updateCalendar(this);
			return false;				
		});
		pNode.appendChild(nxt);
		pNode.appendChild(prev);
		pNode.appendChild(hdr);
		var targID = pNode.id.split('datePickeFor_')[1];
		var tbl = jQuery.datepick.getCalTable(mo,yr,targID);
		pNode.appendChild(tbl);
		return true;
	},
	setCalValue : function(obj){
		document.getElementById(jQuery(obj).attr('fieldRef')).value = jQuery(obj).attr('dateRef');
		this.clearCals();
	},
	updateCalendar : function(obj){
		var dest = document.getElementById(jQuery(obj).attr('fldRef'));
		jQuery.datepick.curDate = new Date(parseFloat(jQuery(obj).attr('yr')),parseFloat(jQuery(obj).attr('mo')),1);
		jQuery.datepick.setCalendar(dest,parseFloat(jQuery(obj).attr('mo')),parseFloat(jQuery(obj).attr('yr')));
		return true;
	},
	initDatePickers : function(){
		jQuery('.datepicker').each(function(){
			if(!jQuery(this).attr('datePicker_initComplete')){
				jQuery(this).datepicker();
				jQuery(this).attr('datePicker_initComplete',true);
			}
		});
		jQuery('.datepickertrigger').each(function(){
			if(!jQuery(this).attr('datePicker_initComplete')){
				jQuery(this).click(function(){
					jQuery.datepick.showDatePicker(document.getElementsByName(jQuery.unob.getNextClassName(this,'datepickertrigger'))[0]);
					return false;
				});
				jQuery(this).attr('datePicker_initComplete',true);
			}
		});
	},
	showDatePicker : function(obj){
		/*Internal Function*/
		if(!this.isVisible || (obj.id==this.curShowing)){
			this.clearCals();
			var getHtmlObject = function(descrip){
				var el = document.createElement(descrip.tagName);
				for(var i in descrip) if(i!='tagName') el[i] = descrip[i];
				return el;
			};
			/*Start creating our calendar*/
			var mainDiv = getHtmlObject({tagName:'div',className:'datepicker_container'});
			if(!obj.id) obj.id = this.getUniqueId();
			mainDiv.id = 'datePickeFor_'+obj.id;
			if(jQuery.datepick.curDate){
				var mo = this.curDate.getMonth();
				var yr = this.curDate.getFullYear();
			} else { 
				var dateRef = new Date();
				var mo = dateRef.getMonth(); 
				var yr = dateRef.getFullYear(); 
				this.curDate = dateRef;
			}
			this.setCalendar(mainDiv,mo,yr);
			var location = jQuery(obj).offset();
			var calLocationTop = location.top+jQuery(obj).outerHeight();
			mainDiv.style.top = calLocationTop+'px';
			mainDiv.style.left = location.left+'px';
			document.body.appendChild(mainDiv);
			jQuery(mainDiv).slideDown('fast');
			this.isVisible = obj;
			this.curShowing = mainDiv.id;
		} else {
			this.clearCals();
		}
	}
};
if(typeof(jQuery.stackPageInit)=='function'){
	jQuery.stackPageInit(jQuery.datepick.initDatePickers);
} else {
	jQuery(document).ready(function(){
		jQuery.datepick.initDatePickers();
	});
}
$(document).click(function(){
	if(jQuery.datepick.isVisible) jQuery.datepick.clearCals();		   
});


