/**
	RichCart operation
*/
richCart={
	id:null, // richCart id
	url:null,//request url
	isAddOpenCart:false, //check if add to cart open the cart,if  it is true mouseleave will don't use.
	isPageFresh:true,	//check if page is fresh, if true will ajax post request to server and then get response data.
	isOpened:false,   //check richCart oppened status,if true richCart is oppened.
	maskDivId:null,   //mask div,div can't cover select in IE , but iframe can cover it.div can cover iframe, so set maskDiv()iframe under the richCart.
	itemCountClass:null,  //display  0 Item = $0.00 
	isInMiniShoppingCart:false,  //display  0 Item = $0.00 mouse is in mini shoppingCart
	delay:3000, //delay close mini shoppingCart time
	beginTime:null, //add item to cart of time
	isClickedAddToCart:false, //flag whether clicked add to cart
	postForm:function(formId,isAutoClose){
		if(!formId){
			return;
		}
		this.isPageFresh=false;
		this.isInMiniShoppingCart=false;
		
		if(isAutoClose!=true){
			this.isAddOpenCart=true;
		}else{
			this.isAddOpenCart=false;
		}
		this.ajaxPostForm(formId);
		this.isInMiniShoppingCart=false;
		this.isClickedAddToCart=true;
	},
	/*
		ajax send request
	*/
	ajaxPostForm:function(formId){ 
		var bindParam={
			type: "POST",
			url:this.url,
			success: function(data){
				richCart.handleResponse(data);
			},
			error:function(xhr, ajaxOptions, thrownError){
				 
			}
		};
		if(!formId){
			bindParam.data=null;
			bindParam.success=function(data){richCart.handleResponse(data,true);};
		}else{
			bindParam.data= getFormData(formId);
		}
		$.ajax(bindParam);
	},
	 
	/*
		handle response data.
		fill data into richCart,show richCart.
	*/
	handleResponse:function(data,firstHover){ 
	    $("#errorMessage").hide();
		$("#errorMessage").html(data);
		if($("#errorInfo").attr("succesflag")=="false"){
		    $("#errorMessage").show();
			return;
		}
	    $("#"+this.id).html(data);
		if(!firstHover){
			if(this.itemCountClass){ 
				 if($("#itemCount").get(0)){ 
				
					$("#checkouttext").html("<span><font color='#666'>"+$("#itemCount").val()+" Items</font></span>");
				 }
			}
		}
		this.beginTime=new Date().getTime();
		if(this.isOpened!=true){
			this.showCart();
			if(this.isClickedAddToCart==true){
				this.closeCartDelayByAddtoCart();
			}
		}
	},
	/*
		show richCart:if cart is'not opened and page don't fresh,direct to show.
				     if cart is not opened and page fresh,read data from server,and then show.
	*/
	showCart:function(){
	    this.isInMiniShoppingCart=true;
	    if(this.id){
			if(this.isOpened!=true){
				if(this.isPageFresh==false){ 
					this.fixIeCover.showMaskDiv(); 
					$("#"+this.id).slideDown("fast",function(){
						richCart.isOpened=true;
					});	
				}else if(this.isPageFresh==true){
					this.ajaxPostForm();
					this.isPageFresh=false;
				}
			}
		}
	},
	/*
		close richCart:if cart is opened and don't add to cart hide page.
				   
	*/
	closeCart:function(){
		if(this.id){
			if(this.isOpened==true){
				if(this.isAddOpenCart==false && this.isInMiniShoppingCart==false){
					$("#"+this.id).slideUp("fast",function(){
						richCart.isOpened=false;
					});
					this.fixIeCover.hideMaskDiv();
				}
				
			}
			this.isClickedAddToCart=false;
		}
		
	},
	/*
		close richCart:if cart  reach delay time.
				   
	*/
	closeCartDelay:function(){
		this.isInMiniShoppingCart=false;
		setTimeout("richCart.closeCart()",this.delay);
	},
	/*
		close richCart:if cart reach delay time by add item to cart.
				   
	*/
	closeCartDelayByAddtoCart:function(){
	    this.isInMiniShoppingCart=false;
		var interval=setInterval(
			function(){
				var endTime=new Date().getTime();
				if((endTime-richCart.beginTime)>=richCart.delay){ 
					 richCart.closeCart();
					 clearInterval(interval);
				}
			}
			,100
		);
		
	},
	/*
		force close cart:don't need to check if  cart is opened or add to cart.
	*/
	forceCloseCart:function(){
		if(this.id){ 
			$("#"+this.id).slideUp("fast",function(){
				richCart.isOpened=false;
			});
			this.isOpened=false;
			this.isAddOpenCart=false;
			this.fixIeCover.hideMaskDiv();
			this.isClickedAddToCart=false;
		}
	},
	fixIeCover:{
		maskDiv:null,
		isIe:function(){
			if(jQuery.browser.msie){
				return true;
			}
			return false;
		},
		initMaskDiv:function(){ 
			this.maskDiv=$("#"+richCart.maskDivId);
			if(!this.isIe() && !richCart.maskDivId) return;
			var tmpRichCart=$("#"+richCart.id);
			var richWidth=tmpRichCart.width(); 
			var richHeight=tmpRichCart.height();
			this.maskDiv.width(richWidth);
			this.maskDiv.height(richHeight);
		},
		showMaskDiv:function(){
			if(!this.isIe() && !richCart.maskDivId) return;
			this.initMaskDiv();
			
			$("#"+richCart.maskDivId).slideDown("fast");
		},
		hideMaskDiv:function(){ 
			if(!this.isIe() && !richCart.maskDivId) return;
			$("#"+richCart.maskDivId).slideUp("fast");
		}
		
	}
}

