Function.prototype.bind = function(object, arguments) {
  var method = this;
  var args = arguments;
  return function (){
  	return method.apply(object, args);
  };
}


function ZoomEffect(element){
	this.toTimer;
	this.intTimer;
	this.element = element;
	this.zoomincrement=2;
	this.waittime=10; //mili seconds
		
	this.cancel = function(){
//		alert('canceling timers');
		clearTimeout(this.toTimer);
		clearInterval(this.intTimer);
	}

	this.zoomHeightTo = function(height){
//		alert(this.element.className);
//		alert("obj h:"+this.element.style.height+" arg height:"+height);
		if(!this.element.style.height && this.element.height){
			this.element.style.height=""+this.element.height;
		}
		this.cancel();
		if(parseInt(this.element.style.height)>parseInt(height)){ //300 > 900, make small (zoom out)
//			alert('1');
			this.intTimer = setInterval(this.zoomOut.bind(this,[null,height]),this.waittime);
		}else if(parseInt(this.element.style.height)<parseInt(height)){ // 300 < 900, inlarge (zoom in)
//			alert('2');
			this.intTimer = setInterval(this.zoomIn.bind(this,[null,height]),this.waittime);
		}else{
			return;
		}
	}		
	
	this.zoomTo = function(type, width, height){
		if(!this.element.style.width && this.element.width){
			this.element.style.width=""+this.element.width;
		}	
		if(!this.element.style.height && this.element.height){
			this.element.style.height = ""+this.element.height;
		}
		this.cancel();
		if(parseInt(this.element.style.width)>width){ //300 > 900, make small (zoom out)
			switch(type){
				case "ps":				
				this.intTimer = setInterval(this.zoomOut.bind(this,[width,height]),this.waittime);
				break;
				case "sp":
				this.intTimer = setInterval(this.zoomSPOut.bind(this,[width,height]),this.waittime);		
				break;				
			}
		}else if(parseInt(this.element.style.width)<width){ // 300 < 900, inlarge (zoom in)
			switch(type){
				case "ps":			
				this.intTimer = setInterval(this.zoomIn.bind(this,[width,height]),this.waittime);
				break;
				case "sp":
				this.intTimer = setInterval(this.zoomSPIn.bind(this,[width,height]),this.waittime);
				break;
			}
		}else{
			return;
		}
	}
	
	this.zoomWidthTo = function(width){
		if(!this.element.style.width && this.element.width){
			this.element.style.width=""+this.element.width;			
		}	
		this.cancel();
		if(parseInt(this.element.style.width)>width || parseInt(this.element.width)>width){ //300 > 900, make small
			this.intTimer = setInterval(this.zoomOut.bind(this,[width]),this.waittime);
		}else if(parseInt(this.element.style.width)<width || parseInt(this.element.width)<width){ // 300 < 900, inlarge (zoom in)
			this.intTimer = setInterval(this.zoomIn.bind(this,[width]),this.waittime);
		}else{
			return;
		}
	}	

	this.zoomTo = function(type, width, height){
		if(!this.element.style.width && this.element.width){
			this.element.style.width=""+this.element.width;
		}	
		if(!this.element.style.height && this.element.height){
			this.element.style.height = ""+this.element.height;
		}
		this.cancel();
		if(parseInt(this.element.style.width)>width){ //300 > 900, make small (zoom out)
			switch(type){
				case "ps":				
				this.intTimer = setInterval(this.zoomOut.bind(this,[width,height]),this.waittime);
				break;
				case "sp":
				this.intTimer = setInterval(this.zoomSPOut.bind(this,[width,height]),this.waittime);		
				break;				
			}
		}else if(parseInt(this.element.style.width)<width){ // 300 < 900, inlarge (zoom in)
			switch(type){
				case "ps":			
				this.intTimer = setInterval(this.zoomIn.bind(this,[width,height]),this.waittime);
				break;
				case "sp":
				this.intTimer = setInterval(this.zoomSPIn.bind(this,[width,height]),this.waittime);
				break;
			}
		}else{
			return;
		}
	}
	this.zoomSPIn = function(width, height){	
		if(height){
			if(parseInt(this.element.style.height)<height){
				this.element.style.height = parseInt(this.element.style.height) + this.zoomincrement;
				return;
			}
		}
		if(width){	
			if(parseInt(this.element.style.width)<width){
				this.element.style.width = parseInt(this.element.style.width) + this.zoomincrement;
				return;
			}
		}
		clearInterval(this.intTimer);
	}
	this.zoomSPOut = function(width, height){
		if(height){
			if(parseInt(this.element.style.height)>height){
				this.element.style.height = parseInt(this.element.style.height) - this.zoomincrement;
				return;
			}
		}
		if(width){
			if(parseInt(this.element.style.width)>width){
				this.element.style.width = parseInt(this.element.style.width) - this.zoomincrement;
				return;
			}
		}
		
		clearInterval(this.intTimer);
	}	
	this.zoomIn = function(width, height){	
		if(width){
			if(this.element.style.width != ""){
				if(parseInt(this.element.style.width)<width){
					this.element.style.width = parseInt(this.element.style.width) + this.zoomincrement;
					this.element.width = parseInt(this.element.style.width) + this.zoomincrement;				
					return;
				}
			}else{
				if(parseInt(this.element.width)<width){
					this.element.style.width = parseInt(this.element.width) + this.zoomincrement;
					this.element.width = parseInt(this.element.width) + this.zoomincrement;				
					return;
				}
			}
		}
		if(height){
			if(parseInt(this.element.style.height)<parseInt(height)){
				this.element.style.height = parseInt(this.element.style.height) + this.zoomincrement;
				return;
			}
			
		}	
		clearInterval(this.intTimer);
	}
	this.zoomOut = function(width, height){		
		if(width){
			if(this.element.style.width!=""){
				if(parseInt(this.element.style.width)>width){				
					var zoom = parseInt(this.element.style.width) - this.zoomincrement;
					if(zoom<width) zoom = width;
					this.element.style.width = zoom;
					return;
				}
			}else{
				if(parseInt(this.element.width)>width){				
					var zoom = parseInt(this.element.width) - this.zoomincrement;
					if(zoom<width) zoom = width;
					this.element.width = zoom;
					return;
				}				
			}
		}
		if(height){
			if(parseInt(this.element.style.height)>parseInt(height)){
				var zoom = parseInt(this.element.style.height) - this.zoomincrement;
				if(zoom<height) zoom = height;
				this.element.style.height = zoom;
				
				return;
			}
		}
		clearInterval(this.intTimer);
	}
}

function loadMenu(name, minh, offset){
	maxh=150;//default backup
	//generate default pan height
	var ml = getElementsById('ml');
	var mh = getElementsById('mh');	
	var m = getElementsById('m');
	for(i=0;i<m.length;i++){
		m[i].setAttribute("n",i);
		var divs = ml[i].getElementsByTagName("DIV");
		var total = minh * divs.length;
		m[i].setAttribute('ph',total+offset);
		//get cookie d
		var cname = name+"-m-"+i;
		var d = parseInt(getCookie(cname));
		switch(d){
			case 1: //down
			m[i].style.height=total+offset;			
			break;
			
			case 0: //up
			m[i].style.height=minh;
			mh[i].firstChild.src = "../images/arrowsup.jpg";
			break;
			
			default: //down
			m[i].style.height=total+offset;			
		}
	}
	
	//load event triggers	
	for(i=0;i<m.length;i++){
		var tmp = m[i];
		tmp.ze = new ZoomEffect(tmp);
		tmp.ze.zoomincrement=5;
		for(k=0;k<tmp.childNodes.length;k++){
			var c = tmp.childNodes[k];
			if(c.className=="mh"){
				c.onclick = function(e, c) {
					if(parseInt(this.parentNode.style.height)>minh){ //close 
						this.firstChild.src = "../images/arrowsup.jpg";
						this.parentNode.ze.zoomHeightTo(minh); 
						setCookie(name+"-m-"+this.parentNode.getAttribute('n'), "0", 365);
					}else{ //open
						this.firstChild.src = "../images/arrowsdown.jpg";
						setCookie(name+"-m-"+this.parentNode.getAttribute('n'), "1", 365);							
						if(this.parentNode.getAttribute('ph')){ 
							this.parentNode.ze.zoomHeightTo(parseInt(this.parentNode.getAttribute('ph')));
						}else{
							this.parentNode.ze.zoomHeightTo(maxh);
						}
					}
				};//end onclick handler
			}
		}
	}
}

