/**
* Captano Microsite
* Custom Function JS
* (c) blueend web:applications AG
*/


FeatureSlider = Class.create();
FeatureSlider.prototype = {
	initialize:function(el,options){
		this.el = $(el);
		if (!this.el) return false;
		this.items = $(el).select('.item');

		// Options
		this.slide_margin = 10; // Margin between Slides
		this.auto_next = 5; // Go to next slide each x Seconds
		this.center_vertical = true; // Center items vertically

		// Init
		this.current_item = 0;
		this.item_height = 0;
		this.item_width = this.items[0].getWidth();
		this.items.each(function(el){
			if (this.item_height<el.getHeight()) this.item_height = el.getHeight();
			el.setStyle({position: 'absolute', width: this.item_width});
			el.hide();
		}.bind(this));

		this.el.setStyle({height: this.item_height, width: this.item_width, overflow: 'hidden', position: 'relative'});

		if (this.center_vertical)
			this.items.each(function(el){
				var new_height = ((this.item_height - el.getHeight()) / 2);
				el.setStyle({top: new_height});
			}.bind(this));

		// Set Slide Positions based on item size
		this.position = [];
		this.position['start'] = [-this.item_width-this.slide_margin,0]; // Start
		this.position['show'] = [0,0]; // Visible
		this.position['end'] = [this.item_width+this.slide_margin,0]; // End
		this.status = 'ready'; // Mini-Queue
		this.queue = [];
		this._moveIn(0, true);

		if (this.auto_next>0)
			new PeriodicalExecuter(this._autoNext.bind(this), this.auto_next);
	},

	nextItem: function(no_queue){
		this.auto_next = false;
		this._nextItem();
	},

	previousItem: function(){
		this.auto_next = false;
		this._previousItem();
	},

	_autoNext: function(){
		if (this.auto_next>0) this._nextItem();
	},

	_nextItem: function(no_queue){
	  var nextItem = ((this.current_item + 1)>(this.items.length-1)) ? 0 : (this.current_item + 1);
		this._moveIn(nextItem,true);
		this._moveOut(this.current_item,true);
		this.current_item = nextItem;
	},

	_previousItem: function(no_queue){
	  var nextItem = ((this.current_item - 1)>=(0)) ? (this.current_item - 1) : this.items.length-1;
		this._moveIn(nextItem, false);
		this._moveOut(this.current_item, false);
		this.current_item = nextItem;
	},

	_moveIn:function(itemID, reverse){
		this.status = 'running';
		var startPos = (reverse) ? 'end' : 'start';
		this.items[itemID].setStyle({left: this.position[startPos][0]}).show(); //top: this.position[startPos][1]
		new Effect.Morph(this.items[itemID],{style: 'left: '+this.position['show'][0]+'px;', afterFinish: this.endEffect.bindAsEventListener(this)}); //top:'+this.position['show'][1]+'px;'
	},

	_moveOut:function(itemID, reverse){
		this.status = 'running';
		var endPos = (!reverse) ? 'end' : 'start';
		new Effect.Morph(this.items[itemID],{style: 'left: '+this.position[endPos][0]+'px;', afterFinish: this.endEffect.bindAsEventListener(this)}); //top:'+this.position[endPos][1]+'px;
	},

	endEffect:function(){
		return true;
		if (this.queue.length>0){
			var click = this.queue.shift();
			if (click=='next') this.nextItem(true); else this.previousItem(true);
		} else {
			this.status = 'ready';
		}
	},

	checkQueue:function(click){
		return true;
		if (this.status == 'running'){
			this.queue.push(click);
			return false;
		}
		return true;
	}
}

