$(document).ready(function(){
	

	$("#car_make").bind("change",function(){
		load_model($(this).attr("rel"),$(this).val());
	})

	$("#car_model").bind("change",function(){
		load_year($(this).attr("rel"),$("#car_make").val(),$(this).val());
	})

	$("#car_year").bind("change",function(){
		var rel = $(this).attr("rel")
		if($(this).val()){
			$.cookie(rel+'-year', $(this).val(), {path: '/',domain: document.domain});
		}else{
	        $.cookie(rel+'-year', "", {path: '/',domain: document.domain});
		}
		window.location.href = "/"+rel+"/";
	})
        
        $("#item_price").bind("dblclick",function(){
            var it = $(this);
            $.getJSON("/cgi-bin/admin/api.cgi",{action:"allow"},function(data){
                if(data["access"] == "ok"){
                    var value = prompt("Новая цена","");
                    if(value){
                        $.getJSON("/cgi-bin/admin/api.cgi",{action:"setprice",value:value,id:it.attr("rel")},function(ddata){
                            if(ddata["status"] == "ok"){
                                $.cookie('nocache', "1", {path: '/',domain: document.domain}); 
                                window.location.reload();
                            }
                        })
                    }
                }
            })
        });


});


function load_model(rel,make){
	if(make){
        $.cookie(rel+'-make',make, {path: '/',domain: document.domain});
		$.getJSON("/"+rel+"/ajax/load_model/",{make:make,ajax_get:1},function(data){
			var html = '<option value="">Модель</option>';
			$.each(data.items, function(i,item){
				var selected = "";
                if(item.model == $.cookie(rel+'-model')){
                    selected = 'selected="selected"';
                }
				html = html+"<"+"option value='"+item.model+"' "+selected+" >"+item.model+"</"+"option"+">";
				
			});
			$("#car_model").html(html);
            $("#car_model").attr("disabled","");	
		})
        
	}else{
		$.cookie(rel+'-make', "", {path: '/',domain: document.domain});
		$.cookie(rel+'-model',"", {path: '/',domain: document.domain});
        $.cookie(rel+'-year', "", {path: '/',domain: document.domain});
		window.location.href = "/"+rel+"/";
	}
}

function load_year(rel,make,model){
	if(model){
        $.cookie(rel+'-model',model, {path: '/',domain: document.domain});
		$.getJSON("/body-kits/ajax/load_year/",{make:make,model:model,ajax_get:1},function(data){
			var html = '<option value="">Год выпуска</option>';
			$.each(data.items, function(i,item){
				var selected = "";
                if(item.year == $.cookie(rel+'-year')){
                    selected = 'selected="selected"';
                }
				html = html+"<"+"option value='"+item.year+"' "+selected+" >"+item.year+"</"+"option"+">";
				
			});
			$("#car_year").html(html);
            $("#car_year").attr("disabled","");	
		})
        
	}else{
		$.cookie(rel+'-model',"", {path: '/',domain: document.domain});
        $.cookie(rel+'-year', "", {path: '/',domain: document.domain});
		window.location.href = "/"+rel+"/";
	}
}
