// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 01/24/03 ---------------------------------------------------------

// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Cross-Browser Functions

var dom = document.getElementById;
var iex = document.all;
var ns4 = document.layers;

function addEvent(event,method){
	this[event] = method;
	if(ns4) this.captureEvents(Event[event.substr(2,event.length).toUpperCase()]);
}

function removeEvent(event){
	this[event] = null;
	if(ns4) this.releaseEvents(Event[event.substr(2,event.length).toUpperCase()]);
}

function getElement(name,nest){
	nest = nest ? "document."+nest+"." : "";
	var el = dom ?
	  document.getElementById(name) :
	  iex ?
	     document.all[name] :
	     ns4 ?
	        eval(nest+"document."+name) : false;
	el.css = ns4 ? el : el.style;
	el.getTop = function(){return parseInt(el.css.top) || 0};
	el.setTop = function(y){el.css.top = ns4 ? y: y+"px"};
	el.getHeight = function(){return ns4 ? el.document.height : el.offsetHeight};
	el.getClipHeight = function(){return ns4 ? el.clip.height : el.offsetHeight};

   // new horizontally oriented functions for each element
 	el.getLeft = function(){return parseInt(el.css.left) || 0};
	el.setLeft = function(x){el.css.left = ns4 ? x: x+"px"};
	el.getWidth = function(){return ns4 ? el.document.width : el.offsetWidth};
	el.getClipWidth = function(){return ns4 ? el.clip.width : el.offsetWidth};   
   
	el.hideVis = function(){el.css.visibility="hidden"};
	el.addEvent = addEvent;
	el.removeEvent = removeEvent;
	return el;
}

function getYMouse(e){
	return iex ? event.clientY : e.pageY;
}

// new horizonal mouse function
function getXMouse(e){
	return iex ? event.clientX : e.pageX;
}

document.addEvent = addEvent;
document.removeEvent = removeEvent;

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Scroller Class


ScrollObj = function(speed, dragLength, trackLength, trackObj, backObj, forwardObj, dragObj, contentMaskObj, contentObj, orientation){

   this.orientation = orientation;    // veritcal | horizontal

	this.speed = speed;
	this.dragLength = dragLength;
	this.trackLength = trackLength;
	this.trackObj = getElement(trackObj);
	this.backObj = getElement(backObj);
	this.forwardObj = getElement(forwardObj);
	this.dragObj = getElement(dragObj);
	this.contentMaskObj = getElement(contentMaskObj);
	this.contentObj = getElement(contentObj,contentMaskObj);
	this.obj = contentObj+"Object";
	eval(this.obj+"=this");            /// hmmmm. what the purpose here ???
	
   if (this.orientation == 'vertical') {
      this.trackStart = this.dragObj.getTop();      
   } else if (this.orientation == 'horizontal') {
      this.trackStart = this.dragObj.getLeft();      
   }
	this.trackLength = this.trackLength-this.dragLength;
	this.trackEnd = this.trackStart+this.trackLength;
   
   if (this.orientation == 'vertical') {
      this.contentMaskSize = this.contentMaskObj.getClipHeight();
      this.contentSize = this.contentObj.getHeight();
      this.contentLength = this.contentSize-this.contentMaskSize;
   } else if (this.orientation == 'horizontal') {
      this.contentMaskSize = this.contentMaskObj.getClipWidth();
      this.contentSize = this.contentObj.getWidth();
      this.contentLength = this.contentSize-this.contentMaskSize;
   } else {
      alert('bad orientation: '+this.orientation);
   }
   
	this.scrollLength = this.trackLength/this.contentLength;
	this.scrollTimer = null;

   //alert ('contentSize: '+this.contentSize);
   //alert ('contentLength: '+this.contentLength);
   //alert("contentSize : contentMaskSize\n"+this.contentSize + " : " + this.contentMaskSize);
	
	if(this.contentSize <= this.contentMaskSize){
		this.dragObj.hideVis();
		alert("hiding");
	}else{
		var self = this;
		this.trackObj.addEvent("onmousedown", function(e){self.scrollJump(e);return false});
		this.backObj.addEvent("onmousedown", function(){self.scroll(self.speed);return false});
		this.backObj.addEvent("onmouseup", function(){self.stopScroll()});
		this.backObj.addEvent("onmouseout", function(){self.stopScroll()});
		this.forwardObj.addEvent("onmousedown", function(){self.scroll(-self.speed);return false});
		this.forwardObj.addEvent("onmouseup", function(){self.stopScroll()});
		this.forwardObj.addEvent("onmouseout", function(){self.stopScroll()});
		this.dragObj.addEvent("onmousedown", function(e){self.startDrag(e);return false});
		if(iex) this.dragObj.addEvent("ondragstart", function(){return false});
	}
}

ScrollObj.prototype.startDrag = function(e){
   if (this.orientation == 'vertical') {      
      this.dragStartMouse = getYMouse(e);
      this.dragStartOffset = this.dragObj.getTop();
   } else if (this.orientation == 'horizontal') {
      this.dragStartMouse = getXMouse(e);
      this.dragStartOffset = this.dragObj.getLeft();
   }
	var self = this;
	document.addEvent("onmousemove", function(e){self.drag(e)});
	document.addEvent("onmouseup", function(){self.stopDrag()});
}

ScrollObj.prototype.stopDrag = function(){
	document.removeEvent("onmousemove");
	document.removeEvent("onmouseup");
}

ScrollObj.prototype.drag = function(e){
   if (this.orientation == 'vertical') {
      var currentMouse = getYMouse(e);
   } else if (this.orientation == 'horizontal') {
      var currentMouse = getXMouse(e);
   }
	var mouseDifference = currentMouse-this.dragStartMouse;
	var dragDistance = this.dragStartOffset+mouseDifference;
	var dragMovement = (dragDistance<this.trackStart) ?
	  this.trackStart : 
	  (dragDistance>this.trackEnd) ?
	     this.trackEnd :
	     dragDistance;
   if (this.orientation == 'vertical') {
      this.dragObj.setTop(dragMovement);
   } else if (this.orientation == 'horizontal') {
      this.dragObj.setLeft(dragMovement);
   }
	var contentMovement = -(dragMovement-this.trackStart)*(1/this.scrollLength);
   if (this.orientation == 'vertical') {
      this.contentObj.setTop(contentMovement);
   } else if (this.orientation == 'horizontal') {
      this.contentObj.setLeft(contentMovement);
   }
}

ScrollObj.prototype.scroll = function(speed){
   if (this.orientation == 'vertical') {      
      var contentMovement = this.contentObj.getTop()+speed;
      var dragMovement = this.trackStart-Math.round(
        this.contentObj.getTop()*(this.trackLength/this.contentLength));
   } else if (this.orientation == 'horizontal') {
      var contentMovement = this.contentObj.getLeft()+speed;
      var dragMovement = this.trackStart-Math.round(
        this.contentObj.getLeft()*(this.trackLength/this.contentLength));
   }
	if(contentMovement > 0){
		contentMovement = 0;
	}else if(contentMovement < -this.contentLength){
		contentMovement = -this.contentLength;
	}
	if(dragMovement < this.trackStart){
		dragMovement = this.trackStart;
	}else if(dragMovement > this.trackEnd){
		dragMovement = this.trackEnd;
	}
	
   if (this.orientation == 'vertical') {
      this.contentObj.setTop(contentMovement);
      this.dragObj.setTop(dragMovement);
   } else if (this.orientation == 'horizontal') {
      this.contentObj.setLeft(contentMovement);
      this.dragObj.setLeft(dragMovement);
   }
	this.scrollTimer = window.setTimeout(this.obj+".scroll("+speed+")",25);
}

ScrollObj.prototype.stopScroll = function(){
	if(this.scrollTimer){
		window.clearTimeout(this.scrollTimer);
		this.scrollTimer = null;
	}
}

ScrollObj.prototype.scrollJump = function(e){
   if (this.orientation == 'vertical') {
      var currentMouse = getYMouse(e);
   } else if (this.orientation == 'horizontal') {
      var currentMouse = getXMouse(e);
   }
	var dragDistance = currentMouse-(this.dragLength/2);
	var dragMovement = (dragDistance<this.trackStart) ? 
	  this.trackStart : 
	  (dragDistance>this.trackEnd) ? 
	     this.trackEnd : 
	     dragDistance;
   if (this.orientation == 'vertical') {
      this.dragObj.setTop(dragMovement);
   } else if (this.orientation == 'horizontal') {
      this.dragObj.setLeft(dragMovement);
   }
	var contentMovement = -(dragMovement-this.trackStart)*(1/this.scrollLength);
   if (this.orientation == 'vertical') {
      this.contentObj.setTop(contentMovement);
   } else if (this.orientation == 'horizontal') {
      this.contentObj.setLeft(contentMovement);
   }
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Misc Functions

function fixNetscape4(){
	if(ns4origWidth != window.innerWidth || ns4origHeight != window.innerHeight){
		window.location.reload();
	}	
}
if(document.layers){
	ns4origWidth = window.innerWidth;
	ns4origHeight = window.innerHeight;
	window.onresize = fixNetscape4;
}

