//
// Global bugScrollBar management -- BSB (Bugimus ScrollBar)
//
// This object must be placed on elements with the relative or absolute 
// position attributes set.

bugBase.prototype.attachBSB = function() {
	this.BSBnum = 0
	this.BSBobj = new Array()
	this.BSBselected = 0
}
bugBase.prototype.BSBclearScrolling = function() {
//	alert("bugScrollBar::clearScrolling: selected index = "+this.BSBselected)
	for(i=0; i<this.BSBnum; i++ ) {
		this.BSBobj[i].scrolling = false
		clearTimeout( this.BSBobj[i].scrollTimer )
	}
}
window.bugimus.attachBSB()

//
// Local bugScrollBar functionality
//

// Define scrollbar object
function bugScrollBar( refObj, wBase, hBase, upNormImg, upHoverImg, upDownImg, downNormImg, downHoverImg, downDownImg, dragBaseImg, dragNormImg, dragHoverImg, dragDownImg, wBtn, hBtn, tPad, bPad, hDtn, cGap ) {

	var defaultHTML = "To add content to this window, use one of these three methods:<ol><li>setText( textstring ), accepts text only</li><li>setInnerHTML( htmlstring ), accepts HTML code</li><li>setClonedHTML( elementref ) accepts reference to an <u>existing</u> HTML element</li></ol>";

	this.objname = "window.bugimus.BSBobj[" + window.bugimus.BSBnum + "]"
	this.objnum = window.bugimus.BSBnum
	window.bugimus.BSBobj[window.bugimus.BSBnum++] = this

	this.baseWidth = wBase
	this.baseHeight = hBase
	this.upNormalImage = upNormImg
	this.upHoverImage = upHoverImg
	this.upDownImage = upDownImg
	this.downNormalImage = downNormImg
	this.downHoverImage = downHoverImg
	this.downDownImage = downDownImg
	this.baseImage = dragBaseImg
	this.dragNormalImage = dragNormImg
	this.dragHoverImage = dragHoverImg
	this.dragDownImage = dragDownImg
	this.btnWidth = wBtn   // all buttons
	this.btnHeight = hBtn  // up/down buttons
	this.topPadding = tPad
	this.bottomPadding = bPad
	this.dtnHeight = hDtn  // drag button
	this.contentToBarGap = cGap // gap between content and scrollbar
	
	this.scrolling = false
	this.scrollEnabled = false
	this.scrollInt = 80
	this.scrollInc = 0.02

	// create base
	refObj.appendChild(this.baseObj=document.createElement("DIV"))
	with(this.baseObj.style) {
		position="relative"
		width=this.baseWidth+"px"
		height=this.baseHeight+"px"
		styleFloat="left"
		cssFloat="left"
		overflow="hidden"
		padding="0px"
		zIndex=0
	}
		// scrollbar mousedown handler
		this.baseObj.onmousedown = function(e) {
			bugimus.BSBselected = this.parentObj.objnum
		}
		this.baseObj.parentObj = this

	// scrollbar face image
	this.baseObj.appendChild( this.faceImgObj = document.createElement("IMG") )
		with(this.faceImgObj) {
			src = this.baseImage
			width = this.btnWidth
			height = this.baseHeight
			style.styleFloat="right"
			style.cssFloat="right"
			style.zIndex=0
		}
		// scrollbar face mousedown handler
		this.faceImgObj.onmousedown = function(e) {
			if(bugimus.IE5) {
				y = window.event.clientY
			} else {
				y = e.clientY
			}
			y += bugimus.BMIscrollY() - getOffsetTops(this.parentObj.baseObj) - this.parentObj.topPadding 
			y /= this.parentObj.maxValue + this.parentObj.dtnHeight - 20
			this.parentObj.setValue(y)
           return false
		}
		this.faceImgObj.parentObj = this

	// scrollbar drag button
	this.sliderObj = new dragComponent(this.baseObj, this.baseWidth-this.btnWidth, this.topPadding, this.btnWidth, this.dtnHeight)
	this.sliderObj.dragObject.setz(10)
	this.constrainX(true)
	this.maxValue = this.baseHeight-this.dtnHeight-this.bottomPadding-this.topPadding
	this.sliderObj.setDragArea(this.topPadding, this.baseWidth, this.baseHeight-this.dtnHeight-this.bottomPadding, 0)
	this.sliderBtn = new bugButton(this.sliderObj.dragObject, this.dragNormalImage, this.dragHoverImage, this.dragDownImage, this.btnWidth, this.dtnHeight)
		this.sliderBtn.buttonObj.onmouseover = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_HOVER)
		}

	// redefine mousemove handler for scrollbar drag button
	//  - repositioning the content div and scrollbar drag button must be done 
	//    together for smooth performance
	this.sliderObj.handlemousemove = function() {
		this.parentObj.setContentPos( this.parentObj.getValue() )
	}
	this.sliderObj.parentObj = this

	// scrollbar up arrow
	this.baseObj.appendChild(this.upArrowObj=document.createElement("DIV"))
		with(this.upArrowObj.style) {
			position="absolute"
			top="0px"
			left=this.baseWidth-this.btnWidth+"px"
			width=this.btnWidth+"px"
			height=this.btnHeight+"px"
			overflow="visible"
			padding="0px"
			zIndex=10
		}
		this.upArrowBtn = new bugButton(this.upArrowObj, this.upNormalImage, this.upHoverImage, this.upDownImage, this.btnWidth, this.btnHeight)
		// redefine all button events
		this.upArrowBtn.onmouseup = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
			this.scrollBaseObj.scrolling = false
		}
		this.upArrowBtn.buttonObj.onmouseover = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_HOVER)
		}
		this.upArrowBtn.buttonObj.onmousedown = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_DOWN)
			this.scrollBaseObj.scrolling = true
			this.scrollBaseObj.scrollContent(this.scrollBaseObj.scrollInc)
			return false
		}
		this.upArrowBtn.buttonObj.onmouseup = function() { 
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
			this.scrollBaseObj.scrolling = false
		}
		this.upArrowBtn.buttonObj.scrollBaseObj = this

	// scrollbar down arrow
	this.baseObj.appendChild(this.downArrowObj=document.createElement("DIV"))
		with(this.downArrowObj.style) {
			position="absolute"
			top=this.baseHeight-this.btnHeight+"px"
			left=this.baseWidth-this.btnWidth+"px"
			width=this.btnWidth+"px"
			height=this.btnHeight+"px"
			overflow="visible"
			padding="0px"
			zIndex=10
		}
		this.downArrowBtn = new bugButton(this.downArrowObj, this.downNormalImage, this.downHoverImage, this.downDownImage, this.btnWidth, this.btnHeight)
		// redefine all button events
		this.downArrowBtn.onmouseup = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
			this.scrollBaseObj.scrolling = false
		}
		this.downArrowBtn.buttonObj.onmouseover = function() {
//			this.parentObj.buttonObj.style.cursor = "pointer"
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_HOVER)
		}
		this.downArrowBtn.buttonObj.onmousedown = function() {
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_DOWN)
			this.scrollBaseObj.scrolling = true
			this.scrollBaseObj.scrollContent(-this.scrollBaseObj.scrollInc)
			return false
		}
		this.downArrowBtn.buttonObj.onmouseup = function() { 
			this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
			this.scrollBaseObj.scrolling = false
		}
		this.downArrowBtn.buttonObj.scrollBaseObj = this

	// create default content
	this.baseObj.appendChild(this.contentObj=document.createElement("DIV"))
	this.setInnerHTML( defaultHTML )
}
//-----------------------------------------------------------------
bugScrollBar.prototype.constrainX = function(val) { 
	this.sliderObj.constrainX = val
}
//-----------------------------------------------------------------
bugScrollBar.prototype.constrainY = function(val) { 
	this.sliderObj.constrainY = val
}
//-----------------------------------------------------------------
bugScrollBar.prototype.getValue = function() { 
	return ( 1.0 - ((this.maxValue - this.sliderObj.dragObject.gety() + this.topPadding ) / this.maxValue))
}
//-----------------------------------------------------------------
// Accepts a percentage from 0.0 to 1.0 representing percentage.
bugScrollBar.prototype.setValue = function(pct) {
	if(pct<0.0)pct=0.0
	if(pct>1.0)pct=1.0
	this.sliderObj.dragObject.sety(this.maxValue + this.topPadding - (1.0-pct)*this.maxValue)
	this.setContentPos( this.getValue() )
}
//-----------------------------------------------------------------
// Accepts a percentage from 0.0 to 1.0 representing percentage.
bugScrollBar.prototype.setContentPos = function(pct) {
	if(pct<0.0)pct=0.0
	if(pct>1.0)pct=1.0
	maxScroll = this.contentHeight - this.baseHeight
	this.contentObj.sety(-maxScroll*pct)
}
//-----------------------------------------------------------------
// Accepts an increment value from -1.0 to 1.0
// positive scrolls down and negative up
bugScrollBar.prototype.scrollContent = function(inc) {
	maxInc=0.05
	if(inc<-maxInc) inc = -maxInc
	if(inc>maxInc) inc = maxInc
	this.setValue( this.getValue()-inc )
	if(this.scrolling==false) {
		clearTimeout( this.scrollTimer )
	} else {
		this.scrollTimer = setTimeout(this.objname+".scrollContent("+inc+")", this.scrollInt)
	}
}
//-----------------------------------------------------------------
bugScrollBar.prototype.setBorder = function(s) { 
	this.baseObj.style.border=s
}
//-----------------------------------------------------------------
bugScrollBar.prototype.setBackgroundColor = function(s) { 
	this.baseObj.style.backgroundColor=s 
}
//-----------------------------------------------------------------
bugScrollBar.prototype.setInnerHTML = function (htmlString) {
	try {
		newObj = document.createElement("DIV")
			with(newObj.style) {
				position="absolute"
				top="0px"
				left="0px"
				width=this.baseWidth-this.btnWidth-this.contentToBarGap+"px"
				overflow="hidden"
				backgroundColor="transparent"
			}
			newObj.innerHTML = htmlString
			this.baseObj.replaceChild( newObj, this.contentObj )
			this.contentObj = newObj
			this.contentObj.sety = setoY
			// do not set style.height because you need offsetHeight to follow the content
			this.contentHeight = newObj.offsetHeight
			this.setScroll()
	} catch(err) { 
		alert("Error in bugScrollBar::setInnerHTML: "+err) 
	}
}

//-----------------------------------------------------------------
bugScrollBar.prototype.setClonedHTML = function (cloneRef) {
	try {
		cloneRef.style.visibility = "visible"
		cloneRef.style.display = "block"
		newObj = document.createElement("DIV")
			with(newObj.style) {
				position="absolute"
				top="0px"
				left="0px"
				width=this.baseWidth-this.btnWidth-this.contentToBarGap+"px"
				overflow="hidden"
				backgroundColor="transparent"
			}
			newObj.appendChild(clonedRef)
			this.baseObj.replaceChild( newObj, this.contentObj )
			this.contentObj = newObj
			this.contentObj.sety = setoY
			// do not set style.height because you need offsetHeight to follow the content
			this.contentHeight = newObj.offsetHeight
			this.setScroll()
	} catch(err) { 
		alert("Error in bugScrollBar::setClonedHTML: "+err) 
	}
}

//-----------------------------------------------------------------
bugScrollBar.prototype.setText = function (textString) {
	try {
		newObj = document.createElement("DIV")
			with(newObj.style) {
				position="absolute"
				top="0px"
				left="0px"
				width=this.baseWidth-this.btnWidth-this.contentToBarGap+"px"
				overflow="hidden"
				backgroundColor="transparent"
			}
			newObj.appendChild(document.createTextNode(textString))
			this.baseObj.replaceChild( newObj, this.contentObj )
			this.contentObj = newObj
			this.contentObj.sety = setoY
			// do not set style.height because you need offsetHeight to follow the content
			this.contentHeight = newObj.offsetHeight
			this.setScroll()
	} catch(err) { 
		alert("Error in bugScrollBar::setClonedHTML: "+err) 
	}
}

//-----------------------------------------------------------------
bugScrollBar.prototype.setScroll = function () {
	try {
		if ( this.contentHeight <= this.baseHeight ) {
			this.scrollEnabled = false
			this.upArrowObj.style.visibility = "hidden"
			this.sliderObj.dragObject.style.visibility = "hidden"
			this.faceImgObj.style.visibility = "hidden"
			this.downArrowObj.style.visibility = "hidden"
		} else {
			this.scrollEnabled = true
			this.upArrowObj.style.visibility = "visible"
			this.sliderObj.dragObject.style.visibility = "visible"
			this.faceImgObj.style.visibility = "visible"
			this.downArrowObj.style.visibility = "visible"
		}
	} catch(err) { 
		alert("Error in bugScrollBar::setScroll: "+err) 
	}
}

//-----------------------------------------------------------------
// You have to redefine the global mousemove handler from BMI
bugBase.prototype.handlemousemove = function (e){
	if(bugimus.IE5) { 
		bugimus.BMImousex = event.clientX
		bugimus.BMImousey = event.clientY
	} else if(bugimus.NN4) {
		bugimus.BMImousex = e.pageX+2
		bugimus.BMImousey = e.pageY+2
	} else if(bugimus.NN6) {
		bugimus.BMImousex = e.pageX
		bugimus.BMImousey = e.pageY
	}

	if(bugimus.BDDobj && bugimus.BDDnum) bugimus.BDDobj[bugimus.BDDselected].obj.handlemousemove()
	if(bugimus.BDCobj && bugimus.BDCnum) bugimus.BDCobj[bugimus.BDCselected].dragObject.handlemousemove()
	if(bugimus.BSBobj && bugimus.BSBnum) bugimus.BSBobj[bugimus.BSBselected].sliderObj.handlemousemove()

	// prevents selecting text on main page when cursor falls off edge... 
	//a global dragging flag might be a good way to have the cake and eat it too
	return false
}
window.bugimus.attachBMI()
