/**
 * @fileOverview Enhancements for overflow:hidden-Elements
 * @version 0.1
 * @author Michael Schieben <michael@twoantennas.com> 
 */

/*jslint nomen: true, debug: true, evil: false, onevar: false, browser: true, plusplus: false, undef: true */
/*global window: true, KfW: true, jQuery: true */

(function ($) {
	
	function Teaser() {
		
		$('.eui-teaser').enhancedTeaser();
		
	}
	
	$.fn.enhancedTeaser = function (options) {
		
		var defaultSettings = {
			interval: 7000
		};
		
		options = $.extend(defaultSettings, options);
		
		return this.each(function() {
			
			var teaser = $(this);
			var header = teaser.find('.header');
			var container = teaser.find('.eui-teaser-wrapper');
			var pagination = null;
			var scroller = null;
			var items = teaser.find('.news');
			var timer = null;
			var pause = false;
			var that = this;
			var startDate = new Date();
			
			function slide(li) {
				
				if(timer && !pause) {
					window.clearInterval(timer);
					timer = null;
				}
				
				var id = li.find('a').anchor();
				pagination.find('li').removeClass('selected');
				li.addClass('selected');
				scroller.scrollTo('#' + id, 500, function() {
					if(!timer && !pause) {
						timer = window.setInterval(next, options.interval);
					}
				});
			}			
			
			function initializePagination() {
				
				var h = '';
				
				items.each(function(i){
					
					var id = $(this).attr('id');
					var title = $(this).find('h2').text();
					
					if (id === '') { id = KfW.id('eui-teaser-item'); }
					$(this).attr('id', id);
					
					h += KfW.Template.render('eui-teaser-pagination-item', {id: id, title: title});
					
				});
				
				
				pagination = KfW.Template.element('eui-teaser-pagination', {items: h});
				header.append(pagination);
				
				pagination.find('li a').click(function(e) {
					e.preventDefault();
					slide($(this).parents('li'));
				});
				
				header.append(KfW.Template.render('eui-teaser-pagination-switch'));
				header.find('.eui-teaser-pagination-switch a').click(function(e) {
					e.preventDefault();
					var toggle = $(this);
					
					if(timer && !pause) {
						pause = true;
						window.clearInterval(timer);
						timer = null
						toggle.parent().removeClass('eui-teaser-pagination-switch-pause');
						toggle.parent().addClass('eui-teaser-pagination-switch-play');
						toggle.text('Slide-Show starten …')
					} else {
						pause = false;
						start();
						toggle.parent().removeClass('eui-teaser-pagination-switch-play');
						toggle.parent().addClass('eui-teaser-pagination-switch-pause');
						toggle.text('Slide-Show pausieren …')
					}
				});
				
			}
			
			function initializeScroller() {
				
				var scrollerId = KfW.id('eui-teaser-wrapper');
				container.wrap('<div id="' + scrollerId + '"></div>');
				scroller = teaser.find('#' + scrollerId);
				scroller.css('width', teaser.width()).css('overflow', 'hidden');
				container.css('width', items.length * items.width());
				
				scroller.mouseenter(function (e) {
					window.clearInterval(timer);
					timer = null;
				});
				
				scroller.mouseleave(function (e) {
					start();
				});
			}
			
			function next() {
				var nextItem = pagination.find('li.selected').next();
				if (nextItem.size() === 0) {
					nextItem = pagination.find('li:first');
				}
				slide(nextItem);
			}			
			
			function start() {
				if(timer == null && pause == false) {
					timer = window.setInterval(next, options.interval);
				}
			}
									
			initializeScroller();
			initializePagination();
			slide(pagination.find('li:first'));
			start();
		});
		
	};
	
	KfW.addInitializer(Teaser);

}(jQuery));
