var FM_TeaserControl = new Class({
	
	init: function(itemContainer){
		
		this.itemContainer = itemContainer;
		this.fadeinfx = null;
		this.fadeoutfx = null;
		this.options = {
			zIndex 		: 200,
			duration 	: 2500,
			interval 	: 6000
		};
		this.teaserItems = this.getTeaserItems();
		
		if(this.teaserItems.length> 1) {
			this.timer = this.showNextItem.bind(this).periodical(this.options.interval);
		}
	},
	
	getTeaserItems : function(){
		
		var tempItems = [];
		$A($(this.itemContainer).getElements('div')).each(function(el,index){
			if(el.hasClass('teaser_karussell_item')){
				tempItems.push(el);
				
				if(el.getStyle('visibility') != 'hidden'){
					this.currentItem = index;
				}
			}			
		}, this);
		
		return tempItems;
		
	},
	
	showNextItem : function(){
		this.showItemNumber((this.currentItem+1) % this.teaserItems.length);
	},
	
	showPrevItem : function(){		
		this.showItemNumber((this.currentItem-1 + this.teaserItems.length) % this.teaserItems.length);
	},

	forward : function(){
	    	this.stopEffect();

		$clear(this.timer);
		this.timer = null;

		this.showItemNumber((this.currentItem+1) % this.teaserItems.length);

		this.timer = this.showNextItem.bind(this).periodical(this.options.interval);
	},

	backward : function(){
	    	this.stopEffect();
		$clear(this.timer);
		this.timer = null;
		if(this.currentItem == 0) {
			var itemNum = this.teaserItems.length;
		} else {
			var itemNum = this.currentItem;
		}
		this.showItemNumber((itemNum-1) % this.teaserItems.length);

		this.timer = this.showNextItem.bind(this).periodical(this.options.interval);
	},
	
	clickShowItem : function(event, nextItem){
		this.stopEffect();
		
		$clear(this.timer);
		this.timer = null;
		
		this.showItemNumber(nextItem);
		
		this.timer = this.showNextItem.bind(this).periodical(this.options.interval);
	},
	
	showItemNumber : function (nextItem) {		
		this.stopEffect();
		
		var e1 = this.teaserItems[this.currentItem];
		var e2 = this.teaserItems[nextItem];
		var to1 = 0;
		var to2 = 1;

		this.fadeoutfx = new Fx.Tween(e1, {duration: this.options.duration, property: 'opacity', wait: true, fps:16 ,transition: Fx.Transitions.linear});
		this.fadeinfx = new Fx.Tween(e2, {duration: this.options.duration, property: 'opacity', wait: true, fps:16 ,transition: Fx.Transitions.linear});

		if (this.currentItem == 0) {
			e1.setStyle("z-index",200);
		}

		e2.setStyle("z-index",(e1.getStyle("z-index")-1));
		e2.setStyle('visibility','visible');
		e1.setStyle("opacity",to2);
		e2.setStyle("opacity",to1);

		this.fadeoutfx.start(to1);
		this.fadeinfx.start(to2);
		this.currentItem = nextItem;
	},

	stopEffect : function(){
		if(this.fadeoutfx){
			this.fadeoutfx.cancel();
		}
		
		if(this.fadeinfx){
			this.fadeinfx.cancel();
		}
	},

	photograph : function(){
		var currentItem = this.teaserItems[this.currentItem];
		var info = currentItem.getElement('div').getElement('div').getElement('.photographersinfo');
		$('photographersinfo_box').set('html', info.get('html'));
		$('photographersinfo_box').setStyle('display', 'block');
	},

	startIntervall : function() {
		this.timer = this.showNextItem.bind(this).periodical(this.options.interval);
	},

	stopIntervall : function() {
		this.stopEffect();
		$clear(this.timer);
	}
});

window.addEvent('domready', function()    {
   var header = $$('div.teaser_karussell');
   header.each(function(el){
   		el.fm_teaserControl = new FM_TeaserControl()
   		el.fm_teaserControl.init(el);
		$('forward').addEvent('click',function(e){
			e.stop();
			el.fm_teaserControl.forward();
		});
		$('backward').addEvent('click',function(e){
			e.stop();
			el.fm_teaserControl.backward();
		});
		$('photograph').addEvents({
			    'mouseenter': function() { 
				el.fm_teaserControl.photograph();
				el.fm_teaserControl.stopIntervall();
			    },
			    'mouseleave': function() { 
				$('photographersinfo_box').setStyle('display', 'none');
				el.fm_teaserControl.startIntervall();
			    }
		});
		$('photographersinfo_box').addEvents({
			    'mouseenter': function() {
				$(this).setStyle('display', 'block');
				el.fm_teaserControl.stopIntervall();
			    },
			    'mouseleave': function() {
				$(this).setStyle('display', 'none');
				el.fm_teaserControl.startIntervall();
			    }
		});
   });

   var karussell_small = $$('div.teaser_karussell_small');
   karussell_small.each(function(el){
   		el.fm_teaserControl = new FM_TeaserControl()
   		el.fm_teaserControl.init(el);
		$('forward_small').addEvent('click',function(e){
			e.stop();
			el.fm_teaserControl.forward();
		});
		$('backward_small').addEvent('click',function(e){
			e.stop();
			el.fm_teaserControl.backward();
		});
   });

   //Make the teaser items clickable 
   var teaserItems = $$('div.teaser_karussell_item');
   teaserItems.each(function(el){
	   var teaserUrl = el.get('teaser_url');
	   if(teaserUrl){
		   el.teaserUrl = teaserUrl
		   el.addEvent('click',function(e){
			   /*If there is no slash at the beginning of the url the ie 6 & 7 dosen't jump
			   to the new url, they only add it to the existing url*/
			   window.location = "/" + el.teaserUrl;
		   });
	   }
   });
   
});
