

/**
 * @class ProductBubble_Class
 * This class handles operation with shopping cart like add to baga, retrive shopping cart, display etc..
 * for more details, look at its method
 * P.S: This class has not been quite finished as it needs some more methods in order to hanadle most of our needs
 * @constructor 
 */
 Core.ShoppingCartBubble_Class=function () {
    //Properties
     /**
     *  The main dhtml layer for the shopping cart
     */
    this.ShoppingCartBubble_DivID='ShoppingCartBubble_Div';
    //IDS
    /**
     *  A dhtml layer where subtotal will be displayed
     */
    this.ShoppingCartBubble_SubTotalID='ShoppingCartBubble_SubTotal';
     /**
     *  A dhtml layer where subtotal will be displayed
     */
    this.ShoppingCartBubble_SubTotalID_1='ShoppingCartBubble_SubTotal_1';
     /**
     *  A dhtml layer where subtotal will be displayed on the header outside of the bubble
     */
   this.ShoppingCartBubble_SubTotalID_Header='ShoppingCartBubble_SubTotal_header';
    /**
     *  A dhtml layer where total will be displayed
     */
    this.ShoppingCartBubble_TotalID='ShoppingCartBubble_Total';
    /**
     *  A dhtml layer where tax will be displayed
     */
    this.ShoppingCartBubble_TaxTotalID='ShoppingCartBubble_TaxTotal';
    /**
     *  A dhtml layer where shipping subtotal will be displayed
     */
    this.ShoppingCartBubble_ShippingSubTotalID='ShoppingCartBubble_ShippingSubTotal';
    //
    /**
     * A dhtml layer where total items will be displayed
     */
    this.ShoppingCartBubble_TotalItemsID='ShoppingCartBubble_TotalItems'
    /**
     * A dhtml layer where total items will be displayed outside of the bubble
     */
    this.ShoppingCartBubble_TotalItemsID_Header_Outside='ShoppingCartBubble_TotalItems_header'
    /**
     *  success layer, not used yet
     */
    this.ShoppingCartBubble_SuccessID='ShoppingCartBubble_Success';
    /**
     *  failure layer not used yet
     */
    this.ShoppingCartBubble_FailureID='ShoppingCartBubble_Failure';

 
    /**
     * It holds the DHTML Object ID for product Name of last item added
     */
    this.ShoppingCartBubble_ProductNameID="ShoppingCartBubble_ProductName";  
     /**
     * It holds the DHTML Object ID for product Image of last item added
     */
    this.ShoppingCartBubble_ProductImageID="ShoppingCartBubble_ProductImage";  
    /**
     * It holds the DHTML Object ID for Variant Name of last item added
     */
    this.ShoppingCartBubble_VariantNameID="ShoppingCartBubble_VariantName";  
    /**
     * It holds the DHTML Object ID for item unit price of last item added
     */
    this.ShoppingCartBubble_ItemPriceID="ShoppingCartBubble_ItemPrice";      
    /**
     * It holds the DHTML Object ID for item quantity of last item added
     */
    this.ShoppingCartBubble_ItemQuantityID="ShoppingCartBubble_ItemQuantity"; 
    /**
     * It holds the DHTML Object ID for last item total example: UnitPrice * Quantity
     */
    this.ShoppingCartBubble_LastItemTotalAmountID="ShoppingCartBubble_LastItemTotalAmount";
     /**
     * It holds the DHTML Object ID for last item layer
     */
    this.ShoppingCartBubble_Last_Item_Added="ShoppingCartBubble_Last_Item_Added";
     /**
     * It holds the DHTML Object ID for last item layer as a stand alone object
     */
    this.ShoppingCartBubble_Last_Item_Added_Caption="ShoppingCartBubble_Last_Item_Added_Caption";
    /**
     *
     * Events
     */
     /**
     * the name of the function to be called before the popup opens
     */
    this.beforeOpen=null; 
    
    /**
     * the name of the function to be called before the popup closes
     */
    this.beforeClose=null;
    /**
     * Handles the callback from the web service AddSingleProductToCart
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.AddSingleProductToCartResultHandler=function(result)  {
          DoDefault();
        if (result != null) {
            if (result.ActivityStatus==true) { 
                //Display The Shopping Cart
                //var _WebServiceAPI=new  WebServiceAPI_Class();
                _WebServiceAPI.RetrieveShoppingCart(GetCurrentCustomerID(),_ShoppingCartBubble_Class.RetrieveShoppingCartResultHandler);
            }
            else  {
                       if (result.ErrorString.indexOf('The maximum amount of the shopping cart is reach')!=-1 &&  result.ErrorString.indexOf(':')!=-1) {
                        var error_array=result.ErrorString.split(':');
                        error_array[0]='The total amount of your order may not exceed $';
                        alert (error_array[0] + formatAsMoney(error_array[1].replace(" ","")));
                    }
                    else {
                     alert(result.ErrorString);
                    }
              }  
         }
    }

    /**
     * Handles the callback from the web service RetrieveShoppingCartLastItems
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.RetrieveShoppingCartLastItemsResultHandler=function (result) {
        DoDefault();
        if (result != null) {
            _ShoppingCartBubble_Class.DisplayShoppingCartAndLastItems(result);
        }
    }

    /**
     * Displays the shopping and last items
     */
    this.DisplayShoppingCartAndLastItems=function() {
        alert('this method is not finished yet, it is in progress');
        this.Show();
    }

    /**
     * Handles the callback from the web service RetrieveShoppingCart
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.RetrieveShoppingCartResultHandler=function (result) {
        DoDefault();
        if (result != null) {
            _ShoppingCartBubble_Class.DisplayShoppingCart(result);
        }
    }

    /**
     * Displays the shopping cart information
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.DisplayShoppingCart=function(result) {
        DoDefault();
        //display subtotal
         var obj=$get(this.ShoppingCartBubble_SubTotalID);
        if (obj!=null) {
            obj.innerHTML=result.SubTotal.toFixed(2);
            obj.innerHTML="$" + obj.innerHTML;
        }
         var obj=$get(this.ShoppingCartBubble_SubTotalID_1);
        if (obj!=null) {
            obj.innerHTML=result.SubTotal.toFixed(2);
            obj.innerHTML="$" + obj.innerHTML;
        }
        //populate header subtotal
        var obj_h=$get(this.ShoppingCartBubble_SubTotalID_Header);
        
        if (obj_h!=null) {
            obj_h.innerHTML=result.SubTotal.toFixed(2);
            obj_h.innerHTML="$" + obj_h.innerHTML;
        }
         
        //display total amount
        if ($get(this.ShoppingCartBubble_TotalID)!=null) {
            $get(this.ShoppingCartBubble_TotalID).innerHTML=result.Total.toFixed(2);
        }
        //display tax 
        if ($get(this.ShoppingCartBubble_TaxTotalID)!=null) {
            $get(this.ShoppingCartBubble_TaxTotalID).innerHTML=result.TaxTotal.toFixed(2);
        }
        //display shipping cost
        if ($get(this.ShoppingCartBubble_ShippingSubTotalID)!=null) {
         $get(this.ShoppingCartBubble_ShippingSubTotalID).innerHTML=result.ShippingSubTotal.toFixed(2);
        
        }
        //total items within bubble
        if ($get(this.ShoppingCartBubble_TotalItemsID)!=null) {
            $get(this.ShoppingCartBubble_TotalItemsID).innerHTML=result.Quantity;
        }
        //updating total items outside of bubble like header
         if ($get(this.ShoppingCartBubble_TotalItemsID_Header_Outside)!=null) {
            $get(this.ShoppingCartBubble_TotalItemsID_Header_Outside).innerHTML=result.Quantity;
        }
         this.DisplayShoppingCartLastItem(); //new
         this.Show();
    }

   /**
     * Displays the shopping cart information
     */
    this.DisplayShoppingCartLastItem=function() { //new method
        //product name
       if ($get(this.ShoppingCartBubble_ProductNameID)!=null) {
            $get(this.ShoppingCartBubble_ProductNameID).innerHTML=  _LastShoppingCartItem.ItemParentName;
        }
        
        var objimage=$get(this.ShoppingCartBubble_ProductImageID);
        if (objimage!=null) {
         objimage.src=IMAGES_RELATIVE_OR_ABSOLUTE_PATH + PRODUCT_SMALL_IMAGE_PREFIX  + _LastShoppingCartItem.ItemParentCode +  PRODUCT_SMALL_IMAGE_SUFFIX;
        }
        //variant name
        if ($get(this.ShoppingCartBubble_VariantNameID)!=null) {
            $get(this.ShoppingCartBubble_VariantNameID).innerHTML=  _LastShoppingCartItem.ItemName;
        }
        //variant price
        if ($get(this.ShoppingCartBubble_ItemPriceID)!=null) {
               $get(this.ShoppingCartBubble_ItemPriceID).innerHTML=  "$" + _LastShoppingCartItem.ItemPrice;//.toFixed(2);
        }
        //variant quantity
        if ($get( this.ShoppingCartBubble_ItemQuantityID)!=null) {
               $get( this.ShoppingCartBubble_ItemQuantityID).innerHTML=  _LastShoppingCartItem.ItemQuantity;
        }
     
        //total item for last item quantity * price
        if ($get( this.ShoppingCartBubble_LastItemTotalAmountID)!=null) {
                var _ItemTotalAmount=0
                _ItemTotalAmount=parseInt(_LastShoppingCartItem.ItemQuantity) * _LastShoppingCartItem.ItemPrice;
               $get( this.ShoppingCartBubble_LastItemTotalAmountID).innerHTML=  _ItemTotalAmount.toFixed(2);
        }
        
         if ($get( this.ShoppingCartBubble_Last_Item_Added)!=null) {
             $get( this.ShoppingCartBubble_Last_Item_Added).style.display="";
         }
         if ($get( this.ShoppingCartBubble_Last_Item_Added_Caption)!=null) {
            $get( this.ShoppingCartBubble_Last_Item_Added_Caption).style.display="";
         }
    
    
    }
    /**
     * Displays the shopping cart bubble
     */
    this.Show=function() {
       // var obj=$get(this.ShoppingCartBubble_DivID);
       // obj.style.visibility='visible';
        jQuery('#' + this.ShoppingCartBubble_DivID).slideDown(1500);
        //obj.style.display='';
        // slideContent(this.ShoppingCartBubble_DivID.toString(),10);
          if (this.beforeOpen!=null) {
             eval(this.beforeOpen + "();");
        
        }
        setTimeout(function () { _ShoppingCartBubble_Class.Hide();},5500);
    }

    /**
     * Hides the shopping cart bubble
     */
    this.Hide=function() {
        //var obj=$get(this.ShoppingCartBubble_DivID);
        //obj.style.visibility='hidden';
        jQuery('#' + this.ShoppingCartBubble_DivID).slideUp(1500);
        // obj.style.display='none';
        _QuickShop_Class.Hide();
        if (this.beforeClose!=null) {
             eval(this.beforeClose + "();");
        
        }
    }
}

 /**
 * It registers the ShoppingCartBubble_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ShoppingCartBubble_Class.registerClass('Core.ShoppingCartBubble_Class'); }


/**
 * @class ShoppingCartItem_Class
 * This class stores the information of the last item added to bag
 * @constructor 
 */
 Core.ShoppingCartItem_Class=function () {
    /**
     * Variant code of the item added to shopping cart
     */    
    this.ItemCode="";
    /**
     * Variant name
     */ 
    this.ItemName="";
    /**
     * Product code(parent product code of the variant)
     */ 
    this.ItemParentCode="";
    /**
     * Product Name(parent product name of the variant)
     */ 
    this.ItemParentName="";
    /**
     * quantity of the last add to bag
     */ 
    this.ItemQuantity=1;
    /**
     * item unit price
     */ 
    this.ItemPrice=0;
    
    /**
     * Populates the major data of the object
     @param {_ItemCode,_ItemName,_ItemParentCode,_ItemParentName,_ItemPrice} 
     */ 
    this.PopulateData=function(_ItemCode,_ItemName,_ItemParentCode,_ItemParentName,_ItemPrice){
                this.ItemCode=_ItemCode;
                this.ItemName=_ItemName;
                this.ItemParentCode=_ItemParentCode;
                this.ItemParentName=_ItemParentName;
                this.ItemPrice=_ItemPrice;
        
        }
}

/**
 * It registers the Core.ShoppingCartItem_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ShoppingCartItem_Class.registerClass('Core.ShoppingCartItem_Class'); }

