function fcnGetCookie(strName)
{
  var dc = document.cookie;
  var strPrefix = strName + "=";
  var intBegin = dc.indexOf("; " + strPrefix);

  if (intBegin == -1)
  {
    intBegin = dc.indexOf(strPrefix);
    if (intBegin != 0)
      return null;
  }
  else
    intBegin += 2;

  var intEnd = document.cookie.indexOf(";", intBegin);

  if (intEnd == -1)
    intEnd = dc.length;

  return unescape(dc.substring(intBegin + strPrefix.length, intEnd));
}

function fcnSetCookie(strName, strValue, strExpires, strPath, strDomain, blnIsSecure)
{
  var today = new Date();
  today.setTime(today.getTime());

  /*
  if the expires variable is set, make the correct expires time, the current script 
  below will set it for x number of days, to make it for hours, delete * 24, for 
  minutes, delete * 60 * 24
  */
  if (strExpires)
  {
    strExpires = strExpires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date(today.getTime() + (strExpires));

  document.cookie = strName + "=" + escape(strValue) +
    ((strExpires) ? "; expires=" + expires_date.toGMTString() : "") +
    ((strPath) ? "; path=" + strPath : "") +
    ((strDomain) ? "; domain=" + strDomain : "") +
    ((blnIsSecure) ? "; secure" : "");
}

function IsSilverlightInstalled()
{
  var strIsSilverlightInstalled = "false";

  try
  {
    //Check on IE.
    try
    {
      var slControl = new ActiveXObject('AgControl.AgControl');
      strIsSilverlightInstalled = "true";
    }
    catch (e)
    {
      //either not installed or not IE. Check Firefox
      if (navigator.plugins["Silverlight Plug-In"])
        strIsSilverlightInstalled = "false";
    }
  }
  catch (e)
  {
    //we don't want to leak exceptions. However, you may want
    //to add exception tracking code here.
  }

  return strIsSilverlightInstalled;
}

function fcnClearAltImage(a_strSelectedID, a_strImagePath, a_strMainImageID)
{
  document.getElementById(a_strMainImageID).src = a_strImagePath;

  var objImgList = document.getElementById('divAltImages').getElementsByTagName('img');
  for (var i = 0; i < objImgList.length; i++)
  {
    var objImg = objImgList[i];

    if (objImg.tagName == 'IMG')
      objImg.className = '';
  }
  document.getElementById(a_strSelectedID).className = 'selected';
}

function fcnCreditCardExpandCollapseArea(a_strContainerID, a_strDivControlID, a_strParaControlID)
{
  var divs = document.getElementById(a_strContainerID).getElementsByTagName("div");
  var paras = document.getElementById(a_strContainerID).getElementsByTagName("p");

  for (var i = 0; i < divs.length; i++)
  {
    if (divs[i].id.length > 0 && divs[i].id.substr(0, 6) == 'divCsc')
      divs[i].style.display = 'none';
  }

  for (var j = 0; j < paras.length; j++)
  {
    paras[j].style.display = 'block';
  }

  document.getElementById(a_strDivControlID).style.display = 'block';
  document.getElementById(a_strParaControlID).style.display = 'none';
}

function fcnExpandCollapseArea(a_blnExpand, a_strControlID)
{
  if (a_blnExpand)
    document.getElementById(a_strControlID).style.display = 'block';
  else
    document.getElementById(a_strControlID).style.display = 'none';
}

function fcnShowQuickLook(a_strTargetElementSelector, a_strQuickLookElement)
{
  try
  {
    var targetElement = $(a_strTargetElementSelector);
    var mainPanel = $(a_strQuickLookElement);
    var leftOffset = targetElement.position().left;
    var topOffset = targetElement.position().top;
    var intWidth = targetElement.width() / 2 - 65;
    var intHeight = targetElement.height() / 2 - 28;

    mainPanel.css({ left: leftOffset + intWidth, top: topOffset + intHeight, display: 'block' });
  }
  catch (e)
  {
    //we don't want to leak exceptions. However, you may want
    //to add exception tracking code here.
  }
}

function onKeyPressing(sender, eventArgs)
{
  if (!sender.get_dropDownVisible())
    sender.showDropDown();
}

function fcnCheckStateEntry(sender, eventArgs)
{
  try
  {
    if (sender.get_value() == '')
    {
      var strText = sender.get_text().toUpperCase();

      if (strText.length == 2)
      {
        var objItem = sender.findItemByValue(strText);

        if (objItem != null)
          sender.set_value(strText);
      }
    }
  }
  catch (e)
  {
    //we don't want to leak exceptions. However, you may want
    //to add exception tracking code here.
  }
}


function fcnCloseActiveToolTip()
{
  try
  {
    var tooltip = Telerik.Web.UI.RadToolTip.getCurrent();
    if (tooltip)
      tooltip.hide();
  }
  catch (a)
  {}
}

//constants (for adjustment)
var dropoutConstants =
  {
    upSpeed: 400,
    noTouchDelay: 3000,
    downSpeed: 750,
    mouseOutDelay: 1500
  };

var quantityCookieName = "__THYMES_ITEMS_IN_CART";
var quantityCookieExpireDays = 7;

//Tracking
var shoppingBagDropoutPanelMouseOver = false;
var shoppingBagDropoutPanelIsRaising = false;

function fcnGetInputValue(a_strTextboxID)
{
  try
  {
    var radInput = $find(a_strTextboxID);

    return radInput.get_value();
  }
  catch (e)
  {
    e = null;

    return 1;
  }
}

function fcnAddSingleItem(a_intProductID)
{
  try
  {
    fcnAddItem(a_intProductID, 1, 0);
  }
  catch (e)
  {
    var strError = escape("Description1:" + e.description + " Message:" + e.message + " Name: " + e.name);

    window.location = "https://www.Thymes.com/Secure/Checkout/AddItemBackup.aspx?pid=" + a_intProductID + "&qty=1&crossid=0&error=" + strError;
  }
}

function fcnAddItemFromQty(a_intProductID, a_strQtyID, a_intCrossSellID)
{
  var intQty = 0;

  try
  {
    intQty = parseInt(fcnGetInputValue(a_strQtyID));
    fcnAddItem(a_intProductID, intQty, a_intCrossSellID);
  }
  catch (e)
  {
    var strError = escape("Description2:" + e.description + " Message:" + e.message + " Name: " + e.name);

    window.location = "https://www.Thymes.com/Secure/Checkout/AddItemBackup.aspx?pid=" + a_intProductID + "&qty=" + intQty + "&crossid=" + a_intCrossSellID + "&error=" + strError;
  }
}

function fcnAddItem(a_intProductID, a_intQty, a_intCrossSellID)
{
  fcnCloseActiveToolTip();
  window.scroll(0, 0);

  var intTotalQuantity = 0;
  var intQuantityCookieValue = 0;

  try
  {
    intQuantityCookieValue = fcnGetCookie(quantityCookieName);

    if (intQuantityCookieValue)
      intTotalQuantity = parseInt(intQuantityCookieValue);
  }
  catch (e)
  {
  }

  intTotalQuantity += parseInt(a_intQty);

  fcnSetCookie(quantityCookieName, intTotalQuantity, "7", "", "", false);

  document.getElementById('hypViewShoppingBag').innerHTML = 'Shopping Bag (' + intTotalQuantity + ')';

  $.ajax({ url: "/Secure/Checkout/AddItem.aspx/AddProduct",
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: '{"productID":"' + a_intProductID + '", "quantity":' + a_intQty + ', "crossSellID":"' + a_intCrossSellID + '"}',
    dataType: 'json',
    success: function (data)
    {
      //Ajax callback (the ".d" is built-in to the Page Method response by .net)
      dropShoppingBag(data.d, dropoutConstants.downSpeed, dropoutConstants.noTouchDelay)
    }
  });
}

function fcnGetQuantity(control)
{
  //The next() jquery function will get the next "input" dom object after the passed-in control
  //Reference: http://api.jquery.com/next/
  //This assumes that the quantity textbox is AFTER the control.
  //If this is NOT the case, the .prev() function may be used instead.
  var quantity = $(control).prev("input").val();
  return quantity;
}

$(document).ready(function ()
{
  //SETUP
  //Put the drop-out div where we want it
  setShoppingBagDropoutLeftOffset("#hypViewShoppingBag", -90, 0);

  //This function will correct the position of the dropout when the window is resized
  $(window).resize(function ()
  {
    //Note that these offset values need to be changed below as well
    setShoppingBagDropoutLeftOffset("#hypViewShoppingBag", -90, 0);
  });

  $("#hypCloseShopping").click(function (e) { e.preventDefault(); raiseShoppingBag(true); });

  $("#shopping_bag_dropout").mouseover(function ()
  {
    shoppingBagDropoutPanelMouseOver = true;
  });

  $("#shopping_bag_dropout").mouseout(function ()
  {
    shoppingBagDropoutPanelMouseOver = false;
    setTimeout("raiseShoppingBag(false)", dropoutConstants.mouseOutDelay);
  });
});

function dropShoppingBag(innerHtml, delayTime)
{
  var mainPanel = $("#shopping_bag_dropout");
  var contentDiv = $("#shopping_bag_dropout_content");
  contentDiv.html(innerHtml);
  //debugger;

  //Note:  this "show-slide" method requires the jquery-ui script plugin included
  mainPanel.show("slide", { direction: "up" }, dropoutConstants.downSpeed, function () { setTimeout("raiseShoppingBag(false);", dropoutConstants.noTouchDelay); });
}

function raiseShoppingBag(viaClick)
{
  if (shoppingBagDropoutPanelIsRaising == true) return;
  var mainPanel = $("#shopping_bag_dropout");
  var isHidden = mainPanel.is(":hidden");

  //If the panel is already hidden, don't do anything
  //If we are moused over the panel, don't do anything, unless we have clicked to close the panel
  if (isHidden || (shoppingBagDropoutPanelMouseOver && !viaClick)) return;
  shoppingBagDropoutPanelIsRaising = true;

  //Note:  this "hide-slide" method requires the jquery-ui script plugin included
  mainPanel.hide("slide", { direction: "up" }, dropoutConstants.upSpeed, function () { shoppingBagDropoutPanelIsRaising = false; });
}

function setShoppingBagDropoutLeftOffset(targetElementSelector, offsetXAdjustment, offsetYAdjustment)
{
  var targetElement = $(targetElementSelector);
  var mainPanel = $("#shopping_bag_dropout");
  var leftOffset = targetElement.position().left;
  var topOffset = targetElement.position().top;
  mainPanel.css({ left: leftOffset + offsetXAdjustment, top: topOffset + offsetYAdjustment });
}

