/*********************/
/* Objecte calendari */
/*********************/
function Calendario( divPadre, objSelector ){																				

    this.objSelector = objSelector;
	
	this.locale = new Array();	
	this.locale["meses"]=new Array('enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre');    
	this.locale["dias"]=new Array( "L", "M", "X", "J", "V", "S", "D" );	

	if( objSelector ){
		if( objSelector.getLocale()["meses"] ){
			this.locale["meses"]=objSelector.getLocale()["meses"];
		}
		if( objSelector.getLocale()["dias"] ){
			this.locale["dias"]=objSelector.getLocale()["dias"];
		}
	}
	
	this.obj=document.createElement('table');
	this.obj.cellPadding=0;
	this.obj.cellSpacing=1;
	this.obj.border=0;
	this.obj.className='calendario';
    
    this.mes;
    this.anyo;
	
    var thead, tr, th, tbody, td, a, fecha, boton;
    
    //cream caption
    caption=document.createElement('caption');
    this.obj.appendChild(caption);				
    caption.appendChild(document.createTextNode(''));
    
	thead=document.createElement('thead');
	this.obj.appendChild(thead);
	
	tr=document.createElement('tr');
	thead.appendChild(tr);				
	
	for(var dia=0; dia<7; dia++){		
		th=document.createElement('th');
		th.appendChild( document.createTextNode( this.locale["dias"][ dia ] ) );			
		if(dia==6){
			th.className='domingo';
		}
		tr.appendChild(th);
	}				
		
	var tbody=document.createElement('tbody');
	this.obj.appendChild(tbody);									

	for(var sem=0; sem<6; sem++){
		tr=document.createElement('tr');
		for( var dia=0; dia<7; dia++ ){
			td=document.createElement('td');
			td.setAttribute("valign","top");
			
			if(dia==6){
				td.className='domingo';
			}								
			tr.appendChild(td);
			
			a=document.createElement('a');
			a.appendChild( document.createTextNode( '' ) );			
			a.id="";
			a.href="";
			td.appendChild( a );
		}
		tbody.appendChild(tr);
	}				
	
	divPadre.appendChild( this.obj );				
	
	//--------------------------------------------------------------------------------
	
	this.borrar=function(){
		var td, a;
		
		for( var sem=0; sem<6; sem++ ){
			for( var dia=0; dia<7; dia++ ){						
				td=tbody.childNodes[sem].childNodes[dia];
				a=td.childNodes[0];					
				a.id="";
				a.href="";
				a.className="";
				a.childNodes[0].nodeValue=" ";								
			}
		}
	}
	
	//--------------------------------------------------------------------------------
	
	this.setLocale=function( locale ){
		this.locale=locale;
	}
	
	//--------------------------------------------------------------------------------
	
	this.setMesAnyo=function( mes, any ){
    
        //guardem més i any dins l'objecte
        this.mes = mes;
        this.anyo= any;								
    
        var numDia=1;
		var caption=this.obj.childNodes[0];
		var tbody=this.obj.childNodes[2];
		var td, a, fecha;	
		var id, clase;
		var html;		
        
        //fixem el nom del mes i l'any al calendari
        caption.innerHTML= this.locale["meses"][this.mes]+" <strong>"+this.anyo+"</strong>";
        	
        //borra el contingut de les cel.les i els links
		this.borrar();
        
        //fixem el dia 1 del mes
        var fecha = new Date(any,mes,1,0,0,0,0);
        
		// Obtenemos el array de dias marcados.	
		var diasMarcados = this.objSelector.diasMarcados;
		
		var diaInicio = fecha.getDay()-1;
		if(diaInicio==-1){
			diaInicio=6;
		}
		
		while(fecha.getMonth()==mes){			
			for( var sem=0; sem<6; sem++ ){
				for( var dia=diaInicio; dia<7 && fecha.getMonth()==mes; dia++ ){						
                    td=tbody.childNodes[sem].childNodes[dia];
					
					id=fecha.toDMA();
					
					html='<a';
					
					//si esteim al dia actual, verifiquem l'hora límit per fer reserves
					if (fecha.getDate()==objSelector.fechaActual.getDate() && fecha.getMonth()==objSelector.fechaActual.getMonth() && fecha.getFullYear()==objSelector.fechaActual.getFullYear()){
					    
					    //data auxiliar per agafar l'hora actual
                        var horaActual = new Date();
                        fecha.setHours(horaActual.getHours(),horaActual.getMinutes(),horaActual.getSeconds(),horaActual.getMilliseconds());
                        
                        //fixem la data limit al dia actual
                        objSelector.fechaActual.setHours(14,0,0,0);      
                        
                        //si el l'hora és major que l'hora limit el dia ja ha passat
                        diaPassat = fecha.getTime()>=objSelector.fechaActual.getTime();
                        
					    //tornem a fixar les hores a 0
					    fecha.setHours(0,0,0,0);
					    objSelector.fechaActual.setHours(0,0,0,0);
					    
					}else{
					    
					    diaPassat = false;
					    
					}

					if( (fecha.compareTo( objSelector.fechaActual ) < 0) || diaPassat){
					    html+=' class="diaPasado"';						
					}else if( fecha.compareTo( objSelector.fechaSeleccionada ) == 0 && diasMarcados[id] == "disponible" ){
						html+=' class="diaSeleccionado"';
						html+=' id="'+id+'"';
						html+=' href="javascript:void(0)" onclick="this.oFather.setDia(\''+fecha.toDMA()+'\')"';
					}else if( diasMarcados[id]=="reservado" ){
						html+=' class="diaReservado"';						
					}else if( diasMarcados[id]=="disponible" ){
						html+=' class="diaDisponible"';
						html+=' id="'+id+'"';
						html+=' href="javascript:void(0)" onclick="this.oFather.setDia(\''+fecha.toDMA()+'\')"';
					}else{
						html+=' class="dia"';						
					}
					td.innerHTML=html+'>'+fecha.getDate()+'</a>';
																
					//assignem event al tag <a></a>	                
					if( ( objA = document.getElementById(fecha.toDMA()) ) ){
		                objA.oFather=this.objSelector;					                                        
					}
					
					fecha.setDate( ++numDia );
					diaInicio=0;
				}
			}
			fecha.setDate( numDia++ );
		}
	}
}