/**
 * @author matthijs groen
 */

/**
 * @author matthijs groen
 */

(function($) {

	ImageScroller = $.klass({
		initialize: function(item_selector, speed) {
			this.images = $(item_selector, this.element).hide();
			this.max = this.images.size();
			
			this.current_image = 0;
			this.next_image = this.getNextIndex(this.current_image);				
			this.images.eq(this.current_image).show();
			
			this.element.bind("next", $.bind(this.nextImage, this));
			this.speed = speed
			
			this.callNext();				
		},
		nextImage: function() {
			this.images.eq(this.current_image).css({ zIndex: 10 })

			if (jQuery.support.opacity) {
		  	this.images.eq(this.next_image).css({
		  		zIndex: 12,
		  		opacity: 0
		  	}).show().animate({
		  		opacity: 1
		  	}, 2000, "linear", $.bind(function(){
		  		this.images.eq(this.current_image).hide();
		  		this.current_image = this.next_image;
		  		this.next_image = this.getNextIndex(this.current_image);
		  		this.callNext();
		  	}, this));
		  } else {
		  	this.images.eq(this.next_image).css({
		  		zIndex: 12
		  	}).show();
		  	
		  	this.images.eq(this.current_image).hide();
		  	this.current_image = this.next_image;
		  	this.next_image = this.getNextIndex(this.current_image);
		  	this.callNext();
		  }
		},
		getNextIndex: function(index) {
			return (index >= this.max -1) ? 0 : index + 1;  
		},
		callNext: function() {
			setTimeout($.bind(this.nextImage, this), this.speed);
		}
	});

	$(document).ready(function($) {
		$("#imagebar").attach(ImageScroller, ".front_page_image", 9000);
		
	});	
	
})(jQuery);
