var LinkedListHome = Class.create({
	initialize: function(container,navigation,loopspeed) {
		this.loopSpeed = loopspeed || 0;
		this.container = $(container); // the container
		this.navElements = $(navigation).childElements();
		this.lockAnimation = false; // lock
		this.elements = $(container).childElements(); // list of all elements
		this.current = 0;	// define current slide
		this.timeout = null;
		
		this.inqueue = -1;
		
		// set height and width hardcoded on container
		this.container.setStyle({
			height: $(container).getDimensions().height + 'px',
			width: $(container).getDimensions().width + 'px'
		});
		
		// set slides as absolute
		this.elements.invoke('setStyle', {
			position: 'absolute', 
			left: '0px', 
			top: '0px',
			height: $(container).getDimensions().height + 'px',
			width: $(container).getDimensions().width + 'px'
		});
		
		this.elements[this.current].show(); // show tab content
		this.elements[this.current].siblings().invoke('hide'); // hide other tabs
		
		var oThis = this;
		this.navElements.each(function(e,index){
			/*e.onclick = function(){
				oThis.change(index);
				return false;
			};*/
			e.onmouseover = function(){
				oThis.hover(index);
				return false;
			};
		})
		
		if(this.loopSpeed > 0){
			this.startLoop();
		}
	},
	change: function(target) {
		var target = target % this.elements.length; // catch possible invalid nr
		if(!this.lockAnimation && target != this.current){
			this.inqueue = -1;
			// lock switch
			this.lockAnimation = true;
			
			var currentSlide = this.elements[this.current];
			var nextSlide = this.elements[target]
			// move current to back
			currentSlide.style.zIndex = 10;
			
			// set z-index (in front existing slide) & hide it
			nextSlide.style.opacity = 0;
			nextSlide.style.filter = 'alpha(opacity=0)';
			nextSlide.style.zIndex = 20;
			
			this.navElements[this.current%this.navElements.length].removeClassName('current');
			this.navElements[target%this.navElements.length].addClassName('current');
			
			
			this.current = target;
			var oThis = this;
			// animate
			nextSlide.appear({ 
				duration: 0.6, 
				from: 0, 
				to: 1, 
				afterFinish: function(){
					// unlock switch
					oThis.lockAnimation = false;
					currentSlide.hide();
					if(oThis.inqueue != -1 ){
						oThis.change(oThis.inqueue);
					}else{
						oThis.startLoop();
					}
					
				} 
			});
		}else {
			this.inqueue = target;
		}
	},
	hover: function(nr){
		this.inqueue = nr;
		var oThis = this;
		try{clearTimeout(this.timeout)}catch(e){};
		this.timeout = setTimeout(function(){oThis.change(nr);},300);
	},
	next: function(){
		this.change((this.current + 1)% this.elements.length);
	},
	previous: function(){
		this.change((this.current+this.elements.length - 1)% this.elements.length);
	},
	startLoop: function(){
		var oThis = this;
		try{clearTimeout(this.timeout)}catch(e){};
		this.timeout = setTimeout(function(){oThis.next();},oThis.loopSpeed);
	}
});
