/*
	scrolling.js
	
	used to make the images on the home page scroll accross the screen.
*/


var scroll = new Object();

scroll.Horizontal = Class.create();
scroll.Horizontal.prototype = {
	
	initialize: function (el, options) {
		this.el = $(el);
		this.setOptions(options);
		
	
		this.el = $(el);
		this.setOptions(options);
		
		Element.cleanWhitespace(this.el);

		this.el.style.position = 'relative';
		this.el.style.overflow = 'hidden';
		this.inner = this.el.firstChild;
		
		this.initialWidth = el.offsetWidth;

		Element.cleanWhitespace(this.inner);
		
		var dif = this.el.offsetWidth - this.calcInnerWidth();
		
		while(dif > 0)
		{
			var n = this.inner.childNodes[index].cloneNode(true);
			this.inner.appendChild(n);
			dif = this.el.offsetWidth - this.calcInnerWidth();
		}
		
		for(var i = 0; i < this.inner.childNodes.length; i++)
		{
			if(this.options.onItemOver)
				this.inner.childNodes[i].onmouseover = this.options.onItemOver;
				
			if(this.options.onItemOut)
				this.inner.childNodes[i].onmouseout = this.options.onItemOut;
				
			if(this.options.onItemClick)
				this.inner.childNodes[i].onclick = this.options.onClick;
		}
			
		this.go();
	},
	
	calcInnerWidth : function() {
		var w = 0;
		for(var i = 0; i < this.inner.childNodes.length; i++)
			w += this.inner.childNodes[i].offsetWidth;
			
		return w;
	},
	
	setOptions: function(options) {
		
		this.options = {
			onComplete: '',
			interval: 50,
			scrolling: true,
			onItemOver: '',
			onItemOut: '',
			onItemClick: ''
		}
		
		Object.extend(this.options, options || {});	
	},
	
	go: function() {
		if(this.options.scrolling)
			this.timer = setInterval (this.step.bind(this), this.options.interval);
	},

	step: function() {
		this.pan();
		clearInterval(this.timer);
		this.timer = null;
		if(this.options.scrolling)
			this.timer = setInterval (this.step.bind(this), this.options.interval);
	},

	pan : function() {
		var left = (this.inner.style.left)? parseInt(this.inner.style.left) : 0;
		this.inner.style.left = left - 1 + "px";
		
		var fc = this.inner.firstChild;

		if(fc.offsetWidth - Math.abs(this.inner.offsetLeft) < -5)
		{
			this.inner.style.left = "-6px";
			this.inner.appendChild(fc);
		}
	},

	clearTimer: function() {
		clearInterval(this.timer);
		this.timer = null;
	},
	
	toggle : function() {
		this.options.scrolling = !this.options.scrolling;
		this.go();
	}
}


function standardizeEvent(event) {

	if( !event.stopPropagation )
		event.stopPropagation = new Function('this.cancelBubble = true');

	if ( !event.preventDefault )
		event.preventDefault = new Function('this.returnValue = true')

	if( typeof event.layerX == 'undefined' &&
	    typeof event.offsetX == 'number' ) {
	
		event.layerX = event.offsetX
		event.layerY = event.offsetY
	}

	if( !event.target && event.srcElement ) {
	
		event.target = event.srcElement

		if ( event.type == 'onmouseout' ) {
			event.relatedTarget = event.toElement

		} 
		else if( event.type == 'onmouseover' ) {
			event.relatedTarget = event.fromElement
		}
	}
}
