﻿var slidePadding = 0;
var slideInUse = false;

function slider(objId){
	var elm = objId+'_body';
	this.obj = document.getElementById(elm);
	this.up = function(){
		if(this.obj.style.display=='block'){
			Slide(elm).up();
			sliderClass.up(objId);
		}
	}
	this.down = function(){
		if(this.obj.style.display=='none'){
			Slide(elm).down();
			sliderClass.down(objId);
		}
	}
	this.toogle = function(){
		if(this.obj.style.display=='block'){
			Slide(elm).up();
			sliderClass.up(objId);
		}
		if(this.obj.style.display=='none' || this.obj.style.display==''){
			Slide(elm).down();
			sliderClass.down(objId);
		}
	}
	return this;
}

function sliderCombi(open,close){
	var elmOpen = open+'_body';
	var elmClose = close+'_body';
	this.toogle = function(){
		if(document.getElementById(elmOpen).style.display!='block'){
			if(elmOpen){
				Slide(elmOpen).down();
				sliderClass.up(open);
			}
			if(elmClose){
				Slide(elmClose).up();
				sliderClass.down(close);
			}
		}
	}
	return this;
}

var sliderClass = function(){
	return {
		down:function(f){
			var obj = document.getElementById(f);
			if(obj){obj.className = 'hd up';}
		},
		up:function(f){
			var obj = document.getElementById(f);
			if(obj){obj.className = 'hd down';}
		}
	};
}();

function Slide(objId, options){
	this.obj = document.getElementById(objId);
	this.duration = 0.4;
	
	// Get container height (modified workaround)
	if(this.obj.style.display == 'none'){
		this.obj.style.display = 'block';
		this.height = parseInt(this.obj.offsetHeight-slidePadding);
		this.obj.style.display = 'none';
	}else if(this.obj.style.display == 'block' || this.obj.style.display == ''){
		this.obj.style.display = 'block';
		this.height = parseInt(this.obj.offsetHeight-slidePadding);
	}

	if(typeof options!='undefined'){this.options = options;}else{this.options = {};}
	if(this.options.duration){this.duration = this.options.duration;}
		
	this.up = function(){
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true){
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function(){
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true){
			this.obj.style.height = '1px';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function(){
		slideInUse[objId] = true;
		var frames = 30 * duration; // Running at 30 fps

		var tIncrement = (duration*1000) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++){
			if(i < frames/2){
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++){
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete){
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height){
		this.obj.style.height = height + 'px';
		this.obj.style.display = 'none';
	}
	
	return this;
}