var mouseDown = null;
var mouseY = 0;

$(document).ready(function(){
    
    var menuI = 0;
    
    $("#main-menu > li:nth-child(" + activeMenu + ")").addClass("active");
    $("#main-menu > li:nth-child(" + (activeMenu - 1) + ")").css("background", "none");
    
    $("#main-menu li").each(function(){
        var w = $(this).outerWidth();
        $(this).find("ul").css("width", w);
    });
    
    $("#main-menu > li").hover(function(){
        menuI = $(this).index();
        $("#main-menu > li").eq(menuI - 1).addClass('nobg');
    }, function(){
        $("#main-menu > li").eq(menuI - 1).removeClass('nobg');
    });
    
    $(".scrollable").each(function(){
        if ($(this).find("div").eq(0).outerHeight() - $(this).innerHeight() > 0)
            $(this).next('.scrollbar').css('height', $(this).outerHeight()).css('display', 'inline-block');
    });
    
    
    $(".scrollbar div").mousedown(function(event){mouseDown = event.target;});
    $(document).mouseup(function(){mouseDown = null;});
    $(document).mousemove(function(event)
    {
        if (mouseDown != null)
        {
            var elem  = $(mouseDown).parent('.scrollbar').prev('.scrollable');
            var elemT = $(mouseDown);

            if (mouseY == 0)
            {
                mouseY = event.pageY;
            }
            move(elem, elemT, event.pageY - mouseY);
            mouseY = event.pageY;
        }
    });
    
});

function move(elem, elemT, pos)
{
    var mt = parseInt($(elemT).css('margin-top'));
    var sh = $(elemT).parent('.scrollbar').height() - 36;
    var pos = mt + pos;
    if (pos < 0) pos = 0;
    if (pos > $(elem).outerHeight() - 36) pos = $(elem).outerHeight() - 36;
    $(elemT).css('margin-top', pos);
    
    var ih = $(elem).innerHeight();
    var ch = $(elem).find("div").eq(0).outerHeight();
    $(elem).scrollTop(Math.round((ch - ih) / sh * pos));
}

function newsletterAdd(name, email)
{
    if (name.length > 1 && email.length > 5)
    {
        $.ajax({
            type: "POST",
            url: "Newsletter.php",
            data: "name=" + name + "&email=" + email,
            success: function(resp){
                alert(resp);;
            }
        });
    }
    else
    {
        alert('Nincs minden adat kitöltve.');
    }
}
