var pause = false;
var slide = 0;

// $(window).ready - fix bug IE to prealoded images
$(window).ready(function() {
    
   $(".slider img").each(function(){

       if ( $(this).hasClass("active") )
           $(this).css({"opacity": "100"});
       else
           $(this).css({"opacity": "0"});

   }); 
   
});

$(window).load(function() {

    $(".nav li, .drop li").hover(
        function(){
            slide = $(this).index();
            rotator( $(this).index() );
            pause = true;
        }, function(){ pause = false; }
    );

    setInterval( 'rotator(null);' , 5000);

});

function rotator(index){

    var index_start = index;

    if( pause ) return false;

    var prev = $(".slider img.active");

    if( index == null) {
        if( ( $(".slider img").length - 1 ) == prev.index() ) index = 0;
        else index = prev.index() + 1;
    }

    if( index == prev.index() ) return false;

    // Navigation
    $( ".nav li.active, .drop li.active" ).removeClass();
    $( ".nav li" ).eq( index ).addClass( "active" );
    $( ".drop li" ).eq( index ).addClass( "active" );

    setTimeout(function() {
       if( index != slide && index_start != null ) {
           return false;
       } else {

            /* Not black effect */
            $(".slider").css({background: 'url(' + prev.attr('src') + ') no-repeat'});

            prev.stop(false).css({"opacity": "0"}).removeClass('active');
            $(".slider img:eq(" + index + ")")
                .stop()
                .animate({"opacity": "1"}, 1500)
                .addClass( "active" );

            return false;
       }
    }, 350);


}
