//
// Global bugSlider management -- BSL (Bugimus SLider)
//
// This object must be placed on elements with the relative or absolute 
// position attributes set.

bugBase.prototype.attachBSL = function() {
	this.BSLnum = 0
	this.BSLobj = new Array()
}
window.bugimus.attachBSL()

//
// Local bugSlider functionality
//

// Define slider object
function bugSlider( refObj, baseImg, wBase, hBase, tPad, rPad, bPad, lPad, mode, nBtnImg, hBtnImg, dBtnImg, wBtn, hBtn ) {

	this.objname = "window.bugimus.BSLobj[" + window.bugimus.BSLnum + "]"
	window.bugimus.BSLobj[window.bugimus.BSLnum++] = this

	this.baseImage = baseImg
	this.baseWidth = wBase
	this.baseHeight = hBase
	this.topPadding = tPad
	this.rightPadding = rPad
	this.bottomPadding = bPad
	this.leftPadding = lPad
	this.mode = mode
	this.normalImage = nBtnImg
	this.hoverImage = hBtnImg
	this.downImage = dBtnImg
	this.btnWidth = wBtn
	this.btnHeight = hBtn

	// 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="visible"
		padding="0px"
		zIndex=0
	}
	this.baseImgObj = document.createElement("IMG")
	this.baseImgObj.src = this.baseImage
	this.baseObj.appendChild( this.baseImgObj )

	this.sliderObj = new dragComponent(this.baseObj, this.leftPadding, this.topPadding, this.btnWidth, this.btnHeight)
	this.sliderObj.dragObject.setz(10)

	if(this.mode=="v") {
		this.constrainX(true)
		this.maxValue = this.baseHeight-this.btnHeight-this.bottomPadding-this.topPadding
		this.sliderObj.setDragArea(this.topPadding, this.baseWidth, this.baseHeight-this.btnHeight-this.bottomPadding, this.leftPadding)
	} else {
		this.constrainY(true)
		this.maxValue = this.baseWidth-this.btnWidth-this.rightPadding-this.leftPadding
		this.sliderObj.setDragArea(this.topPadding, this.baseWidth-this.btnWidth-this.rightPadding, this.baseHeight, this.leftPadding)
	}
	new bugButton(this.sliderObj.dragObject, this.normalImage, this.hoverImage, this.downImage, this.btnWidth, this.btnHeight)
	// create groove
	this.baseObj.appendChild(this.grooveObj=document.createElement("IMG"))
	if(this.mode=="v") {
		with(this.grooveObj.style) {
			position="absolute"
			top=this.topPadding+"px"
			left=parseInt(this.baseWidth/2)-3+"px"
			width="4px"
			height=this.baseHeight-this.topPadding-this.bottomPadding+"px"
			zIndex=5
		}
	} else {
		with(this.grooveObj.style) {
			position="absolute"
			top=parseInt(this.baseHeight/2)-3+"px"
			left=this.leftPadding+"px"
			width=this.baseWidth-this.leftPadding-this.rightPadding+"px"
			height="4px"
			zIndex=5
		}
	}
		
	this.grooveObj.onmousedown = function(e) {
		if(this.parentObj.mode == "v") {
			if(bugimus.IE5) {
				y = window.event.clientY
			} else {
				y = e.clientY
			}
			y += bugimus.BMIscrollY() - getOffsetTops(this.parentObj.baseObj) - this.parentObj.topPadding - this.parentObj.btnHeight
			y = parseInt(100*(this.parentObj.maxValue - y) / this.parentObj.maxValue)
			if(y<0) y=0
			if(y>100) y=100
			this.parentObj.setValue(y)
		} else {
			if(bugimus.IE5) {
				x = window.event.clientX
			} else {
				x = e.clientX
			}
			x += bugimus.BMIscrollX() - getOffsetLefts(this.parentObj.baseObj) - this.parentObj.leftPadding - this.parentObj.btnWidth
			x = parseInt(100*(this.parentObj.maxValue - x) / this.parentObj.maxValue)
			if(x<0) x=0
			if(x>100) x=100
			this.parentObj.setValue(x)
		}
	}
	this.grooveObj.onmouseover = function() {
		this.parentObj.grooveObj.style.cursor = "pointer"
	}
	this.grooveObj.parentObj = this

	if(this.mode=="v") this.setValue(0)

}
//-----------------------------------------------------------------
bugSlider.prototype.constrainX = function(val) { 
	this.sliderObj.constrainX = val
}
//-----------------------------------------------------------------
bugSlider.prototype.constrainY = function(val) { 
	this.sliderObj.constrainY = val
}
//-----------------------------------------------------------------
bugSlider.prototype.getValue = function() { 
	if(this.mode=="v") {
		return ( this.maxValue - this.sliderObj.dragObject.gety() + this.topPadding ) / this.maxValue
	} else {
		return ( this.sliderObj.dragObject.getx() - this.leftPadding ) / this.maxValue
	}
}
//-----------------------------------------------------------------
// Accepts a number from 0 to 100 representing percentage.
bugSlider.prototype.setValue = function(val) {
	if(val<0)val=0
	if(val>100)val=100
	if(this.mode=="v") {
		this.sliderObj.dragObject.sety(this.maxValue + this.topPadding - val*this.maxValue/100)
	} else {
		this.sliderObj.dragObject.setx(this.maxValue + this.leftPadding - val*this.maxValue/100)
	}
}
//-----------------------------------------------------------------
bugSlider.prototype.setBorder = function(s) { 
	this.baseObj.style.border=s
}
//-----------------------------------------------------------------
bugSlider.prototype.setBackgroundColor = function(s) { 
	this.baseObj.style.backgroundColor=s 
}
