
var thumb_width = 75;
var visible_elems = 12;
var stripe_pos = 0;
function check_position(thumbs_count) {
    var left = parseInt($('#carousel').css('left'));
    if (left < 0) {
        $('#gallery-next').removeClass('disabled');
    }
    if ((thumb_width*thumbs_count)-(thumb_width*visible_elems)>=left > 0) {
        $('#gallery-prev').removeClass('disabled');
    }
    
    if(left >= 0) {
        $('#gallery-prev').addClass('disabled');    
    } else if(left <= -((thumb_width*thumbs_count)-(thumb_width*visible_elems))) {
        $('#gallery-next').addClass('disabled');
    }
}

function move_stripe(new_pos) {
    var thumbs_count = $('#carousel li').length
    if (thumbs_count > visible_elems) {
        if (new_pos < 0) new_pos = 0;
        if (new_pos > thumbs_count - visible_elems) new_pos = thumbs_count - visible_elems;
        stripe_pos = new_pos;
        new_pos = -(new_pos*thumb_width);
        $('#carousel').animate({left: new_pos+'px'}, function() {
            check_position(thumbs_count);
        });
    }
}

function init_gallery() {
    var thumbs_count = $('#carousel li').length;
    if (thumbs_count > visible_elems) {
        $('#gallery-next').removeClass('disabled');
    }
    $('#carousel li a').each(function() {
        if (window.location == $(this).attr('href')) {
            li_id = $(this).parent().attr('id').split('_')[1];
            if (li_id > visible_elems) move_stripe(li_id-visible_elems+1);
        }
    });
}

$().ready( function() {
   
   init_gallery();
   
   $('#gallery-prev').click(function() {
       move_stripe(--stripe_pos);
   });
   
   $('#gallery-next').click(function() {
       move_stripe(++stripe_pos);
   });
   
    $('#content-gallery *').live( 'mouseenter', function() {
        $('.gallery-move-left, .gallery-move-right').show();
        $('.img-after').hide();
    });
    $('#content-gallery *').live( 'mouseleave', function () {
        $('.gallery-move-left, .gallery-move-right').hide();
        $('.img-after').show();
    });

    $('.fancybox').fancybox();

    $('.gallery #carousel a').click(function() {
        if (!$('img', this).hasClass('selected')) {
            $('#carousel img').removeClass('selected');
            $('img', this).addClass('selected');
            $('#content').load($(this).attr('href'));
        }
        return false;
    });
   
   $('.gallery-move-left a').live('click', function() {
       $('#content').load($(this).attr('href'));
       var li = $('.gallery #carousel a img.selected')
            .removeClass('selected')
            .parent()
            .parent()
            .prev();
       var li_id = li.attr('id').split('_')[1];
       li.find('img')
            .addClass('selected');
        if(li_id-stripe_pos < 0) move_stripe(--stripe_pos);
        return false;
   });
   
   $('.gallery-move-right a').live('click', function() {
       $('#content').load($(this).attr('href'));
       var li = $('.gallery #carousel a img.selected')
            .removeClass('selected')
            .parent()
            .parent()
            .next();
       var li_id = li.attr('id').split('_')[1];
       li.find('img')
         .addClass('selected')
         .attr('id');
        if(li_id-visible_elems-stripe_pos >= 0 ) move_stripe(++stripe_pos);
        return false;
   });
    
    $(document).ajaxStart(function(){
        $.fancybox.showActivity();
    });
    
    $(document).ajaxStop(function(){
        $.fancybox.hideActivity();
    });
    
    
    $('#gallery-nav a').tipsy({gravity: 's', html: true});
    
});
