/**
 * Author: Stanley Lin
 * Copyright 2008
 * Free for distribution, as long as this acknowledgement header is included
 * email spanchovy at gmail for questions or bugs
*/
function scroller(divId, maxTime, initialShow) {
	// set init vars
	this.maxTime = maxTime
	this.divObj=document.getElementById(divId)
	this.originalHeight = parseInt(this.divObj.style.height)
	if (isNaN(this.originalHeight)){ // if this is not ie? firefox?
		this.originalHeight = this.divObj.offsetHeight
	}
	this.isScrolling = false
	
	// set mandatory styles to allow scrolling
	this.divObj.style.overflow="hidden"
	// the following is to say, do not set your DIV to be 'hidden' by default. we'll do it for you if need be
	if (initialShow)
		this.divObj.style.display="block"
	else
		this.divObj.style.display="none"
}

// PUBLIC METHOD: test to see if this is open, OR if its in the process of opening
scroller.prototype.isPanelOpened=function() {
	return ("none" != this.divObj.style.display);
}

// PUBLIC METHOD: test to see if this is closed, OR if its in the process of closing
scroller.prototype.isPanelClosed=function() {
	return ("none" == this.divObj.style.display);
}
// PUBLIC METHOD: test to see if this is in the process of moving
scroller.prototype.isPanelSliding=function() {
	return (this.isScrolling);
}

// PUBLIC METHOD: used to to block all other calls to 'scroll'
scroller.prototype.disable=function(delayTime) {
	// block calls while animation is going on
	if (this.isScrolling) return
	this.isScrolling = true
	var thisobj=this
	setTimeout(function(){thisobj.enable()}, delayTime)
}
// internal method. should not be called publicly
scroller.prototype.enable=function(delayTime) {
	this.isScrolling = false
}
// PUBLIC METHOD: used to prime the motion and block all other calls to 'scroll'
scroller.prototype.scroll_delay=function(delayTime) {
	// block calls while animation is going on
	if (this.isScrolling) return
	this.isScrolling = true
	var thisobj=this
	setTimeout(function(){thisobj.scrollImmediately()}, delayTime)
}

// PUBLIC METHOD: used to execute the motion with no delay, blocking all other calls to 'scroll'
scroller.prototype.scroll=function() {
	// block calls while animation is going on
	if (this.isScrolling) return
	this.isScrolling = true
	this.scrollImmediately()
}

// internal method, should not be used publicly
scroller.prototype.scrollImmediately=function() {
	// init variables to allow for scrolling
	// we set the display style to indicate whether its closed or open
	this.isCollapseMode = ("none" != this.divObj.style.display)
	this.startTime=new Date().getTime()
	
	// start the looping animation
	var thisobj=this
	setTimeout(function(){thisobj.runscroll()}, 5)
}

// internal method
scroller.prototype.runscroll=function() {
	var elapsed=new Date().getTime() - this.startTime //get time animation has run
	var percentage = getScrollPercent(elapsed / this.maxTime)
	if (percentage < .99) {
		if (this.isCollapseMode) percentage = 1 - percentage
		var pixelHeight = percentage * this.originalHeight
		// the following is for IE. it doesnt like 0px (it instead displays the div at full height)
		if (pixelHeight < 0) pixelHeight = 1
		this.divObj.style.height = pixelHeight + "px"
		if ("none" == this.divObj.style.display) this.divObj.style.display = "block"
		var thisobj=this
		setTimeout(function(){thisobj.runscroll()}, 5)
	} else {
		if (this.isCollapseMode) {
			// if ("hidden" == divObj.style.visibility) divObj.style.visibility = "visible"
			//divObj.style.visibility="hidden"
			this.divObj.style.display="none"
		}
		this.divObj.style.height = this.originalHeight
		if (isNaN(parseInt(this.divObj.style.height))) { // if this is not ie? firefox?
			this.divObj.offsetHeight = this.originalHeight
		}
		this.isScrolling = false
	}
}

// internal method
function getScrollPercent(percent){
	return (1 - Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}

