/* ------------------------------------------------------------------------
 s3Slider
 
 Developped By: Boban KariÅ¡ik -> http://www.serie3.info/
 CSS Help: MÃ©szÃ¡ros RÃ³bert -> http://www.perspectived.com/
 Version: 1.0
 
 Copyright: Feel free to redistribute the script/modify it, as
 long as you leave my infos at the top.
 edited by Christian Jaentsch
 ------------------------------------------------------------------------- */

Slider = {
	initialize: function(){
		jQuery.noConflict();
		Slider.Gallery.initialize();
	}
}

Slider.Gallery = {
	initialize: function(){
		jQuery(document).ready(function(){
			var galleries = jQuery('.slider');
			
			galleries.each(function(){
				var gallery = jQuery(this);
				

				
				if(gallery.length > 0) {
					gallery.s3Slider({
						timeOut: 8000
					});
				}
			});
		});
	}
}


Slider.initialize();



(function(jQuery){

	jQuery.fn.s3Slider = function(vars){
	
		var element = this;
		var timeOut = (vars.timeOut != undefined) ? vars.timeOut : 4000;
		var current = null;
		var timeOutFn = null;
		var buttonTimeOutFn = null;
		var paused = false;
		
	
		var items = jQuery(element).find("li.sliderImage");
		var itemsSpan = jQuery(element).find("li.sliderImage span");
		var buttons = jQuery(element).find(".sliderMenuButton");
		var img = jQuery(element).find("li.sliderImage img");

		buttons.each(function(i){
			jQuery(buttons[i]).click(function(){
				
				if (jQuery(this).hasClass("play")) {
					togglePlayPause(true);
					paused = false;
					fadeElement(true);
				} else if (jQuery(this).hasClass("pause")) {
					paused = true;
					togglePlayPause(false);
				} else if (jQuery(this).hasClass("prev")) {
					current = (current != null) ? current : items[(items.length - 1)];
					var prev = jQuery.inArray(current, items) - 1;
					prev = (prev < 0) ? (items.length - 1) : (prev);
					fade(prev, true);
				} else if (jQuery(this).hasClass("next")) {
					current = (current != null) ? current : items[(items.length - 1)];
					var next = jQuery.inArray(current, items) + 1;
					next = (next == items.length) ? 0 : (next);
					fade(next, true);
				}
				
				
			});
		});
		
		
		var togglePlayPause = function(play) {
			if(play) {
				buttons.each(function(i){
					if (jQuery(this).hasClass("play")) {
						jQuery(buttons[i]).hide();
					} else if (jQuery(this).hasClass("pause")) {
						jQuery(buttons[i]).show();
					}
				});
			} else {
				buttons.each(function(i){
					if (jQuery(this).hasClass("play")) {
						jQuery(buttons[i]).show();
					} else if (jQuery(this).hasClass("pause")) {
						jQuery(buttons[i]).hide();
					}
				});
			}
			
		};

		var fadeElement = function(isMouseOut){
			var thisTimeOut = (isMouseOut) ? (timeOut / 2) : timeOut;
			thisTimeOut = thisTimeOut;
			if (items.length > 0) {
				if (timeOutFn) {
					clearTimeout(timeOutFn);
				}
				timeOutFn = setTimeout(makeSlider, thisTimeOut);
			}
			else {
			}
		}
		
		var fade = function(i, buttonPress){
		
			var currNo = jQuery.inArray(current, items);
			var timeOutLocal = timeOut;
			if (buttonPress) {
				timeOutLocal /= 4;
				if (timeOutFn) {
					clearTimeout(timeOutFn);
					timeOutFn = setTimeout(makeSlider, timeOut);
				}
			}
			
			if (currNo != i) {
				if (current != null && currNo < items.length) {
					jQuery(items[currNo]).fadeOut((timeOutLocal / 6));
					if (itemsSpan.length > 0) {
						jQuery(itemsSpan[currNo]).fadeOut((timeOutLocal / 6));
					}
				}
				
				current = items[i];
				jQuery(items[i]).fadeIn((timeOutLocal / 6), function(){
					if (itemsSpan.length > 0) {
						jQuery(itemsSpan[i]).fadeIn((timeOutLocal / 6));
					}
				});
			}
		}
		
		
		var makeSlider = function(){
			current = (current != null) ? current : items[(items.length - 1)];
			var currNo = jQuery.inArray(current, items) + 1;
			currNo = (currNo == items.length) ? 0 : (currNo);
			
			if (!paused) {
				fade(currNo);
				fadeElement(false);
			}
		}
		
		makeSlider();
		
	};
	
})(jQuery);

