function Basket() {
}

Basket.prototype = {
	
	products:		{},
	products_count:	0,
	sum_price:		0,
	
    add_product: function(product_option_id) {
    	if (!this.products[product_option_id] || this.products[product_option_id].primary) {
    		return this.add_main_product(product_option_id);
    	}
    	return this.add_related_product(product_option_id);
    },
	
    dec_product: function(product_option_id) {
		var text = $.ajax({
			url: "/basket/changeproductcount/?action=dec&product_option_id="+product_option_id,
			async: false
		}).responseText;
		if(text == "Error") {
			return false;
		}
		this.products = eval( '('+text+')' );
		$.cookie('basket_products', text, { path: '/' });
		this.refresh();
		return true;
    },
	
    del_product: function(product_option_id) {

        /** удаление товара из корзины **/
        _gaq.push(['_trackEvent', 'basket', 'delete']);

		var text = $.ajax({
			url: "/basket/changeproductcount/?action=del&product_option_id="+product_option_id,
			async: false
		}).responseText;
		if(text == "Error") {
			return false;
		}
		this.products = eval( '('+text+')' );
		$.cookie('basket_products', text, { path: '/' });
		this.refresh();
		return true;
    },
	
    add_main_product: function(product_option_id) {
		var text = $.ajax({
			url: "/basket/changeproductcount/?action=add&product_option_id="+product_option_id,
			async: false
		}).responseText;
		if(text == "Error") {
			return false;
		}
		this.products = eval( '('+text+')' );
		$.cookie('basket_products', text, { path: '/' });
		this.refresh();
		return true;
    },
	
    add_related_product: function(product_option_id) {
    	if(this.products[product_option_id] && this.products[product_option_id].count < this.products[product_option_id].max_count) {
    		this.add_main_product(product_option_id);
    		return true;
    	}
    	else {
    		return false;
    	}
    },
    
    refresh: function() {
    	this.recount();
    	$("#basket_products_count").html(this.products_count);
    	$("#basket_sum_price").html(this.sum_price);
    	var basket_img_src = this.products_count ? "/static/img/cart_full.gif" : "/static/img/cart_empty.gif";
    	$("#basket_image").attr('src', basket_img_src);
    	$("#cart_block").show();
    },
    
    recount: function() {
    	if(isEmpty(this.products)) {
    		this.init();
    	}
    	this.products_count = 0;
    	this.sum_price = 0;
    	for(var product_option_id in this.products) if (this.products.hasOwnProperty(product_option_id)) {
    		this.sum_price += this.products[product_option_id].price * this.products[product_option_id].count;
    		this.products_count += this.products[product_option_id].count;
    	}
    },
    
    update_basket_page: function() {
    	if(!this.products) {
    		this.init();
    	}
    	var products_count = 0;
    	var sum_price = 0;
    	
    	var products = this.products;
    	
    	$("table.cart_table input").each(function() {
    		var product_option_id = $(this).attr('product_option_id');
    		
    		if(!products[product_option_id] || !products[product_option_id].count) {
    			$("#basket_product_option_block_" + product_option_id).remove();
    			return ;
    		}
    		
    		var cost = products[product_option_id].price * products[product_option_id].count;
    		
    		$("#cost_" + product_option_id).html(cost + ' р.');
    		$("#basket_product_" + product_option_id).val(products[product_option_id].count);
    		sum_price += cost;
    		products_count += products[product_option_id].count;
    	});
    	
    	$('#total_price').html(sum_price + ' руб.');
    },
    
    init: function() {
    	var basket_products = $.cookie('basket_products')
    	if(basket_products) {
    		this.products = eval('('+basket_products+')');
    	}
    }
}

basket = new Basket();
