var Interface = { }

Interface.Glider = Class.create({
	initialize: function(element,prev,next,options) {
		this.element = $(element);
		this.items = new Array();
		this.prev_button = $(prev);
		this.next_button = $(next);
		this.index = 0;
		this.distance = 0;

		this.options = options || { };
		this.options.itemclass = this.options.itemclass || 'item';
		this.options.tagname = this.options.tagname || 'div';
		this.options.slotclass = this.options.slotclass || 'slot';
		this.options.initpage = this.options.initpage || 0;
		
		var tmp = this.element.select('.'+this.options.itemclass);

		this.element.style.overflow = 'hidden';
		
		var total_width = 0;
		for(var i=0;i<tmp.length;i++){
			var slotcount = tmp[i].select('.'+this.options.slotclass).length;
			
			if(slotcount>0){	
				this.items.push(tmp[i]);
				total_width += Element.getWidth(tmp[i]);
			}
		}

		this.prev_button.hide();

		if(total_width<Element.getWidth(this.element)+10){
			this.next_button.hide();
		}
		
		if(this.options.initpage>0) this.jump(this.options.initpage);
		
		Event.observe(this.prev_button, 'click', this.prev.bindAsEventListener(this));
		Event.observe(this.next_button, 'click', this.next.bindAsEventListener(this));
	},
	
	move: function(nextIndex) {
		this.index = nextIndex;
		
		Position.prepare();
		container_offset = Position.cumulativeOffset(this.element),
		element_offset = Position.cumulativeOffset(this.items[this.index]);

		this.scrolling = new Effect.SmoothScroll(this.element, { duration: 0.5, x:element_offset[0]-container_offset[0], y:element_offset[1]-container_offset[1] });
		
		if(this.index>0){
			this.prev_button.show();
		}else{
			this.prev_button.hide();
		}
		
		if(this.index<this.items.length-1){
			this.next_button.show();
		}else{
			this.next_button.hide();
		}
	},
	
	jump: function(nextIndex) {
		this.index = nextIndex;
		
		Position.prepare();
		container_offset = Position.cumulativeOffset(this.element),
		element_offset = Position.cumulativeOffset(this.items[this.index]);

		this.element.scrollLeft = element_offset[0]-container_offset[0];
		this.element.scrollTop = element_offset[1]-container_offset[1];
		
		if(this.index>0){
			this.prev_button.show();
		}else{
			this.prev_button.hide();
		}
		
		if(this.index<this.items.length-1){
			this.next_button.show();
		}else{
			this.next_button.hide();
		}
	},
	
	prev: function() {
		this.move(this.index-1);
		this.prev_button.blur();
	},
	
	next: function() {
		this.move(this.index+1);
		this.next_button.blur();
	},
	
	first: function() {
		this.move(0);
	},
	
	last: function() {
		this.move(this.items.length-1);
	}
	
});

Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
	initialize: function(element) {
		this.element = $(element);
		var options = Object.extend({x:0,y:0,mode: 'absolute'}, arguments[1] || {});
		this.start(options);
	},
	
	setup: function() {
		if (this.options.continuous && !this.element._ext ) {
			this.element.cleanWhitespace();
			this.element._ext=true;
			this.element.appendChild(this.element.firstChild);
		}
	
		this.originalLeft=this.element.scrollLeft;
		this.originalTop=this.element.scrollTop;
	
		if(this.options.mode == 'absolute') {
			this.options.x -= this.originalLeft;
			this.options.y -= this.originalTop;
		} 		
	},
	
	update: function(position) {
		this.element.scrollLeft = this.options.x * position + this.originalLeft;
		this.element.scrollTop  = this.options.y * position + this.originalTop;
	}
});