

/**
 * @class ProductBubble_Class
 * This class is responsible into managing the Product 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.
 *  this.RowSize=8 is a very important property, it manages how many variants will be shown in one row if
 * the product is a makeup product. It customizes building of shade table.
 */

Core.ProductBubble_Class=function() {
     /**
     * This property holds skus as comma delimited and determines to display only skus stored in 
     * the property below example prdcode1,prdcode2,prdcode3,prdcode4
     */
    this.FilterVariantList='';
    /**
     * The DHTML ID of the main layer of the Product bubble
     */
    this.Bubble_DivID="ProductBubble_Div";
    /**
     * The DHTML ID of the Product Image object
     */
    this.Bubble_ImageID="ProductBubble_Image";
    /**
     * The DHTML ID of the Quantity object
     */
    this.Bubble_QuantityID="ProductBubble_Quantity";
    /**
     * The DHTML ID   of the Product price label
     */
    this.Bubble_PriceID="ProductBubble_Price";
    /**
     * The DHTML ID   of the Product Name label
     */
    this.Bubble_ProductNameID="ProductBubble_ProductName";
    
     this.Bubble_ProductNameID2="ProductBubble2_ProductName";
    /**
     * The DHTML ID   of the Product description label
     */
    this.Bubble_ProductDescriptionID="ProductBubble_ProductDescription";
    /**
     * The DHTML ID   of the Product Fragrance label
     */
    this.Bubble_ProductFragranceID="ProductBubble_Fragrance";
    /**
     * The DHTML ID   of the Product Ingredients label
     */
    this.Bubble_ProductIngredientsID="ProductBubble_Ingredients";
    /**
     * The DHTML ID   of the Variant Name label
     */
    this.Bubble_VariantNameID="ProductBubble_VariantName";
    /**
     * The DHTML ID   of the Variant SKU
     */
    this.Bubble_VariantSKUID="ProductBubble_VariantSKU";
    /**
     * The DHTML ID   of the Add to bag object
     */
    this.Bubble_AddToBagID="ProductBubble_AddToBag"; //not used yet on js code
    
     /**
     * The DHTML ID   of the Out Of Stock Object (Optional);
     */
    this.Bubble_OutOfStockID="ProductBubble_OutOfStock"

    //Small Product Images
    /**
     * Small Image Prefix
     */
    this.Bubble_ImagePrefix_Product_Medium=IMAGES_RELATIVE_OR_ABSOLUTE_PATH + PRODUCT_MEDIUM_IMAGE_PREFIX;
    /**
     * Small Image Suffix
     */
    this.Bubble_ImageSuffix_Product_Medium=PRODUCT_MEDIUM_IMAGE_SUFFIX;

    //Small Shades
    /**
     * small shades prefix
     */
    this.Bubble_ImagePrefix_SmallShade=IMAGES_RELATIVE_OR_ABSOLUTE_PATH + VARIANT_SMALL_SHADE_IMAGE_PREFIX;
    /**
     * small shades suffix
     */
    this.Bubble_ImageSuffix_SmallShade=VARIANT_SMALL_SHADE_IMAGE_SUFFIX;

    //thumbnails
    /**
     * product thumbnails prefix
     */
    this.Bubble_ImagePrefix_MediumShade=IMAGES_RELATIVE_OR_ABSOLUTE_PATH + VARIANT_MEDIUM_SHADE_IMAGE_PREFIX;
    /**
     * product thumbnails suffix
     */
    this.Bubble_ImageSuffix_MediumShade=VARIANT_MEDIUM_SHADE_IMAGE_SUFFIX;

    /**
     * Product Code
     */
    this.Code="";
    /**
     *  The DHTML ID   of the drop down layer that lists all variants
     */
    this.Bubble_LayerDropDownListID="ProductBubble_VariantDropDownList";
    /**
     * The DHTML ID   of the drop down that lists all variants
     */
    this.Bubble_DropDownListID="DropDown_" + this.Bubble_LayerDropDownListID;
    /**
     * The DHTML ID   of small shades layer
     */
    this.ProductBubble_Small_ShadesID="ProductBubble_Small_Shades"
    /**
     * set to tru if has shades else set to false //check any valid property in db ???
     */
    this.IsMakeupProduct=false;
    /**
     * Number of elements per row
     */
    this.RowSize=7;        //Can be Changed
    /**
     * Current Index of the row
     */
    this.CurrentIndex=0;
    /**
     * temporary x postion of the mouse
     */
    this.tempX=0;
    /**
     * temporary y position of the mouse
     */
    this.tempY=0
    /**
     * The DHTML ID   of the variants thumbnail table
     */
    this.Variants_Main_All_Thumbnails='Ajax_Main_Bubble_All_Thumbnail_Shades';
    /**
     * The DHTML ID   of all variants
     */
    this.Variants_ThumbnailDivID='Ajax_allShades_layer';
    /**
     * set to true if multiple prices else set to false
     */
    this.HasMultiplePrices=false;
    /**
     * if all variants same price, this variable will hold the variant price
     */
    this.DistinctPrice=0;
     /**
     * if set to true, price will show on drop down if multiprice items
     */
    this.ShowPriceOnDropDown=false;
    
    this.ProductBubble_FavConfirmID = PRD_FAVORITE_CONFIRM_ID;
    this.ProductBubble_ProductInfoID = PRD_PRODUCT_INFO_ID;
    /**
     * If not null, this will hold a reference to the CoreModalPopup object to use
     * when show/hiding this object
     */
    this.PopUp=null;

    this.VIEWMOREDETAILS_ID="PRODUCT_BUBBLE_VIEWMOREDETAILS";
    this.VIEWMOREDETAILS_INNERHTML_TEMPLATE="";
    this.SelectShadeLabel="selectshadeLabel";
    this.DropDownShadeLabel="DropDownShadeLabel";
    this.DropDownSizeLabel="DropDownSizeLabel";
    //
    this.VIEWMOREDETAILS2_ID="PRODUCT_BUBBLE2_VIEWMOREDETAILS";
    this.VIEWMOREDETAILS_INNERHTML_TEMPLATE2="";
    this.lastClicked=null;
    /**
     * when product bubble is shown, preserve the category code and name
     * in case they are needed for the shades that will be selected from drop down
     * or from the color table of the bubble
     */
    this.CategoryCode="";
    this.CategoryName="";
    this.ProductName="";
    
    
    /**
     * 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.LoadProductRequestComplete=function(result) {
        this.PopulateData(result);
    }


    this.ReportSelectedVarToCoremetrics=function() {
    
    
    
    }
     /**
     * This Method is called when the users selects different options in size drop down
     *
     */
    this.SelectItem=function() {
       var priceobj=$get(this.Bubble_PriceID);
        
         //TODO Reset Image if needed later.
        // alert('1');
        var selectedvalue=this.GetSelectedItem();
        if (selectedvalue!="") {
            var sku="";
            var price="";
            var webStock=0;
            if (selectedvalue.indexOf("#") != -1){
		            var selectedvalue_array=selectedvalue.split("#");
		            sku=selectedvalue_array[0];
                    price=selectedvalue_array[1];
                    webStock=selectedvalue_array[2];
                    //alert(webStock);
                    this.ManageStock(webStock);
                    priceobj.innerHTML="$ " + price;
                    //if make up product with shades
                    //set the color image on
                    //set the image on
                    //set the shade name selected
                    if (this.IsMakeupProduct) { //F.N added on 4/4/2007
                            try {
                            // change image state
                             var selimg=$get('SM_' +  sku);
                             if (selimg!=null) {
                                     this.getswapimagesrc(selimg, 'on');
                             }
                            }
                            catch(e) {
                            
                            }
                            try {
                            //set image name on the bubble
                            this.SetVariantImageandName(sku,this.GetSelectedItemText());
                            }
                            catch(e) {
                            
                            }
                    }
	        }
	    }
      }
    
    
      /**
       * 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.Bubble_AddToBagID);
           var _outofstockobj=$get(this.Bubble_OutOfStockID);
            var StockTresHold=1;
            try { //placed try catch to handle any possible error on js because of js file caching, the AJAX_GetTresHoldNumber function was added on 5/29/2007
             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 methods set as selected an option of variant drop down.
       * item is in stock or out of stock
       *
       */
     this.SetVariantDropDownItemSelected=function(dropDownID,valueToSelect) {
            var selectedstr=valueToSelect;
            var selectedVal;
            if (selectedstr.indexOf("#") != -1){
		        selectedstr=selectedstr.slice(0,sku.indexOf("#"));
	        }
            var options=document.getElementById(dropDownID)
            if (options!=null) {
                for (var i=0; i< options.length; i++) {
                    var str = options[i].value;
			        str = str.slice(0,str.indexOf("#"));
                    if (str==selectedstr) { //valueToSelect
                        options.selectedIndex=i;
                        selectedVal = options[i].text;
                        break;
                    }
                }
            }
        } 
       
    this.SetVariantImageandName=function(varSku, varName) {        
        // change the shade image and shade name
        var imgObj=$get(this.Bubble_ImageID);
        var varNameobj = $get(this.Bubble_VariantNameID);
        
        if (imgObj != null)
            imgObj.src=this.Bubble_ImagePrefix_MediumShade + varSku + this.Bubble_ImageSuffix_MediumShade;
        if (varNameobj != null)
            varNameobj.innerHTML = varName;        
          
         //report to coremetrics
             try {
                 _CoreAjaxCoremetrics.CreateProductViewTag(varSku, this.ProductName + ' - ' + varName,this.CategoryCode,this.CategoryName);
             }
             catch (err) {  }     
    }
      /**
     * This Method is called internaly to get the value of selected option
     *
     */
    this.GetSelectedItem=function() {
      var objdrp=$get(this.Bubble_DropDownListID);
      if (objdrp!=null) {
      return objdrp.options[objdrp.selectedIndex].value;
      }
      else {
        return "";
      }
    
    }
   this.GetSelectedItemText=function() {
      var objdrp=$get(this.Bubble_DropDownListID);
      if (objdrp!=null) {
    
      return objdrp.options[objdrp.selectedIndex].text;
      }
      else {
        return "";
      }
    
    }
    
    this.ContainsOneEmptyTextVariant=function() {
         var ContainsOneEmptyVariant=false;
         var objdrp=$get(this.Bubble_DropDownListID);
          if (objdrp!=null) {
              if (objdrp.options.length==1 ) {
                 if (objdrp.options[0].text=='') {
                   ContainsOneEmptyVariant=true;
                 }
              }
         }
        return ContainsOneEmptyVariant;
    }

this.getswapimagesrc=function(img, imgState)
{
    var imgSrc = '';
    var lastimgSrc = '';
    if (img != null)
    {
        var imgStateOn = "_on.";
        imgSrc = img.src;
        var imgPathAndFileName = imgSrc.slice(0, imgSrc.lastIndexOf(".") + 1);
        var imgFileExtension = imgSrc.slice(imgSrc.lastIndexOf(".") + 1, imgSrc.length);

        if (this.lastClicked != null)
        {
           if (this.lastClicked.src==img.src) return; //F.N: clicked on the same shade over and over, the if prevent by not changing the img src of the shade.
            lastimgSrc = this.lastClicked.src;
            var lastimgPathAndFileName = lastimgSrc.slice(0, lastimgSrc.lastIndexOf(".") + 1);           
            var lastimgFileExtension = lastimgSrc.slice(lastimgSrc.lastIndexOf(".") + 1, lastimgSrc.length);
	        lastimgSrc = lastimgPathAndFileName.slice(0, lastimgPathAndFileName.indexOf(imgStateOn)) + "." + lastimgFileExtension;
	        this.lastClicked.src = lastimgSrc
	        
        }
        if (imgState == 'on')
        {
	        imgSrc = imgPathAndFileName.slice(0, imgPathAndFileName.lastIndexOf(".")) + imgStateOn + imgFileExtension;
	        this.lastClicked = img;
	        img.src = imgSrc;
        }
    }
}	


     /**
     * it resets the quantity to 1 if previously selected and it is not 1
     * 
     */
    this.ResetQuantity=function() {
      try {
                 var _qtyobj=$get( this.Bubble_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) {  } 
     }
    /**
     * populates the product bubble
     * If it belongs to make up, it also builds the shade table with defautl 8 cells in one row.
     * @param {object} result the data that comes into a JASON format from a web service
     */
    this.PopulateData=function(result)  {
        if (result==null) {return false;}
        if (result.Variants==null) {return false;}               //product has no variants
        if (typeof(result)=='undefined') {return false;}
        if (typeof(result.Variants)=='undefined') {return false;} //product has no variants

        //check if the main layer exist. If it does not, return.
        
        var prodBubble=$get(this.Bubble_DivID);
        if ((prodBubble==null) || (prodBubble=='undefined')) {
            alert('The \"' + this.Bubble_DivID + '\" is not found.');
            return;
        }

        this.DetermineMultiplePrices(result.Variants);
        _QuickShop_Class.Hide();
        DoDefault();

        var obj=$get(this.Bubble_ImageID);
        var varNameobj = $get(this.Bubble_VariantNameID);
        // if the product is a Makeup product then show the first 
        // product swatch in the product imgage spot
        // add the Variant's Name under the image
        if (this.IsMakeupProduct) {
            // set the variant product image for the first variant
            if (obj!=null) {
                obj.src=this.Bubble_ImagePrefix_MediumShade + result.Variants[0].SKU + this.Bubble_ImageSuffix_MediumShade;
            }
            
            if (varNameobj != null)
                varNameobj.innerHTML = result.Variants[0].Name;  
        }else{
            // show the product image and variant name            
            if (obj!=null) {
                obj.src=this.Bubble_ImagePrefix_Product_Medium + this.Code + this.Bubble_ImageSuffix_Product_Medium;
                if (varNameobj != null)
                {
                    //Sudeshna - Overriden this to display the Variant name
                    //varNameobj.innerHTML = "";
                    varNameobj.innerHTML = result.Variants[0].Name;;
                 }
            }
        }

        var prodnameobj=$get(this.Bubble_ProductNameID);
        var prodnameobj2=$get(this.Bubble_ProductNameID2);
      
        var priceobj=$get(this.Bubble_PriceID);
        var dropdownlistObj=$get(this.Bubble_LayerDropDownListID);
        var productBubbleSmShadesObj =$get(this.ProductBubble_Small_ShadesID);
        var productdescrobj=$get(this.Bubble_ProductDescriptionID);
        //Fragrance
        var productfragobj=$get(this.Bubble_ProductFragranceID);
        var productingrobj=$get(this.Bubble_ProductIngredientsID);
        var prodInfo = $get(this.ProductBubble_ProductInfoID);
        if (prodInfo!=null) {
          prodInfo.style.display = "block";
        }
        var confirmMsg = $get(this.ProductBubble_FavConfirmID);
        if (confirmMsg!=null) {
          confirmMsg.innerHTML='';
        }
        
        if (priceobj!=null) {
            //priceobj.innerHTML="$ " + result.MainPrice;
            if (this.DistinctPrice!=0) { //means same price variants
                priceobj.innerHTML="$ " + this.DistinctPrice;
            }
            else { //means multiple prices
                priceobj.innerHTML=""; 
            }
            //
            if(prodnameobj!=null) {
               
                prodnameobj.innerHTML=result.Name;
            }
            //
            if(prodnameobj2!=null) {
                prodnameobj2.innerHTML=result.Name;
            }
            //
            if (productdescrobj!=null) {
               productdescrobj.innerHTML=result.Description;
            }
            //Fragrance - Retrieve from Extended Property "product_fragrance"
            if (productfragobj!=null) {               
               productfragobj.innerHTML=this.GetPropertyValue(result.Properties,"product_fragrance");
              if (productfragobj.innerHTML == '') {
                document.getElementById("PBCont_FR").style.display = 'none';
              }
            }
            //Inredients - Retrieve from Extended Property "precious_ingredients"
            if (productingrobj!=null) {               
              productingrobj.innerHTML=this.GetPropertyValue(result.Properties,"precious_ingredients");
              if (productingrobj.innerHTML == '') {
                document.getElementById("PBCont_PI").style.display = 'none';
              }
            }
            //
            var labelObj=$get( this.SelectShadeLabel);
            //
            var obj1=$get(this.DropDownShadeLabel);
            //
            var obj2=$get(this.DropDownSizeLabel);
           
            if (productBubbleSmShadesObj!=null) { //products with shades
                if (this.IsMakeupProduct) {
                    
                    if (labelObj!=null) {
                      labelObj.style.display="";
                    }
                    if (obj1!=null) {
                      obj1.style.display="";
                    }
                    if (obj2!=null) {
                      obj2.style.display="none";
                    }
                    
                     productBubbleSmShadesObj.innerHTML=this.GetShadeTableString(result,true);//'Makeup shades go here';
                     productBubbleSmShadesObj.style.display="";
             }
                else { //products with no shades
              
                     if (labelObj!=null) {
                      labelObj.style.display="none";
                     }
                       if (obj1!=null) {
                      obj1.style.display="none";
                    }
                     if (obj2!=null) {
                      obj2.style.display="";
                    }
                    productBubbleSmShadesObj.innerHTML="";
                    productBubbleSmShadesObj.style.display="none";
                }
            }
            
            if (dropdownlistObj!=null) {
                if (result.Variants!=null) {
                    var hasuniqueprices=false;
                    if (this.DistinctPrice!=0) {
                        hasuniqueprices=true;
                    }
                    dropdownlistObj.innerHTML=GenerateDropDownVariants(result.Variants,this.Bubble_DropDownListID,hasuniqueprices,this.ShowPriceOnDropDown,'_ProductBubble_Class.SelectItem();');
                    if (this.ContainsOneEmptyTextVariant()) { //hide if one empty variant with text=""
                       dropdownlistObj.style.visibility='hidden';
                       //hide labels for size and shade name
                        if (obj1!=null) {
                         obj1.style.display="none"; //shade name
                       }
                        if (obj2!=null) {
                          obj2.style.display="none"; //size
                        }
                    
                    }
                    else {
                    dropdownlistObj.style.visibility='visible'; //show the layer of the drop down
                    }
                    //dropdownlistObj.style.display='block';
                }
                else {
                    dropdownlistObj.style.visibility='hidden';
                    dropdownlistObj.style.display='none';
                }
            }
             var viewmoredetailsobj=$get(this.VIEWMOREDETAILS_ID);
             if(viewmoredetailsobj!=null) {
               
                 //var strviewmoredetails=viewmoredetailsobj.innerHTML;
                 var strviewmoredetails=AJAX_BUBBLE_PRODUCT_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_PRODUCT_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$$$",this.Code);
                  strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductCategoryCode$$$",result.CategoryPath);
                  strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductRelativePath$$$",this.GetRelativePath(result.CategoryHomePage));
                  // url rewriting replacement
                  if (_urlrewritten!="") {
                     strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductURLRewrittenHomePage$$$",_urlrewritten);
                  }
                  //
                  viewmoredetailsobj.innerHTML= strviewmoredetails.toString();
               }
              //Populate some of the LastShoppingCartItem object properties, be sure to handle the price upon the addtobag item
              _LastShoppingCartItem.PopulateData("","",this.Code,result.Name,this.DistinctPrice);
              //report coremetrics for the product
              //save category name and code for the shades of product if clicked
              this.CategoryCode=result.CategoryCode;
              this.CategoryName=result.CategoryName;
              this.ProductName=result.Name;
              try {
                  _CoreAjaxCoremetrics.CreateProductViewTag(this.Code,result.Name,result.CategoryCode,result.CategoryName);
              }
              catch (err) {  }   
           // if (this.DistinctPrice==0){
                    this.SelectItem();//set the first item as selected
           // }
            //MM_showHideLayers( this.Bubble_DivID,'','show','QuickLook'); //????
             $get(this.Bubble_DivID).style.visibility='visible'
            _CoreModalPopUp._PopupControlID=this.Bubble_DivID;
            _CoreModalPopUp.ShowModal();
            
             this.ResetQuantity(); //reset qty to one if reviously selected 2,3
           
        }
    }

    /**
     * determines if all variants has the same exact price or a diffrent one. It will be needed the way
     * variants are displayed
     * @param {object} _Variants the data that comes into a JASON format from a web service
     */
    this.DetermineMultiplePrices=function(_Variants) {
        this.DistinctPrice=0;
        this.HasMultiplePrices=false;
        var ComparePrice =-1;

        for(var i=0; i<_Variants.length; i++){
            if(_Variants[i].MainPrice !=ComparePrice &&  i > 0) {
                this.HasMultiplePrices=true;
                break;
            }
            ComparePrice=_Variants[i].MainPrice;
        }

        if (!this.HasMultiplePrices) {
            this.DistinctPrice=ComparePrice;
        }
    }

    /**
     * it manages to build up the shade table
     * @param {object} ProductObject the data that comes into a JASON format from a web service
     */
    this.DisplayShadeTable=function(ProductObject) {
        if (ProductObject.Result!=null  && ProductObject.Result.Variants!=null) {
            // Show Modal Popup?
            /*
            if (this.PopUp != null)
            {
                this.PopUp._PopupControlID = this.Variants_Main_All_Thumbnails;
                this.PopUp.ShowModal();
            }
             */
            var innerHTMlString = this.GetShadeTableString(ProductObject.Result,false);
           // var obj = $get(this.Variants_Main_All_Thumbnails);
           var obj = $get(this.Variants_ThumbnailDivID);
           
            if (obj!=null) {
                //alert(innerHTMlString);
                obj.innerHTML=innerHTMlString; 
            }
            var prodnameobj2=$get(this.Bubble_ProductNameID2);

            if(prodnameobj2!=null) {
                prodnameobj2.innerHTML= ProductObject.Result.Name;
            }
            //href
            var viewmoredetailsobj=$get(this.VIEWMOREDETAILS2_ID);
             if(viewmoredetailsobj!=null) {
       
                 //var strviewmoredetails=viewmoredetailsobj.innerHTML;
                 var strviewmoredetails=AJAX_BUBBLE_PRODUCTTHUMBNAIL_VIEWDETAIL_TEMPLATE;
                   // url rewriting determination
                 var _urlrewritten=""
                 if (ProductObject.Result.URLRewrittenHomePage) {
                    if (ProductObject.Result.URLRewrittenHomePage!="") {
                      //on this case override the actual url with urlrewriting.
                       strviewmoredetails=AJAX_BUBBLE_PRODUCTTHUMBNAIL_VIEWDETAIL_TEMPLATE_URL_REWRITTEN;
                       _urlrewritten=ProductObject.Result.URLRewrittenHomePage;
                    }
                 }
                 //
                 if (this.VIEWMOREDETAILS_INNERHTML_TEMPLATE2=="") {
                    this.VIEWMOREDETAILS_INNERHTML_TEMPLATE2=strviewmoredetails;//preserve for future clicks
                 }
                 else {
                     strviewmoredetails=this.VIEWMOREDETAILS_INNERHTML_TEMPLATE2;
                 }
                  strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ParentProductCode$$$",this.Code);
                  strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductCategoryCode$$$",ProductObject.Result.CategoryPath);
                  strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductRelativePath$$$",this.GetRelativePath(ProductObject.Result.CategoryHomePage));
                  // url rewriting replacement
                  if (_urlrewritten!="") {
                     strviewmoredetails=strviewmoredetails.replace("$$$AJAX_ProductURLRewrittenHomePage$$$",_urlrewritten);
                  }
                  //
                  viewmoredetailsobj.innerHTML= strviewmoredetails.toString();
             }
            //coremetrics for product view
              this.CategoryCode=ProductObject.Result.CategoryCode;
              this.CategoryName=ProductObject.Result.CategoryName;
              this.ProductName=ProductObject.Result.Name;
              try {
                  _CoreAjaxCoremetrics.CreateProductViewTag(this.Code,ProductObject.Result.Name,ProductObject.Result.CategoryCode,ProductObject.Result.CategoryName);
              }
              catch (err) {  }  
            
            //
            this.ShowShades();
        }
    }
    
    
     /**
     * builds path for product details page from category page
     * @path {string} 
     */
    this.GetRelativePath=function(path) {
       //alert(path);
       var rel_path=path;
       var path_array=path.split("/");
       if (path_array[path_array.length-1].indexOf(".") != -1){
         rel_path=rel_path.replace(path_array[path_array.length-1],"")
       }
       return rel_path
    }
    /**
     * it manages to build up the string of the shade table
     * please be aware that this method uses the javascript
     * string builder class to improve the performance vs the regular string concatination
     * @param {object} result the data that comes into a JASON format from a web service
     * @param {bool} SmallShadesWanted true or false, if true, we build the thumbnails
     */
    this.GetShadeTableString=function(result,SmallShadesWanted) {
        if (result!=null  && result.Variants!=null) {
            var VariantsToBeDisplayed=result.Variants;
            var CurrentRowNumber=0;
            var CurrentCellInCurrentRow=0;
            var sb = new StringBuilder();
            var HasShadeSubGroup=false;
            var StartSubGroupConst='$#Begin#$';
            var PreviousSubGroupValue=StartSubGroupConst;
              var swatchW;
              var swatchH;
              if (SmallShadesWanted) {
                    swatchW=VARIANT_SMALL_SHADE_WIDTH;
                    swatchH=VARIANT_SMALL_SHADE_HEIGHT;
              }
              else {
              
                    swatchW=VARIANT_MEDIUM_SHADE_WIDTH;
                    swatchH=VARIANT_MEDIUM_SHADE_HEIGHT;
              }
            
            //alert(VariantsToBeDisplayed.Properties);
            if (typeof(SHADE_SUB_GROUP_FLD)!='undefined' || SHADE_SUB_GROUP_FLD!='') {
                   HasShadeSubGroup=true;
                   sb.append("<table border=0 cellspacing=1 cellpadding=1 width='100%'><tr><td colspan='2'><div class='BL_pink1px'></div></td></tr><tr><td  valign=\"top\" align=\"left\" width=\"50\"  style=\"padding-left: 10px; padding-top: 10px; padding-right: 50px;\">");
            }
            else {
                    sb.append("<table border=0 cellspacing=1 cellpadding=1><tr>");
            }
           
            var filter= this.FilterVariantList; //
            for(var i=0; i<VariantsToBeDisplayed.length; i++) {
            // if ((filter=='') || (filter.indexOf(VariantsToBeDisplayed[i].SKU)) !=-1)) {
              if (filter=='' || filter.indexOf(VariantsToBeDisplayed[i].SKU)!=-1) {
               if (HasShadeSubGroup==true) {
              //subgrouping get value
               var currenSubGroupValue="";
               if (VariantsToBeDisplayed[i].Properties!=null) {
                    var _Properties=VariantsToBeDisplayed[i].Properties;
                    for(var j=0; j<_Properties.length; j++) {
                        //alert(_Properties[j].Name);
                        //alert(_Properties[j].Value);
                        if (_Properties[j].Name.toLowerCase()==SHADE_SUB_GROUP_FLD.toLowerCase()) {
                          currenSubGroupValue=_Properties[j].Value;
                          //alert( currenSubGroupValue);
                          }
                    }
                }

                //subgrouping build table
                 if (PreviousSubGroupValue!=currenSubGroupValue && HasShadeSubGroup==true) {
                        if (PreviousSubGroupValue!=StartSubGroupConst) { //close subgrouping
                            if (CurrentRowNumber>0 && CurrentCellInCurrentRow<this.RowSize) {
                                for(var k=CurrentCellInCurrentRow; k<this.RowSize; k++)
                                {
                                    sb.append("<td><img src=\"" + SPACER_IMAGE + "\"" + " width=\"" + swatchW + "\" height=\"" +swatchH + "\" border=\"0\"></td>");
                                   
                                }
                             }
                            //sb.append("</tr></table>11</div></td></tr></table></div>" );
                            sb.append("</tr></table></div></td></tr><tr><td colspan='2'><div class='BL_pink1px'></div></td></tr><td valign=\"top\" align=\"left\" width=\"50\" style=\"padding-left: 10px; padding-top: 10px; padding-right: 50px;\">" );
                        }
                       // sb.append("<table border=0 cellspacing=1 cellpadding=1><tr height=\"1px\"><td  colspan=\"2\"><hr class=\"BL_pink1px\"></td></tr><tr><td width=\"29%\" class=\"Adrkpink10pxR\" >" );
                       // sb.append("<div class=\"BL_pink1px\"><table border=1 cellspacing=1 cellpadding=1><tr><td width=\"29%\" class=\"Adrkpink10pxR\" >" );
                       sb.append("<div style='margin-left:0px' class=\"Adrkpink10pxR\" >" );
                      
                        sb.append(currenSubGroupValue);
                        sb.append("</div></td><td align=\"left\" style=\"padding-right: 20px;\">" );
                        sb.append("<div class=\"Adrkpink10pxR\"><table border=0 cellspacing=1 cellpadding=1><tr>");
                        CurrentRowNumber+=1;
                        CurrentCellInCurrentRow=0;
                        PreviousSubGroupValue=currenSubGroupValue;
                
                    }
                 }
               
                //CurrentCellInCurrentRow = i -  CurrentRowNumber * this.RowSize - this.CurrentIndex;
               
                if (CurrentCellInCurrentRow >= this.RowSize ) {
                    sb.append("</tr><tr>");
                    CurrentRowNumber+=1;
                    CurrentCellInCurrentRow=0;
                }
            
                if (SmallShadesWanted) {
                   // sb.append("<td><img id='A_" + VariantsToBeDisplayed[i].SKU +"' src='" + this.Bubble_ImagePrefix_SmallShade + VariantsToBeDisplayed[i].SKU +  this.Bubble_ImageSuffix_SmallShade + "' onclick=\"javascript:SetDropDownItemSelected('" + this.Bubble_DropDownListID + "','" + VariantsToBeDisplayed[i].SKU +"');\" style=\"cursor:pointer\" ></td>");
                   // sb.append("<td><img id='A_" + VariantsToBeDisplayed[i].SKU +"' src='" + this.Bubble_ImagePrefix_SmallShade + VariantsToBeDisplayed[i].SKU +  this.Bubble_ImageSuffix_SmallShade + "' onclick=\"javascript:SetDropDownItemSelected('" + this.Bubble_DropDownListID + "','" + VariantsToBeDisplayed[i].SKU +"');\" style=\"cursor:pointer\""  + " width=\"" + swatchW + "\"  height=\"" +swatchH + "\"></td>");
                   sb.append("<td>");
                   sb.append("<img id='SM_" + VariantsToBeDisplayed[i].SKU +"' src='" + this.Bubble_ImagePrefix_SmallShade + VariantsToBeDisplayed[i].SKU +  this.Bubble_ImageSuffix_SmallShade + "' onclick=\"javascript:_ProductBubble_Class.getswapimagesrc(this,'on');_ProductBubble_Class.SetVariantDropDownItemSelected('" + this.Bubble_DropDownListID + "','" + VariantsToBeDisplayed[i].SKU + "');_ProductBubble_Class.SetVariantImageandName('" + VariantsToBeDisplayed[i].SKU + "', _ProductBubble_Class.GetSelectedItemText());_ProductBubble_Class.ManageStock(" + VariantsToBeDisplayed[i].StockWeb.toString() + ");\" style=\"cursor:pointer\""  + " width=\"" + swatchW + "\"  height=\"" +swatchH + "\"" + "alt=\"" + VariantsToBeDisplayed[i].Name + "\">");
                   sb.append("</td>");           
                }
                else {
                     // sb.append("<td><img id='A_" + VariantsToBeDisplayed[i].SKU +"' src='" +  this.Bubble_Thumbnail_ImagePrefix + VariantsToBeDisplayed[i].SKU +  this.Bubble_Thumbnail_ImageSuffix + "'       onmouseover=\"javascript:_QuickShop_Class.ShowByObjectPosition(this,'" + VariantsToBeDisplayed[i].SKU +"',true,5,5);\" style=\"cursor:pointer;\"  width=\"100\" height=\"100\"></td>");
                     // sb.append("<td><img id='A_" + VariantsToBeDisplayed[i].SKU +"' src='" +  this.Bubble_ImagePrefix_MediumShade + VariantsToBeDisplayed[i].SKU +  this.Bubble_ImageSuffix_MediumShade + "'       onmouseover=\"javascript:_QuickShop_Class.ShowByObjectPosition(this,'" + VariantsToBeDisplayed[i].SKU +"',true,5,5);\" style=\"cursor:pointer;\""  + " width=\"" + swatchW + "\"  height=\"" +swatchH + "\"></td>");
                     sb.append("<td valign=\"top\">");
                     if (VariantsToBeDisplayed[i].RecommendedByDiag == true)
                     {
                         sb.append("<div align=\"center\"><img src=\"" + DIAG_RECOMMENDATION_IMAGE + "\" width=\"" + DIAG_RECOMMENDATION_IMAGE_WIDTH + "\" height=\"" + DIAG_RECOMMENDATION_IMAGE_HEIGHT + "\" border=\"0\" alt=\"" + DIAG_RECOMMENDATION_IMAGE_ALT + "\" /></div>");
                     }
                     else
                     {
                         sb.append("<div align=\"center\"><img src=\"" + SPACER_IMAGE + "\"" + " width=\"" + DIAG_RECOMMENDATION_IMAGE_WIDTH + "\" height=\"" +DIAG_RECOMMENDATION_IMAGE_HEIGHT + "\" border=\"0\"></div>");
                     }
                     sb.append("<img id='A_" + VariantsToBeDisplayed[i].SKU +"' src='" +  this.Bubble_ImagePrefix_MediumShade + VariantsToBeDisplayed[i].SKU +  this.Bubble_ImageSuffix_MediumShade + "'       onmouseover=\"javascript:_QuickShop_Class.parentControlID='" + this.Variants_Main_All_Thumbnails + "';_QuickShop_Class.ShowIcon(this,'" + VariantsToBeDisplayed[i].SKU +"',true,5,5,true," +  VariantsToBeDisplayed[i].StockWeb.toString() + ");\" style=\"cursor:pointer;\""  + " width=\"" + swatchW + "\"  height=\"" +swatchH + "\"" + "alt=\"" + VariantsToBeDisplayed[i].Name + "\"><div align=\"center\" class=\"Ablack9pxRLH\">" + VariantsToBeDisplayed[i].Name  + "</div>");
                     sb.append("</td>");
                }
                 CurrentCellInCurrentRow+=1;
             }
            }
             //end of for loop
             if (CurrentRowNumber>0 && CurrentCellInCurrentRow<this.RowSize) {
                    for(var i=CurrentCellInCurrentRow; i<this.RowSize; i++)
                    {
                       // sb.append("<td>&nbsp;</td>");
                       sb.append("<td><img src=\"" + SPACER_IMAGE + "\"" + " width=\"" + swatchW + "\" height=\"" +swatchH + "\" border=\"0\"></td>");
                                   
                    }
             }
             //
             sb.append("</tr></table>");
             
             //
             if (HasShadeSubGroup) {
                sb.append("</div></td></tr></table></div></td></tr></table>" );
             }
             if (CurrentRowNumber>5) {
               _CoreModalPopUp._IsforegroundElementLarge=true;
             }
             else {
               _CoreModalPopUp._IsforegroundElementLarge=false;
             }
             
            //alert(sb.toString());
            return sb.toString();
        }
        return "";
    }

    /**
     * set the shade table visible
     */
    this.ShowShades=function () {
        var obj= $get(this.Variants_Main_All_Thumbnails);
        
        if (obj!=null) {
            obj.style.visibility='visible';
            _CoreModalPopUp._IsforegroundElementLarge=true;
            _CoreModalPopUp._PopupControlID=this.Variants_Main_All_Thumbnails;
            _CoreModalPopUp.ShowModal();

            var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
            scrollLeft=scrollLeft+(document.documentElement.offsetWidth-obj.children[0].offsetWidth)/2;
            scrollTop=(document.documentElement.offsetHeight-obj.children[0].offsetHeight)/2;
            
            obj.style.visibility='visible';
            obj.style.top=scrollTop + "px";
            obj.style.left=scrollLeft +"px";
            // obj.style.top=this.tempY + "px";
            // obj.style.left=this.tempX +"px";
        }
    }

    /**
     * hides shades/variants
     */
    this.HideShades=function() {
        var obj= $get(this.Variants_Main_All_Thumbnails);
        if (obj!=null) {
            obj.style.visibility='hidden';
        }
        
        if (this.PopUp != null)
        {
            this.PopUp.Hide();
        }
        
        _QuickShop_Class.Hide();
    }

    /**
     * saves the current cursor coordinates when clicked on a particular link
     * needed when we want to display the bubble relative with cursor position clicked
     */
    this.LockCoordinates=function() {
        this.tempX=GetMouseCoordinateX();
        this.tempY=GetMouseCoordinateY();
    }

     /**
     * returns the current variant code from the drop down.
     * 
     */
    this.GetSelectedVariantCode=function() {
     
         var ItemCode="";
             ItemCode=GetSelectedValue(this.Bubble_DropDownListID);
           if(ItemCode!=null) {
               if(ItemCode!="") {
                   if (ItemCode.indexOf("#") != -1){
                      var my_array=ItemCode.split("#");
                      itemPrice=my_array[1];
		              ItemCode=my_array[0]; 
	               }
	            }
	       }
	       return ItemCode;
     }
     /**
     * returns the current variant code from the span tag - for single variant per product.
     * 
     */
    this.GetSelectedSingleVariantCode=function() {
     
         var ItemCode="";
             ItemCode=GetSelectedValue(this.Bubble_DropDownListID);
           if(ItemCode!=null) {
               if(ItemCode!="") {
                   if (ItemCode.indexOf("#") != -1){
                      var my_array=ItemCode.split("#");
                      itemPrice=my_array[1];
		              ItemCode=my_array[0]; 
	               }
	            }
	       }
	       return ItemCode;
     }
    /**
     * Adds an item in the bag, the function determines the selected sku and calls the webservice method
     * to add item to the bag
     */
    this.AddSingleProductToCart=function () {
         var ItemQuantity=1;
         ItemQuantity=GetSelectedValue(this.Bubble_QuantityID);
         var ItemCode="";
         var itemPrice=0;
         ItemCode=GetSelectedValue(this.Bubble_DropDownListID);
         
           if(ItemCode!=null) {
               if(ItemCode!="") {
                   if (ItemCode.indexOf("#") != -1){
                      var my_array=ItemCode.split("#");
                      itemPrice=my_array[1];
		              ItemCode=my_array[0]; 
	    	          
	    	          //alert(itemPrice);
		              //alert(ItemCode);
	               }
	            }
	       }
	      var variantName=GetSelectedText(this.Bubble_DropDownListID);
          //alert(ItemCode);
          MM_showHideLayers(this.Bubble_DivID,'','hide') 
          //set shopping cart last item properties
          _LastShoppingCartItem.ItemQuantity=ItemQuantity; //save the quantity for later retrival
          _LastShoppingCartItem.ItemCode=ItemCode;         //save variant code
          _LastShoppingCartItem.ItemPrice=0;
          _LastShoppingCartItem.ItemName=variantName;
          if (itemPrice!=0) {
              _LastShoppingCartItem.ItemPrice=itemPrice; 
          }
       // hide modal
        _CoreModalPopUp.Hide();
        //adds the var code to cookie to determine for coremetrics on checkout coming from quick shop
        CoreAjaxCookie_AddVariant(ItemCode);
        //call web service methods - Callback function is the modified one for Shu Art of Hair
        _WebServiceAPI.AddSingleProductToCart (GetCurrentCustomerID(),ItemCode,ItemQuantity,AddSingleProductToCartResultHandlerModified)
    }
    
    /**
     * This function is used to return the Extended property value from the Properties array in JASON     
     */
     this.GetPropertyValue = function(Properties,Name){
        
        if ( Properties == null ) {return "";}
        if ( Name == null ) {return "";}
        for(var j=0; j<Properties.length; j++) {
            
            if (Properties[j].Name.toLowerCase()==Name.toLowerCase()) {
              return Properties[j].Value;
            }
        }
        return "";           
     }
     
     /**
     * Adds a recommendation product item in the bag from Basket Page.
     * The function determines the selected sku and calls the webservice method
     * to add item to the bag. 
     * It has been overriden because it needs to call a CallBack function different from other pages.
     */
    this.AddSingleProductToCartFromBasketPage=function () {
         var ItemQuantity=1;
         ItemQuantity=GetSelectedValue(this.Bubble_QuantityID);
         var ItemCode="";
         var itemPrice=0;
         ItemCode=GetSelectedValue(this.Bubble_DropDownListID);
         
           if(ItemCode!=null) {
               if(ItemCode!="") {
                   if (ItemCode.indexOf("#") != -1){
                      var my_array=ItemCode.split("#");
                      itemPrice=my_array[1];
		              ItemCode=my_array[0]; 
	    	          
	    	          //alert(itemPrice);
		              //alert(ItemCode);
	               }
	            }
	       }
	      var variantName=GetSelectedText(this.Bubble_DropDownListID);
          //alert(ItemCode);
          MM_showHideLayers(this.Bubble_DivID,'','hide') 
          //set shopping cart last item properties
          _LastShoppingCartItem.ItemQuantity=ItemQuantity; //save the quantity for later retrival
          _LastShoppingCartItem.ItemCode=ItemCode;         //save variant code
          _LastShoppingCartItem.ItemPrice=0;
          _LastShoppingCartItem.ItemName=variantName;
          if (itemPrice!=0) {
              _LastShoppingCartItem.ItemPrice=itemPrice; 
          }
       // hide modal
        _CoreModalPopUp.Hide();
        //adds the var code to cookie to determine for coremetrics on checkout coming from quick shop
        CoreAjaxCookie_AddVariant(ItemCode);
        //call web service methods - Callback function is the modified one for Shu Art of Hair
        _WebServiceAPI.AddSingleProductToCart (GetCurrentCustomerID(),ItemCode,ItemQuantity,AddSingleProductToCartResultHandlerModifiedForBasket)
    }
}

/**
 * It registers the ProductBubble_Class using registerClass method of MS AJAX Library
 */
 if (IsTypeDefined) { Core.ProductBubble_Class.registerClass('Core.ProductBubble_Class'); }


