$(document).ready(function(){
    $("#gallery-photo").slider();
    $("#video-gallery").slider();
    $("#images-gallery").slider();
    $("#support-photo-man").slider({ showedItems: 5});
    $("#support-photo-org").slider({ showedItems: 5});
    $("#jury-gallery").slider({ showedItems: 3,  container: 'div.gallery-container-mini'});
    
});

/**
* Скроллер картинок
* @author: Anatoly Lelevkin (Weltkind)
**/
jQuery.fn.slider = function(options){
    // настройки по умолчанию
    var options = jQuery.extend({
        container: 'div.gallery-container', // идентификатор контейнера
        tracker: 'ul.item_list', // идентификатор скроллера
        leftButton: 'a.item_arrow_l', // идентификатор левой стрелки
        rightButton: 'a.item_arrow_r', // идентификатор правой стрелки
        showedItems: 3 //столько картинок выводится на скроллере
    },options);

    var galleryId = null;
    var tracker = null;


    var currentItem = 0;
    var currentLeft = 0;
    var countItems = 0;

    var leftButton = null;
    var rightButton = null;
    var photoItems = null;
    var itemWidth = null;

    if (jQuery(this)==null){
        return;
    }

    galleryId = jQuery(this).attr('id');
    tracker = $('#'+galleryId +' '+ options.tracker);
    photoItems = tracker.children('li');
    countItems = photoItems.size();


    itemWidth = photoItems.width() + parseInt(photoItems.css('margin-left')) + parseInt(photoItems.css('margin-right')) +  parseInt(photoItems.css('padding-right')) + parseInt(photoItems.css('padding-left'));

    leftButton = $("#"+galleryId + ' a.item_arrow_l');
    rightButton = $("#"+galleryId + ' a.item_arrow_r');

    leftButton.click(function(){
        if (currentItem<=0){
            return false;
        }
        currentItem-=1;
        currentLeft+=itemWidth;
        moveItem();
        return false;
    });




    rightButton.click(function(){
        if (currentItem>=countItems-options.showedItems){
            return false;
        }
        currentItem+=1;
        currentLeft-=itemWidth;
        moveItem();
        return false;

    });


    function moveItem(){
        $(tracker).animate({
            left: currentLeft
        }, 1000);
    }
};
