$(document).ready(function(){
    $("li.parent_articles").mouseover(function(){
        $("div.category_articles").slideDown("slow");        
    });
    $("li[id^='directory_li_']").mouseover(function(){
        var directory_li_id = $(this).attr("id").substring(13,$(this).attr("id").length);
        $("#directory_" +directory_li_id).slideDown("slow");        
    });    
    $("#extended_search_link").click(function(){
        $("#extended_search").animate({opacity: "show"},"slow");
        $("#extended_search_link").animate({opacity: "hide"},"slow");
        $("#ordinary_search_link").animate({opacity: "show"},"slow");
    });
    $("#ordinary_search_link").click(function(){
        $("#extended_search").animate({opacity: "hide"},"slow");
        $("#ordinary_search_link").animate({opacity: "hide"},"slow");
        $("#extended_search_link").animate({opacity: "show"},"slow");
    });
    
    $("#shopcart tbody tr:odd").addClass("odd");
    $("#shopcart tbody tr:even").addClass("even");
});

function addItemToCart (id)
{
    var product_id = parseInt(id);
    var visitor_id = getCookie('visitor_id');
    
    if (isNaN(product_id))
    {
        return;
    }
    if (visitor_id == null)
    {
        alert('Ошибка! В Вашем браузере должны быть включены куки.');
        return;
    }
    $.ajax ({
        type: 'POST',
        url:  'ajax/shopcart.php',
        data: 'action=add&product_id=' + product_id + '&visitor_id=' + visitor_id
    });
}

function removeItemFromCart (id)
{
    if (!confirm('Вы действительно хотите удалить позицию?'))
    {
        return;
    }
    var product_id = parseInt(id);
    var visitor_id = getCookie('visitor_id');
    
    if (isNaN(product_id))
    {
        return;
    }
    if (visitor_id == null)
    {
        alert('Ошибка! В Вашем браузере должны быть включены куки.');
        return;
    }
    $.ajax ({
        type: 'POST',
        url:  'ajax/shopcart.php',
        data: 'action=remove&product_id=' + product_id + '&visitor_id=' + visitor_id,
        success: function (response)
        {
            $("#totalPrice").html(response);
        }
    });
    $("#product_id_" + product_id).remove();
    $("#shopcart tbody tr:odd").removeClass().addClass("odd");
    $("#shopcart tbody tr:even").removeClass().addClass("even");
    
    if ($("#shopcart tbody tr").length < 1)
    {
        $("#shopcart").parent().remove();
        return;
    }
}

function recalcOrder (element)
{
    var quantity   = parseInt(element.value);
    var visitor_id = getCookie('visitor_id');
    var trId       = $(element).parent().parent().attr("id");
    var product_id = trId.substring(11, trId.length);
    
    if (visitor_id == null)
    {
        alert('Ошибка! В Вашем браузере должны быть включены куки.');
        return;
    }
    if (isNaN(quantity) || quantity < 1)
    {
        alert('Количество должно быть числом больше ноля');
        $(element).val(1);
    }
    $.ajax ({
        type: 'POST',
        url:  'ajax/shopcart.php',
        data: 'action=recalc&product_id=' + product_id + '&visitor_id=' + visitor_id + '&quantity=' + $(element).val(),
        success: function (response)
        {
            var responseArray = response.split(',');
            $("#itemTotalPrice_" + product_id).html(responseArray[1]);
            $("#totalPrice").html(responseArray[0]);
        }
    });
}

function showQuickOrderForm ()
{
    $("#quickOrderForm").prev("div").hide();
    $("#quickOrderForm").animate({opacity: "show"}, "slow");
    $("#quickOrderForm input:first").focus();
}

function sendQuickOrder ()
{
    var name       = document.getElementById("name");
    var phone      = document.getElementById("phone");
    var address    = document.getElementById("address");
    var comment    = document.getElementById("comment");
    var visitor_id = getCookie('visitor_id');

    if (visitor_id == null)
    {
        alert('Ошибка! В Вашем браузере должны быть включены куки.');
        return;
    }
    if (name.value.replace(/^\s+|\s+$/g,"").length < 3)
    {
        alert('Введите Ваше имя!');
        name.focus();
        return false;
    }
    if (phone.value.replace(/^\s+|\s+$/g,"").length < 10)
    {
        alert('Введите номер моб. телефона!');
        phone.focus();
        return false;
    }
    $.ajax({
        type: 'POST',
        url:  'ajax/shopcart.php',
        data: 'action=sendOrder&visitor_id=' + visitor_id + '&name=' + name.value + '&phone=' + phone.value + '&address=' + address.value + '&comment=' + comment.value,
        success: function ()
        {
            $("#shopcart").parent().remove();
            var p = document.createElement('p');
            $(".CCol").append(p);
            $(".CCol p:first").attr("align", "center").html('Ваш заказ отправлен')
        }
    });
}

function getCookie (name)
{
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end    = 0;
    
    if (cookie.length > 0)
    {
        offset = cookie.indexOf(search);
        
        if (offset != -1)
        {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            
            if (end == -1)
            {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

function setCookie (name, value, expires, path, domain, secure)
{
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getRandNumber(min, max){
return Math.round(Math.random()*(max-min))+min;
}
