/* 
 * author unknown
 */
var gallery = new Object();
gallery.PREVIEW = "body .photoContainer a img#gallery_big_image";
gallery.THUMBS  = "body .thumbsContainer a";
gallery.THUMBS_COUNT = 0;
gallery.CUR_INDEX = 0;
gallery.PATH_IMG = "";
gallery.DEFAULT_THUMB_LEFT = 0;
gallery.THUMBS_INLAY_WIDTH = 0;
gallery.THUMBS_WIDTH = 0;
gallery.THUMBS_MAX_LEFT = 0;

function setIndex(index, moveThumbs){
    var selector = gallery.THUMBS + "[index="+index+"]";
    var thumb = $(selector);
    if(thumb.size()){
        var duration = 300;
        if(moveThumbs){
            var thumb_left =  -1*thumb.position().left;
            if(thumb_left < gallery.THUMBS_MAX_LEFT){
                thumb_left = gallery.THUMBS_MAX_LEFT;
            }
            $('.thumbsContainerInlay').animate({
                left: thumb_left
            }, "slow" );
        }
        $('body .photoContainer img#dummy').attr('src', gallery.PATH_IMG+thumb.attr('image'));
        $(gallery.PREVIEW).fadeOut(duration , function(){
            $(gallery.PREVIEW).removeAttr('src');
            $(gallery.PREVIEW).attr('src', gallery.PATH_IMG+thumb.attr('image')).fadeIn(duration );
            gallery.CUR_INDEX = index;
            updateButtons();
        });
    }
}
function updateButtons(){
    if(gallery.CUR_INDEX == 0){
        $('#gallery_back').addClass("gallery-hide-button");
    } else {
        $('#gallery_back').removeClass("gallery-hide-button");
    }
    if(gallery.CUR_INDEX >= gallery.THUMBS_COUNT){
        $('#gallery_next').addClass("gallery-hide-button");
    } else {
        $('#gallery_next').removeClass("gallery-hide-button");
    }
}
function setWidth(){
    var lastElement = $('.thumbsContainerInlay a:last');
    var width = lastElement.offset().left + lastElement.outerWidth();
    gallery.THUMBS_MAX_LEFT = $('.thumbsContainer').width()+ (-1*width);
}
setInterval(function(){
    if( $('.thumbsContainerInlay').position().left+5 >= 0){
        $('.hoverspace-left').hide();
    } else {
        $('.hoverspace-left').show();
    }
    if($('.thumbsContainerInlay').position().left-5 <= gallery.THUMBS_MAX_LEFT){
        $('.hoverspace-right').hide();
    }else {
        $('.hoverspace-right').show();
    }
}, 200);
function posThumbsContainer(){
    $('.thumbsContainer').css('top', GALLERY_MIN_TOP+'px');
    var space = $(window).height()-$('.thumbsContainer').position().top-113;
    if(space > 0){
        $('.thumbsContainer').css('top', (GALLERY_MIN_TOP+space)+'px');
    }
}
$(window).load(function() {
  var pic = $(gallery.THUMBS + ' img');
  pic.removeAttr("width"); 
  pic.removeAttr("height");
    gallery.DEFAULT_THUMB_LEFT =  $('body .thumbsContainer').position().left;
    gallery.PATH_IMG = $('.photoContainer[path]').attr('path');
    gallery.THUMBS_COUNT = $(gallery.THUMBS).size()-1;
    setWidth();
    posThumbsContainer();
    updateButtons();
});
$(document).ready(function() {
    $('.thumbsContainer , .photoContainer').hide().fadeIn(1000);
    $(window).resize(function(){
        setWidth();
        posThumbsContainer();
    });
    $(gallery.THUMBS+ " img").hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
    $('.hoverspace-left').mouseover(function(){
        //setWidth();
        var time = Math.abs($('.thumbsContainerInlay').position().left*GALLERY_SPEED) ;
        $('.thumbsContainerInlay').animate({
            left: 0
        }, time );
    }).mouseout(function(){
        $('.thumbsContainerInlay').stop();
    });
    $('.hoverspace-right').mouseover(function(){
        //setWidth();
        var time = Math.abs(gallery.THUMBS_MAX_LEFT - $('.thumbsContainerInlay').position().left)
        time = time*GALLERY_SPEED ;
        $('.thumbsContainerInlay').animate({
            left: gallery.THUMBS_MAX_LEFT
        }, time);
    }).mouseout(function(){
        $('.thumbsContainerInlay').stop();
    });
    $('#gallery_next , '+gallery.PREVIEW).click(function(){
        if(gallery.CUR_INDEX < gallery.THUMBS_COUNT){
            setIndex(gallery.CUR_INDEX + 1, true)
        }
    });
    $('#gallery_back').click(function(){
        if(gallery.CUR_INDEX > 0){
            setIndex(gallery.CUR_INDEX - 1, true)
        }
    });
    updateButtons();
});
