function Calendar(obj){
	this.saida = 'cal'; // Elemento de Retorno do Calendário
	
	this.data = new Date();
		
	this.dia = this.data.getDate();
	this.mes = this.data.getMonth();
	this.ano = this.data.getFullYear();
	
	this.valor 	  = null; // Variavel, que guarda a última seleção de data (dd/mm/yyyy)
	this.url 	  = null; // Caso setada, joga a data (dd-mm-yyyy) como argumento final ex: index.jsp?data=
	this.elemento = null; // Id, do elemento de retorno ex: div,p,input,textarea
	this.funcao   = null; // Função à ser chamada com o argumento requerido de retornod e valor da data (dd/mm/yyyy), this.selecao
	this.colorir = 0;
	
	this.cabecalho = null; //Cabeçalho do Calendário
	this.navegador = null;
	this.estrutura = ''; //Calendário
	
	this.meses = new Array('Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');
	
	this.obj = obj;
	this.selecao = null;
}
Calendar.prototype.montaCalendario = function(){
	var mes_atual = new Date(this.ano, this.mes, 1);
	var mes_proximo = new Date(this.ano, this.mes + 1, 1);
		
	var primeiro_dia = mes_atual.getDay();
	var dias_no_mes = Math.round((mes_proximo.getTime() - mes_atual.getTime()) / (1000 * 60 * 60 * 24));
	if(this.cabecalho != null)
		this.estrutura = this.cabecalho;
	else
		this.estrutura = '';
	if(this.navegador == "links")
		this.estrutura += '<p style=\"padding:0 0 5px 0;\"><a href="javascript:void(0);" onClick="' + this.obj  + '.anteriorMes();">«</a><label>' + this.meses[this.mes] + '/' + this.ano + '</label><a href="javascript:void(0);" onClick="' + this.obj  + '.proximoMes();">»</a></p>\n';		
	if(this.navegador == "mes")
		this.estrutura += '<p style=\"padding:0 0 5px 0;>' + this.meses[this.mes] + '</p>\n';		
	this.estrutura += '<ul>\n\t<li class="dias"><label>D</label><label>S</label><label>T</label><label>Q</label><label>Q</label><label>S</label><label>S</label></li>\n\t<li>';
	for(i=0; i < primeiro_dia; i++)
		this.estrutura += '<label class="vazio">&nbsp;</label>';   
	i = primeiro_dia;
	for(j = 1; j <= dias_no_mes; j++){
		k = i%7;
		if(k == 0 && k && i != 0){
			this.estrutura += '</li>\n\t<li>';
		}
			
		if(this.dia == j && this.selecao != j && this.data.getMonth() == this.mes)   
			this.estrutura += '<label class="hoje"><a href="javascript:' + this.obj  + '.selecionaData(' + j + ',this); void(0);">' + j + '</a></label>';
		else if(this.selecao == j)
			this.estrutura += '<label><a href="javascript:' + this.obj  + '.selecionaData(' + j + ',this); void(0);" class="atual">' + j + '</a></label>';
		else
			this.estrutura += '<label><a href="javascript:' + this.obj  + '.selecionaData(' + j + ',this); void(0);">' + j + '</a></label>';
		i++;
	}
	if(k==0)
		this.estrutura += '</ul>';
	else
		this.estrutura += '</li>\n</ul>';	
}
Calendar.prototype.mostraCalendario = function (){
	document.getElementById(this.saida).innerHTML = this.estrutura;	
}
Calendar.prototype.selecionaData = function (diaS,el){
	this.selecao = diaS;
	//this.valor = this.completaZero(diaS,2) + '/' + this.completaZero(this.mes+1,2) + '/' + this.ano;	
	this.valor = this.completaZero(diaS,2) + '-' + this.completaZero((this.mes+1),2) + '-' + this.ano;
	if(this.url != null){
		window.location.href = this.url + 'dt=' + this.completaZero(diaS,2) + '-' + this.completaZero((this.mes+1),2) + '-' + this.ano;
	}
	if(this.elemento != null){
		var d = document.getElementById(this.elemento);
		if(d.value === undefined)
			d.innerHTML = this.valor;	
		else
			d.value = this.valor;
	}
	if(this.funcao != null){
		eval(this.funcao);
	}			
	if(this.colorir != 0){
		this.montaCalendario();
		this.mostraCalendario();
		el.className = 'atual';
	}
}
Calendar.prototype.completaZero = function(valor,formatacao){
	var retorno = "";
	var nt = valor.toString().length;
    var nn = formatacao-nt;
	
	for(x=1; x<=nn; x++)
		retorno += "0";
		
    retorno += valor;
	return retorno;	
}
Calendar.prototype.proximoMes = function (){
	this.mes = this.mes+1;
	if(this.mes > 11){
		this.mes = 0;
		this.ano = this.ano+1;
	}
	this.selecao = null;
	/*if(this.url != null)
		window.location.href = this.url + 'mes=' + this.completaZero((this.mes+1),2) + '&ano=' + this.ano;*/
	this.montaCalendario();
	this.mostraCalendario();
}
Calendar.prototype.anteriorMes = function (){
	this.mes = this.mes-1;	
	if(this.mes < 0){
		this.mes = 11;
		this.ano = this.ano-1;
	}
	this.selecao = null;
	/*if(this.url != null)
		window.location.href = this.url + 'mes=' + this.completaZero((this.mes+1),2) + '&ano=' + this.ano;*/
	this.montaCalendario();
	this.mostraCalendario();
}
Calendar.prototype.verificaUrl = function(){
	var url = window.location.href.toString();
	var a = (url.lastIndexOf("dt=") + 3);
	if(url.substring(a,a+10).length == 10 && a > 2){
		var dg1 = parseInt(url.substring(a+3,a+4));
		var dg2 = parseInt(url.substring(a+4,a+5));
		var d3 = dg1 + "" + dg2;
		this.mes = d3-1;
		this.ano = parseInt(url.substring(url.lastIndexOf("dt=")+9,url.length));

		if(url.substring(a+3,a+5) ==  this.completaZero((this.mes+1),2)){
			this.selecao = parseInt(url.substring(a,a+2));
			this.colorir = 1;
		}
	}
}
Calendar.prototype.carregarCalendario = function (){
	this.montaCalendario();
	this.mostraCalendario();	
}
Calendar.prototype.contaDias = function(obj){
	return Math.round((new Date(this.ano,this.mes, this.selecao).getTime() - new Date(obj.ano,obj.mes,obj.selecao).getTime()) / (1000 * 60 * 60 * 24));	
}