/**
 * slideshow
 * 
 * 
 * @version 1.1
 */
(function($){
    $.fn.slideshow = function(options){
    
        var defaults = {
            speed: 1000,
            interval: 5000,
            navigation: '#navigation'
        }, $settings = $.extend({}, defaults, options), $run = true, $navigation = $($settings.navigation),
		$element = 'a';
        
        this.each(function(){
            var $this = $(this);	
			var $c = $($element,$this).length;		

			// mouse handler
            $($this).mouseover(function(){
                $run = false;
            }).mouseout(function(){
                $run = true;
            });
            
            // onclick handler
            $("li a", $navigation).click(function(){
                var index = $("li a", $navigation).index(this);
                if (parseInt(index) >= 0) {
                    switchTo($this, index);
                }
                return false;
            });
			
			// start. pozice
            var $active = $($element + '.active', $this);
            if ($active.length == 0) {
				setIndex($this, 0);
				$($element + ':first',$this).addClass('active');	
			} else {
				setIndex($this, '');
			}			
			
			// run slideshow
			if ($c > 1) {			
				window.setInterval(function(){
					slideSwitch($this);
				}, $settings.interval);
			}
        });
        /**
         * animace - prolinani obrazku
         * @param {Object} $this
         */
        function slideSwitch($this){
            if ($run == true) {
                var $active = $($element + '.active', $this);
                if ($active.length == 0) 
                    $active = $($element + ':first', $this);

                // srovnani poradi
                var $next = $active.next().length ? $active.next() : $($element + ':first', $this);
                
                // random order
                
                // var $sibs  = $active.siblings();
                // var rndNum = Math.floor(Math.random() * $sibs.length );
                // var $next  = $( $sibs[ rndNum ] );
                
                
                $active.addClass('last-active');
                
                $next.css({
                    opacity: 0.0
                }).addClass('active').animate({
                    opacity: 1.0
                }, $settings.speed, function(){
                    $active.removeClass('active last-active');
                });
                // nastaveni aktivniho indexu 
                var $index = $this.find($element).index($next);
                setIndex($this, $index);
            }
        }
        /**
         * prepnuti na obrazek (panel) dle indexu
         * @param {Object} $this
         * @param {Object} $index index aktivni polozky
         */
        function switchTo($this, $index){
            // stop animace
            stop();
            // odstraneni css trid
            $($element, $this).each(function(){
                $(this).removeClass('active last-active');
            });
            // nastaveni aktivni polozky
            $this.find($element + ':eq(' + $index + ')').addClass('active');
            // nastaveni indexu
            setIndex($this, $index);
            // spusteni animace
			setTimeout(function() { 
        		play(); 
    		}, $settings.interval); 
        }
        /**
         * zastaveni animace
         */
        function stop(){
            $run = false;
        }
        /**
         * spusteni animace
         */
        function play(){
            $run = true;
        }
        /**
         * nastaveni aktivni tridy aktualnimu indexu
         * @param {Object} $this
         * @param {Object} $index
         */
        function setIndex($this, $index){
            var $active = $($element + '.active', $this);
            if (isNaN(parseInt($index))) {				
               var $index = $this.find($element).index($active);			   
            }
            
            if ($index >= 0) {
                $('li', $navigation).each(function(){
                    $(this).removeClass('active');
                });
				
                $($navigation).find('li:eq(' + $index + ')').addClass('active');
            }
        }
        return this;
    }
})(jQuery);

