/*
 * jQuery Nivo Slider v2.4
 * http://nivo.dev7studios.com
 *
 * Copyright 2011, Gilbert Pellegrom
 * Free to use and abuse under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * May 2010 - Pick random effect from specified set of effects by toronegro
 * May 2010 - controlNavThumbsFromRel option added by nerd-sh
 * May 2010 - Do not start nivoRun timer if there is only 1 slide by msielski
 * April 2010 - controlNavThumbs option added by Jamie Thompson (http://jamiethompson.co.uk)
 * March 2010 - manualAdvance option added by HelloPablo (http://hellopablo.co.uk)
 * Feb 2011 - removed all unneeded crap MaxTobiasWeber
 */

(function($) {

    var NivoSlider = function(element, options){
		//Defaults are below
		var settings = $.extend({}, $.fn.nivoSlider.defaults, options);

        //Useful variables. Play carefully.
        var vars = {
            currentSlide: 0,
            currentImage: '',
            totalSlides: 0,
            randAnim: '',
            running: false,
            paused: false,
            stop: false
        };
    
        //Get this slider
        var slider = $(element);
        slider.data('nivo:vars', vars);
        slider.css('position','relative');
        slider.addClass('nivoSlider');
        
        //Find our slider children
        var kids = slider.children();
        kids.each(function() {
            var child = $(this);

            if(!child.is('img')){
                if(child.is('a')){
                    child.addClass('nivo-imageLink');
                    link = child;
                }
            }

            vars.totalSlides++;
        });
        
		
		$(kids[(vars.totalSlides-1)]).addClass('active');
		vars.currentSlide = (vars.totalSlides-1);
    var timer = 0;
    if(!settings.manualAdvance && kids.length > 1){
        timer = setInterval(function(){ nivoRun(slider, kids, settings); }, settings.pauseTime);
    }

	var nivoRun = function(slider, kids, settings){
		
		//Get our vars
		var vars = slider.data('nivo:vars');
		
        var kids = slider.children();
		
		// on first call set first image active
		$('.active', slider).fadeOut(settings.animSpeed);
		$('.active', slider).removeClass('active');
		// find next active
		if((vars.currentSlide+1) == vars.totalSlides){
			vars.currentSlide = 0;
		}else{
			vars.currentSlide++;
		}	
		
		$(kids[vars.currentSlide]).addClass('active');
		$(kids[vars.currentSlide]).fadeIn(settings.animSpeed);
	
	}
	
}
	
    $.fn.nivoSlider = function(options) {
    
        return this.each(function(){
            var element = $(this);
            // Return early if this element already has a plugin instance
            if (element.data('nivoslider')) return;
            // Pass options to plugin constructor
            var nivoslider = new NivoSlider(this, options);
            // Store plugin object in this element's data
            element.data('nivoslider', nivoslider);
        });

	};
	
	//Default settings
	$.fn.nivoSlider.defaults = {
		effect: 'random',
		slices: 15,
		animSpeed: 1500,
		pauseTime: 4000,
		startSlide: 0,
		directionNav: true,
		directionNavHide: true,
		controlNav: true,
		controlNavThumbs: false,
        controlNavThumbsFromRel: false,
		controlNavThumbsSearch: '.jpg',
		controlNavThumbsReplace: '_thumb.jpg',
		keyboardNav: true,
		pauseOnHover: true,
		manualAdvance: false,
		captionOpacity: 0.8,
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){},
        lastSlide: function(){},
        afterLoad: function(){}
	};
	
	$.fn._reverse = [].reverse;
	
})(jQuery);
