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()
{
	$("a.prettyPhoto").click(function() { alert(1);});
   
   
    $("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", "#be3e20");
    });

    $("div.projects div.item div.icons_container div.icons div.scroller img").mouseout(function()
    {
        if(!$(this).hasClass("active")) $(this).css("border-color", "#c1c0bf");
    });

    $("div.projects div.item div.icons_container div.icons div.scroller img").click(function()
    {
        $(this).siblings("img.active").css("border-color", "#be3e20").removeClass("active");
        $(this).addClass("active");

        src = $(this).attr("src");
        src = src.replace("small", "medium");

        $(this).parent().parent().parent().parent().find("img.image").animate({ opacity: 0 }, 500, "linear", function()
        {
            $(this).attr("src", src);
            $(this).load(function() { $(this).animate({ opacity: 1 }, 500); });
        })
    });

    //var div = $('div.submenu'),
               //ul = $('ul.submenu'),
               // unordered list's left margin
               //ulPadding = 15;

  //Get menu width
  //var divWidth = div.width();

  //Remove scrollbars
  //div.css({overflow: 'hidden'});

  //Find last image container
 // var lastLi = ul.find('li:last-child');

  //When user move mouse over menu
  //div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    //var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

    //var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
   // div.scrollLeft(left);
  //});


});

function ShowGallery(file_name, gal_id) {
    var path = "/storage/files/uploaded2/medium/"+file_name;
    $("#main_"+gal_id).animate({opacity: 0},1000,function(){
	      $(this).html("<img src=" + path + " width='372' alt='' />").find("img").bind("load",function(){
	        $(this).parent().animate({opacity: 1},1000);
	      });
	    });
}
