/**
 * @author jochen
 * 
 * custom version of fancyDate v0.1 beta for aussietours
 */

var months = new Array('jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec');
var mdays = new Array(31,28,31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var dayNames = new Array('M', 'T', 'W', 'T', 'F', 'S', 'S');

var fancyObj = null;

function getOffsetPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function fancydate(input){
	this.input = document.getElementById(input);
	this.input.readOnly = 'readonly';
	this.input.title = 'dd/mm/yyyy';
	this.input.value = '';
	var pos = getOffsetPos(this.input);
	this.posX = pos[0] + this.input.scrollLeft;
	this.posY = pos[1] + this.input.scrollTop + this.input.offsetHeight;
	this.iwidth = this.input.offsetWidth;
	this.oDiv = null;
	this.alterInput();
	this.state = false;
	this.overDiv = false;
	this.date = new Date();		
	this.today = leadingZero(this.date.getDate()) + '/' + leadingZero(this.date.getMonth() + 1) + '/' + this.date.getUTCFullYear()
	this.month = this.date.getMonth();	
	this.year = this.date.getUTCFullYear();
	
	this.input.onclick = bind(this, this.toggleCalender);
//	this.input.onfocus = bind(this, this.showCalender);
/*
	var blurfunc = this.input.onblur;
  	this.input.onblur = function(){if (blurfunc){blurfunc();};bind(this, this.shouldHideCalender);};*/
		this.input.onblur = bind(this, this.shouldHideCalender);		

//	document.body.onclick = bind(this, this.hideCalender)

	this.createCalender();

}

fancydate.prototype = {
	
	alterMonth			:		function(move){
										this.month += move;
										if (this.month < 0){
											this.month = 11;
											this.year += move;
										}else if (this.month > 11){
											this.month = 0;
											this.year += move;
										}
										this.refillCalender();
	}
	
	,
	
	alterInput			:		function(){
									this.input.style.backgroundImage = "url('/assets/web/fancydate/calendar.png')";
									this.input.style.backgroundRepeat = "no-repeat";
									this.input.style.backgroundPosition = "right center";
									this.input.style.cursor	= 'default';
	}
	
	,
	
	toggleCalender		:		function(){
		
									if (this.state){
										this.hideCalender();
									}else{
										this.showCalender();
									}
		
	}
	
	,
	
	mouseOut				:		function(){
		
								this.overDiv = false;
		
	}
	
	,
	
	mouseOver			:		function(){
								this.overDiv = true;	
	}
	
	,
	
	shouldHideCalender:		function(){
		
									if (!this.overDiv){
										this.hideCalender();										
									}else{
										var inp = this.input.id;
										window.setTimeout("document.getElementById('" + inp + "').focus();", 1);
									}
		
	}
	
	,
	
	hideCalender		:		function(){
									document.getElementById('calender_' + this.input.id).style.display = 'none';
									this.state = false;
									this.overDiv = false;					
									this.refillCalender();
	}
	
	,
	
	showCalender		:		function(){
									if (!this.state) {
						   			document.getElementById('calender_' + this.input.id).style.display = 'block';
								   	this.state = true;
								   }
	}
	
	,
	
	createCalender		:		function(){
								// create the calender div
									var cdiv = document.createElement('div');
									cdiv.id = 'calender_' + this.input.id;
									cdiv.className = 'calender';
									cdiv.style.position = 'absolute';
									cdiv.style.left = this.posX + 'px';
									cdiv.style.top = this.posY + 'px';
									cdiv.style.width = this.iwidth - 2 + 'px';
									cdiv.style.display = 'none';
									this.oDiv = cdiv;
								// fill her up scotty
									this.fillCalender(this.oDiv);
								// append div to the body
									document.body.appendChild(this.oDiv);								
									this.oDiv.onmouseover = bind(this, this.mouseOver);
									this.oDiv.onmouseout = bind(this, this.mouseOut);

									

	}		
		
	,
	
	refillCalender		:		function(){
									this.oDiv.innerHTML = '';
									this.fillCalender();
	}
	
	,
	
	fillCalender		:		function(){
								// create a date object
									var date = new Date();		
									
									var days = new Array(7);
									var weeks = new Array();

									var tempDate = new Date();
									tempDate.setMonth(this.month);
									tempDate.setFullYear(this.year);
									for (var n = 1;n<=mdays[this.month];n++){
										tempDate.setDate(n);
										var dag = tempDate.getUTCDay();
										dag -= 1;
										if (dag < 0){
											dag = 6;
										}
										
										days[dag] = n;
										
										if (dag == 6){
											weeks.push(days);
											days = new Array(7);
										}												
									}
									
									if (days){
										weeks.push(days);											
									}

									ctable = document.createElement('table');
									ctable.width = '100%'
									crow = document.createElement('tr');
									ccol = document.createElement('td');
									var pre = new Selector('/assets/web/fancydate/previous.gif', 'month', -1, this);
									ccol.appendChild(pre.img);
									crow.appendChild(ccol);	
									ccol = document.createElement('td');
									ccol.setAttribute('colspan', 2)
									ccoltxt = document.createTextNode(months[this.month]);
									ccol.appendChild(ccoltxt);
									crow.appendChild(ccol);									
									ccol = document.createElement('td');
									var next = new Selector('/assets/web/fancydate/next.gif', 'month', 1, this);
									ccol.appendChild(next.img);
									crow.appendChild(ccol);
									ccol = document.createElement('td');
									ccol.setAttribute('colspan', 3)
									ccol.style.textAlign = 'right'
									ccoltxt = document.createTextNode(this.year);
									ccol.appendChild(ccoltxt);
									crow.appendChild(ccol);
									ctable.appendChild(crow);
									
									var drow = document.createElement('tr');
									for (var i = 0;i < dayNames.length;i++){
										var ccol = document.createElement('td');
										var ctxt = document.createTextNode(dayNames[i]);
										ccol.appendChild(ctxt);
										drow.appendChild(ccol);
									}
									ctable.appendChild(drow);
									
									for (var i = 0;i < weeks.length;i++){
										drow = document.createElement('tr');
										for (var j = 0;j < weeks[i].length;j++){
											var dcol = document.createElement('td');											
											if (weeks[i][j]){
												var title = leadingZero(weeks[i][j]) + '/' + leadingZero(this.month + 1) + '/' + this.year;
												dcol.title = title;
												dcol.style.cursor = 'pointer';
												new colClick(dcol, this);
												var dcolt = document.createTextNode(weeks[i][j]);
												dcol.appendChild(dcolt);
												if (title != this.today){
													dcol.className = 'day';
												}else{
													dcol.className = 'day today';
												}
												
												if (j == 6){
													dcol.className += ' sunday';
												}
												
												if (this.input.value == title){
													dcol.className += ' selected';
												}
												
											}else{
												dcol.innerHTML = '&nbsp;';
											}
											drow.appendChild(dcol);
										}
										ctable.appendChild(drow);
									}

									this.oDiv.appendChild(ctable);		
							
	}

}

function bind(el, func){
	return function(){
		func.call(el);
	}
}

function leadingZero(val){
	var r = '';
	if (parseInt(val) < 10){
		r = '0' + val;
	}else{
		r = val;
	}
	
	return r;
}

function doSomething(){
	fancyObj.input.value = this.title;
}

function colClick(td, parent){
	this.td = td;
	this.parent = parent;
	this.td.onclick = bind(this, this.setValue);
	
}

colClick.prototype = {
	
	setValue : function(){
		this.parent.input.value = this.td.title;
		this.parent.hideCalender();
	}
	
}

function Selector(img, type, move, p){
	this.img = new Image();
	this.img.src = img;
	this.img.style.width = '6px';
	this.img.style.height = '9px';
	this.img.style.marginRight = '2px';
	this.img.style.cursor = 'pointer';
	this.img.alt = '';
	this.img.title = '';	
	this.type = type;
	this.parent = p;
	this.move = move;
	this.img.onclick = bind(this, this.doMove);
}

Selector.prototype = {
	
	doMove : function(){
		this.parent.alterMonth(this.move);
	}
	
}
