links = Array(); X0 = Array(); Y0 = Array(); Hh=Array(); Hw=Array(); Pad = Array();
nLinks=0; Link = Array();

function moveMenu(){
    $(document).mousemove(function(event){
        xm = event.pageX - additionalOffset;
        ym = event.pageY;

        for (i = 0; i < nLinks; i++) {
            x0 = X0[i];
            y0 = Y0[i];
            dx = xm - x0;
            dy = ym - y0;
            r = Math.sqrt(dx * dx + dy * dy);
            if (r < max_rad) {
                if (r > 20) {
                    d = 66 - 900 / r - r / 5;
                    xshift = dx * d / (r * 2);
                    yshift = dy * d / r;
                }
                else {
                    xshift = dx;
                    yshift = dy;
                }
            }
            else {
                xshift = 0;
                yshift = 0;
            }
            
            x = x0 + xshift - Hw[i];
            y = y0 + yshift - Hh[i];
            if (y < 0) 
                y = 0;
            links[i].css({
                left: x
            });
            Link[i].css({
                marginBottom: y + Pad[i]
            });
        }
    });
}



$(document).ready(function()
{
    lastX = 0; lastW = 0;
    $(".menu div.item").each(function(){
        if(lastX == 0)
        {
            Width = $(this).width();
            $(this).css({left:50, top:0, width:Width});
            lastX=50; lastW = Width;
            
            $(this).find(".line").css({
                width: Width, height: 3, padding:0
            });
        } else {
            Width = $(this).width();
            $(this).css({
                left:lastX + lastW + 50,
                top:0,
                width: Width
            });
            
            $(this).children(".line").css({
                width: Width, height: 3, padding:0
            });
            
            lastX = lastX + lastW + 50; lastW = Width;
        }
    });

    additionalOffset = ($(document).width() - 980)/2;

    $(".menu div.item").each(function(){
        x = $(this).offset().left + ($(this).width() / 2) - additionalOffset;
        y = $(this).offset().top + ($(this).height() / 2);

        pad = $(this).children("a,span").css("padding-top"); 
        pad = pad.substr(0,pad.indexOf("px"))-0;
        links[nLinks]=$(this);
        X0[nLinks]=x; Y0[nLinks]=y;
        Hh[nLinks]= pad + $(this).height() / 2;
        Hw[nLinks] = $(this).width() / 2;
        Link[nLinks] =  $(this).children(".line");
        $(this).children(".line").css({marginBottom:pad});
        Pad[nLinks] = pad;
        nLinks++;
    });

    max_rad = 300;
    min_rad = 15;
    k=0;
    setTimeout("moveMenu()",0);
    
    $(".menu div.item").each(function(){
            $(this).slideDown("slow");
        });
    $(".menu div.item").mouseover(function(){
        $(this).children(".line").css({
            backgroundColor: "#AAAAAA"
        });
    });
    $(".menu div.item").mouseout(function(){
        $(this).children(".line").css({
            backgroundColor: "#FFFFFF"
        });
    });

    $('.show-more-works').live('click', function(){
        var url = $(this).attr('href');
        $.ajax({
            url: url,
            success: function(text){
                var tmp = $('#works-temporary-container');
                var button = $('.show-more-works');

                tmp.html(text).find('div.work-preview').each(function(){
                    $(this).hide().css('height', '0px');
                    tmp.before($(this));

                    $(this).show().animate({
                        height: '+158'
                    }, 1000, function(){});
                });
                button.replaceWith(tmp.find('.show-more-works'));
            }
        });
        return false;
    });
});

