/*
 * jQuery Kollermedia Slider 1.0 | 30.07.2011
 * 
 * Download and Infos:
 * http://www.kollermedia.at
 *
 * Copyright (c) Juergen Koller [Kollermedia.at]
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

(function($){
 	$.fn.extend({ 
 		kollermedia_slider: function(options) {

			var defaults = {
				startElement: 0, //element to start
			    speed: 1000, //value in milliseconds
				pauseDuration: 4000, // value in milliseconds
				pauseOnHover:false, //true false
				pauseOnBlur:true, //true false
				animationDir:"left", //top, right, bottom, left, topright, topleft, bottomright, bottomleft
				animationOpacity:0.1, //0.0-1
				captionPosition: "bottom", //bottom top left right none
				captionWidth: "100%", //px auto %
				captionHeight: "auto", //px auto %
				navClass:"nav", //class name for the navigatin
				navType:"number", //number thumb none
				prevNextNav:true, //true false
				nextClass:"next", //id name for the next button
				prevClass:"prev", //id name for the prev button
				nextTitle:"Next Image",
				prevTitle:"Previous Image"
			};
			
			var options = $.extend(defaults, options);
		
    		this.each(function() {
			//vars
			var o =options;
			var obj = $(this);	
			var items = $("li", obj);
			var caption = $("li div", obj);
			var total = $(items).length-1;
			var height = obj.height();
			var width = obj.width();
			var s = o.startElement;
			//check and reset options if wrong
			if(s>total) {s=total;}
			if(o.pauseDuration<o.speed) {o.pauseDuration=o.speed+200;};
			
			//set caption position
			if (o.captionPosition=="none") {$(caption).css("display", "none");}
			else if(o.captionPosition=="top") {$(caption).css({"left":0, "top":0, "width": o.captionWidth, "height": o.captionHeight}).addClass("cTop");}
			else if(o.captionPosition=="left") {$(caption).css({"top":0, "left":0, "width": o.captionWidth, "height": o.captionHeight}).addClass("cLeft");}
			else if(o.captionPosition=="right") {$(caption).css({"top":0, "right":0, "width": o.captionWidth, "height": o.captionHeight}).addClass("cRight");}
			else {$(caption).css({"bottom":0, "width": o.captionWidth, "height": o.captionHeight}).addClass("cBottom");}
			
			//build custom Animation {left=+920, opacity:0}
			var customAnimation = {};
			var customAnimationReverse={};
			if (o.animationDir=="right") {customAnimation["left"] = +width; customAnimationReverse["left"] = -width;}
			else if (o.animationDir=="bottom") {customAnimation["top"] = +height; customAnimationReverse["top"] = -height;}
			else if (o.animationDir=="top") {customAnimation["top"] = -height; customAnimationReverse["top"] = +height;}
			else if (o.animationDir=="topleft") {customAnimation["top"] = -height; customAnimation["left"] = -width; customAnimationReverse["top"] = +height; customAnimationReverse["left"] = +width;}
			else if (o.animationDir=="topright") {customAnimation["top"] = -height; customAnimation["left"] = +width; customAnimationReverse["top"] = +height; customAnimationReverse["left"] = -width;}
			else if (o.animationDir=="bottomleft") {customAnimation["top"] = +height; customAnimation["left"] = -width; customAnimationReverse["top"] = -height; customAnimationReverse["left"] = +width;}
			else if (o.animationDir=="bottomright") {customAnimation["top"] = +height; customAnimation["left"] = +width; customAnimationReverse["top"] = -height; customAnimationReverse["left"] = -width;}
			else  {customAnimation["left"] = -width; customAnimationReverse["left"] = +width;}
			customAnimation["opacity"] = customAnimationReverse["opacity"] = o.animationOpacity;
			
			//create Nav
			createNav(o.navType);
			
			//create prevNextNav
			createPrevNextNav();
	
			//start Slider
			$(items).not(":eq("+s+")").hide();
			startSlider(s);

			
			//startSlider and Animation
			function startSlider(s)
			{
			function startAnimation()
			{	
			$(items).eq(s).show().css("z-index", 1);
			$(items).eq(s+1).show().css("z-index",0);
			if(s==total) {$(items).eq(0).show().css("z-index",0); activateCurrent(0);}else {activateCurrent(s+1);}	
			$(items).eq(s).animate(customAnimation, o.speed, function() {
			$(items).eq(s).css({"left":"0", "top":"0", "z-index":"0","opacity":1}).hide();
			s++;
			if (s==total+1) {s=0;}	
			})
			}
			setIntervalID = setInterval(startAnimation,o.pauseDuration);
			}

			//create Navigation
			function createNav(type) {
			if (type =="number") {	
			$(obj).append("<div class="+o.navClass+"></div>");
			$(items).each(function(e){
			var nr = e+1;
			$("."+o.navClass).append("<a href\"javascript:void(0);\">"+nr+"</a>");	
			});
			}
			else if (type=="thumb") {
			$(obj).append("<div class="+o.navClass+"></div>");
			$("img", items).each(function(e){
			var src = $(this).attr("src");
			$("."+o.navClass, obj).append("<a href\"javascript:void(0);\"><img src=\""+src+"\" /></a>");
			});
			}
			activateCurrent(o.startElement);
			}
			
			//create PrevNextNav
			function createPrevNextNav() {
			if(o.prevNextNav){
			$(obj).append("<a href=\"\" class=\""+o.prevClass+"\"  title=\""+o.prevTitle+"\">&laquo;</a><a href=\"\" class=\""+o.nextClass+"\" title=\""+o.nextTitle+"\">&raquo;</a>");
			}
			}

			//Activate Current Nav
			function activateCurrent(s) {
			if(s>total) {var activeSlide=0;} else if(s<0) {var activeSlide=total;} else {var activeSlide=s;}
			$("li", obj).removeClass("active");
			$("li:eq("+activeSlide+")", obj).addClass("active");
			if (o.navType!="none") {
			$("."+o.navClass+" a", obj).removeClass("active");
			$("."+o.navClass+" a:eq("+activeSlide+")", obj).addClass("active");
			}
			}

			//getCurrent Slide
			function getCurrent() {
			var activeSlide = $("li.active", obj).index();
			if(activeSlide==-1) {activeSlide =0;}
			return activeSlide;
			}
			
			//stopSlider
			function stopSlider() {
			clearInterval(setIntervalID);	
			}


			//Navi Click Function
			$("."+o.navClass+" a", obj).live('click', function() {
			activateCurrent($(this).index());
			slideTo(getCurrent(), "random");
			});

			
			function slideTo(s, dir) {
			stopSlider();
			var current = s;
			if(current==0)  {var prev = total;} else {var prev = current-1;}
			if(current==total)  {var next=0;} else {var next=current+1;}
			$(items).hide().stop().css({"left":"0", "top":"0", "z-index":0,"opacity": 1});
			if (dir=="next") {
			$("li:eq("+current+")", obj).show();
			$("li:eq("+prev+")", obj).show().css({"left":"0", "top":"0", "z-index":1,"opacity": 1});
			$("li:eq("+prev+")", obj).animate(customAnimation, o.speed, function() {
			$("li:eq("+prev+")", obj).css({"left":"0", "top":"0", "z-index":"0","opacity":1}).hide().stop();																	 
			});
			}
			else if (dir=="prev") {
			$("li:eq("+current+")", obj).show();
			$("li:eq("+next+")", obj).show().css({"left":"0", "top":"0", "z-index":1,"opacity": 1});
			$("li:eq("+next+")", obj).animate(customAnimationReverse, o.speed, function() {
			$("li:eq("+next+")", obj).css({"left":"0", "top":"0", "z-index":"0","opacity":1}).hide().stop();																	 
			});
			}
			else if (dir=="random") {
			$("li:eq("+current+")", obj).show();
			}
			startSlider(current);
			}


			$("a."+o.nextClass, obj).live('click', function() {
			activateCurrent(getCurrent()+1);
			slideTo(getCurrent(), "next");
			return false;
			});
			
			$("a."+o.prevClass, obj).live('click', function() {
			activateCurrent(getCurrent()-1);
			slideTo(getCurrent(), "prev");
			return false;
			});

			//Pause on Hover Function
			if(o.pauseOnHover){
			items.mouseover(function() {
			stopSlider();
			}).mouseout(function() {
			startSlider($(this).index());
			});
			}

			//Pause On Blur Function
			if (o.pauseOnBlur) {
			$(window).blur(function() {
			stopSlider();
			});
			
			//Restart on Focus Function
			$(window).focus(function() {
			stopSlider();
			startSlider(getCurrent());
			});
			}


});
}
});
})(jQuery);
