﻿/*
Ressource:
http://blog.tamalwhite.com/2007/3/6/re-how-to-create-digg-comment-style-sliding-divs-with-javascript-and-css

Handler examples:
- Slide('mydiv').down();
- Slide('mydiv',{duration:2}).down();
- Slide('mydiv',{onComplete:function(){alert('The DIV is up.');}}).up();
*/

var slideInUse = new Array();
var slideAtClose = true;
var slidePadding = 0;

function loadSlider(){

	var items = document.getElementsByTagName('div');
	var current = items.length;
	for(d=0;d<current;d+=1){
		if(items[d].className!=''){
			var str = items[d].className;
			if(str.indexOf('portlet_t1')!='-1'){
				if(str.indexOf('expanded')!='-1'){
					var port_body = document.getElementById(items[d].id+'_body');
					var port_foot = document.getElementById(items[d].id+'_foot');
					if(port_body){port_body.style.display = 'block';}
					if(port_foot){port_foot.className = 'up';}
				}
				if(str.indexOf('collapsed')!='-1'){
					var port_body = document.getElementById(items[d].id+'_body');
					var port_foot = document.getElementById(items[d].id+'_foot');
					if(port_body){port_body.style.display = 'none';}
					if(port_foot){port_foot.className = 'down';}
				}
				if(str.indexOf('static')!='-1'){
					var port_foot = document.getElementById(items[d].id+'_foot');
					if(port_foot){
						port_foot.className = 'none';
						port_foot.href = 'javascript:;';
					}
				}
				items[d].onmouseover = function(){sliderMouse(this,'over');}
				items[d].onmouseout = function(){sliderMouse(this);}
			}
		}
	}

}

function slider(objId){
	var elm = objId+'_body';
	this.obj = document.getElementById(elm);
	this.up = function(){
		if(this.obj.style.display=='block'){
			Slide(elm,{onComplete:function(){footer(objId,'up');}}).up();
		}
	}
	this.down = function(){
		if(this.obj.style.display=='none'){
			Slide(elm,{onComplete:function(){footer(objId,'down');}}).down();
		}
	}
	this.toogle = function(){
		if(this.obj.style.display=='block'){
			Slide(elm,{onComplete:function(){footer(objId,'up');}}).up();
		}
		if(this.obj.style.display=='none'){
			Slide(elm,{onComplete:function(){footer(objId,'down');}}).down();
		}
	}
	return this;
}

function footer(elm,command){
	var port_foot = document.getElementById(elm+'_foot');
	if(port_foot){
		if(command == 'up'){port_foot.className = 'down';}
		if(command == 'down'){port_foot.className = 'up';}
	}
}

function Slide(objId, options){
	this.obj = document.getElementById(objId);
	this.duration = 0.4;
	
	// Get container height (modified workaround)
	if(!slideAtClose){
		this.obj.style.display = 'block';
		this.height = parseInt(this.obj.offsetHeight-slidePadding);
		this.obj.style.display = 'none';
	}else{
		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 = '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+"']);document.getElementById('"+objId+"').style.height='auto';",tIncrement * i);
		
		if(this.options.onComplete){
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
			// this.obj.style.height = 'auto';
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height){
		this.obj.style.height = height + 'px';
		this.obj.style.display = 'none';
	}
	
	return this;
}

var sliderIsOver = false;
var sliderOverTimer;

function sliderOver(f,state){
	var port = document.getElementById(f);
	var elm = document.getElementById(f+'_foot');
	var portBox = port.className;
	if(elm){
		if(portBox.indexOf('static')=='-1'){
			if(state=='over'){
				elm.style.backgroundColor = '#ddeefb';
				sliderIsOver = true;
				sliderOverTimer = setTimeout("sliderAuto('"+f+"');", 500);
			}
			if(state=='out'){
				elm.style.backgroundColor = '';
				sliderIsOver = false;
			}
		}
	}
}

function sliderAuto(f){
	var port_body = document.getElementById(f+'_body');
	if(sliderIsOver){
		sliderIsOver = false;
		clearTimeout(sliderOverTimer);
		if(port_body.style.display=='none' || port_body.style.display==''){
			slider(f).down();
		}
	}
}

function sliderMouse(f,state){
	var port = document.getElementById(f.id);
	var port_body = document.getElementById(f.id+'_body');
	var port_foot = document.getElementById(f.id+'_foot');
	if(port && port_body && port_foot){
		if(state=='over'){
			var str = port.className;
			if(str.indexOf('nomouse')=='-1'){
				if(port_body.style.display == 'block' || port_body.style.display == ''){
					port.style.backgroundColor = '#ddeefb';
					port.style.cursor = 'pointer';
					port_foot.style.backgroundColor = '#ddeefb';
				}
			}
		}else{
			port.style.backgroundColor = '';
			port.style.cursor = '';
			port_foot.style.backgroundColor = '';
		}
	}
}

function sliderGoTo(f,url){
	var port = document.getElementById(f.id);
	if(port){
		if(port.style.display == 'block' || port.style.display == ''){
			goToUrl('',url);
		}
	}
}