if (!TT) { var TT = {}; }
if (!(TT.VerticalTicker)) {
	TT.VerticalTicker = Class.create({
		initialize: function(tickerArray, obj, config){
			var is_ie6 = (window.external && typeof window.XMLHttpRequest == "undefined"), 
				scrollerinstance = this;
			
			if(typeof obj == 'string') { // object can be string or object
				obj = $$(obj).first();
			}
			this.bnImageLoaded = false, 
			this.tTimer_counter = 0;				
			this.config = config || {};
			this.tickerColor = this.config.tickerColor || null;
			this.headerColor = this.config.headerColor || null;
			this.fontColor = this.config.fontColor || null;
			this.tickerHeight = 0;
			this.actualHeight = 0;
			this.current = 0;
			this.last = null;
			this.pauseit = 1;
			this.scrollspeed = 2;
			this.copyspeed = this.scrollspeed;
			this.pausespeed = (this.pauseit == 0) ? this.copyspeed : 0;
			this.pause = false;
			this.counter = 0;
			this.image = null;
			this.fontColor = null;
			this.pausetime = this.config.pausetime || 1;
			
			this.content = tickerArray;
			this.tickerContentDiv = obj;
			this.last = this.content.length - 1;
			
			var header = this.tickerContentDiv.parentNode.firstChild;
			if (header.style == null) 
				header = this.tickerContentDiv.parentNode.firstChild.nextSibling;
			
			//Get tickerImage
			if (header.innerHTML.match("ticker-image")) {
				this.image = header.firstChild;
				if (this.image == null) 
					this.image = header.firstChild.nextSibling;
			}
			
			//Check to see if there is a ticker-image
			if (!this.image) {
				this.tickerContentDiv.parentNode.style.height = "19px";
				if (header.innerHTML) {
					header.style.paddingRight = "10px";
					header.style.paddingLeft = "10px";
				}
				else {
					header.style.padding = "0px";
				}
				this.startScroll(this.tickerColor, this.headerColor, this.fontColor);
			} else if (this.config.postLoadCall) {
				if (!this.bnImageLoaded) {
					if (this.image.height > 35) 
						obj.parentNode.style.height = "35px";
					else 
						obj.parentNode.style.height = this.height + "px";
					this.startScroll(this.tickerColor, this.headerColor, this.fontColor);
					this.bnImageLoaded = true;
				}
				if (is_ie6 == true) {
					this.tTimer = setInterval(this.bn_tTimer, 500);
				}
				
			} else {
				this.image.onload = function(){
					if (!scrollerinstance.bnImageLoaded) {
						if (this.height > 35) 
							obj.parentNode.style.height = "35px";
						else 
							obj.parentNode.style.height = this.height + "px";
						scrollerinstance.startScroll(this.tickerColor, this.headerColor, this.fontColor);
						scrollerinstance.bnImageLoaded = true;
					}
				};
				if (is_ie6 == true) {
					this.tTimer = setInterval(this.bn_tTimer, 500);
				}
			}
			if (this.content.length > 1) {
				obj.observe('mouseover',function(){scrollerinstance.mouseStop();});
				obj.observe('mouseout', function(){scrollerinstance.mouseStart();});
			}
			
		},
		
		mouseStop : function() {
			var scrollerinstance = this;
			scrollerinstance.copyspeed = scrollerinstance.pausespeed;
			clearInterval(scrollerinstance.scrollInterval);
		},
		
		mouseStart : function(){
			var scrollerinstance = this;
			scrollerinstance.copyspeed = scrollerinstance.scrollspeed;
			scrollerinstance.scrollInterval = setInterval(function(){
				scrollerinstance.scroll()
			}, 100);
		},
		
		bn_tTimer: function(){
			if (this.bnImageLoaded) {
				return false;
			}
			
			this.tTimer_counter++;
			
			if (this.tTimer_counter > 10 && this.bnImageLoaded == false) {
				clearInterval(this.tTimer);
				obj.parentNode.style.height = "35px";
				this.startScroll(this.tickerColor, this.headerColor, this.fontColor);
				this.bnImageLoaded = true;
			}
		},
		
		startScroll: function(tickerColor, headerColor, fontColor){
			var scrollerinstance = this;
			this.setStyle(tickerColor, headerColor, fontColor);
			this.tickerHeight = this.tickerContentDiv.parentNode.offsetHeight;
			this.tickerContentDiv.style.top = this.getVerticalMiddle(this.tickerHeight + 1) + "px";
			
			//height of ticker content div
			this.actualHeight = this.tickerContentDiv.offsetHeight;
			if (window.opera || navigator.userAgent.indexOf("Netscape/7") != -1) {
				tickerContentDiv.style.height = tickerHeight + "px";
			}
			
			this.tickerContentDiv.style.overflow = "hidden";
			
			this.tickerContentDiv.innerHTML = this.content[this.current];
			var anchor = this.tickerContentDiv.firstChild;
			if (anchor.style == null) 
				anchor = this.tickerContentDiv.firstChild.nextSibling
			
			if (this.fontColor != null && this.fontColor != "") 
				anchor.style.cssText = 'text-decoration:none;color:' + this.fontColor + ' !important';
			
			
			if (this.content.length > 1) {
				this.scrollInterval = setInterval(function(){
					scrollerinstance.scroll();
				}, 100);
			}
			else {
				this.tickerContentDiv.style.top = this.getVerticalMiddle(this.tickerHeight + 1) + "px";
			}
		},
		
		scroll: function(){
			var stop = (!this.image)?0:this.getVerticalMiddle(this.tickerHeight + 1);
			var start = (this.tickerHeight + 1) * this.pausetime;
			
			
			if (!this.pause) {
				if (parseInt(this.tickerContentDiv.style.top) == stop) {
					//Pause if content is at the top and we havent paused yet
					this.copyspeed = this.pausespeed;
					this.pause = true;
				}
				else {
					//move scroller upwards
					if (parseInt(this.tickerContentDiv.style.top) <= (-1) * parseInt(this.tickerHeight)) {
						this.tickerContentDiv.innerHTML = this.content[this.current];
						var anchor = this.tickerContentDiv.firstChild;
						if (anchor.style == null) 
							anchor = this.tickerContentDiv.firstChild.nextSibling
						
						if (this.fontColor != null && this.fontColor != "") 
							anchor.style.cssText = 'text-decoration:none;color:' + this.fontColor + ' !important';
						this.tickerContentDiv.style.top = parseInt(this.tickerHeight) + 1 + "px";
					}
					else {
						this.tickerContentDiv.style.top = parseInt(this.tickerContentDiv.style.top) - this.copyspeed + "px";
					}
				}
			}
			else if (this.pause) {
				if (this.counter >= start) { //Restart if paused
					//else, reset to original position
					this.current = (this.current + 1) % (this.last + 1);
					
					this.copyspeed = this.scrollspeed;
					this.tickerContentDiv.style.top = parseInt(this.tickerContentDiv.style.top) - this.copyspeed + "px";
					
					//move scroller upwards
					this.pause = false;
					this.counter = 0;
				}
			}
			this.counter = (this.counter+1) % (start + 1);
		},
		
		setStyle: function(tickerColor, headerColor, fontColor){
			var style;
			var header = this.tickerContentDiv.parentNode.firstChild;
			if (header.style == null) 
				header = this.tickerContentDiv.parentNode.firstChild.nextSibling;
			
			if (tickerColor != null && tickerColor != "") {
				style = this.tickerContentDiv.parentNode.style.cssText;
				this.tickerContentDiv.parentNode.style.cssText = style + ';background-color: ' + tickerColor + ' !important';
				
				style = this.tickerContentDiv.style.cssText;
				this.tickerContentDiv.style.cssText = style + ';background-color:' + tickerColor + ' !important';
			}
			
			if (headerColor != null && headerColor != "") {
				style = header.style.cssText;
				header.style.cssText = style + ';background-color:' + headerColor + ' !important';
			}
			
			if (fontColor != null && fontColor != "") {
				style = header.style.cssText;
				header.style.cssText = style + ';color:' + fontColor + ' !important';
				this.fontColor = fontColor;
			}
		},
		getVerticalMiddle: function(divHeight)
		{
		    //The the text should be positioned in middle of the parent div and then is moved up by half line-height
		    var middle = parseInt(divHeight / 2);
		
		    if ((middle % 2 == 0 && divHeight % 2 != 0) || (middle % 2 != 0 && divHeight % 2 == 0)) {
		        middle = middle + 1;
		    }
		
		    //The default font size is 12px
		    middle -= 10;
			
			this.getVerticalMiddle = function() {return middle;}
			
			return middle;
		}
	});
}



