/* 
----------------------------------------------------
BANDIT.FM 
http://www.bandit.fm
----------------------------------------------------
SLIDING FX
Author: Sony BMG Music Australia
----------------------------------------------------
*/


function curveincrement(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}

function updatePos(toExpandObject,initSize,size,startTime,direction,timelength) {
	var elapsed=new Date().getTime()-startTime //get time animation has run
	timelength = timelength*1;
	//var timelength = 200;
	
	if (elapsed<timelength){ //if time run is less than specified length
		var distancepercent = curveincrement(elapsed/timelength);
		var newSizeValue   = (direction==1)? distancepercent * size + initSize + "px" : initSize - (distancepercent * size) + "px";
		toExpandObject.style.left = newSizeValue;
		runtimer=setTimeout(function(){updatePos(toExpandObject,initSize,size,startTime,direction,timelength)}, 10);
		} else{ //if animation finished
		
			// dispatch event that the object has finished moving
			/* if( document.createEvent ) {
				var evObj = document.createEvent('HTMLEvents');
				evObj.initEvent( 'tabclose', true, false );
				toExpandObject.dispatchEvent(evObj);
				toExpandObject.removeEventListener('tabclose',openNewTab,true);
			} else if( document.createEventObject ) {
			  	toExpandObject.fireEvent('onmousemove');
				toExpandObject.detachEvent('onmousemove',openNewTab);
			} 
			 */ 
			toExpandObject.style.left=(direction==1)? size+initSize+"px" : initSize-size+"px";
			runtimer=null;
	} 
}

function slidePos(toExpandObject,height,direction,timelength) {
	this.startTime=new Date().getTime() //Set animation start time
	//var toExpandObject	= document.getElementById(layer);
	var initPos = toExpandObject.offsetLeft;
	updatePos(toExpandObject,initPos,height,startTime,direction,timelength);
}

function doFade(id,direction,faderVal) {
		direction=='out' ? faderVal -= 5 : faderVal += 5;
		if((direction=='out'&&faderVal>=0)||(direction=='in'&&faderVal<=100)) {
			var faderOpacity = faderVal/100;
			document.getElementById(id).style.filter = 'alpha(opacity='+faderVal+')';
			document.getElementById(id).style.opacity = faderOpacity;		
			setTimeout(function() { doFade(id,direction,faderVal)	}, 10);
		}
}

function fadeId(id,direction) {
	var faderVal;
	direction=='out' ? faderVal = 100 : faderVal = 0;
	doFade(id,direction,faderVal);
}

function doFadeObject(id,direction,faderVal) {
direction=='out' ? faderVal -= 10 : faderVal += 10;
if((direction=='out'&&faderVal>=0)||(direction=='in'&&faderVal<=100)) {
		var faderOpacity = faderVal/100;
		id.style.filter = 'alpha(opacity='+faderVal+')';
		id.style.opacity = faderOpacity;		
		setTimeout(function() { doFadeObject(id,direction,faderVal)	}, 10);
		} else direction=='out' ? id.style.visibility= 'hidden' : id.style.visibility= 'visible'
} 

function fadeObject(id,direction) {
	var faderVal;
	direction=='out' ? faderVal = 100 : faderVal = 0;
	doFadeObject(id,direction,faderVal);
}


function updateHeight(toExpandObject,height,startTime,direction) {
	var elapsed=new Date().getTime()-startTime //get time animation has run
	var timelength = 700;

	if (elapsed<timelength){ //if time run is less than specified length
		var distancepercent=(direction==1)? curveincrement(elapsed/timelength) : 1-curveincrement(elapsed/timelength);
	toExpandObject.style.height=distancepercent * height +"px";
	runtimer=setTimeout(function(){updateHeight(toExpandObject,height,startTime,direction)}, 10);
	} else{ //if animation finished
		toExpandObject.style.height=(direction==1)? height+"px" : 0
		var isExpanded=(direction==1)? "yes" : "no" 
		runtimer=null;
	}
}



function slideit(layer,height,direction) {
	this.startTime=new Date().getTime() //Set animation start time
	var toExpandObject	= document.getElementById(layer);
	updateHeight(toExpandObject,height,startTime,direction);

}


function updatePosVertically(toExpandObject,initSize,size,startTime,direction,timelength) {
	var elapsed=new Date().getTime()-startTime //get time animation has run
	timelength = timelength*1;
	//var timelength = 200;
	
	if (elapsed<timelength){ //if time run is less than specified length
		var distancepercent = curveincrement(elapsed/timelength);
		var newSizeValue   = (direction==1)? distancepercent * size + initSize + "px" : initSize - (distancepercent * size) + "px";
		toExpandObject.style.top = newSizeValue;
		runtimer=setTimeout(function(){updatePosVertically(toExpandObject,initSize,size,startTime,direction,timelength)}, 10);
		} else{ //if animation finished
			toExpandObject.style.top=(direction==1)? size+initSize+"px" : initSize-size+"px";
			runtimer=null;
	} 
}

function slidePosVertically(toExpandObject,height,direction,timelength) {
	this.startTime=new Date().getTime() //Set animation start time
	//var toExpandObject	= document.getElementById(layer);
	var initPos = toExpandObject.offsetTop;
	updatePosVertically(toExpandObject,initPos,height,startTime,direction,timelength);
}