function curveincrement(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}

function updateSize(dimension,toExpandObject,initSize,size,startTime,direction,timelength,flagEnd) {
	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";
		dimension=='width' ? toExpandObject.style.width = newSizeValue : toExpandObject.style.height = newSizeValue;
		runtimer=setTimeout(function(){updateSize(dimension,toExpandObject,initSize,size,startTime,direction,timelength,flagEnd)}, 10);
		} else{ //if animation finished
		
			// dispatch event that the tab has finished closing
			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);
			} 
			
			if(dimension=='width') toExpandObject.style.width=(direction==1)? size+initSize+"px" : initSize-size+"px";
			else toExpandObject.style.height=(direction==1)? size+initSize+"px" : initSize-size+"px";
			runtimer=null;
			if(flagEnd) currentlyExpanding=false;
	} 
}

function slideHeight(layer,height,direction,timelength) {
	this.startTime=new Date().getTime() //Set animation start time
	var initHeight = toExpandObject.offsetHeight*1;
	updateSize('height',toExpandObject,initHeight,height,startTime,direction,timelength);
}

function slideWidth(toExpandObject,width,direction,timelength,flagEnd) {
	this.startTime=new Date().getTime() //Set animation start time
	var initWidth = toExpandObject.offsetWidth*1;
	updateSize('width',toExpandObject,initWidth,width,startTime,direction,timelength,flagEnd);
}