jiyiri.register_namespace('jiyiri.page.promoshoppingcart');

jiyiri.require_once('jiyiri.page.promoshoppingcart.shoppingcartapi');

(
function()
{

	/**
	 * 一个产品 放入购物车的按钮显示控制
	 * 如果已近存在购物车 则显示Disable状态
	 * 如果没有 则显示放入购物车状态
	 */
	var Product_CartButton = Class.create();


	Product_CartButton.prototype = {

		initialize:function() {

			this._Product_Type;

			this._Product_Id;

			this._Product_Num;

			this._Cart_Button;

			this._Cart_Button_Disable_Img 	= '/Public/v6/img/appendToCart_6.gif';
			this._Cart_Button_Able_Img 		= '/Public/v6/img/appendToCart_5.gif';


		},
		config:function( ProductType  , ProductId ,  ProductNum , CartButton ){
			this._Product_Type	= ProductType;
			this._Product_Id	= ProductId;
			this._Product_Num	= ProductNum;
			this._Cart_Button	= $(CartButton);

			this._Cart_Button.onclick =  jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'add_cart');
		},
		show:function(){
			this._check_is_exsit_in_cart( jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_after_check_cart'));
		},
		add_cart:function(){
			if( this._Product_Type == ProductType.Promo ){
				var type = 'promo';
			}else if( this._Product_Type == ProductType.Card ){
				var type = 'card';
			}

			jiyiri.page.promoshoppingcart.ShoppingCartAPI.GetInstance().
							put_product_to_cart( type , this._Product_Id , this._Product_Num ,
												 jiyiri.helper.eventhelper.EventHelper.create_callback_function(this,'_on_complete_add_cart'));
		},
		_check_is_exsit_in_cart:function( callback_function ){

			var param = new jiyiri.helper.net.Parameter();
			param.add_param('product_type',this._Product_Type);
			param.add_param('product_id',this._Product_Id);
			param.add_param('product_num',this._Product_Num);
			jiyiri.helper.net.Ajax.post_tp('Cart','isProductExist', param,callback_function
					);

		},
		_on_complete_add_cart:function( response ){

			var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
			if(!rst || !rst.status)
			{
				alert( rst.info );
			}
			else
			{
				this._show_disable();
			}
		},
		_after_check_cart:function( response ){

			var rst = jiyiri.helper.net.Ajax.parse_tp_return(response.responseText);
			if(!rst || !rst.status)
			{
				alert( rst.info );
			}
			else
			{
				if( rst.info ){
					this._show_disable();
				}else{
					this._show_able();
				}
			}

		},
		_show_able:function(){
			this._Cart_Button.disabled = false;
			this._Cart_Button.src = this._Cart_Button_Able_Img;
		},
		_show_disable:function(){
			this._Cart_Button.src = this._Cart_Button_Disable_Img;
			this._Cart_Button.disabled = true;
		}

	}


	jiyiri.page.promoshoppingcart.Product_CartButton = Product_CartButton;

}
)
();

