/*
	jQuery amazingSlider plugin - v1.0.1
	Copyright 2011 b:dreizehn, Germany
	Autor: Daniel Sattler

	"jQuery amazingSlider plugin" is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.
	
	"jQuery amazingSlider plugin" is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with "jQuery amazingSlider plugin".	If not, see <http://www.gnu.org/licenses/>.
*/

(function($, undefined) {

	$.amazingSlider = function(el, options) {
		// avoid scope issues
		var me = this;
		
		// shorthand options notation
		var opts;
		
		var timeout;
		
		var duration = {
			'timeout': 8000
		}
		
		//show next teaser img
		me.showNextTeaser = function(teaserNr) {
			var crtEl = me.$entry.siblings('.current');
			
			me.$entry.eq(teaserNr).addClass('current').css({'zIndex': '10'});
			$(crtEl).removeClass('current').css({'zIndex': '5'});
			
			me.$entry.eq(teaserNr).fadeIn('slow', function() {
				$(crtEl).hide();
			});
			//refresh page Browser
			me.$pageBrowser.children().eq(teaserNr).addClass('current').siblings().removeClass('current');
		}
		
		//start animation
		me.startAnimation = function() {
			var crtEl = me.$entry.siblings('.current');
			var nextEl = $(crtEl).next('.singleTeaser');
			
			if (nextEl.length < 1) {
				nextEl = me.$entry.first();
			}
			
			var showTeaserNr = nextEl.index();
			//doing the animation
			me.showNextTeaser(showTeaserNr);
			
			timeout = window.setTimeout(function() {
				me.startAnimation();
			}, duration.timeout);
		}
		
		
		// initialization function; private
		me.init = function() {

			// store DOM element and jQuery object for later use
			me.el = el;
			me.$el = $(el);
			
			// store options
			opts = me.options = $.extend({}, me.defaultOptions, options);
			
			//img entrys
			me.$entry = me.$el.find('.singleTeaser');
			
			//check if the el has more then 2 img's else return
			if (me.$entry.length < 2) {
				return;
			}
			
			//init the inline style - used for the animation. start with the first img
			me.$entry.first().show().addClass("current").css({'zIndex': '5'}).siblings().hide().css({'zIndex': '10'});
			
			//build Controls for the pageBrowser
			var pageBrowserObj = "<div class='pageBrowser'>";
			
			for (i = 0; i < me.$entry.length; i++) {
				if (i == 0) {
					pageBrowserObj += "<span class='current' style='cursor: pointer;'></span>";
				} else {
					pageBrowserObj += "<span style='cursor: pointer;'></span>";
				}
			}
			pageBrowserObj += "</div>";
			
			//append page browser to the slider
			me.$entry.parent().append(pageBrowserObj);
			
			//referens to the pageBrowser
			me.$pageBrowser = me.$entry.add('.pageBrowser');
			me.$pageBrowser = me.$pageBrowser.last();
			
			
			// initialize events
			me.initEvents();
			
			//start animation
			timeout = window.setTimeout(function() {
				me.startAnimation();
			}, duration.timeout);
			
			return me;
		};

		me.initEvents = function() {
			
			//restart animation
			continueAnimation = function() {
				me.$pageBrowser.unbind('mouseleave');
				window.setTimeout(function() {
					me.startAnimation();
					me.$pageBrowser.bind('mouseleave', function() {
						continueAnimation();
					});
				}, duration.timeout);
			}
			
			//stop the animation when the mouse is over the page browser
			me.$pageBrowser.mouseenter(function() {
				//stop animation
				window.clearTimeout(timeout);
			})
			me.$pageBrowser.bind('mouseleave', function() {
				//restart animation
				continueAnimation();
			});
			
			me.$pageBrowser.children().click(function() {
				me.$pageBrowser.stop(true, true);
				if (!$(this).hasClass('current')) {
					var showTeaserNr = $(this).index();
					me.showNextTeaser(showTeaserNr);
				}
			});
		};
		
		// initialize ourself
		me.init();
	};

	$.fn.amazingSlider = function(options) {
	   // apply all util functions to ourself (for use in templates, etc.)
	   //$.extend($.amazingSlider, <insert your util object here>);
		return this.each(function() {
			(new $.amazingSlider(this, options));
		});
	};

})(jQuery);


