
/*
 * this function compares the passed in shadeImageName to the currentShadeImage global
 * variable to control the mouse over behavior of the shades on the product detail screen
 */
function shadeImageOver(productLineNameInternal, ndxShadeImage, shadeImageName)
{
  if (currentProductInfo[productLineNameInternal].currentShadeName != shadeImageName)
  {
    jQuery ("#" + productLineNameInternal + "ShadeImage"          + ndxShadeImage).animate({width:70,  height:10}, "fast");
    jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + ndxShadeImage).animate({height:16, paddingLeft:0, paddingTop:0}, "fast");
    jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + ndxShadeImage).css    ("cursor", "pointer");
  }
}


/*
 * this function compares the passed in shadeImageName to the currentShadeImage global
 * variable to control the mouse out behavior of the shades on the product detail screen
 */
function shadeImageOut(productLineNameInternal, ndxShadeImage, shadeImageName)
{
  if (currentProductInfo[productLineNameInternal].currentShadeName != shadeImageName)
  {
    jQuery ("#" + productLineNameInternal + "ShadeImage"          + ndxShadeImage).animate({width:66,  height:6}, "fast");
    jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + ndxShadeImage).animate({height:14, paddingLeft:2, paddingTop:2}, "fast");
    jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + ndxShadeImage).css    ("cursor", "default");
  }
}

/*
 * this function controls the mouse click behavior of the shades on the product detail screen
 */
function shadeImageClick(productLineNameInternal, ndxShadeImage, shadeImageName, productLineName, productName, productImageName, numberOfProducts, shoppingCartImageName, upc)
{
  var newImgSrc = currentProductInfo[productLineNameInternal].productImagePath + productImageName;

  // using the global numberOfProducts variable that was set when the product 
  for (x = 0; x < numberOfProducts; x++)
  {
    if(x != ndxShadeImage)
    {
      jQuery ("#" + productLineNameInternal + "ShadeImage"          + x).animate({width:66,  height:6}, "fast");
      jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + x).animate({height:14, paddingLeft:2, paddingTop:2}, "fast");
    }
    else
    {
      jQuery ("#" + productLineNameInternal + "ShadeImage"          + x).animate({width:70,  height:10}, "fast");
      jQuery ("#" + productLineNameInternal + "ShadeImageContainer" + x).animate({height:16, paddingLeft:0, paddingTop:0}, "fast");
    }
  }

  setContent   (productLineNameInternal + "ProductName", productName);

  jQuery("#" + productLineNameInternal + "ProductImg").fadeTo(500,0,
    function()
    { 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({src:   newImgSrc}); 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({alt:   productName}); 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({title: productName}); 
    }).fadeTo(500, 1); //Fades in

  // set the global currentShadeImage variable to the shadeImageName that was just passed in- used in shadeImageOver() and shadeImageOut()
  // also set the global shopping cart values
  currentProductInfo[productLineNameInternal].currentShadeName      = shadeImageName;
  currentProductInfo[productLineNameInternal].productLineName       = productLineName;
  currentProductInfo[productLineNameInternal].productName           = productName;
  currentProductInfo[productLineNameInternal].shoppingCartImageName = shoppingCartImageName;
  currentProductInfo[productLineNameInternal].shadeImageName        = shadeImageName;
  currentProductInfo[productLineNameInternal].upc                   = upc;
}

function proToolNameOver(productLineNameInternal, ndxShadeImage, productName)
{
  if (currentProductInfo[productLineNameInternal].productName != productName)
  {
    jQuery ("#" + productLineNameInternal + "ProductName"          + ndxShadeImage).css    ("text-decoration", "underline");
    jQuery ("#" + productLineNameInternal + "ProductName"          + ndxShadeImage).css    ("cursor", "pointer");
  }
}

/*
 * this function compares the passed in shadeImageName to the currentShadeImage global
 * variable to control the mouse out behavior of the shades on the product detail screen
 */
function proToolNameOut(productLineNameInternal, ndxShadeImage, productName)
{
  if (currentProductInfo[productLineNameInternal].productName != productName)
  {
    jQuery ("#" + productLineNameInternal + "ProductName"          + ndxShadeImage).css    ("text-decoration", "none");
    jQuery ("#" + productLineNameInternal + "ProductName"          + ndxShadeImage).css    ("cursor", "default");
  }
}

/*
 * this function controls the mouse click behavior of the shades on the product detail screen
 */
function proToolNameClick(productLineNameInternal, ndxShadeImage, productLineName, productName, productImageName, numberOfProducts, shoppingCartImageName, upc)
{
   var newImgSrc = currentProductInfo[productLineNameInternal].productImagePath + productImageName
   var x = 0;
      
   for (y = 0; y < 2; y++)
   {
      x++;

      if(x != ndxShadeImage)
      {
         jQuery ("#" + productLineNameInternal + "ProductName"          + x).css    ("text-decoration", "none");
      }
      else
      {
         jQuery ("#" + productLineNameInternal + "ProductName"          + x).css    ("text-decoration", "underline");
      }
   }

  jQuery("#" + productLineNameInternal + "ProductImg").fadeTo(500,0,
    function()
    { 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({src:   newImgSrc}); 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({alt:   productName}); 
      jQuery("#" + productLineNameInternal + "ProductImg").attr({title: productName}); 
    }).fadeTo(500, 1); //Fades in


  // set the global currentShadeImage variable to the shadeImageName that was just passed in- used in shadeImageOver() and shadeImageOut()
  // also set the global shopping cart values
//currentProductInfo[productLineNameInternal].currentShadeName      = shadeImageName;
  currentProductInfo[productLineNameInternal].currentShadeName      = "";
  currentProductInfo[productLineNameInternal].productLineName       = productLineName;
  currentProductInfo[productLineNameInternal].productName           = productName;
  currentProductInfo[productLineNameInternal].shoppingCartImageName = shoppingCartImageName;
//currentProductInfo[productLineNameInternal].productDescription    = productDescription;
  currentProductInfo[productLineNameInternal].upc                   = upc;
}


function showInitialProductImage(productLineNameInternal)
{
  // we only want to show this message 3 times
  if (nTimesProductHelpShown < 3)
    setTimeout("showInitialProductImageCallback('" + productLineNameInternal + "')", 5000);
  else
  {
    jQuery("#" + productLineNameInternal + "InitialText").hide(); 
    jQuery("#" + productLineNameInternal + "ProductImg") .show(); 
  }
}

function showInitialProductImageCallback(productLineNameInternal)
{
  nTimesProductHelpShown++;
  jQuery("#" + productLineNameInternal + "InitialText").fadeTo("300", 0.0, function(){showInitialProductImageComplete(this.id)}); 
}

function showInitialProductImageComplete(productLineNameInternal)
{
  var id = productLineNameInternal.replace(/InitialText/, "ProductImg");

  jQuery("#" + productLineNameInternal).hide(); 
  jQuery("#" + id).css("opacity", 0.0).css("display", "block").fadeTo("600", 1.0); 
}

/*
 * This function is called from the Ratings/Revies overlay.  It compares the
 * passed in divName to the global currentRatingsNavName variable to control the 
 * mouse over behavior for the Read All Reviews and Write A Review tabs. 
 */
function ratingsNavOver(divName, ndxNavDiv)
{ 
  var completeNavDivName = "nav_" + divName;
  if (currentRatingsNavName != completeNavDivName)
  {
    
     if(divName == "readReview")
     {
        jQuery ("#" + "nav_" + divName).css    ("cursor", "pointer");   
     }
     else if (divName == "writeReview")
     {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "pointer");
     }
  }
  else
  {
     if(divName == "readReview")
     {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "default");   
     }
     else if (divName == "writeReview")
     {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "default");
     }
  }
}

/*
 * This function is called from the Ratings/Revies overlay.  It compares the
 * passed in divName to the global currentRatingsNavName variable to control 
 * the mouse out behavior for the Read All Reviews and Write A Review tabs. 
 */
function ratingsNavOut(divName, ndxNavDiv)
{
  var completeNavDivName = "nav_" + divName;
  if (currentRatingsNavName != completeNavDivName)
  {    
    if(divName == "readReview")
    {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "default");
    }
    else if (divName == "writeReview")
    {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "default");
    }
  }
  else
  {
      if(divName == "readReview")
     {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "pointer");   
     }
     else if (divName == "writeReview")
     {
        jQuery ("#" + "nav_" +divName).css    ("cursor", "pointer");
     }
  }
}

/*
 * This function controls the Read Reviews and Write a Review
 * tabs for the Ratings/Reviews overlay.
 */
function ratingsNavClick(divName, ndxNavDiv)
{
   var completeNavDivName = "nav_" + divName;
   var x = 0;
   
   if(divName == "readReview")
   {
      jQuery("#" + completeNavDivName).attr("class", "readReviewActive");
      jQuery("#" + "nav_writeReview").attr("class", "writeReview");
      
      jQuery ("#" + divName).show();
      jQuery ("#" + "writeReview").hide();   
   }
   else if (divName == "writeReview")
   {
      // the clicked the Write A Review tab so make sure they are
      // logged in
      if(!loggedIn)
      {
         login('loginMainContainer', 'loginForm');
      }
      else
      {
        // call the function that will encode ther user authentication
        // string and update the BazaarVoice code.
        encodeUserAuthString();
        
        jQuery("#" + completeNavDivName).attr("class", "writeReviewActive");
        jQuery("#" + "nav_readReview").attr("class", "readReview");
        
       /* 
        var waitImage = "";
        if(currentHost == "www.maxfactor.com" || currentHost == "maxfactor.com")
        {
           waitImage = '<img src="http://reviews.maxfactor.com/static/8869/bv_sub_loading_60.gif" id="BVLoaderImage" width="60" height="60"/>';
        }
        else
        {
           waitImage = '<img src="http://reviews.maxfactor.com/bvstaging/static/8869/bv_sub_loading_60.gif" id="BVLoaderImage" width="60" height="60"/>'
        }
        
        jQuery("#BVSubmissionContainer").html(waitImage);*/
      
        jQuery ("#" + divName).show();
        jQuery ("#" + "readReview").hide();
      }
   }
   currentRatingsNavName = completeNavDivName;
}

/* This function gets called when the Write A Review tab is clicked
 * in the Ratings/Reviews overlay.  It is responsible for encoding
 * the user authentication string that gets passed to BazaarVoice.
 */
function encodeUserAuthString()
{
   var tempSubmissionFrameString = "";
   
   jQuery.post("/EncodeUserId.do", 
           function (encodeString)
           {
              var userId = "";
              var date = "";
              var maxage = 90;
              if (encodeString != null && encodeString != "")
              {
                 var encodeStringResult = encodeString.split("|");
                 var scroll = 0;
             
                 for(i = 0; i < encodeStringResult.length; i++)
                 {
                    var pos = encodeStringResult[i].indexOf('=');
                    if (pos > 0) 
                    {
                       var key = encodeStringResult[i].substring(0,pos);
                       var val = encodeStringResult[i].substring(pos+1);
                    }
                                                      
                    if(key == "userId")
                    {
                       userId = val;
                    }
                 }
                                  
                 if(currentHost == "www.maxfactor.com" || currentHost == "maxfactor.com")
                 {
                   var BVSubmissionFrameString = "http://reviews.maxfactor.com/8869/" + bazaarVoiceProductLineID + "/action.htm?format=embedded&action=AddReview&user=__USERID__&userdisplayname=__USERDISPLAYNAME__&return=http%3A%2F%2Flocal.maxfactor.com%3A8080%2index.jsp&campaignid=BV_RATING_SUMMARY_ZERO_REVIEWS";
                 }
                 else
                 {
                   var BVSubmissionFrameString = "http://reviews.maxfactor.com/bvstaging/8869/" + bazaarVoiceProductLineID + "/action.htm?format=embedded&action=AddReview&user=__USERID__&userdisplayname=__USERDISPLAYNAME__&return=http%3A%2F%2Flocal.maxfactor.com%3A8080%2index.jsp&campaignid=BV_RATING_SUMMARY_ZERO_REVIEWS";
                 }
                 
                 tempSubmissionFrameString = BVSubmissionFrameString.replace("__USERID__" , userId);
                 //alert(tempSubmissionFrameString);
                 jQuery("#BVSubmissionFrame").attr({src: tempSubmissionFrameString});
              }
           }          
        );
}

/*
 * This function is called from the Ratings/Revies overlay.  
 */
function showRatingsReviewsOverlay(divOverlayName, divName)
{   
   var contentPanesLeft = null;
   var contentPanesTop = null;
   var left = null;
   var top = null;
   
   contentPanesLeft = parseInt($("contentPanes").style.left);
   contentPanesTop = parseInt($("contentPanes").style.top);
   
   left = contentPanesLeft + 50;
   top = contentPanesTop - 350;
                
   trackOverlay (divName);      // tell web analytics that we're displaying this overlay
   productsOverlayDisplayBackground(0, divOverlayName)
   jQuery("#"+divName).fadeIn("slow");
}

/* 
 * This function hides the Ratings/Reviews overlay 
 */
function hideRatingsOverlay(divOverlayName, divName) 
{
   jQuery("#" + divOverlayName).hide ();
   jQuery("#" + divName).hide ();
}

// this variable gets set in the loadRatingsReviewsData() function
// and is used in the encodeUserAuthString() function to set the current
// product line id in BVSubmissionFrame
var bazaarVoiceProductLineID = null; 

/* This function gets called every time a product line is loaded.  It 
 * is responsible for setting the correct product line id in the BVFrame
 * for BazaarVoice, as well as setting the correct product line image and
 * description in the Ratings/Reviews overlay. 
 */
function loadRatingsReviewsData(productLineId, productLineNameInternal, productLineImage, productLineName, productLineDescription)
{  
   var ratingsOverlayString = "";
   var ratingsOverlayBackgroundName = productLineNameInternal + "RatingsOverlayBackground";
   var ratingsContainerName = productLineNameInternal + "RatingsContainer";
   bazaarVoiceProductLineID = productLineId;

   var contentPanesLeft = null;
   contentPanesLeft = parseInt($("contentPanes").style.left);
   jQuery("#ratings").css  ("left", contentPanesLeft);
   
   if(currentHost == "www.maxfactor.com" || currentHost == "maxfactor.com")
   {
     // we are on production so set the src of the BVFrame to the Bazaarvoice production URL
     jQuery("#BVFrame").attr({src:   "http://reviews.maxfactor.com/8869/" + productLineId + "/reviews.htm?format=embedded"});
   }
   else
   {
     // we are on local server, dev, or staging so set the src of the BVFrame to the Bazaarvoice staging URL
     jQuery("#BVFrame").attr({src:   "http://reviews.maxfactor.com/bvstaging/8869/" + productLineId + "/reviews.htm?format=embedded"});
   }
   
   jQuery("#reviews").css    ("width", 450);
   
   jQuery("#ratingImg").attr({src: productLineImage});
   jQuery("#ratingsProductName").html  (productLineName);
   jQuery("#ratingsProductDescription").html  (productLineDescription);
  
   jQuery("#ratings").show ();
}

/*
 * This function gets called from the showRatingsReviewsOverlay() function
 * and is used when displaying the Ratings/Reviews overlay
 */
function productsOverlayDisplayBackground(opacity, divName)
{
   var contentPanesLeft = null;
   var contentPanesTop = null;
   var left = null;
   var top = null;
   
   contentPanesLeft = parseInt($("contentPanes").style.left);
   contentPanesTop = parseInt($("contentPanes").style.top);
  
  if (opacity === undefined)
    opacity = 0.55;
  
  jQuery("#" + divName).css  ('opacity', opacity);
  jQuery("#" + divName).css  ('height',  jQuery(window).height());
  jQuery("#" + divName).css  ('width',  jQuery(window).width());
  jQuery("#" + divName).css  ('left', 0);
  jQuery("#" + divName).css  ('top',  0);
  jQuery("#" + divName).show ();
}
