var _slide ;
var _slide_stop = 1;
var _active=0;
var _close = 0;
(function($) {
    $.fn.gallery = function(options) {
        var config = {
            'interval':3000,
            'speed_box':800
        };
    
        if (options) $.extend(config, options);
        $('.loading_gallery').remove();
                
        var __top = 0;
        var height_ = 0;
        var _interval = 0;
        
        var mouse_up = 0;
        var tot_img = 0;
        var _active = null;
        
        var speed_move = 15000;
        
        this.each(function(i) {
            $this = $(this);
            $(this).after('<div class="gallery-box" id="gallery-'+ i +'"></div>');
            content ='';
            tot_img = $(this).find('li').size();
            content_images = '<ul class="images-box">';
            $(this).find('li').each(function(x){
              _min = $(this).find('img').attr('src');
              _src = $(this).find('a').attr('href');
              if (x==0){
                active = ' active';
                img_background = _src;
              }else{
                active = '';
              }
              content += '<img src="' + _src + '" id="img-' + x + '" class="img-slide' + active +'" rel="'+ x +'"/>';
              content_images += '<li id="image-'+ x +'"><a href="#img-' + x + '" rel="'+ x +'"><img src="'+_min+'" class="img-box"/></a></li>';
            });
            content_images += '</ul>';
            $('#gallery-'+ i).html('<span>' + content+ '</span><div class="slide_box" id="slide_box'+ i +'"><a href="#slide_box'+ i +'" class="pulsante_images"></a><div class="move"><a href="#" class="up"><img src="/temi/default/css/images/up.png" /></a><div class="content_box">' + content_images + '</div><a href="#" class="down"><img src="/temi/default/css/images/down.png" /></a></div></div>');
            $(this).remove();
            height_ = 0 - ($('.images-box li').size() - 5) * 73;
            if ($.browser.msie && jQuery.browser.version=='7.0'){
              height_ -= ($('.images-box li').size() * 5);
            }
            _interval = config.interval;
            _slide = window.setInterval( "slideSwitch("+ config.interval +", " + i + ")", config.interval);
            speed_move = ($('.images-box li').size() / 3) * 1000;
        });    
        
        $('.content_box').css('height', '320px');
        
        $('a.pulsante_images').click(function(){
          _id = $(this).attr('href');
          _id = _id.split('#');
          _id = '#' + _id[1];
          _top = $(_id).css('top');
          if (_top=='353px'){
            $('.pulsante_images').css('background-image', 'url(/temi/default/css/images/open.png)'); 
            $(_id).animate({'top':'-10px'}, config.speed_box);
          }else{
            $('.pulsante_images').css('background-image', 'url(/temi/default/css/images/close.png)');
            $(_id).animate({'top':'353px'},config.speed_box);
          }
          return false;
          });
          
          
          var move = 0;
                    
          $('.move').mousemove(function(e){
            a = $('.slide_box').offset();
            _a = Math.abs(parseFloat($('.images-box').css('top')));
            _top = parseFloat(e.pageY) - parseFloat(a.top);
            top_box = parseFloat($(".slide_box").css('top'));
            if (_top>180 && _top<280){
              $(".images-box").stop();
              move = 2;
            }else if (_top<175 && move!=0){
              move = 0;
              $(".images-box").stop().animate({"top": 0}, speed_move);
            }else if (_top>280 && move!=1){
              _speed = _a;
              move = 1;
              b = Math.ceil((speed_move - (_a * 5)));
              $(".images-box").stop().animate({"top":  height_ + 'px'}, b);
            }
          });
          
          $('a.up').hover(function(e){
             _top = parseFloat(e.pageY) - parseFloat(a.top);
             move = 0;
             $(".images-box").stop().animate({"top": 0}, speed_move);
          });
          
          $('a.down').hover(function(e){
             _top = parseFloat(e.pageY) - parseFloat(a.top);
             _a = Math.abs(parseFloat($('.images-box').css('top')));
             b = Math.ceil((speed_move - (_a * 5)));
             move = 1;
             $(".images-box").stop().animate({"top":  height_ + 'px'}, b);
          });
          
                   
          $('.img-box').hover(function(){
            $(this).animate({opacity: 0.7})
          }, function () {
              $(this).animate({opacity: 1})
          });
          
        
        $('.gallery-box span').mouseenter(function(){
         if(_slide_stop == 0){
            _slide_stop = 1;
            window.clearInterval(_slide);
            _slide = window.setInterval( "slideSwitch("+ _interval +", 0)", _interval); 
          }
        });
        
        $('.gallery-box').mouseout(function(){
          if(_slide_stop == 0){
            _slide_stop = 1;
            window.clearInterval(_slide);
            _slide = window.setInterval( "slideSwitch("+ _interval +", 0)", _interval); 
          }
        });
          
        $('.img-box').parent().click(function(){
          $(".images-box").stop();
          _id = $(this).attr('href');
          _id = _id.split('#');
          _id = '#' + _id[1];
          x = 0;
          $('.gallery-box span img.active').each(function(){
            if(($(this).attr('class')=='img-slide active')){
               $active = $(this);
               x++;
            } 
           });
           if (x>1){
            return false;
           }
           if (x==0){
            return false;
           }
            num_slide = parseFloat(($active.attr('rel')));
            num_select = parseFloat(($(this).attr('rel')));
            if(num_slide==num_select){
               return false; 
            }
            _slide_stop = 0;
            if((num_slide - 1)==num_select){
               window.clearInterval(_slide); 
               _slide_stop = 0;
               $(_id).css({opacity: 1}).addClass('active')
               $active.css({opacity: 1}).animate({opacity: 0},1500, function() {
                  $active.removeClass('active last-active');
                 // _slide = window.setInterval( "slideSwitch("+ _interval +", 0)", _interval); 
               });
               return false; 
            }
            if(num_slide==num_select){
               return false; 
            }
            window.clearInterval(_slide);
            if(num_slide<num_select){
              $(_id).css({opacity: 0.0}).addClass('active').animate({opacity: 1},1500, function(){
                 $active.removeClass('active last-active').css({opacity: 0.0});
                //_slide = window.setInterval( "slideSwitch("+ _interval +", 0)", _interval); 
              });
            }else if(num_slide>num_select){
              $(_id).css({opacity: 1}).addClass('active')
              $active.css({opacity: 1}).animate({opacity: 0},1500, function() {
                $active.removeClass('active last-active');
                //_slide = window.setInterval( "slideSwitch("+ _interval +", 0)", _interval); 
              });
           }
          return false;
        });
        return false;
        return this;  
    }
})(jQuery);
var a = 0;

function slideSwitch(interval_slide, id_gallery){
    var $active = jQuery('.gallery-box span img.active');
    if ( $active.length == 0 ) $active = jQuery('.gallery-box span img:last');
    var $next =  $active.next().length ? $active.next() : jQuery('.gallery-box span img:first');
    //alert($next.attr('id'))
    $active.addClass('last-active');
    $next.css({opacity: 0.0}).addClass('active').animate({opacity: 1},2000, function() {
      $active.removeClass('active last-active');
    });
}

