

var CLDsemana = new Array('dom','seg','ter','qua','qui','sex','sab');
var CLDmeses = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var CLDmes = new Array('Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');

var CLDferiados = new Array();
for(var i=1;i<=12;i++){
	CLDferiados[i] = new Array();
}

CLDferiados[1][1] = 'Confraternização Universal';
CLDferiados[4][21] = 'Tiradentes';
CLDferiados[5][1] = 'Dia do Trabalho';
CLDferiados[9][7] = 'Independência do Brasil';
CLDferiados[10][12] = 'Nossa Senhora Aparecida';
CLDferiados[11][2] = 'Finados';
CLDferiados[11][15] = 'Proclamação da República';
CLDferiados[12][25] = 'Natal';

function Calendario(link,id,d,m,a,obj,ref,frm,method){
	this.dia = d;
	this.mes = m;
	this.ano = a;
	this.obj = obj;
	this.ref = ref;
	this.frm = frm;
	this.chaid = id;
	this.method = method;
	
	this.P=false;
	this.Pstart = 0;
	this.Pend = 0;
	this.Rend = 0;
	this.Pcount = 0;
		
	this.selectDay = d;
	
	this.selected = false;
	
	this.date = new Date();
	this.date.setFullYear(a);
	this.date.setMonth(m-1);
	this.date.setDate(d);
	
	this.header = document.createElement("th");
	
	this.cells = new Array();
	this.days = new Array();
	
	this.maxdays = function(){
		if(this.bis()){
			CLDmeses[1] = 29;
		}else{
			CLDmeses[1] = 28;
		}
		return CLDmeses[this.date.getMonth()];
	}
	
	this.bis = function(){
		var a = this.date.getFullYear();
		return !((!(a%100)?a/100:a)%4);
	}
	

	this.show = function(){
		var i,j;
		var tab = document.createElement("table");
		var hdr = document.createElement("tr");
		var dvs;
		var tvs;
		
		dvs = document.createElement("th");
		dvs.calendario = this;
		dvs.onclick = this.previosMes;
		tvs = document.createTextNode("<<<");
		dvs.appendChild(tvs);
		hdr.appendChild(dvs);
		
		this.header = document.createElement("th");
		this.header.setAttribute("colspan","5");
		tvs = document.createTextNode(CLDmes[this.date.getMonth()]+"/"+this.date.getFullYear());
		this.header.appendChild(tvs);
		hdr.appendChild(this.header);
		
		dvs = document.createElement("th");
		dvs.calendario = this;
		dvs.onclick = this.nextMes;
		tvs = document.createTextNode(">>>");
		dvs.appendChild(tvs);
		hdr.appendChild(dvs);
		
		tab.appendChild(hdr);
		hdr = document.createElement("tr");
		for(i=0;i<CLDsemana.length;i++){
			dvs = document.createElement("th");
			tvs = document.createTextNode(CLDsemana[i]);
			dvs.appendChild(tvs);
			hdr.appendChild(dvs);
		}
		tab.appendChild(hdr);
		
		var row;
		for(i=0;i<6;i++){
			row = document.createElement("tr");
			this.cells[i] = new Array();
			for(j=0;j<7;j++){
				this.cells[i][j] = document.createElement("td");
				if(j==0){
					this.cells[i][j].setAttribute("class","feriado");
				}
				row.appendChild(this.cells[i][j]);
			}
			tab.appendChild(row);
		}
		obj.appendChild(tab);
		
		this.updateMes();
		
	}
	
	this.updateMes = function(){
		var c;
		this.date.setDate(1);
		c = this.date.getDay();
		this.days = new Array();
		this.header.innerHTML = CLDmes[this.date.getMonth()]+"/"+this.date.getFullYear();
		
		this.updateForm();
		
		for(i=0;i<6;i++){
			for(j=0;j<7;j++){
				this.cells[i][j].innerHTML = "";
				this.cells[i][j].setAttribute("title","");
				this.cells[i][j].setAttribute("opt","false");
				this.cells[i][j].setAttribute("hover","false");
				this.cells[i][j].onselectstart=function(){return false};
				this.cells[i][j].onmousedown=function(){return false};
				if(j!=0){
					this.cells[i][j].setAttribute("class","");
				}
			}
		}
		var cl;
		for(var i=0;i<this.maxdays();i++){
			this.date.setDate(i+1);
			cl = this.cells[parseInt((c+i)/7)][this.date.getDay()];
			cl.innerHTML = i+1;
			this.days[i] = cl;
			if(CLDferiados[this.date.getMonth()+1][i+1]){
				cl.setAttribute("class","feriado");
				cl.setAttribute("title",CLDferiados[this.date.getMonth()+1][i+1]);
			}
			cl.day = i+1;
			cl.opted = false;
			cl.calendario = this;
			cl.onclick = this.select;
			cl.onmousedown = this.md;
			cl.onmouseup = this.mu;
			cl.onmouseover = this.mo;
			cl.onselectstart=function(){return false};
		}
		if(this.dia<=this.maxdays()){
			this.date.setDate(this.dia);
		}
		if(this.method==0){
			go(link+"chacaras/pages/dias.php?id="+this.chaid+"&ano="+this.date.getFullYear()+"&mes="+(this.date.getMonth()+1),this.ref+".getdias");
		}
	}
	
	this.nextMes = function(){
		if(this.calendario.date.getMonth()<12){
			if(this.calendario.date.getDate()>CLDmeses[this.calendario.date.getMonth()+1]){
				this.calendario.date.setDate(CLDmeses[this.calendario.date.getMonth()+1]);
			}
			this.calendario.date.setMonth(this.calendario.date.getMonth()+1);
			this.calendario.updateMes();
		}else{
			this.calendario.date.setFullYear((this.calendario.date.getFullYear()+1));
			this.calendario.date.setMonth(0);
			this.calendario.updateMes();
		}
	}
	
	this.previosMes = function(){
		if(this.calendario.date.getMonth()>0){
			if(this.calendario.date.getDate()>CLDmeses[this.calendario.date.getMonth()-1]){
				this.calendario.date.setDate(CLDmeses[this.calendario.date.getMonth()-1]);
			}
			this.calendario.date.setMonth(this.calendario.date.getMonth()-1);
			this.calendario.updateMes();
		}else{
			this.calendario.date.setFullYear(this.calendario.date.getFullYear()-1);
			this.calendario.date.setMonth(11);
			this.calendario.updateMes();
		}
	}
	
	this.generateMapDays = function(){
		var t="";
		var i=0;
		for(i=0;i<this.maxdays();i++){
			if(this.days[i].opted){
				t=t+"1";
			}else{
				t=t+"0";
			}
		}
		this.selectDay = t;
	}
	
	this.complete = function(){
		var i=0;
		var c=0;
		if(this.Pstart!=this.Pend){
			var st = Math.min(this.Pstart,this.Pend);
			var fn = Math.max(this.Pstart,this.Pend);
			for(i=0;i<this.maxdays();i++){
				if(this.days[i].opted){
					this.days[i].opted = false;
					this.days[i].setAttribute("opt","false");
				}
				this.days[i].setAttribute("hover","false");
			}
			for(i=st;i<=fn;i++){
					this.days[i].opted = true;
					this.days[i].setAttribute("opt","true");
					c++;
			}	
			if(this.selected){
				this.selected.setAttribute("selected","false");
			}
			this.selected = this.days[st];
			this.days[st].setAttribute("selected","true");
			this.Pcount = c;
			this.updateForm();
		}else{
			for(i=0;i<this.maxdays();i++){
				if((this.days[i].opted)&&(i!=this.Pstart)){
					this.days[i].opted = false;
					this.days[i].setAttribute("opt","false");
				}
			}
			this.Pcount = 1;
			this.updateForm();
		}
	}
	
	this.Rcomplete = function(){
		var i=0;
		if(this.Pstart!=this.Rend){
			var st = Math.min(this.Pstart,this.Rend);
			var fn = Math.max(this.Pstart,this.Rend);
			for(i=0;i<this.maxdays();i++){
				this.days[i].setAttribute("hover","false");
			}
			for(i=st;i<=fn;i++){
					this.days[i].opted = true;
					this.days[i].setAttribute("hover","true");
			}	
		}
	}
	
	this.mo = function(){
		if(this.calendario.method==1){
			if(this.calendario.P){
				this.calendario.Rend = this.day-1;
				this.calendario.Rcomplete();
			}
		}
	}
	
	this.md = function(){
		if(this.calendario.method==1){
			this.calendario.Pstart = this.day-1;
			this.calendario.P = true;
		}
		return false;
	}
	
	this.mu = function(){
		if(this.calendario.method==1){				
			this.calendario.Pend = this.day-1;
			this.calendario.complete();
			this.calendario.P = false;
		}		
	}
	
	this.select=function(){
	
		if(this.calendario.selected){
			this.calendario.selected.setAttribute("selected","false");
		}
		this.calendario.selected = this;
		this.setAttribute("selected","true");
		this.opted = !this.opted;
		this.setAttribute("opt",this.opted?"true":"false");
		
		if(this.calendario.method==0){
			this.calendario.generateMapDays();
		}else if(this.calendario.method==1){
			for(i=0;i<this.calendario.maxdays();i++){
				this.calendario.days[i].setAttribute("hover","false");
			}
		}
		this.calendario.updateForm();
		
	}

	this.getdias = function(ds){
		var i,k;
		for(i=0;i<ds.length;i++){
			k=(ds.charAt(i)=='1');
			this.days[this.maxdays()-ds.length+i].opted=k;
			this.days[this.maxdays()-ds.length+i].setAttribute("opt",k?"true":"false");
		}
		this.generateMapDays();
	}
	
	this.updateForm = function(){
		if(this.method==0){
			this.frm.dias.value= this.selectDay;
			this.frm.mes.value = this.date.getMonth()+1;
			this.frm.ano.value = this.date.getFullYear();
		}else if(this.method==1){
			if(this.selected){
				this.frm.data.value = (this.selected.day<10?"0"+this.selected.day:this.selected.day)+"/"+((this.date.getMonth()+1)<10?('0'+(this.date.getMonth()+1)):(this.date.getMonth()+1))+"/"+this.date.getFullYear();
			}else{
				this.frm.data.value = (this.dia<10?"0"+this.dia:this.dia)+"/"+((this.date.getMonth()+1)<10?('0'+(this.date.getMonth()+1)):(this.date.getMonth()+1))+"/"+this.date.getFullYear();
			}
			this.frm.dias.value = this.Pcount;
		}
	}

}
