/*!
 * imageSlider.js v1.0.0 - (c) Wojciech Jerczynski, freely distributable under the terms of the MIT license.
 */
(function($){
            $.fn.imageSlider = function(options){
            
            
            return this.each(function(index,element){
                $this = $(this);
                if($this.find(".cont").length>0){
                        
                }else{
                        $this.append('<div class="cont"/>')
                }
                $.fn.imageSlider.doTheFunkyStuff($this,options);
            })
         
            };
            
            $.fn.imageSlider.doTheFunkyStuff = function($this,options){
                var zIndex = 550;
                
                $allImages = $this.find("img");
                $slidecont = $this.find(".slide:visible");
                $slides = $slidecont.find("img");
                
                $.each($allImages,function(){
                    $(this).css("z-index",zIndex);
                    zIndex--;
                })
                
                $numslides = $slides.length;
                for(i=1;i<=$numslides;i++){
                    $(".cont").append('<a class="im_indi" href="#"></a>')
                }
                $index = 0;
                $slides.show();
                $(".im_indi").eq(0).addClass("active");
                
                var frameRun = null;
                
                var playImageSlides = function(){
                    
                   frameRun = setInterval(function(){
                   $slidecont = $this.find(".slide:visible");
                   $slides = $slidecont.find("img");
                   $numslides = $slides.length;

                   if($slidecont.is(":visible")){
                    
                    if($index < $numslides-1){
                        $slides.eq($index).prevAll().hide();
                        $slides.eq($index).fadeOut("slow");
                        $index++;
                        $(".im_indi").removeClass("active");
                        $(".im_indi").eq($index).addClass("active");
                    }else{
                         $slides.eq(0).fadeIn("slow",function(){
                            $index = 0;
                            $slides.show();
                            $(".im_indi").removeClass("active");
                            $(".im_indi").eq($index).addClass("active");
                         });
                         
                    }
                    
                   }
                  },(options.slideDelay*1000));    
                }
                
                playImageSlides();
                
                var resetImageSlides = function(){
                    clearInterval(frameRun);
                    playImageSlides();
                }
                
               
                $(".im_indi").live("click",function(){
                    $index = $(this).index();  
                    $(this).siblings().removeClass("active");
                    $(this).addClass("active");
                    $slides.eq($index).show();
                    $slides.eq($index).prevAll().hide();
                    $slides.eq($index).nextAll().show();
                    resetImageSlides();
                });
                
                $(window).resize(function(){
                    $(".im_indi").eq(0).trigger("click");
                });
                
            }
     
    })(jQuery);
