/*  
 *             CREDITS
 *
 * 		Slider - Javascript rollon div and menu
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.00
 *		Contact : http://www.danielecontarino.it
 *
 */
 
function Slider (time, delay){
	this.time = time
	this.delay = delay;
	this.currentItem = null;
	this.previousItem = null;
	this.sliderOpen = Array();
	
	this.appendChildSlider = function (slider){
		this.childSlider.append(slider);
	}

	this.open = function (element, parent) {
		if(this.currentItem != null) this.previousItem = this.currentItem;

		//se ho cliccato per la seconda volta sul menu
		if(this.currentItem == this.getObject(element)){
			//Chiudo tutto
			while(this.sliderOpen.length > 0) 
				this.rapidClose(this.sliderOpen.pop());

			this.currentItem = null;
			return;
		}

		this.currentItem = this.getObject(element);
		parent = this.getObject(parent);

		//Se non sono in un sottomenu
		if(parent== null){
			//Chiudo tutti
			while(this.sliderOpen.length > 0) 
				this.rapidClose(this.sliderOpen.pop());
			
		}else if(parent != this.previousItem){
			//nascondo il vecchio menu
			do{
				//Ottengo l'ultimo menu aperto
				aux = this.sliderOpen.pop();
				
				//Se l'ultimo menu aperto non è quello padre ( quindi è un figlio ) lo chiudo, altrimenti lo rimetto al suo posto
				if( parent != aux) this.slideUp(aux);
				else this.sliderOpen.push(aux);
			}while(parent != aux);
				
		}
		
		//Aggiungo lo slider alla lista dei slider aperti
		this.sliderOpen.push(this.currentItem);
		
		//mostro il nuovo Menu
		this.slideDown();
	}
		
	this.slideDown = function (element){
		if(typeof( element ) == "undefined" ) element = this.currentItem;

		this.currentItem.style.display = "inline-table";
		maxHeight = (parseInt(this.currentItem.style.height).toString()  != "NaN")? parseInt(this.currentItem.style.height) : this.currentItem.clientHeight;
		this.currentItem.style.height= "0px";
		step = (maxHeight / time) * delay;
		this.moveDown(this.currentItem, step, maxHeight, step);
	}

	this.slideUp = function (element){
		if(typeof( element ) == "undefined" ) element = this.previousItem;

		element.style.overflow = "hidden";
		maxHeight = (parseInt(element.style.height).toString()  != "NaN")? parseInt(element.style.height) : element.clientHeight;
		step =  maxHeight/time  * delay;
		
		this.moveUp(this.previousItem, maxHeight, maxHeight, step );
	}

	this.moveDown = function (element, currentHeight, maxHeight, step){
		var _self = this;		
		if(currentHeight < maxHeight){
			currentHeight += step;
			element.style.height= currentHeight +"px";
			setTimeout(function(){_self.moveDown(element, currentHeight, maxHeight, step); }, _self.delay);

		}else{
			element.style.height= "auto";
			element.style.overflow = "visible";

		}
	}

	this.moveUp = function (element, currentHeight, maxHeight, step){
		var _self = this;		

		if(currentHeight > 0){
			currentHeight -= step;
			element.style.height= currentHeight +"px";
			setTimeout(function(){_self.moveUp(element, currentHeight, maxHeight, step); }, _self.delay);

		}else if(element != null){
			element.style.display="none";
			element.style.overflow = "hidden";
			element.style.height= maxHeight +"px";			
		}
	}
	
	this.rapidClose = function (element){
		maxHeight = (parseInt(element.style.height).toString()  != "NaN")? parseInt(element.style.height) : element.clientHeight;

		element.style.display="none";
		element.style.overflow = "hidden";
		element.style.height= maxHeight +"px";
	}
	
	this.getObject = function (element){
		if(typeof element == "object") return element;
		else if(typeof conteiner != "null")return document.getElementById(element);
		else return null;
	
	}

}

