/*
gbTicker v1.0 mit MooTools v1.2

von gb media (www.gb-media.biz) 2009
Vielen Dank an die MooTools Entwickler
*/

var gbTicker = new Class({

	Implements: Options,
	
	options: {
		handler		: 'itemTicker',
		btnStop		: 'btn_stop',
		btnPlay		: 'btn_play',
		btnDirect_h	: 'btn_hori',
		btnDirect_v	: 'btn_verti',
		current		: 0,
		intervall	: 5,
		play		: true,
		duration	: 1500,
		transition	: Fx.Transitions.Expo.easeOut,
		direction	: 'vertical'
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.elTicker = $(this.options.handler);
		this.w = this.elTicker.getSize().x;
		this.h = this.elTicker.getSize().y;
		this.newDirection = this.options.direction;
		this.bound = {};
		if ($(this.options.btnStop)){
			stopEl = $(this.options.btnStop).getElement('a');
			this.bound.stopTicker = this.stopTicker.bindWithEvent(this, stopEl);
			stopEl.addEvent('click', this.bound.stopTicker);
		}
		if ($(this.options.btnPlay)){
			playEl = $(this.options.btnPlay).getElement('a');
			this.bound.playTicker = this.playTicker.bindWithEvent(this, playEl);
			playEl.addEvent('click', this.bound.playTicker);
		}
		if ($(this.options.btnDirect_h)){
			horiEl = $(this.options.btnDirect_h).getElement('a');
			this.bound.setHorizontal = this.setHorizontal.bindWithEvent(this, horiEl);
			horiEl.addEvent('click', this.bound.setHorizontal);
		}
		if ($(this.options.btnDirect_v)){
			vertiEl = $(this.options.btnDirect_v).getElement('a');
			this.bound.setVertical = this.setVertical.bindWithEvent(this, vertiEl);
			vertiEl.addEvent('click', this.bound.setVertical);
		}
		
		this.setTicker ();
	},
	
	setTicker: function (){
		this.items = this.elTicker.getElements('li');
		this.options.current = this.options.current > this.items.length-1 || this.options.current < 0 ? 0 : this.options.current;
		w = 0;
		h = 0;
		if(this.options.direction.toLowerCase()=='horizontal') {
			this.setBtnDirection('none', 'inline');
			h = this.h;
			this.items.each(function(el,i) {
				w += el.getSize().x;
			});
		} else {
			this.setBtnDirection('inline', 'none');
			w = this.w;
			this.items.each(function(el,i) {
				h += el.getSize().y;
			});
		}
		pos = this.getPos();
		this.elTicker.setStyles({
			position: 'absolute',
			width: w,
			height: h,
			top: pos.top,
			left: pos.left
		});
		this.fx = new Fx.Morph(this.elTicker,	{
			duration: this.options.duration,
			transition: this.options.transition,
			onComplete:function() {
				this.injectEl();
			}.bind(this)
		});
		for (i=0; i<this.options.current; i++){
			this.injectEl(i);
		}
		this.setBtn('inline', 'none');
		if (!this.options.play){
			this.setBtn('none', 'inline');
		}
		//alert (this.options.current);
		this.playItem();
	},
	
	stopTicker: function (){
		$clear(this.intervall);
		this.options.play = false;
		this.setBtn('none', 'inline');
	},
	
	playTicker: function (){
		this.options.play = true;
		this.nextItem();
		this.setBtn('inline', 'none');
	},
	
	setHorizontal: function (){
		this.setBtnDirection('none', 'inline');
		this.newDirection = 'horizontal';
	},
	
	setVertical: function (){
		this.setBtnDirection('inline', 'none');
		this.newDirection = 'vertical';
	},
	
	setBtn: function (s, p){
		if ($(this.options.btnStop)){
			$(this.options.btnStop).setStyles({
				display: s
			});
		}
		if ($(this.options.btnPlay)){
			$(this.options.btnPlay).setStyles({
				display: p
			});
		}
	},
	
	setBtnDirection: function (h, v){
		if ($(this.options.btnDirect_h)){
			$(this.options.btnDirect_h).setStyles({
				display: h
			});
		}
		if ($(this.options.btnDirect_v)){
			$(this.options.btnDirect_v).setStyles({
				display: v
			});
		}
	},
	
	playItem: function (){
		if (!this.options.play){
			return ;
		}else if (this.newDirection != this.options.direction){
			this.options.direction = this.newDirection;
			this.options.current = this.aktuell-1;
			this.setTicker ();
			return ;
		}
		
		this.intervall = this.nextItem.delay((this.options.intervall*1000), this);
	},
	
	nextItem: function() {
		this.aktuell = this.options.current;
		this.options.current = this.options.current+1 >= this.items.length ? 0 : this.options.current+1;
		pos = this.getPos();
		this.fx.start({
			top: pos.top,
			left: pos.left
		}).chain(function() {
			this.playItem();
		}.bind(this));
	},
	
	getPos: function (){
		var el = this.items[this.options.current];
		pos = {
			top: -el.offsetTop,
			left: -el.offsetLeft
		}
		
		return pos;
	},
	
	injectEl: function (i){
		if (typeof i == 'undefined'){
			var i = this.options.current==0 ? this.items.length : this.options.current;
			i --;
		}
		this.items[i].injectInside(this.elTicker);
		this.elTicker.setStyles({
			left:0,
			top:0
		});
	}
	
});

gbTicker.implement(new Options);

window.addEvent('domready', function(){
	if (typeof gbTickerOpt == 'undefined'){
		gbTickerOpt = {}; 
	}
	
	gbTicker = new gbTicker(gbTickerOpt);
});
