//All Functions in calendar.js by Lucas Ferreira - www.lucasferreira.com
//arquivos necessários
if(Calendar == undefined) var Calendar = new Object();

Calendar.meses = ["JANEIRO", "FEVEREIRO", "MARÇO", "ABRIL", "MAIO", "JUNHO", "JULHO", "AGOSTO", "SETEMBRO", "OUTUBRO", "NOVEMBRO", "DEZEMBRO"];
Calendar.letrasSemana = ["D","S","T","Q","Q","S","S"];
Calendar.diasMes = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Calendar.bissexto = function(y) {
	return  (y % 4 == 0) ? true : false;
}
Calendar.cDate = function(d, m, y){
	var hj = new Date();
	if(d == undefined) var d = hj.getDate();
	if(m == undefined) var m = hj.getMonth();
	if(y == undefined) var y = hj.getFullYear();
	this.d = d; this.m = m; this.y = y;
	this.bi = Calendar.bissexto(this.y) ? 29 : 28;
	this.diasMes = (this.m != 1 ? Calendar.diasMes[this.m] : this.bi);
	this.inicio = new Date(this.y, this.m, 1, 0, 0, 0, 0);
}
Calendar.create = function(id, d, m, y){
	
	dControl = d;
	mControl = m;
	yControl = y;
	
	if(typeof id == "string"){
		var id = $(id);
	}
	
	this.id = id;
	this.onSelect = function(){};
	
	var calend_head = document.createElement("DIV");
	calend_head.id = "head";
	calend_head.innerHTML = "<a href='javascript:;' title='Ir para mês anterior' class='arrow' style='float: left; width: 23px; height: 20px'>&laquo;</a><span class='month'>&nbsp;</span><a href='javascript:;' title='Ir para próximo mês' class='arrow' style='float: right; width: 23px; height: 20px'>&raquo;</a>";
	this.id.appendChild(calend_head);
	
	var clickArrows = function(e){
		var obj = getTargetByEvent(e);
		if(obj.innerHTML.toString() == "»" || obj.innerHTML.toString() == "&raquo;"){
			this.iDate.m = Math.round(this.iDate.m) + 1;
			if(this.iDate.m > 11){
				this.iDate.d = 1;
				this.iDate.m = 0;
				this.iDate.y = Math.round(this.iDate.y) + 1;
			}
		} else {
			this.iDate.m = Math.round(this.iDate.m) - 1;
			if(this.iDate.m < 0){
				this.iDate.m = 11;
				this.iDate.d = this.diasMes;
				this.iDate.y = Math.round(this.iDate.y) - 1;
			}
		}

		if (this.iDate.m == mControl) {
			this.setCDate(dControl, this.iDate.m, this.iDate.y);
		} else {
			this.setCDate('', this.iDate.m, this.iDate.y);
		}
	}
	
	var a_left = $tc("arrow", "A", this.id)[0];
	var a_right = $tc("arrow", "A", this.id)[1];
	a_left.onclick = a_right.onclick = Delegate.create(this, clickArrows);
	
	var calend_tbl = document.createElement("TABLE");
	calend_tbl.cellPadding = 0;
	calend_tbl.cellSpacing = 0;
	calend_tbl.border = 0;
	calend_tbl.width = "100%";
	
	var calend_tbody = document.createElement("TBODY");
	var calend_tr = document.createElement("TR");
	for(var i=0; i<Calendar.letrasSemana.length; i++){
		var calend_th = document.createElement("TH");
		calend_th.innerHTML = Calendar.letrasSemana[i];
		calend_tr.appendChild(calend_th);
	}
	calend_tbody.appendChild(calend_tr);
	
	var iCount = 0;
	var calend_tr_dias = document.createElement("TR");
	for(var i=1; i<38; i++){
		var calend_td_dias = document.createElement("TD");
		calend_tr_dias.appendChild(calend_td_dias);
		if(iCount > 5){
			calend_tbody.appendChild(calend_tr_dias);
			calend_tr_dias = document.createElement("TR");
			iCount = 0;
		} else {
			iCount++;
		}
	}
	
	calend_tbl.appendChild(calend_tbody);
	this.id.appendChild(calend_tbl);
	
	this.calend_tbody = calend_tbody;
	this.calend_head = calend_head;
	this.setCDate(d, m, y);
	
}
Calendar.create.prototype = {

	setCDate: function(d, m, y){
		this.iDate = new Calendar.cDate(d, m, y);
		this.clearCalendar();
		
		var tds = $t("TD", this.calend_tbody);
	
		var s_head = $tc("month", "SPAN", this.id)[0];
		s_head.innerHTML = Calendar.meses[this.iDate.m] + " - " + this.iDate.y;
	
		var iDia = 1;
		for(var i=this.iDate.inicio.getDay(); i<tds.length; i++){
			if(iDia == this.iDate.d) {
				tds[i].className = "today";
			}
			tds[i].innerHTML = iDia;
			tds[i].onmouseover = function(){
				if(this.className != null && this.className == "today"){
					this.className = "overToday";
				} else this.className = "over";
			}
			tds[i].onmouseout = function(){
				if(this.className != null && this.className == "overToday"){
					this.className = "today";
				} else this.className = "";
			}
			
			tds[i].onclick = Delegate.create(this, function(d, m, y){
				var d = d < 10 ? "0" + d : d;
				var m = m < 10 ? "0" + m : m;				
				this.onSelect(d, m, y);
			}, [iDia, (this.iDate.m+1), this.iDate.y]);
			
			if(iDia >= this.iDate.diasMes || iDia >= 31 || iDia > tds.legnth){
				break;
			}
			iDia++;
		}		
	},
	
	clearCalendar: function(){
		var tds = $t("TD", this.calend_tbody);
		if(tds != null){
			for(var i=0; i<tds.length; i++){
				tds[i].className = "";
				tds[i].innerHTML = "&nbsp;";
				tds[i].onmouseover = function(){};
				tds[i].onmouseout = function(){};
				tds[i].onclick = function(){};				
			}
		}
	},
	
	show: function(){
		this.id.style.visibility = "visible";
	},
	
	hide: function(){
		this.id.style.visibility = "hidden";
	}
	
}
