//********************************************************
//********************************************************
//**
//** CLASS :: TDRAWER
//**
//********************************************************
//********************************************************

function TDrawer(){
	

	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@
	//@@  ATTRIBUTES
	//@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	// private

	this.drawers = new Array();
	this.animation = false;
	this.id = "";
	this.next_id = "";
	this.ts = new Date().getTime();
	this.nstep = 0;
	this.cspeed = 0;


	// public

	this.prefix = "drawer_"; // prefisso da usare per gli elementi del drawer				
	this.speed = 45; // velocitą animazione
	this.height = 400; // altezza del drawer
	this.open = ""; // id del drawer da aprire all'inizio se "" doInit lo imposta al primo che trova
	this.resetTime = 10000; // dopo resetTime ms... si riapre il drawer this.open

	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@
	//@@  METHODS
	//@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


	//-----------------------------------------------------------------------
	// toggleBox
	//-----------------------------------------------------------------------

	this.toggleBox = function(id, iState) // 1 visible, 0 hidden
	{
	   if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			var el = document.getElementById(id);
			if (el!=null){
				// el.style.display = iState ? "block" : "none";
				el.style.display = iState ? "" : "none";	// per problemi di compatibilitą con Fire Fox, anzichč metter block
															// non si mette nulla in modo che ogni browser usa il suo default.
			}		
		}
		
	}

	//-----------------------------------------------------------------------
	// doInit
	//-----------------------------------------------------------------------

	this.doInit = function(){

		this.ts = new Date().getTime();

		this.cspeed = this.speed;

		var first = this.open; // di default vale ""
		var last = this.open; // di default vale ""
		
		// determino tuti i div con id del tipo drawer_*
		var items = document.getElementsByTagName("div");			
		var c = 0;
		for (var i = 0; i<items.length; i++){
			
			var idvalue = items[i].getAttribute('id');
			if (idvalue){
				if (idvalue.substring(0,this.prefix.length)==this.prefix){
					
					if (idvalue!=this.open){
						items[i].style.height="1px";
						items[i].style.overflow="hidden";
						this.toggleBox(idvalue,0);
						this.toggleBox("tr_"+idvalue,0);
					}else{
						items[i].style.height=this.height;
						items[i].style.overflow="hidden";
						this.toggleBox(idvalue,1);
						this.toggleBox("tr_"+idvalue,1);
					}
					

					this.drawers[c] = idvalue;
					
					c++;
					
					if (first==""){
						first=idvalue;
					}

					last=idvalue;
				}		
			}
		}

		//if (this.open==''){
			//this.open=last;
		//}

		if (c>0){
			
			this.doOpen(this.open);

			if (this.resetTime!=0){
				
					var self = this;
					window.setInterval(function() { self.doReset() },100);
			}				

		}
		
	}


	//-----------------------------------------------------------------------
	// doStep
	//-----------------------------------------------------------------------


	this.doStep = function(){
			
		if (this.animation){
			
			this.ts = new Date().getTime();

			if (this.nstep>((this.height/this.speed)*(1/2))){

				this.cspeed = Math.floor((this.speed*1)*(1/(this.nstep+1)));
				if (this.cspeed<=0){
					this.cspeed=1;
				}

			}

			this.animation=false;
			for (var i=0; i<this.drawers.length;i++){

				var el = document.getElementById(this.drawers[i]);
				var h = parseInt(el.style.height.replace("px",""));		

				if (this.id==this.drawers[i]){
					this.nstep++;																				
					
					if (h<this.height){

						if (this.nstep==1){
							this.toggleBox(this.drawers[i],1);
							this.toggleBox("tr_"+this.drawers[i],1);

							h+=this.cspeed-2;

						}else{
							
							h+=this.cspeed;						

							if (h>this.height) h=this.height;							
						}
						this.animation=true;
					}
					
				}else{
					
					if (h>1){
						
						h-=this.cspeed;						


						if (h<1) h=1;
						this.animation=true;						
					}
					
					if(h==1){
						this.toggleBox(this.drawers[i],0);
						this.toggleBox("tr_"+this.drawers[i],0);
					}					
					
					
				}

				el.style.height=h+"px";
				
			}

			var self = this;
			window.setTimeout(function() { self.doStep() },20);

		}else{
			this.nstep = 0;
			this.cspeed = this.speed;

			if (this.next_id!=''){
				
				this.id = this.next_id;
				this.next_id = '';		
				
				this.animation = true;

				var self = this;
				window.setTimeout(function() { self.doStep() },20);
				

			}

		}
	}


	//-----------------------------------------------------------------------
	// doAnimation
	//-----------------------------------------------------------------------

	this.doAnimation = function(id){

		if (id!=this.id){
			
			if (!this.animation){
			
				this.id = id;
				this.animation = true;

				this.nstep=0;
				this.cspeed=this.speed;
				
				var self = this;
				window.setTimeout(function(){ self.doStep(); },10);

			}else{

				this.next_id = id;

			}

		}
		
	}

	//-----------------------------------------------------------------------
	// doAbort
	//-----------------------------------------------------------------------

	this.doAbort = function(id){
			
		this.next_id = '';
		
	}

	//-----------------------------------------------------------------------
	// doOpen
	//-----------------------------------------------------------------------

	this.doOpen = function(id){

		
		var el = document.getElementById(id);
		if(el){
			var h = this.height;
			el.style.height=h+"px";

			this.toggleBox(id,1);
			this.toggleBox("tr_"+id,1);
		}
		
	}

	//-----------------------------------------------------------------------
	// doReset
	//-----------------------------------------------------------------------

	this.doReset = function(){

		var elapsed = new Date().getTime()-this.ts;
		if (this.resetTime!=0){
		
			
			if (elapsed>this.resetTime){

				this.ts =new Date().getTime();
				this.doAnimation(this.open);

			}
			

		}
		
	}

	
	
}

