﻿ var MINICART_MOVEMENT_TIMEOUT = 40;
 var MINICART_MOVEMENT_LENGTH = 7;
 var MINICART_NOTIFY_HEIGHT = 125;
 var MINICART_NOTIFY_TIMEOUT = 8000;
 var MINICART_TABLE_HEIGHT = 50;

function formatCurrency(dblValue) {
    var strBefore, strAfter, strSplit, strValue;
    var intIndex, intCount;
    strValue = new String(new Number(dblValue));
    if (strValue.indexOf(".") == -1) {
       strAfter = "00";
       strBefore = strValue;
    }
    else {
       strSplit = strValue.split(".");
       strBefore = strSplit[0];
       strAfter = strSplit[1];
    }
    if (strAfter.length == 1)
       strAfter += "0";
    else if (strAfter.length > 2)
       if (new Number(strAfter.substr(2, 1)) >= 5)
           strAfter = new String(new Number(strAfter.substr(0, 2)) + 1);
       else
           strAfter = strAfter.substr(0, 2);
    if (strBefore.length > 3) {
       strValue = "";
       intCount = -1;
       for (intIndex = strBefore.length - 1; intIndex >= 0; intIndex--) {
           if (intCount + 1 >= 3)
               strValue = "," + strValue;
           strValue = strBefore.substr(intIndex, 1) + strValue;
           if (++intCount >= 3)
               intCount = 0;
       }
       strBefore = strValue;
    }
    return "$" + strBefore + "." + strAfter;
 }
  
function changeMiniCartDropDownPanel(intDirection) {
    var objPanel = document.getElementById("MiniCartDropDownPanel");
    if (intDirection > 0) {
       if (intMiniCartCurHeight == 0) {
           sta.setPanelVisibility(objPanel, true);
           objPanel.style.top = MINICART_TABLE_HEIGHT;
       }
       else if (intMiniCartCurHeight >= MINICART_NOTIFY_HEIGHT) {
           window.clearInterval(intMiniCartInterval);
           intMiniCartInterval = null;
       }
    }
    intMiniCartCurHeight = intMiniCartCurHeight + intDirection;
    if (intMiniCartCurHeight < 0)
       intMiniCartCurHeight = 0;
    objPanel.style.height = intMiniCartCurHeight;
    if (intDirection < 0 && intMiniCartCurHeight == 0) {
       sta.setPanelVisibility(objPanel, false);
       window.clearInterval(intMiniCartInterval);
       intMiniCartInterval = null;
    }
}

function switchMiniCartDropDownPanel() {
    if (intMiniCartInterval == null)
       if (intMiniCartCurHeight >= MINICART_NOTIFY_HEIGHT)
           intMiniCartInterval = window.setInterval("changeMiniCartDropDownPanel(-MINICART_MOVEMENT_LENGTH);", MINICART_MOVEMENT_TIMEOUT);
       else
           intMiniCartInterval = window.setInterval("changeMiniCartDropDownPanel(MINICART_MOVEMENT_LENGTH);", MINICART_MOVEMENT_TIMEOUT);
}

function updateMiniCart(objCart, strLastProductID, strItemsInCartID, strProductTotalID) {
    var strTotal = new String(getCartProductTotal(objCart));
    var strText;
    var objProduct = null;
    var intIndex = objCart.length - 1;
    var objCell = document.getElementById(strLastProductID);
    if (objCart.length == 1)
        strText = "1 item";
    else
        strText = objCart.length + " items";
    strText += " in Your Cart";
    if (objCart.length > 0)
      strText += " (<a class=\"MiniCartLink\" href=\"" + strFullCartUrl + "\">View Cart</a>)";
    document.getElementById(strItemsInCartID).innerHTML = strText;
    document.getElementById(strProductTotalID).innerHTML = "Product Total: " + formatCurrency(strTotal);
    while (intIndex >= 0 && objProduct == null) {
        if (objCart[intIndex].AutoInsertProduct == "false")
            objProduct = objCart[intIndex];
        intIndex -= 1;
    }
    sta.setPanelVisibility(objCell, objProduct != null);
    if (objProduct != null) {
        objCell.innerHTML = "Last Item<br /><a href=\"" + objProduct.AjaxProductUrl + "\"><img src=\"/Sta.DnnWebService/GetProductImage.ashx?ImageOverride=" + getProductImageOverride(objProduct.WarehouseLocs4) + "&ImageTypeID=0\" alt=\"" + getProductName(objProduct) + "\" class=\"MiniCart_ProductImage\" /></a>";
    }
}

function showAddedMiniCartProduct(objProduct, strCheckout, strImageUrl) {
    var objPanel = document.getElementById("MiniCartDropDownPanel");
    var strValue = "<table border=\"0\">" + 
      "<tr><td colspan=\"2\"><p><i>You've just added to your cart:</i><br /><b>" + getProductName(objProduct) + "</b></p></td></tr>" +
      "<tr><td><a href=\"" + objProduct.AjaxProductUrl + "\"><img src=\"/Sta.DnnWebService/GetProductImage.ashx?ImageOverride=" + getProductImageOverride(objProduct.WarehouseLocs4) + "&ImageTypeID=0\" style=\"width: 60px; height: 60px; border: 0px;\" /></a></td>" + 
      "<td><p><b>Brand</b>: " + objProduct.BrandName + "<br /><b>Quantity</b>: " + objProduct.Quantity + "<br /><b>Price</b>: " + objProduct.AjaxUserPrice;
    if (new Number(objProduct.InStock) > 0)
        strValue += "<br /><b>In Stock Now</b>";
    strValue += "</p></td></tr>" + 
      "<tr><td colspan=\"2\"><p><a href=\"javascript:window.clearTimeout(intMiniCartTimeout); void(switchMiniCartDropDownPanel());\">Close Drop-Down Window</a></p>" +
      "</table>";
    objPanel.innerHTML = strValue;
    document.getElementById(strCheckout).src = strImageUrl;
    switchMiniCartDropDownPanel();
    intMiniCartTimeout = window.setTimeout("switchMiniCartDropDownPanel();", MINICART_NOTIFY_TIMEOUT);
}
function getProductImageOverride(strWarehouseLocs) {
	return strWarehouseLocs.substr(0, 8).trim();
}