var hgall = "";
var gal_scroll_on = 1;
var y_mouse;

function div_scroll_gall(obj, on_flag)
{
    if(on_flag && gal_scroll_on)
        scroll_interval_gall = setInterval(function(){ div_scroll_fly_gall(obj); }, 10);
    else
        clearInterval(scroll_interval_gall);
}

function div_scroll_fly_gall(obj)
{
    var screen_height = $(obj).parent().height();
    gall_top = $(obj).parent().offset().top - $(document).scrollTop();

    if($(obj).height() < screen_height)
    {
        div_scroll_gall(obj, 0);
        return;
    }

    y_mouse_ = y_mouse - gall_top;

    var height_delta = Math.round((screen_height-3)/2-y_mouse_);
    var height_delta_abs = Math.abs(height_delta);
    var persents = Math.round(200*(height_delta_abs)/(screen_height));
    var dir = height_delta_abs!=0 ? -height_delta/height_delta_abs : 1;

    if(persents < 30)
        speed = 0;
    else if(persents < 80)
        speed = 1;
    else speed = 2;

    step = dir * speed;

    old_y = parseInt($(obj).position().top);
    new_y = old_y - step;

    if(new_y > 0)
    {
        $(obj).css("top", "0px");
        is_moving_gall = 0;
    }
    else if(new_y < -$(obj).height() + screen_height)
    {
        $(obj).css("top", (-$(obj).height() + screen_height + "px"));
        is_moving_gall = 0;
    }
    else if(new_y != old_y)
    {
        $(obj).css("top", (new_y + "px"));
        is_moving_gall = 1;
    }
    else
        is_moving_gall = 0;
}


$(document).ready(function()
{
    $("div.projects div.item div.icons_container div.icons div.scroller").mouseout(function(){ div_scroll_gall(this, 0); });
    $("div.projects div.item div.icons_container div.icons div.scroller").mouseover(function(){ div_scroll_gall(this, 1); });
    $("div.projects div.item div.icons_container div.icons").mousemove(function(event){ y_mouse = event.clientY; });

    $("div.projects div.item div.icons_container div.icons div.scroller img").mouseover(function()
    {
        $(this).css("border-color", "#000");
    });

    $("div.projects div.item div.icons_container div.icons div.scroller img").mouseout(function()
    {
        if(!$(this).hasClass("active")) $(this).css("border-color", "#fff");
    });

    $("div.projects div.item div.icons_container div.icons div.scroller img").click(function()
    {
        $(this).siblings("img.active").css("border-color", "#fff").removeClass("active");
        $(this).addClass("active");

        src = $(this).attr("src");
        src = src.replace("icon", "small");

        $(this).parent().parent().parent().parent().find("img.image").animate({ opacity: 0 }, 50, "linear", function()
        {
            $(this).attr("src", src);
            $(this).load(function() { $(this).animate({ opacity: 1 }, 50); });
        })
    });
});