/******************************************

******************************************/
function scrollTo(event) {
	if (!this.active)
		return;
	var srcObj = 	(event.target) ? (event.target) : (event.srcElement); 
	if (srcObj!=this.trackElem)
		return;
	var srcStyleObj = (event.target) ? getObjStyle("#"+srcObj.id): getObjStyle("#"+srcObj.id);
	
	if(this.axis=="X") {
	var end = (event.target) ? (event.clientX-(parseInt(event.target.parentNode.style.left)+parseInt(srcStyleObj.left))):(event.clientX-(parseInt(event.srcElement.parentElement.style.left)+parseInt(srcStyleObj.left)))
	scrollX = document.getElementsByTagName('html').item(0).scrollLeft||document.body.scrollLeft||(event.target?pageXOffset:0)||0
	end+=scrollX;
	this.turnBarOn();
	this.animate(this.oldX,end,((end-this.oldX)>0)?(end-this.oldX)*10:(this.oldX-end)*10);
	}
	if (this.axis=="Y") {
	var end = (event.target) ? (event.clientY-(parseInt(event.target.parentNode.style.top)+parseInt(srcStyleObj.top))):(event.clientY-(parseInt(event.srcElement.parentElement.style.top)+parseInt(srcStyleObj.top)))
	end -=(event.target) ? parseInt(this.textContainerElem.parentNode.style.margin): parseInt(this.textContainerElem.parentElement.style.margin)
	if(this.isNested) {
		//end -= (event.target)? parseInt(event.target.parentNode.parentNode.style.top) : parseInt(event.srcElement.parentElement.parentElement.style.top);
		end -= (event.target) ? parseInt(srcStyleObj.top) : parseInt(srcStyleObj.top);
	}
	this.turnBarOn();
	this.animate(this.oldY,end,((end-this.oldY)>0)?(end-this.oldY)*10:(this.oldY-end)*10);
	}
}
function animate(startValue,endValue,duration,startTime,endTime) {
	if(!startTime) var startTime = new Date().valueOf();
	if(!endTime) var endTime = startTime + duration;

	var curTime = new Date().valueOf();

	if(curTime > endTime) {	//finished animation
		this.moveBar(endValue);
		this.turnBarOff();
	} 
	else {		//continue with animation
		var percent = (curTime - startTime) / (endTime - startTime);
		var curValue = (percent * (endValue - startValue)) + startValue;
		this.moveBar(curValue);
		setTimeout(this.objName+".animate(" + startValue + "," + endValue + "," + duration + "," + startTime + "," + endTime + ");",10);
	}
}

function clickScroll(event) {
	if (!this.active)
		return;
	var value = null;
	if (event.target) {
		if (event.target==this.trackElem) {
			if(this.axis=="X") {
				
				//moves in direction of clicked position
				var srcStyleObj = getObjStyle("#"+event.target.id);
				newValue = (event.clientX-(parseInt(event.target.parentNode.style.left)+parseInt(srcStyleObj.left)))
				if (newValue>this.oldX) 
				newValue= this.oldX+this.tabWidth 
			else if (newValue<this.oldX)
				newValue=this.oldX-this.tabWidth;
			}
		if(this.axis=="Y") {
			
			//moves in direction of clicked position
			var srcStyleObj = getObjStyle("#"+event.target.id);
			newValue = (event.clientY-(parseInt(event.target.parentNode.style.top)+parseInt(srcStyleObj.top)))
			if (newValue>this.oldY) 
				newValue = this.oldY+this.tabHeight 
			else if (newValue<this.oldY)
				newValue = this.oldY-this.tabHeight;
		}
		this.moveBar(newValue);
		}
		else return;
	}
	else {
		if (event.srcElement ==this.trackElem) {
			if(this.axis=="X") {
				
			//moves in direction of clicked position
			var srcStyleObj = getObjStyle("#"+event.srcElement.id);
			newValue = (event.clientX-(parseInt(event.srcElement.parentElement.style.left)+parseInt(srcStyleObj.left)));
			if (newValue>this.oldX) 
				newValue= this.oldX+this.tabWidth 
			else if (newValue<this.oldX)
				newValue= this.oldX-this.tabWidth;

			}
			if(this.axis=="Y") {
				
				//moves in direction of clicked position
				var srcStyleObj = getObjStyle("#"+event.srcElement.id);
				newValue =  (event.clientY-(parseInt(event.srcElement.parentElement.style.top)+parseInt(srcStyleObj.top)))
				//alert(event.srcElement.style.top);
				if (newValue>this.oldY) 
					newValue =  this.oldY+this.tabHeight 
				else if (newValue<this.oldY)
					newValue = this.oldY-this.tabHeight;
			}
		this.moveBar(newValue);
		}
		else return;
	}
	

}

function getXPos(event) {
	var x;
	(event.target) ? x =event.clientX + window.scrollX : x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
	return x;
}

function getYPos(event) {
	var y;
	(event.target) ? y = event.clientY + window.scrollY : y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	return y;
}



function scrollBar(objName, axis, textContainerElemId, textElemId, trackElemId, barElemId, tabImgName, edgeHeights,isNested) {
	this.objName  = objName;
	this.axis = axis;
	this.textContainerElem = document.getElementById(textContainerElemId);
	this.textElem = document.getElementById(textElemId);
	this.trackElem = document.getElementById(trackElemId);
	this.barElem = document.getElementById(barElemId);

	if (tabImgName) {
		this.scrTabImg = document.getElementById(tabImgName);
		this.usingImage = true;
	}
	else this.usingImage =false;
	if (edgeHeights)
		this.scrollTabEdge = edgeHeights;
	this.isNested = isNested;
	this.timer=null;
	this.direction=1;//1 for positive eg down and -1 for negative eg up 

	this.active = false;
	//CSS values that makes Mozilla get the correct results...
	if (document.implementation && document.implementation.createDocument) {
		var styleObj = getObjStyle("#"+this.textContainerElem.id);
		this.textContainerElem.style.height = parseInt(styleObj.height)-1 +"px";

	}

	if (this.axis.indexOf("X")!=-1) {
		var textContainerStyleObj =  getObjStyle("#"+this.textContainerElem.id);
		this.textContainerWidth = parseInt(textContainerStyleObj.width);
		

		this.textWidth = parseInt(this.textElem.offsetWidth);

	
		var trackStyleObj =  getObjStyle("#"+this.trackElem.id);
		this.trackWidth = parseInt(trackStyleObj.width);
		if (this.usingImage) {
			var barStyleObj = getObjStyle("#"+this.barElem.id);
			if(parseInt(barStyleObj.paddingRight)>0) 
				this.trackWidth-=parseInt(barStyleObj.paddingRight);
		}
		this.ratio = this.textContainerWidth/this.textWidth;

		this.barElem.style.width = this.ratio * this.trackWidth + "px";
		this.tabWidth = parseInt(this.barElem.style.width);
		if (this.usingImage) 
			this.scrTabImg.width = this.tabWidth-this.scrollTabEdge;
		
		//initialise
		this.oldX=0;
		this.barElem.style.left="0px";
	}
	
	if (this.axis.indexOf("Y")!=-1) {
		var textContainerStyleObj =  getObjStyle("#"+this.textContainerElem.id);
		this.textContainerHeight = parseInt(textContainerStyleObj.height);
		

		this.textHeight = parseInt(this.textElem.offsetHeight);
		
		var trackStyleObj =  getObjStyle("#"+this.trackElem.id);
		this.trackHeight = parseInt(trackStyleObj.height);
		if (this.usingImage) {
			var barStyleObj = getObjStyle("#"+this.barElem.id);
			if(parseInt(barStyleObj.paddingBottom)>0) 
				this.trackHeight-=parseInt(barStyleObj.paddingBottom);
		}
		this.ratio = this.textContainerHeight/this.textHeight;
		this.barElem.style.height = this.ratio * this.trackHeight -10 + "px";
		this.tabHeight  = parseInt(this.barElem.style.height);
		if (this.usingImage) {
			this.scrTabImg.height = this.tabHeight-this.scrollTabEdge;
		}
		this.oldY=0;
		this.barElem.style.top="0px";

	}
	(this.ratio<1)?	this.active = true : this.active = false;
	
	(this.active) ? this.barElem.style.visibility="visible" : null;

	
	this.xMin = 0; 
	this.xMax = parseInt(this.trackWidth-this.tabWidth);
	this.yMin = 0;
	this.yMax = this.trackHeight-this.tabHeight;
	this.elNode = this.barElem;

	this.onStyleColour =  getObjStyle("."+this.barElem.id+"On").backgroundColor;
	this.offStyleColour =  getObjStyle("."+this.barElem.id+"Off").backgroundColor;
	var thisObj = this;
	this.barElem.onmouseover = function() { thisObj.turnBarOn() };
	this.barElem.onmouseout = function() { thisObj.turnBarOff() };

	//methods
	this.animate = animate
	this.clickScroll = clickScroll
	this.scrollTo = scrollTo
	this.scroll=scroll	
	this.dragGo = dragGo
	this.dragStop = dragStop
	this.incScroll = incScroll
	this.moveBar = moveBar
	this.stopScroll = stopScroll
	this.scrollByWheel = scrollByWheel
	this.turnBarOn = function() {
		this.barElem.style.backgroundColor=this.onStyleColour;	
	}
	this.turnBarOff = function() {
		this.barElem.style.backgroundColor=this.offStyleColour;	
	}
	this.grabBar = function(event) {
	
	var x, y;
  
    (event.target) ? this.elNode = event.target : this.elNode = window.event.srcElement;

	//if using an img for the bar, must grab containing div, not imgs
	while(this.elNode.nodeName!="DIV") {
		if (event.target) {
			this.elNode = this.elNode.parentNode;
		}
		else this.elNode = this.elNode.parentElement;
	}

	 x = getXPos(event);

	 y = getYPos(event);
  	 
	 this.oldX = 0;
	 this.oldY = 0;
	// Save starting positions of cursor and element.
	  this.cursorStartX = x;
	  this.cursorStartY = y;
	  this.elStartLeft  = parseInt(this.elNode.style.left, 10);
	  this.elStartTop   = parseInt(this.elNode.style.top,  10);
  
	  if (isNaN(this.elStartLeft)) this.elStartLeft = 0; 
	  if (isNaN(this.elStartTop))  { this.elStartTop  = 0; }
	
	  var me = this;
	  this.dragFuncCall =  function(event) {
  		me.dragGo(event);
		me.turnBarOn();
	  }
	  this.dragStopFuncCall =  function(event) {
  		me.dragStop(event);
		me.turnBarOff();
	  }
  
	if (event.srcElement) {
    	document.attachEvent("onmousemove", this.dragFuncCall );
	    document.attachEvent("onmouseup",   this.dragStopFuncCall);
    	window.event.cancelBubble = true;
	    window.event.returnValue = false;
  	}

	if (document.implementation && document.implementation.createDocument) {
    	document.addEventListener("mousemove", this.dragFuncCall,   true);
	    document.addEventListener("mouseup",   this.dragStopFuncCall, true);
    	event.preventDefault();
	 }
  }
	
	//this.scroll();
	this.textElem.style.top = "0px";
	this.textElem.style.left = "0px";
	
}

function stopScroll(value) {
	window.clearTimeout(this.timer);
	this.timer = null;
	this.stopped = true
	this.turnBarOff();
}

function scroll() {
	
		if (this.axis.indexOf("Y")!=-1) {
			var percentToScroll = (this.oldY/(this.trackHeight-this.tabHeight))*100;
			this.textElem.style.top = -1 * (((this.textHeight-this.textContainerHeight)/100)*percentToScroll) +"px";
		}
		if (this.axis.indexOf("X")!=-1) {
			var percentToScroll = (this.oldX/(this.trackWidth-this.tabWidth))*100;
			this.textElem.style.left = -1 * (((this.textWidth-this.textContainerWidth)/100)*percentToScroll) +"px";
		}


}

function getObjStyle(selectorText) {
	if (!document.styleSheets) 
		return;
	else var numOfStyleSheets = document.styleSheets.length;
	for (i=0;i<numOfStyleSheets;i++) {
		var rules = (document.styleSheets[0].cssRules) ? document.styleSheets[i].cssRules : document.styleSheets[i].rules;
		for(j=0;j<rules.length;j++) {
			if (rules[j].selectorText == selectorText)
				return rules[j].style;
		}
	}
}

function incScroll(increment) {
	if (this.active) {
		var newValue = null
		//stop scroll so system can change direction
		if (this.direction!=increment)
			this.stopScroll();
		this.direction = increment;
		 this.stopped = false;
		if (this.axis.indexOf("X")!=-1) {
			this.oldX+=increment;
			newValue = this.oldX;
		}
		else 
		
		 if(this.axis.indexOf("Y")!=-1) {
			this.oldY+=increment;
			newValue = this.oldY;
		}
		this.scroll();
		this.moveBar(newValue);
		
		if (!this.stopped) {
		 this.timer = setTimeout(this.objName+".incScroll("+increment+")",50);
		 this.stopped = false;
		}
	}
}

function moveBar(newcoord) {
	if (isNaN(newcoord))
		return;
	if (this.axis.indexOf("Y")!=-1) {	
		if (newcoord<this.yMin){
		  this.elNode.style.top  = this.yMin +"px";
		  this.oldY = this.yMin;
	 	  this.stopScroll();
		}
		else if (newcoord>this.yMax)	  {
	 	 	this.elNode.style.top  = this.yMax +"px";
	  		this.oldY = this.yMax;
	  		this.stopScroll();
    	}
		else {
			this.elNode.style.top  = newcoord+ "px";
  			this.oldY = newcoord;
		}
	}
	else if (this.axis.indexOf("X")!=-1) {	
		if (newcoord<this.xMin){
	  		this.elNode.style.left  = this.xMin +"px";
		  	this.oldX = this.xMin;
		    this.stopScroll();
	    }
		else if (newcoord>this.xMax)	  {
	  	this.elNode.style.left  = this.xMax +"px";
	  	this.oldX = this.xMax;
  	  	this.stopScroll();
		}
		else {
			this.elNode.style.left  = newcoord+ "px";
  			this.oldX = newcoord;
		}
	}
	this.scroll();
}

function dragGo(event) {

  
  x = getXPos(event);
  y=getYPos(event);
  var newX = (this.elStartLeft + x - this.cursorStartX)
  var newY = (this.elStartTop  + y - this.cursorStartY);

  if (this.axis.indexOf("X")!=-1)
	this.moveBar(newX);

  if (this.axis.indexOf("Y")!=-1)
	this.moveBar(newY);

  if (event.srcElement) {
	window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
if (document.implementation && document.implementation.createDocument)
    event.preventDefault();
}

function dragStop(event) {
  // Stop capturing mousemove and mouseup events.
	if (event.srcElement) {
    document.detachEvent("onmousemove", this.dragFuncCall);
    document.detachEvent("onmouseup",   dragStop);
  }
if (document.implementation && document.implementation.createDocument) {
    document.removeEventListener("mousemove", this.dragFuncCall,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function scrollByWheel(event) {
	if (event.wheelDelta >= 120) {
        this.incScroll(-20);
		this.stopScroll();
	}
   	else if (event.wheelDelta <= -120) {
        this.incScroll(20);
		this.stopScroll();
	}
	event.returnValue = false	
	return false;
}
