var futureUsAutoScroller = Class.create();

futureUsAutoScroller.winId = null;
futureUsAutoScroller.curSpeed = 1;
futureUsAutoScroller.scrolling = false;
futureUsAutoScroller.numSpeed = 5;

futureUsAutoScroller.prototype = {
	initialize : function () {
		var win = new Window(Object.extend({
				className: "autoscroll",
				title: "&nbsp;",
				top:400,
				left:700,
	            minWidth: 72,
				width:72,
				height:126,
	            recenterAuto: true,
	            resizable:    false,
	 		    closable:     false,
		        minimizable:  false,
	            maximizable:  false,
				showEffectOptions: {duration:1.5}
				}, arguments[0] || {}) );

	    futureUsAutoScroller.winId = win.getId();
	    win.getContent().innerHTML= '<div class="plus"'
						+ ' onmouseover="futureUsAutoScroller.prototype.showSpeedStatus()"'
						+ ' onmouseout="futureUsAutoScroller.prototype.hideSpeedStatus()"'
	    				+ ' ><a href="javascript:void(0)" '
	    				+ ' onclick="futureUsAutoScroller.prototype.changeSpeed(\'+\');return false;"><img '
	    				+ ' src="/img/pixel.gif" width="39" height="38" border="0" alt="Speed Up" /></a></div>'
						+ '<div class="play" id="futureUsAutoScroller_action" '
						+ ' onmouseover="futureUsAutoScroller.prototype.showSpeedStatus()"'
						+ ' onmouseout="futureUsAutoScroller.prototype.hideSpeedStatus()"'
						+ ' ><a href="javascript:void(0)" '
						+ ' onclick="futureUsAutoScroller.prototype.scrollAction();return false;"><img '
						+ ' src="/img/pixel.gif" width="39" height="22" border="0" alt="Pause" /></a></div>'
						+ '<div class="minus" '
						+ ' onmouseover="futureUsAutoScroller.prototype.showSpeedStatus()"'
						+ ' onmouseout="futureUsAutoScroller.prototype.hideSpeedStatus()"'
						+ ' ><a href="javascript:void(0)" '
						+ ' onclick="futureUsAutoScroller.prototype.changeSpeed(\'-\');return false;"><img '
						+ ' src="/img/pixel.gif" width="39" height="38" border="0" alt="Speed Down" /></a></div>';
		win.show();

		this.createSpeedStatus();
	},

	createSpeedStatus: function () {
		var total_height = 60 * futureUsAutoScroller.numSpeed;

		var div = document.createElement('div');		
		div.setAttribute('id', 'futureUsAutoScrollerStatus');
		
		div.style.cssText = 'position:absolute; z-index:2; top:0px; left:0px; width:87px; height:'+total_height+'px; display:none;';

		if (BrowserDetect.browser == "MSIE") {
			div.style.cssText += ' filter:alpha(opacity=80);';
		} else {
			div.style.cssText += ' -moz-opacity: 0.8;';
			div.style.cssText += ' opacity: 0.8;';
		}

		for(var i=(futureUsAutoScroller.numSpeed-1); i>=0; i--) {
			var div2 = document.createElement('div');		
			div2.setAttribute('id', 'futureUsAutoScrollerStatus'+i);
			div2.style.cssText = 'position:relative;width:87px; height:56px; margin: 2px 0 2px 0;';

			var img = document.createElement('img');
			img.id = 'futureUsAutoScrollerStatusImg'+i;
			if (i >= futureUsAutoScroller.curSpeed) {
				img.src = '/img/scroll_status_off.gif';
			} else {
				img.src = '/img/scroll_status_on.gif';
			}
			img.width = 87;
			img.height = 56;
			img.border = 0;
			img.alt = '';

			div2.appendChild(img);
			div.appendChild(div2);
		}

		document.body.appendChild(div);
	},

	showSpeedStatus: function () {
		var div = document.getElementById('futureUsAutoScrollerStatus');

		var x = getElementPosition($(futureUsAutoScroller.winId)).x;
		var y = getElementPosition($(futureUsAutoScroller.winId)).y;

		if (x >= 91) {
			div.style.left = (x - 91) + 'px';
		} else {
			div.style.left = (x + 75) + 'px';
		}
		div.style.top = y + 'px';

		$('futureUsAutoScrollerStatus').show();	
	},

	hideSpeedStatus: function () {
		$('futureUsAutoScrollerStatus').hide();	
	},

    loadSpeed: function () {
		for(var i=0;i<futureUsAutoScroller.curSpeed;i++) {
			$('futureUsAutoScrollerStatusImg'+i).src = '/img/scroll_status_on.gif';
		}
		for(i=i; i<futureUsAutoScroller.numSpeed;i++) {
			$('futureUsAutoScrollerStatusImg'+i).src = '/img/scroll_status_off.gif';
		}
	},

	scrollAction: function (event) {
		if ($('futureUsAutoScroller_action').className == 'pause') {
			$('futureUsAutoScroller_action').className = 'play';
			futureUsAutoScroller.scrolling = false;
		} else {
			$('futureUsAutoScroller_action').className = 'pause';
			futureUsAutoScroller.scrolling = true;

	        this.autoScroll();
		}
	},

	changeSpeed: function (type) {
		if(type=="-")
			futureUsAutoScroller.prototype.setSpeed(futureUsAutoScroller.curSpeed-1);
		if(type=="+")
			futureUsAutoScroller.prototype.setSpeed(futureUsAutoScroller.curSpeed+1);

		this.loadSpeed();
	},

    setSpeed: function (speed) {
        if (speed < 1)
            futureUsAutoScroller.curSpeed = 1;
        else if (speed > futureUsAutoScroller.numSpeed)
            futureUsAutoScroller.curSpeed = futureUsAutoScroller.numSpeed;
        else
            futureUsAutoScroller.curSpeed = speed;
    },

	setElementPosition: function (obj, k, val) {
		var	kk	=	0;

		if(k=='y') {
			if (typeof(obj.offsetTop != undefined)) obj.style.top = val + 'px';

			$('futureUsAutoScrollerStatus').style.top = (getElementPosition($(futureUsAutoScroller.winId)).y) + 'px';
		}
		else if(k=='x') {
			if (typeof(obj.offsetLeft != undefined)) obj.style.left = val + 'px';
		}
	},

	getScrollXY: function () {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return { x:scrOfX, y:scrOfY };
	},

	autoScroll: function () {
		if (! futureUsAutoScroller.scrolling) return false;
		
    	window.scrollBy(0,1); // horizontal and vertical scroll increments
		if ((this.getScrollXY().y+30) > getElementPosition($(futureUsAutoScroller.winId)).y)
	        this.setElementPosition($(futureUsAutoScroller.winId), 'y', this.getScrollXY().y+30);

		scrolldelay = setTimeout('futureUsAutoScroller.prototype.autoScroll()',(futureUsAutoScroller.numSpeed-futureUsAutoScroller.curSpeed+10)*8); // scrolls every 100 milliseconds
	}
}
