/**
Author: Daniel

Requirements:
- Prototype

Example implementation:
<input type="text" class="text date_input"  id="prod_date" name="prod_date" value=""/>
<img class="click vimg" onclick="cal1=new jsCal('jscalendar','cal1','prod_date');return false;" src="today.gif" alt="cal"/>
<img class="click vimg" onclick="if(window.cal1){cal1.closeCal();}$('prod_date').value='';" src="today_clear.gif" alt="clear"/>
<div class="clear"></div>
<div id="jscalendar" class="jscalendar_holder" style="display:none;"></div>
**/

var jsCal = Class.create();
jsCal.prototype  = {
	initialize: function (holder,uid,output){
		this.options = {
      	holder: holder,
      	uid: uid,
      	output: output,
			monthname:["Januari", "Februari", "March", "April", "May", "Juny", "July", "Augustus", "September", "October", "November", "December"],
			weekday:["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    	}
    	this.showToDay();
	},
   Lz: function(x) { //Clean date 1 to 9 => 01...09
 			return (x < 0 || x >= 10 ? "" : "0") + x;
	},
	showToDay: function(){
		var cdate = new Date();
		this.showCal(cdate.getFullYear()+"-"+cdate.getMonth());
		var id = this.options.holder;
		if($(id).style.display=='none'){
			$(id).style.display='';
		}else{
			$(id).style.display='none';
		}
	},
	showCal: function(YrMo) {
		this.preshowCal();
		var MN, WD, LD, St, Dy, D = new Date(),pD=new Date(),nD=new Date(),wD=new Date();
		var T = YrMo.split(/\D+/), Yr = + T[0], Mo = + T[1];
		//Yr = D.getFullYear(),Mo=D.getMonth();
		MN = Mo < 0 || Mo > 11 ? Mo : this.options.monthname[Mo];
		today = D;
		pD.setFullYear(Yr, Mo, 0);
		D.setFullYear(Yr, Mo+1, 0);
		wD.setFullYear(Yr, Mo, 1);
		nD.setFullYear(Yr, Mo+1, 1);
		obj =this.options.uid; 
		St = "<div class=\"jsoptions\"><a class=\"jsclose\" href=\"#\" onclick=\""+obj+".closeCal();return false;\">X</a></div>"+
		"<table class=\"jscalendar\" cellpadding=\"0\" cellspacing=\"0\">"+
		"<tr class=\"yearholder\">"+
		"<td colspan=\"2\"><a class=\"nav navleft\" href=\"#\" onclick=\""+obj+".showCal('"+(D.getFullYear()-1)+"-"+D.getMonth()+"');return false;\"><<\/a><\/td>"+
		"<td colspan=\"3\" style=\"text-align:center;\">" + Yr + "<\/td>"+
		"<td colspan=\"2\"><a class=\"nav navright\" href=\"#\" onclick=\""+obj+".showCal('"+(D.getFullYear()+1)+"-"+D.getMonth()+"');return false;\" >><\/a><\/td>"+
		"<\/tr>"+
		"<tr class=\"monthholder\">"+
		"<td colspan=\"2\"><a class=\"nav navleft\" href=\"#\" onclick=\""+obj+".showCal('"+pD.getFullYear()+"-"+pD.getMonth()+"');return false;\"><<<\/a><\/td>"+
		"<td colspan=\"3\" class=\"monthheader\" style=\"text-align:center;\">"+  MN + "<\/td>"+
		"<td colspan=\"2\"><a class=\"nav navright\" href=\"#\" onclick=\""+obj+".showCal('"+nD.getFullYear()+"-"+nD.getMonth()+"');return false;\" >>><\/a><\/td><\/tr>"+
		"<tr class=\"dayholder\">"+
		"<td class=\"day weekend\">Su<\/td><td class=\"day\">Mo<\/td><td class=\"day\">Tu<\/td><td class=\"day\">We<\/td><td class=\"day\">Th<\/td><td class=\"day\">Fr<\/td><td class=\"day weekend\">Sa<\/td><\/tr><tr>";
		WD = wD.getDay();
		eWD = D.getDay()+1;
		LD = D.getDate();
		Mo = D.getMonth()
		PLD =pD.getDate();
		//alert(LD+"\n"+PLD+"\n"+WD+"\n"+pD+"\n"+wD+"\n"+D+"\n"+nD);
		PLD=PLD-(WD-1);
		for (Dy = 0; Dy < WD; ++Dy) {
			pD.setDate(PLD+Dy);		       	
			St += "<td><a class=\"prevmonth\" href=\"#\" onClick=\""+obj+".updateCal('"+pD+"');return false;\">"+pD.getDate()+"<\/a><\/td>";
		}
		today = new Date();
		today = today.getFullYear().toString()+today.getMonth().toString()+today.getDate().toString();
		for (Dy = 1; Dy <= LD; Dy++) {
			D.setFullYear(Yr,Mo,Dy);
			St += "<td><a class=\"curmonth";
			if ((""+D.getFullYear()+D.getMonth()+D.getDate()) == today ){	St += ' today'; }	
			St += "\" href=\"#\" onClick=\""+obj+".updateCal('"+D+"');return false;\">"+this.Lz(Dy)+"<\/a><\/td>";
			if (++WD % 7 == 0) { St += "<\/tr><tr>"; }
 		}
		D.setFullYear(Yr, Mo+2, 0);
		Yr = nD.getFullYear(),Mo = nD.getMonth(),eind = 7-eWD;
 		for (Dy = 0; Dy < eind; Dy++) {
			nD.setFullYear(Yr,Mo,Dy+1);
			St += "<td><a class=\"nextmonth\" href=\"#\" onClick=\""+obj+".updateCal('"+nD+"');return false;\">"+this.Lz(Dy+1)+"<\/a><\/td>";
	  }
 		$(this.options.holder).innerHTML=St+"<\/tr><\/table>";
		this.postshowCal();
	},
	updateCal: function(val){
		this.preupdateCal(val);
	 	D = new Date(val);
	 	this.postupdateCal(val);
	 	retval=this.Lz(D.getDate())+"-"+this.Lz(D.getMonth()+1)+"-"+D.getFullYear();
		return $(this.options.output).value=retval;
	},
	preupdateCal: function(val){	},
	postupdateCal: function(val){
		var id = this.options.holder;
		if($(id).style.display==''){ $(id).style.display='none'; }else{ $(id).style.display=''; }
	},
	preshowCal: function(){	},
	postshowCal: function(){	},
	closeCal: function(){
		var id = this.options.holder;
		$(id).style.display='none';
		return false;
	}
}	