// ScrollingText class

//takes an absolutely positioned div and adds clipping styles
//if text in div overflows the clipped area scrolling controls are added below




//RE-USABLE FUNCTIONS/////////////////////////////////////
/////////////////////////////////////////////////////////////////

/***** following functions courtesy of Prototype library *************/
var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

Object.extend(Class, {
    extend: function(source,additions) {
        var newclass = Class.create();
        if ($C(source)) {
            Object.extend($C(newclass),$C(source));
        }
        Object.extend($C(newclass), additions);
        return newclass;
    }
});

function $C(object) { return object.prototype; }





/*****************************************************************************************/
var getObj = Class.extend({},{
	
	initialize: function(name){
		  if (document.getElementById){
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;
		  }else if (document.all){
			this.obj = document.all[name];
			this.style = document.all[name].style;
		  }else if (document.layers){
			this.obj = this.getObjNN4(document,name);
			this.style = this.obj;
		  }
	},
	
	getObjNN4: function(obj,name){
			var x = obj.layers;
			var foundLayer;
			for(var i=0;i<x.length;i++){
				if (x[i].id == name)
					foundLayer = x[i];
				else if (x[i].layers.length)
					var tmp = getObjNN4(x[i],name);
				if (tmp) foundLayer = tmp;
			}
			return foundLayer;
	}

});


/*****************************************************************************************/
var ScrollingText = Class.extend({},{

	initialize: function(scroller_id,clipTop,clipWidth,clipBottom,topper){		
		this.DHTML = (document.getElementById || document.all || document.layers);
		if (!this.DHTML) return;
		
		this.scroller = new getObj(scroller_id);
		if (!this.scroller) return;
		
		this.clipTop = clipTop;
		this.clipWidth = clipWidth;
		this.clipBottom = clipBottom;
		this.topper = topper;
		this.lyrheight = 0;
		
		this.thelayer;
		this.amount;
		this.theTime;
		this.theHeight;
		this.time; 
		
		
		this.initScroller();
	},
	

	//sets up the clipping in the scrolling div 
	initScroller: function(){
		if (document.layers){
			this.lyrheight = this.scroller.style.clip.bottom;
			this.lyrheight += 20;
			this.scroller.style.clip.top = this.clipTop;
			this.scroller.style.clip.left = 0;
			this.scroller.style.clip.right = this.clipWidth;
			this.scroller.style.clip.bottom = this.clipBottom;
		}else if (document.getElementById || document.all){
			this.lyrheight = this.scroller.obj.offsetHeight;
			this.scroller.style.clip = 'rect('+this.clipTop+'px,'+this.clipWidth+'px,'+this.clipBottom+'px,0)';
		}
		
		this.initControls();	
	},
	
	
	scrollLayer: function(amt,tim){
		this.amount = amt;
		this.theTime = tim;
		this.doScroll();
	},
	
	doScroll: function(){
		this.clipTop += this.amount;
		this.clipBottom += this.amount;
		this.topper -= this.amount;
		if (this.clipTop < 0 || this.clipBottom > this.lyrheight){
			this.clipTop -= this.amount;
			this.clipBottom -= this.amount;
			this.topper += this.amount;
			return;
		}
		if (document.getElementById || document.all){
			var clipstring = 'rect('+this.clipTop+'px,'+this.clipWidth+'px,'+this.clipBottom+'px,0)';
			this.scroller.style.clip = clipstring;
			this.scroller.style.top = this.topper + 'px';
		}else if (document.layers){
			this.scroller.style.clip.top = this.clipTop;
			this.scroller.style.clip.bottom = this.clipBottom;
			this.scroller.style.top = this.topper;
		}
		this.time = setTimeout(function(){this.doScroll()}.bind(this),this.theTime);
	},
	
	stopScroll: function(){
		if (this.time) clearTimeout(this.time);
	},
	
	initControls: function(){
		if(this.clipBottom > this.lyrheight) return;
		var controls = document.createElement('div');
		var br = document.createElement('br');
		var up = document.createElement('a');
		var down = document.createElement('a');
		var uptext = document.createTextNode('scroll up');
		var downtext = document.createTextNode('scroll down');
		controls.id = 'controls';
		up.href = '#';
		down.href = '#';
		up.id = 'scrollup';
		down.id = 'scrolldown';
		up.appendChild(uptext);
		down.appendChild(downtext);
		controls.appendChild(up);
		controls.appendChild(br);
		controls.appendChild(down);
		insertAfter(controls,this.scroller.obj);
		
		//added for this site specifically as careers page is different size
		if(isCareer) document.getElementById('controls').style.top = '500px';
		//////////////////////////////////////////////////////////////////////	
	
		up.onmouseover = function(){
			this.scrollLayer(-10,100);
		}.bind(this);
		up.onmouseout = this.stopScroll.bind(this);
		down.onmouseover = function(){
			this.scrollLayer(10,100);
		}.bind(this);
		down.onmouseout = this.stopScroll.bind(this);
	}
	

});






var isCareer = false;
var clipBottom =90;

function setup(){
	var arrup = new Image();
	arrup.src = 'stat/scrollup_over.jpg'

	var arrdown = new Image();
	arrdown.src = 'stat/scrolldown_over.jpg'
	
	if(document.getElementById('scroller')){
		var scrollText = new ScrollingText('scroller',0,450,clipBottom,225);
	}
}
addLoadEvent(setup);