$(document).ready(function() {
    $("#menuTop a, .drawButton").mouseenter(function() {
        $(this).css("background-image", "url(/images/menutopBtn_a.png)");
    })
    .mouseleave(function() {
        $(this).css("background-image", "url(/images/menutopBtn.png)");
    });
    
    $(".catMenuLink").click(function(event) {
        event.preventDefault();
        
        var parent = $(this).parents("div:first");
        parent.find(".catSubMenu:first").toggle();
        
        var img = parent.find("img:first");
        img.attr("src", img.attr("src") == "/images/menu_arrow_up.png" ? "/images/menu_arrow_down.png" : "/images/menu_arrow_up.png");
        
        //$(this).parent().next().css("background-color", "red");
    });
    
    $("#cartCheckAll").click(function() {
        var checked = $(this).attr("checked");
        $("#cartTable .cartProductCheckBox").each(function() {
            $(this).attr("checked", checked);
        });
    });
    
    $(".productParamsGroup").click(function() {
        $(this).css("background-image", $(this).css("background-image").indexOf("images/pr_arrow_down.gif") > 0 ? "url(images/pr_arrow_up.gif)" : "url(images/pr_arrow_down.gif)");
        $(this).next().toggle();
    });
});

function ShowMessageForm()
{
    var messageBlock = $("#sendMessage");
    
//    var top = ((document.body.clientHeight / 2) + document.body.scrollTop) - (messageBlock.height() / 2);
//    var left = document.body.clientWidth / 2 - messageBlock.width() / 2;

    var top = ((document.body.clientHeight / 2) + document.body.scrollTop) - (messageBlock.height() / 2);
    var left = document.body.clientWidth / 2;
    
    $("#overallLayout").show();
    $("#sendMessage").css("top", top).css("left", left).show();
}

function CloseMessageForm()
{
    $("#sendMessage").hide();
    $("#overallLayout").hide();
}

function ShowLargeImage(_src)
{
    var imageTag = $("#largeImage");
    
    imageTag.attr("src", _src);
    $("#overallLayout").show();
    
    var eventFunction = function() {
        var imageBlock = $("#largeImageBlock");

        var top = ((document.body.clientHeight / 2) + document.body.scrollTop);
        var left = document.body.clientWidth / 2;
        var margin_top =  (imageBlock.height() / 2) - imageBlock.height();
        var margin_left = (imageBlock.width() / 2) - imageBlock.width();
        //alert("Height: " + imageBlock.height() + ", width: " + imageBlock.width());
        
        $("#largeImageBlock").css("top", top).css("left", left).css("margin-top", margin_top).css("margin-left", margin_left).show();
    }
    
    imageTag.load(eventFunction).ready(eventFunction);
}

function CloseLargeImage()
{
    $("#largeImageBlock").hide();
    $("#overallLayout").hide();
}

function AddToCart(_pr_id, _link)
{
    $.post("index.php", {CallModule: "cart", cartAction: "Add", productId: _pr_id}, function(_data) {
        if(_data != "")
        {
            _data = eval(_data);
            $("#cartSmallBlock").html(_data[0]);
            $("#cartTopLinkBlock").html(_data[1]);
            $(_link).parents("td:first").find(".orderNotice").html(_data[2]);
        }
    });
}

function AddTyingProductToCart(_pr_id, _main_pr_id)
{
    $.post("index.php", {CallModule: "cart", cartAction: "Add", productId: _pr_id, mainProductId: _main_pr_id}, function(_data) {
        if(_data != "")
        {
            _data = eval(_data);
            $("#cartSmallBlock").html(_data[0]);
            $("#cartTopLinkBlock").html(_data[1]);
        }
    });
}

function AddToCompare(_pr_id, _link)
{
    $.post("index.php", {CallModule: "compare", compareAction: "Add", productId: _pr_id}, function(_data) {
        if(_data != "")
        {
            _data = eval(_data);
            $(_link).replaceWith(_data[0]);
            $("#compareInfoBlock").html(_data[1]);
            $("#compareTopLinkBlock").html(_data[2]);
        }
    });
}

function SendOrder()
{
    if($("#nameField").val() == "" && $("#commentsField").val() == "" && $("#contactsField").val() == "")
    {
        ShowNotFilledFieldsMessage();
    }
    else
    {
        document.orderForm.submit();
    }
}

function ShowNotFilledFieldsMessage()
{
    var messageBlock = $("#notFilledFieldsMessage");
    
    var top = ((document.body.clientHeight / 2) + document.body.scrollTop) - (messageBlock.height() / 2);
    var left = document.body.clientWidth / 2 - messageBlock.width() / 2;
    
    $("#overallLayout").show();
    messageBlock.css("top", top).css("left", left).show();
}

function CloseOrderMessage()
{
    $("#notFilledFieldsMessage").hide();
    $("#overallLayout").hide();
}