/*

Copyright 2006 (C) Adam Marchbanks.

You may use this script freely only if this copyright notice
and the credits for the script remain intact.

EM@IL	:: amarchbanks@btinternet.com

SPECIAL CREDITS ::

None

TESTED BROWSERS ::

Internet Explorer	6
Opera			7
Mozillia Firebird	1, 1.5

*/

function percentBar(strContainerId){
	if(!percentBar.instances) percentBar.instances = new Array()
	this.index = percentBar.instances.length
	percentBar.instances[this.index] = this
	
	this.holdId = strContainerId
	this.bWidth = 0
	this.pWidth = 0
	
		if(this.getEl()){
			var objHolder = this.getEl(this.holdId)
			
				if(objHolder){

					this.pWidth = this.getEl(this.holdId).offsetWidth
					
						var b = document.createElement("DIV")
						b.className = "bar"
						b.setAttribute("id", this.holdId + "bar")
						objHolder.appendChild(b)
						
					this.bWidth = this.getEl(this.holdId + "bar").offsetWidth
					this.setPercentage(0)
				} else {
					alert("Progress Bar Script cannot locate the Container DIV. Please Check the ID set.")
				}
		} else {
			if(!percentBar.notdomcompliant){
				alert("Sorry, your browser isn't Standards Compliant. Please upgrade.")
				percentBar.notdomcompliant = true
			}
		}
}

	percentBar.prototype.getEl = function(strObjId){
		if(strObjId){
			return document.getElementById(strObjId)
		} else {
			if(document.getElementById) return true
			else return false
		}
	}
	
	percentBar.prototype.setBarWidth = function(intBarWidth){
		if(this.getEl()){
			var bar = this.getEl(this.holdId + "bar").style
			bar.width = intBarWidth + "px"
			this.bWidth = intBarWidth
		}
	}
	
	percentBar.prototype.setPercentage = function(intPercentage){
		try {
			var bar = this.getEl(this.holdId)
			var span = bar.getElementsByTagName("SPAN")[0]
			span.innerHTML = intPercentage + "%"
		}
		catch(ex){}
	}
	
	percentBar.prototype.setCount = function(intPercentage, booAnimation){
		if(this.getEl()){
			var per = parseInt(intPercentage)
			
				if(per < 0) per = 0
				else if(per > 100) per = 100
				
			var newWidth = (this.pWidth / 100) * per
			this.setBarWidth(newWidth)
			this.setPercentage(per)
			if(!booAnimation) clearTimeout(this.timer)			
			if(this.onchange) this.onchange(per)
		}
	}
	
	percentBar.prototype.incrCount = function(intPercentage, booAnimation){
		if(this.getEl()){
			var cPer = (this.bWidth / this.pWidth) * 100
			var incr = (this.pWidth / 100) * parseInt(intPercentage)
			incr = cPer + incr
			this.setCount(incr)
			
				if(!booAnimation) clearTimeout(this.timer)
		}
	}
	
	percentBar.prototype.decrCount = function(intPercentage, booAnimation){
		if(this.getEl()){
			var cPer = (this.bWidth / this.pWidth) * 100
			incr = cPer - parseInt(intPercentage)
			this.setCount(incr)
			
				if(!booAnimation) clearTimeout(this.timer)
		}
	}
	
	percentBar.prototype.getCount = function(){
		var cPer = (this.bWidth / this.pWidth) * 100
		cPer = Math.round(cPer)
		return cPer
	}
	
	percentBar.prototype.showBar = function(){
		if(this.getEl()){
			var bar = this.getEl(this.holdId)
			bar.style.visibility = "visible"
		}
	}
	
	percentBar.prototype.hideBar = function(){
		if(this.getEl()){
			var bar = this.getEl(this.holdId)
			bar.style.visibility = "hidden"
		}
	}
	
	percentBar.prototype.animateToCount = function(intPercentage, intCount, intRefreshTime, handler){
		if(this.getEl()){
			
				if(intCount < 0) return
				if(this.timer) clearTimeout(this.timer)
			
			var anUp = intPercentage > this.getCount()? 1:0
			this.animationHandler = handler
			
				if(intPercentage == this.getCount()) return
				if(intPercentage < 0) intPercentage = 0
				if(intPercentage > 100) intPercentage = 100
			
			this.animate(anUp, intCount, intPercentage, intRefreshTime)
		}
	}
	
	percentBar.prototype.animate = function(booAnimateUp, intCount, intPercentage, intRefreshTime){
		if(booAnimateUp){
			
			if(this.getCount() + intCount > intPercentage){
				intCount = intPercentage - this.getCount()
			}
			
			this.incrCount(intCount)			
		} else {
		
			if(this.getCount() - intCount < intPercentage){
				intCount = this.getCount() - intPercentage
			}
		
			this.decrCount(intCount)
		}

			if(this.getCount() != intPercentage){			
				this.timer = setTimeout("percentBar.instances[" + this.index + "].animate(" + booAnimateUp + ", " + intCount + ", " + intPercentage + ", " + intRefreshTime + ")", intRefreshTime)		
			} else {
				if(this.animationHandler) this.animationHandler()
				this.animationHandler = null
			}
	}
	
	percentBar.prototype.writeMessage = function(strMessage){
		if(this.getEl()){
			var objHold = this.getEl(this.holdId)
			var divs = objHold.getElementsByTagName("DIV")
			
				for(var i=0; i <= divs.length-1; i++){
					if(divs[i].className == "message"){
						divs[i].innerHTML = strMessage
					}
				}
		}
	}
