/**
* Kalender Klasse
* Weisst den SelectBoxen und den Hidden Felder das Datum zu
*
*/
function SearchBuerstnerKalender(){

	this.first 			= 0;
	this.second 		= 0;
	this.classes 		= new Array();
	this.elem_doc		= document.getElementById('SearchBuerstner_auswahl_kalender').getElementsByTagName('TD');
	this.hidden_from 	= $('SearchBuerstner_from');
	this.hidden_to 		= $('SearchBuerstner_to');
	
	/**
	* Ertmitteln der Farbwerte der einzelnen Tage
	*/
	this.fetchClasses = function(){
		for(i=0;i<this.elem_doc.length;i++){
			if(this.elem_doc[i].id != ''){
				//id = this.elem_doc[i].id.replace('td_','');
				//this.elem_doc[i].id = id;
				this.classes[this.elem_doc[i].id] = new Object;
				this.classes[this.elem_doc[i].id]['color']	= $(this.elem_doc[i].id).getStyle('background-color');
				this.classes[this.elem_doc[i].id]['class']	= $(this.elem_doc[i].id).classNames();
			}
		}
	}
	
	/**
	* Setzen des ersten Klicks
	*/
	this.setFirst = function(id){
		this.first = id;
		this.setFirstColor();
	}
	
	/**
	* Setzen des zweiten Klicks
	*/
	this.setSecond = function(id){
		this.second = id;
	}
	
	/**
	* Überprüfen ob der erste oder zweiter Klick größer ist
	*/
	this.checkFirstSecond = function(){
		if(this.first > this.second && this.second > 0){
			tmp 		= this.second;
			this.second = this.first;
			this.first 	= tmp;
		}
	}
	
	/**
	* Wenn der erste und der zweite Klick vollzogen ist wird der Zeitraum makiert
	* falls ein belegte Tag innerhalb des Zeitraum sich befindet wird der Zeitraum zurückgesetzt
	*/
	this.setZeitraum = function(){

		for(i=0;i<this.elem_doc.length;i++){
			if(this.elem_doc[i].id >= this.first && this.elem_doc[i].id <= this.second){
				
				if($(this.elem_doc[i].id).classNames()  == 'belegt'){
					this.setBack();
					break;
				}
				$(this.elem_doc[i].id).setStyle({backgroundColor: '#B16A14'});
			}else {
				if(this.elem_doc[i].id != ''){
					farbe = this.classes[this.elem_doc[i].id]['color'];
					$(this.elem_doc[i].id).setStyle({backgroundColor: farbe});
				}
			}
		}
		// Ausklappen des Kalenders
		if(this.second != 0){
			do{
				i--;
			}while (this.elem_doc[i].id == '');
			
			if(this.second < this.elem_doc[i].id){
				eltern = $(this.second).ancestors();
			} else {
				eltern = $(this.elem_doc[i].id).ancestors();
			}
			
			
			for(i = 0; i < eltern.length; i++ ){
				if(eltern[i].id == 'more'){
					if($('more').style.display != 'block'){
						showMore();
					}
				}
			}
		}
	}
	
	/**
	* Makiert den ersten Klick im Kalender
	*/
	this.setFirstColor = function(){
		for(i=0;i<this.elem_doc.length;i++){
			if(this.elem_doc[i].id != ''){
				if(this.elem_doc[i].id != this.first){
						farbe = this.classes[this.elem_doc[i].id]['color'];
						$(this.elem_doc[i].id).setStyle({backgroundColor: farbe});
				} else  {
					$(this.elem_doc[i].id).setStyle({backgroundColor: '#B16A14'});				
				}
			}
		}
	}
	
	/**
	* setzt die makierung des Kalenders zurück
	* setzt die Formular Felder 
	*/
	this.setBack = function(){
		alert("Der gewählte Zeitraum ist ungültig");
		for(i=0;i<this.elem_doc.length;i++){
			if(this.elem_doc[i].id != ''){
				farbe = this.classes[this.elem_doc[i].id]['color'];
				$(this.elem_doc[i].id).setStyle({backgroundColor: farbe});
			}
		}
		this.first = 0;
		this.second = 0;
	}
}
