

/**
 * @class VariantBubble_Class
 *  This class is responsible into managing the Varianat Bubble Class
 * Please be aware that the properties of this class needs to be overridden based on the case that we will be using.
 *  The one which needs to be changed are
 *        this.VariantBubble_ImagePrefix   and    this.VariantBubble_ImageSuffix;
 * The other properties are DHTML Object IDs that needs to be populated with the data.....
 * In case there are other data that needs to be populated, we either need to support on this class or we need to prototype
 *  its methods.
 */

Core.VariantBubble_Class=function () {
    /**
     * The DHTML ID of the main layer of the Varianat bubble
     */
    this.VariantBubble_DivID="VariantBubble_Div";

    /**
     * The DHTML ID of the Product/Variant Image
     */
    this.VariantBubble_ImageID="VariantBubble_Image";

    /**
     * The DHTML ID of the quantity
     */
    this.VariantBubble_QuantityID="VariantBubble_Quantity";

    /**
     * The DHTML ID of the price label
     */
    this.VariantBubble_PriceID="VariantBubble_Price";

    /**
     * The DHTML ID of the product name label
     */
    this.VariantBubble_ProductNameID="VariantBubble_ProductName";
    /**
     * The DHTML ID of the product description label
     */
    this.VariantBubble_ProductDescID = "VariantBubble_ProductDesc";

    /**
     * The DHTML ID of the variant name label
     */
    this.VariantBubble_VariantNameID="VariantBubble_VariantName";
    /**
     * The DHTML ID of the variant name label
     */
    this.VariantBubble_VariantNameID2="VariantBubble_VariantName2";

    /**
     * The DHTML ID of the add to bag button on the bubble
     */
    this.VariantBubble_AddToBagID="VariantBubble_AddToBag";
     /**
     * The DHTML ID of the add to bag button on the bubble
     */
    this.VariantBubble_OutOfStockID="VariantBubble_OutOfStock";
    /**
     * The Prefix of the image
     */
    this.VariantBubble_ImagePrefix_LargeShade=IMAGES_RELATIVE_OR_ABSOLUTE_PATH +VARIANT_LARGE_SHADE_IMAGE_PREFIX;

    /**
     * The suffix of the image
     */
    this.VariantBubble_ImageSuffix_LargeShade=VARIANT_LARGE_SHADE_IMAGE_SUFFIX;
    
    this.VariantBubble_FavConfirmID = VAR_FAVORITE_CONFIRM_ID;
    this.VariantBubble_ProductInfoID = VAR_PRODUCT_INFO_ID;
    /**
     * Variant Code
     */
    this.Code="";
    /** 
     * Holds a reference to the parent control. If this value is present,
     * when attempting to show a new bubble, this control will attempt to hide
     * the parent
     */
    this.parentControlID=null;

    this.VIEWMOREDETAILS_ID="VARIANT_VIEWMOREDETAILS_OFPRODUCT";
    this.VIEWMOREDETAILS_INNERHTML_TEMPLATE="";
    
    /**
     * This is used to store Parent Description_Long extended property, it is set for the first time and for any subsequest request the WS call is not made 
     */
    this.VariantBubble_ParentLongDescription = "";
    
    /**
     * This method will attempt to hide the parent control (if present)
     */
    this.HideParentControl=function() {
        if (this.parentControlID!=null)
        {
            //MM_showHideLayers(this.parentControlID,'','hide');
            if (_ProductBubble_Class.PopUp != null)
            {
                _ProductBubble_Class.PopUp._PopupControlID=this.parentControlID;
                _ProductBubble_Class.PopUp.ShowModal();
            }
        }
    }
    
      /**
       * this method will hide add to bag and show outofstock item if the 
       * item is in stock or out of stock
       *
       */
    this.ManageStock=function(stockWeb) {
        if (AJAX_ENABLE_OUTOFSTOCK==true) {
           var stockqty=parseInt(stockWeb);
           var _bagObj=$get(this.VariantBubble_AddToBagID);
           var _outofstockobj=$get(this.VariantBubble_OutOfStockID);
           //
            var StockTresHold=1;
            try {
              StockTresHold=AJAX_GetTresHoldNumber();
            }
            catch (e) {   }
           //
           if (_outofstockobj!=null) {
              if (stockqty>StockTresHold) {
                
                _bagObj.style.display="";
                _outofstockobj.style.display="none";
              }
              else {
                 _bagObj.style.display="none";
                 _outofstockobj.style.display="";
              
              }
           }
         }
       }
    /**
     * This Method is called when the webservice is done with data retrieval
     * @param {object} result The data that comes into a JASON format from a web service
     */
    this.LoadVariantRequestComplete=function(result) {
        
        this.PopulateData(result);
    }
    
    /**
      * This function is used to populate the Description_Long extended property of Parent Product in Shade Bubble      
      */
    this.LoadVariantParentProductRequestComplete=function(result) {
        if ( result != null ) {
            var longDescription = _ProductBubble_Class.GetPropertyValue(result.Properties,"Description_Long");            
            var prodDescObj = $get(_VariantBubble_Class.VariantBubble_ProductDescID);
            if(prodDescObj != null){               
                prodDescObj.innerHTML = longDescription;
                this.VariantBubble_ParentLongDescription = longDescription;
            }
            
        }    
    }
    /**
     * it resets the quantity to 1 if previously selected and it is not 1
     * 
     */
    this.ResetQuantity=function() {
      try {
                 var _qtyobj=$get( this.VariantBubble_QuantityID);
                 if (_qtyobj!=null)  {
                     for (var i=0; i< _qtyobj.options.length; i++) {
                              var str = _qtyobj.options[i].value;
                              if (str=="1") { 
                                  _qtyobj.options.selectedIndex=i;
                                   break;
                               }
                     }
                 
                 }
          }
        catch (err) {  } 
     }
    /**
     * This Method populates the variant bubble
     * @param {object} result The data that comes into a JASON format from a web service
     */
    this.PopulateData=function(result)  {
        if ( this.VariantBubble_ParentLongDescription == ""){
            // before populating the data we need to make a request on the Parent product data to get the Description_Long extended property
            // and populate the VariantBubble_ProductDesc DIV
            _WebServiceAPI.LoadProduct(result.ParentProductCode,false,_VariantBubble_Class.LoadVariantParentProductRequestComplete);    
        }       
        this.HideParentControl();
        _QuickShop_Class.Hide();
        DoDefault();

        var obj=$get(this.VariantBubble_ImageID);
        if (obj!=null) {
           obj.src=this.VariantBubble_ImagePrefix_LargeShade + this.Code + this.VariantBubble_ImageSuffix_LargeShade;
        }
        var prodnameobj=$get(this.VariantBubble_ProductNameID);
        var prodDescObj = $get(this.VariantBubble_ProductDescID);
        var shadenameobj=$get(this.VariantBubble_VariantNameID);
        var shadenameobj2=$get(this.VariantBubble_VariantNameID2);
        var priceobj=$get(this.VariantBubble_PriceID);
        var viewmoredetailsobj=$get(this.VIEWMOREDETAILS_ID);
        var prodInfo = $get(this.VariantBubble_ProductInfoID);
        if (prodInfo!=null) {
          prodInfo.style.display = "block";
        }
        var confirmMsg = $get(this.VariantBubble_FavConfirmID);
        if (confirmMsg!=null) {
          confirmMsg.innerHTML='';
        }
        
        if (priceobj!=null) {
            priceobj.innerHTML="$ " + result.MainPrice;
        }
        if(prodnameobj!=null) {
            prodnameobj.innerHTML=result.ParentName;
        }
        /* This is not required to be set here any more. See this.LoadVariantParentProductRequestComplete=function(result) 
        if(prodDescObj != null){
            prodDescObj.innerHTML = result.ParentDescription;
        } */
        if(shadenameobj!=null) {
            shadenameobj.innerHTML=result.Name;
        }
        if(shadenameobj2!=null) {
            shadenameobj2.innerHTML=result.Name;
        }
        this.ManageStock(result.StockWeb);
        //handle view more details
       if(viewmoredetailsobj!=null) {
       
         //var strviewmoredetails=viewmoredetailsobj.innerHTML;
          var strviewmoredetails=AJAX_BUBBLE_VARIANT_VIEWDETAIL_TEMPLATE;
            // url rewriting determination
                 var _urlrewritten=""
                 if (result.URLRewrittenHomePage) {
                    if (result.URLRewrittenHomePage!="") {
                      //on this case override the actual url with urlrewriting.
                       strviewmoredetails=AJAX_BUBBLE_VARIANT_VIEWDETAIL_TEMPLATE_URL_REWRITTEN;
                       _urlrewritten=result.URLRewrittenHomePage;
                    }
                 }
                 //
         if (this.VIEWMOREDETAILS_INNERHTML_TEMPLATE=="") {
            this.VIEWMOREDETAILS_INNERHTML_TEMPLATE=strviewmoredetails;//preserve for future clicks
         }
         else {
             strviewmoredetails=this.VIEWMOREDETAILS_INNERHTML_TEMPLATE;
         }
         strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ParentProductCode$$$",result.ParentProductCode);
         strviewmoredetails=strviewmoredetails.replace("$$$AJAX_VariantCode$$$",this.Code);
         strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductCategoryCode$$$",result.CategoryPath);
         strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductRelativePath$$$",_ProductBubble_Class.GetRelativePath(result.CategoryHomePage));
          // url rewriting replacement
          if (_urlrewritten!="") {
            strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductURLRewrittenHomePage$$$",_urlrewritten);
          }
                  //
         viewmoredetailsobj.innerHTML= strviewmoredetails.toString();;
       }
         MM_showHideLayers(this.VariantBubble_DivID,'','show','QuickLook');//revist this part in a better manner!
        _CoreModalPopUp._PopupControlID=this.VariantBubble_DivID;
        _CoreModalPopUp.ShowModal();
       
        //The line below sets the last item shopping Cart Item
        _LastShoppingCartItem.PopulateData(this.Code,result.Name,result.ParentProductCode,result.ParentName,result.MainPrice);
        try {
              _CoreAjaxCoremetrics.CreateProductViewTag(this.Code,result.ParentName + ' - ' + result.Name,result.CategoryCode,result.CategoryName);
           }
        catch (err) {  }   
        
        this.ResetQuantity();
    }

    /**
     * This method is called when add to bag is clicked on the variant bubble,
     * it determines the SKU, quantity and customer ID automatically and passes to a web service
     */
    this.AddSingleProductToCart=function () {
        var ItemQuantity=1;
        ItemQuantity=GetSelectedValue(this.VariantBubble_QuantityID);
        MM_showHideLayers(_VariantBubble_Class.VariantBubble_DivID,'','hide')
        _CoreModalPopUp.Hide();
        //Set the last Item Item Shopping Cart Item Quantity
         _LastShoppingCartItem.ItemQuantity=ItemQuantity;
         //
         CoreAjaxCookie_AddVariant(this.Code); //creeates a cookie to determine the product was added via quick shop
        // call webservice method
        _WebServiceAPI.AddSingleProductToCart (GetCurrentCustomerID(),this.Code,ItemQuantity,_ShoppingCartBubble_Class.AddSingleProductToCartResultHandler)
    }
    
    this.AddToCartFromProductDetails=function(_code,_quantity) {
       this.Code=_code;
        _LastShoppingCartItem.ItemQuantity=_quantity;
         _WebServiceAPI.AddSingleProductToCart (GetCurrentCustomerID(),this.Code,_quantity,_ShoppingCartBubble_Class.AddSingleProductToCartResultHandler)   
    }
}

/*
 * It registers the VariantBubble_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.VariantBubble_Class.registerClass('Core.VariantBubble_Class'); }

