$(document).ready(function(){
    var res_price = "";
    $(".lamps_comp").click(function(){
            var id = $(this).val();
            var count = parseInt($(this).next().val());
            var price = parseInt($(this).next().next().val());
	    var str_price = ($("#res_summ").text()).replace(/[ ]+/, "");
            var tmp_price = parseInt(str_price.replace(/[руб]+/, ""));
	    var str_price2 = ($("#allsum").text()).replace(/[ ]+/, "");
            var tmp_price2 = parseInt(str_price2.replace(/[руб]+/, ""));
	    var res_count = parseInt($("#res_col").text());
            
            if($(this).attr("checked")){
		add_to_basket_comp(id,jQuery(this).offset().left-40+"px",jQuery(this).offset().top-140+"px", count);		
		res_price = number_format(tmp_price+price, 0, ",", " ") + " руб";
		res_price2 = number_format(tmp_price2+price, 0, ",", " ") + " руб";
		res_count = res_count + count;
            }
            else{
		delete_from_basket_lamps_comp(id,jQuery(this).offset().left-40+"px",jQuery(this).offset().top-140+"px");
		res_price = number_format(tmp_price-price, 0, ",", " ") + " руб";
		res_price2 = number_format(tmp_price2-price, 0, ",", " ") + " руб";
		res_count = res_count - count;
	    }
            $("#res_summ").text(res_price);
	    $("#allsum").text(res_price2);
	    $("#res_col").text(res_count);
    });
});
function add_to_basket_comp(arg,arg1,arg2,ccount){
	/*$("#ajax_in").html("");
	$("#real_psevdo").html("");
	$("#ajax").css("left",arg1);
	$("#ajax").css("top",arg2);
	$("#ajax").slideDown();*/

	var TID = CPHttpRequest.InitThread();	
	//CPHttpRequest.SetAction(TID, ShowResultComp);
	CPHttpRequest.Send(TID, '/addtobasket.php', {'ID':arg, 'CCOUNT':ccount, 'mode':'ajax', 'backs':window.location.href});
	return false;
}

function HideResultComp() {
	$("#ajax").slideUp();
	$("#ajax_in").html("");
	$("#real_psevdo").html("");
}

function ShowResultComp(data) {
	$("#h_add_comp").html(data);
	$("#real_psevdo").html($("#psevdo_basket").html());
	setTimeout("HideResultComp()",2000);
}

function delete_from_basket_lamps_comp(id,arg1,arg2){
    $.get('/ajax/delete_product.php', { id: id }, function(data) {
        if (data.length > 0) {
	    //$("#ajax").css("left",arg1);
	    //$("#ajax").css("top",arg2);
	    //$("#ajax").slideDown();
	    //$("#real_psevdo").html("<b>"+data+"</b>");
	    //$("#ajax_in").html("");
	    //setTimeout("HideResultComp()",2000);
        }
        else {
            //alert("Товар не удалось удалить из корзины");
        }
    });
}

function number_format( number, decimals, dec_point, thousands_sep ) {

	var i, j, kw, kd, km;

	// input sanitation & defaults
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


	return km + kw + kd;
}

