// JavaScript Document

var newsScroller;

window.onload = function() {
	//initNewsScroller();
	newsScroller = new hScroller();
}


Effect.ScrollVertical = Class.create();

Object.extend(Object.extend(Effect.ScrollVertical.prototype, Effect.Base.prototype), 
{
	initialize: function(element) 
	{
		if(typeof element == "string")
		{		
			this.element = $(element);
			if(!this.element) 
			{
				throw(Effect._elementDoesNotExistError);
			}
		}
		
		var options = Object.extend({
			from: this.element.scrollTop || 0,
			to:   this.element.scrollHeight
		}, arguments[1] || {});
		
		options.to = options.to == this.element.scrollHeight ? options.to : options.from + options.to;
		
		this.start(options);
	},
	
	update: function(position) 
	{
		this.element.scrollTop = position;
	}
});

Effect.ScrollHorizontal = Class.create();

Object.extend(Object.extend(Effect.ScrollHorizontal.prototype, Effect.Base.prototype), 
{
	initialize: function(element) 
	{
		if(typeof element == "string")
		{		
			this.element = $(element);
			if(!this.element) 
			{
				throw(Effect._elementDoesNotExistError);
			}
		}
		
		var options = Object.extend({
			from: this.element.scrollLeft || 0,
			to:   this.element.scrollWidth
		}, arguments[1] || {});
		
		this.start(options);
	},
	
	update: function(position) 
	{
		this.element.scrollLeft = position;
	
	}
});

	
	
var hScroller = Class.create({
		initialize: function() {
			this.scrollWidth = 465;
			this.scrollIndexWidth = 17;
			this.scrollIndexOffset = 238;
			this.elementIndex = 0;
			this.scrollContainer = $('scrollContainer');
			this.numberOfElements = $('elementsContainer').immediateDescendants().length;
			$('scrollMarkerContainer').scrollLeft = this.scrollIndexOffset;
			$('scrollMarkerContainer').setStyle({width: this.scrollIndexWidth * this.numberOfElements + 'px'});
		},
		
		scrollLeft: function() {
			if (this.elementIndex -1 >= 0){
				new Effect.ScrollHorizontal('scrollContainer', { from:this.elementIndex * this.scrollWidth, to:(this.elementIndex-1) * this.scrollWidth });
				new Effect.ScrollHorizontal('scrollMarkerContainer', { from:this.scrollIndexOffset - this.elementIndex * this.scrollIndexWidth, to:this.scrollIndexOffset - (this.elementIndex-1) * this.scrollIndexWidth });
				this.elementIndex--;
			} else {        // scroll to end
				new Effect.ScrollHorizontal('scrollContainer', { from:this.elementIndex * this.scrollWidth, to:(this.numberOfElements-1) * this.scrollWidth });
				new Effect.ScrollHorizontal('scrollMarkerContainer', { from:this.scrollIndexOffset - this.elementIndex * this.scrollIndexWidth, to:this.scrollIndexOffset - (this.numberOfElements-1) * this.scrollIndexWidth });
				this.elementIndex = this.numberOfElements-1;
			}
		},
			
                scrollRight: function() {
                        if (this.elementIndex +1 < this.numberOfElements){
                                new Effect.ScrollHorizontal('scrollContainer', { from:this.elementIndex * this.scrollWidth, to:(this.elementIndex+1) * this.scrollWidth });
                                new Effect.ScrollHorizontal('scrollMarkerContainer', { from:this.scrollIndexOffset - this.elementIndex * this.scrollIndexWidth, to:this.scrollIndexOffset - (this.elementIndex+1) * this.scrollIndexWidth });
                                this.elementIndex++;
                        } else {        // scroll back to start
                                new Effect.ScrollHorizontal('scrollContainer', { from:this.elementIndex * this.scrollWidth, to:0 * this.scrollWidth });
                                new Effect.ScrollHorizontal('scrollMarkerContainer', { from:this.scrollIndexOffset - this.elementIndex * this.scrollIndexWidth, to:this.scrollIndexOffset - 0 * this.scrollIndexWidth });
                                this.elementIndex = 0;
                        }
		}
	});
	
	
	
	