$(document).ready(function(){
        cart_update();
        //count_total();

        if($.cookie('make')){
            setmake();
        }
        

        $("a.fancybox").fancybox();



        $("li.parent").bind("mouseenter",function(){
            $(this).addClass("over");
            $(this).find(".level0").css("left","-1px");
        }).bind("mouseleave",function(){
            $(this).removeClass("over");
            $(this).find(".level0").css("left","-10000px");
        });

        $("#search").bind("focus",function(){
            $(this).attr("value","");
        });

        $("#maker").bind("change",function(){
            window.document.location.href = window.location.pathname+"?maker="+$(this).val();
        })
        $("#filter").bind("change",function(){
            var add = "";
            if($("#maker").val()){
                add = "&maker="+$("#maker").val();
            }
            window.document.location.href = window.location.pathname+"?filter="+$(this).val()+add;
        })
});

function mclear(){
    $.cookie('make', "", {path: '/',domain: document.domain});
    $.cookie('model',"", {path: '/',domain: document.domain});
    window.document.location.href = "/";
}

function setmake(make){
    if(!$("#make").val()){
        $("#model").attr("disabled",true);
        $.cookie('make', "", {path: '/',domain: document.domain});
        $.cookie('model',"", {path: '/',domain: document.domain});
        $("#model").html('<option value="">Выбрать модель</option>');
    }else{
        if($.cookie('make') != $("#make").val()){
            $.cookie('make', $("#make").val(), {path: '/',domain: document.domain});
            $.cookie('model', "", {path: '/',domain: document.domain});
            document.location.href = "/auto/"+$("#make").val()+"/";
        }
        $("#model").attr("disabled",true);
        $.getJSON("/ajax/load_models/"+$("#make").val()+"/",{},function(data){
            var html = '<option value="">Выбрать модель</option>';
            $.each(data.items, function(i,item){
                var selected = "";
                if(item.model == $.cookie('model')){
                    selected = 'selected="selected"';
                }
                html = html+"<"+"option value='"+item.model+"' "+selected+" >"+item.model+"</"+"option"+">";
            });
            $("#model").html(html);
            $("#model").removeAttr("disabled");
            setmodel();
        });
    }
}

function setmodel(model){
    if(!$("#model").val()){ 
        $("#year").attr("disabled","disbled");
        $.cookie('model', "", {path: '/',domain: document.domain});
        $.cookie('year',"", {path: '/',domain: document.domain});
        $("#year").html('<option value="">Выбрать год</option>');
    }else{
        if($.cookie('model') != $("#model").val()){
            $.cookie('make', $("#make").val(), {path: '/',domain: document.domain});
            $.cookie('model', $("#model").val(), {path: '/',domain: document.domain});
            $.cookie('year', "", {path: '/',domain: document.domain});
            document.location.href = "/auto/"+$("#make").val()+"/"+$("#model").val()+"/";
        }
        $("#year").attr("disabled","disbled");
        $.getJSON("/ajax/load_years/"+$("#model").val()+"/",{},function(data){
            var html = '<option value="">Выбрать год</option>';
            $.each(data.items, function(i,item){
                var selected = "";
                if(item.year == $.cookie('year')){
                    selected = 'selected="selected"';
                }
                html = html+"<"+"option value='"+item.year+"' "+selected+" >"+item.year+"</"+"option"+">";
            });
            $("#year").html(html);
            $("#year").attr("disabled","");
        });
    }
}


function setprofilemake(){

    if($("#profilemake").val() == ""){
        $("#profilemodel").attr("disabled","disbled");
        $("#profilemodel").html('<option value="">Выбрать модель</option>');
    }else{
        $("#profilemodel").attr("disabled","disbled");
        $.getJSON("/ajax/load_models/",{make:$("#profilemake").val()},function(data){
            var html = '<option value="">Выбрать модель</option>';
            $.each(data.items, function(i,item){
                var selected = "";
                if(item.model == $('#car_model').val()){
                    selected = 'selected="selected"';
                }
                html = html+"<"+"option value='"+item.model+"' "+selected+" >"+item.model+"</"+"option"+">";
            });
            $("#profilemodel").html(html);
            $("#profilemodel").attr("disabled","");
        });
    }
}

function setyear(){
    $.cookie('year', $("#year").val(), {path: '/',domain: document.domain});
    window.document.location.href = "/";
}

function count_total() {
    var total = 0;
    $("span.ids").each(function(){
        var price = parseInt($("#price_"+this.id).text());
        var count = parseInt($("#count_"+this.id).val());
        var cost = price*count;
        total = total + cost;
        $("#total_"+this.id).text(cost);
    });
    $("#total").text(total);
}

function add_to_cart(id) {
    if($.cookie('cart')){
        var arr = $.cookie('cart').split(/:/);
        var narr = new Array();
        var noadd = 0;
        for (var i = 0; i < arr.length; i++){
            if(id == arr[i]){
                noadd = 1;
            }
            narr.push(arr[i]);
        }
        if(noadd == 0){
            narr.push(id);
        }
        $.cookie('cart', narr.join(":"), {path: '/',domain: document.domain});
    }else{
        $.cookie('cart', id, {path: '/',domain: document.domain});
    }
    cart_update();
    alert("Товар добавлен в корзину");
    window.location.reload();
};

function del_from_cart(id) {
    if(confirm("Вы действительно хотите удалить товар из корзины")){
        var add = $.cookie('cart').split(/:/);
        var narr = new Array();
        for (var i = 0; i < add.length; i++){
            if(id != add[i]){
                narr.push(add[i]);
            }
        }
        add = narr.join(":");
        $.cookie('cart', add, { path: '/',domain: document.domain});
        window.location.reload();
    }
}


function cart_update() {
    if($.cookie('cart')){
        $.getJSON("/ajax/cart_info/1/",{},
        function(data){
            $("#cart_summ").text(data['summ']);
            $(".cart_items").text(data['items']);
        });
    }else{
        $("#cart_summ").text(0);
        $(".cart_items").text(0);
    }
}
function delete_cookie() {
    $.cookie('cart', null,{ path: '/',domain: document.domain});
    $("#cart_summ").text(0);
    $("#cart_items").text(0);
    //window.location.reload(true);
}

function recount(){
    var total = 0;
    $("#cart_table").find(".item_row").each(function(){
        var price = parseInt($(this).find(".item_price").text());
        var count = parseInt($(this).find(".item_count").val());
        var summ = price * count;
        total = total + summ;
        $(this).find(".item_summ").text(summ);
    });
    $("#cart_table").find("#item_total").text(total);
}

function fsubmit(id){
    $("#"+id).submit();
}

function add_to_notepad(id){
    $.getJSON("/ajax/add_to_notepad/",{item_id:id},function(data){
            if(data["status"] == "ok"){
                alert("Товар добавлен в блокнот");
            }
            if(data["status"] == "autherr"){
                alert("Записать в блокнот могут только зарегистрированные пользователи");
            }
            if(data["status"] == "noitemid"){
                alert("Ошибка");
            }
            
    });
}
function rm_from_notepad(id){
    $.getJSON("/ajax/rm_from_notepad/",{item_id:id},function(data){
            if(data["status"] == "ok"){
                $("#item_"+id).hide();
            }
    });
}
