/**
 * @author:		Dini Angelo
 * @copyright	Maxomedia - Agentur für Crossmedia-Kommunikation BSW
 * @version:	1.1 (05.06.2009)
 */

// debug
if (window['console'] === undefined) window.console = { log: $empty };

// $E
$E = document.getElement.bind(document);

// set namespace
var SBB = {};

// carousel
/**
 * TODO:
 * Support for vertical carousels
 * Status view (Slide x of x - clickable)
**/
var Carousel = new Class({

	Implements: [Options, Events],
	
	options: {
		trigger: {
			next: '.btn_next',
			back: '.btn_back'
		},
		link: 'cancel',
		transition: 'sine:in:out',
		duration: 1000
	},
	
	initialize: function (items, options) {
		// check for multiple items to slide
		this.items = [];
		this.multiple = 1;
		if(items.length > 1) {
			items.each(function (item, index) {
				this.items[index] = $$(item);
			}, this);
			this.multiple = items.length;
		} else {
			this.items = $$(items);
		}
		
		// items to be animated
		//this.items = $$(items);
		console.log(this.items);
		this.setOptions(options);
	}

});

// slideshow
SBB.Slideshow = {
	init: function (bound) {
		this.container = $('slideshow');
		this.viewport = this.container.getElement('.viewport');
		this.header = $('jump-mainnav');
		this.position = 0;
		this.bound = bound.toInt();
		
		this.fx = new Fx.Tween(this.viewport, {
			duration: 1000,
			link: 'cancel',
			transition: 'sine:in:out'
		});
		this.fx.set('left', 0);

		this.fxHead = new Fx.Tween(this.header, {
			duration: 1000,
			link: 'cancel',
			transition: 'sine:in:out'
		});
		this.fxHead.set('background-position', '0 0');

		this.trigger = {
			next: this.container.getElement('.btn_next'),
			back: this.container.getElement('.btn_back')
		}
		this.trigger.next.setStyle('display', 'block');
		this.trigger.next.addEvent('click', this.moveViewport.bindWithEvent(this, ['next', true]));

		this.trigger.back.setStyle('display', 'block');
		this.trigger.back.addEvent('click', this.moveViewport.bindWithEvent(this, ['back', true]));
		
		this.timer = (function () { this.moveViewport(false, 'next', false); }).bind(this).periodical(5000);
	},
	moveViewport: function (event, dir, state) {
		if(event) event.stop();
		if(state) $clear(this.timer);
		
		if(dir == 'next' && this.position > -((this.bound-1)*960)) {
			this.position = this.position -960;
		} else if(dir == 'back' && this.positon != 0) {
			this.position = this.position +960;
		} else {
			this.position = 0;
			this.fx.set('left',0);
			this.fxHead.set('background-position', '0 0');
			console.log('trigger');
		}
		// exception
		if(!(this.position < 0)) this.position = 0;
		
		// init
		this.fx.start('left', this.position);
		this.fxHead.start('background-position', this.position + ' 0');
	}
}


// target blank hack
SBB.SetAnchorTarget = new Class({
	Implements: Options,
	
	options: {
		target: '_blank',
		className: 'target:blank'
	},

	initialize: function (options) {
		this.setOptions(options);
		$$('a').each(function (item) {
			if(item.hasClass(this.options.className)) {
				item.set('target', this.options.target);
			}
		}, this);
	}

});

// google analytics external links tracker
SBB.externalTracker = {
	init: function () {
		$$('a').forEach(function (anchor) {
			var href = anchor.get('href');
			if (href) {
				if (href.test(/^http:\/\//) || href.test(/^https:\/\//)) anchor.addEvent('click', this.track.pass(['outbound', '/' + href], this));
			}
		}, this)
	},
	
	track: function (type, href) {
		pageTracker._trackPageview('/' + type + href);
	}
};

// Flashloader
var FlashLoader = new Class({

	initialize: function (containers) {
		this.containers = containers;
		this.containers.forEach(function (item) {
			this.loadParams(item);	
		}.bind(this));
	},

	loadParams: function (item) {
		this.container = $(item);
		this.objTag = this.container.getElement('object');
		
		this.params = new Hash();
		this.paramElements = this.container.getElements('param');
		this.paramElements.each(function (param) {
			this.params.include(param.get('name'), param.get('value'));
		}, this);
		
		this.loadSwiff();
	},
	
	loadSwiff: function () {
		new Swiff(this.params.get('movie'), {
			width: this.objTag.width,
			height: this.objTag.height,
			params: this.params.getClean(),
			container: this.container
		});
	}

});

window.addEvent('domready', function () {
	new SBB.SetAnchorTarget();
	SBB.externalTracker.init();
	//if($('slideshow')) SBB.Slideshow.init('5');
});
