$(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($) {
$(function() {

  $('ul.tabs').delegate('li:not(.current)', 'click', function() {
    $(this).addClass('current').siblings().removeClass('current')
      .parents('div.section').find('div.box').hide().eq($(this).index()).fadeIn(150);
  })

})
})(jQuery)

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]);
        }
    });	
	ShowNotFilledFieldsMessageCart();
}

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 ShowNotFilledFieldsMessageCart()
{
    var messageBlock = $("#notFilledFieldsMessageCart");
    
    var top = ((document.body.clientHeight / 2) + document.body.scrollTop) - (messageBlock.height() / 2);
    var left = document.body.clientWidth / 2 - messageBlock.width() / 2;
    
    $("#overallLayoutCart").show();
    messageBlock.css("top", top).css("left", left).show();
}

function CloseOrderMessage()
{
    $("#notFilledFieldsMessage").hide();
    $("#overallLayout").hide();
}

function CloseOrderMessageCart()
{
    $("#notFilledFieldsMessageCart").hide();
    $("#overallLayoutCart").hide();
}
			
function selectBrand()
{		
        var id_brand = $('select[name="brand"]').val();
        if(id_brand != "")
		{	 
               // $('div[name="selectModel"]').html('');
                $.post("/",{CallModule:"type", BRAND: id_brand}, function(_data){ 
                        var model = _data.split("@#$%!");
                        $('select[name="model"]').html(" ");                        
                        var max_length = model[0].length; //Длина названия модели
                        for(i = 0; i < model.length; i++)
                        {
                            word_length = model[i].length;
                            if(max_length < word_length)
                                max_length = word_length;                              
                            $('select[name="model"]').append("<option value='"+ model[i] +"'>"+ model[i] + "</option>");                      
                         }
                         // Если название слишком длинное, зададим фиксированный
                         // размер для select[name="model"]
                         if(max_length > 68)
                            $('select[name="model"]').css("width", "500px"); 
                         else
                          $('select[name="model"]').css("width", "auto");
                         
                                                    
                      // alert(max_length);
                       });
                
                $('select[name="model"]').css("visibility","visible");
                $('input[name="choose"]').css("visibility","visible");
                
               
        }
        else{	 
        /*        $.ajax({
                        type: "POST",
                        url: "index.php",
                        data: { CallModule: "catalog", action: "GetModelList", id_brand: id_brand },
                        cache: false,
                        success: function(responce){ $('div[name="selectModel"]').html(responce); }
                }); */
                
				/*$.post("/project/modules/catalog/UnitOCatalogModule.php", {catalogAction: "GetModelList" }, function(data) {
                             $('div[name="selectModel"]').html(data); 
							 }*/
				 $('select[name="model"]').html("");
				 $('select[name="model"]').css("visibility","hidden");
				 $('input[name="choose"]').css("visibility","hidden");
				
        }  	
       		
}
