var site = {
  config : { 
    domainname : "www.houseofstaunton.com"
  }
};

// simple debug capability
// The "console" object is not supported on all browsers. Create a dummy object to prevent errors.
if(typeof console === "undefined") {
    console = { log: function() { } };
}
var debug = {
  enabled : false,
  write : function(text) {
    if (debug.enabled) {
      try {
        console.log(text);
      } catch(ex) {
        alert(text)
      }
    }
  }
};


// Customizations with the scope of jQuery's "Document Ready" function
//   Note: Code placed within this function will not execute until after all images have been loaded.
//         This allows us to access and manipulate html elements on a page without the html renderer having
//         encountered that html yet, so we can place all javascript in the page header and not litter the
//         template with "<script>" tags. ...if so desired.
$(function() {
	
/************************************************************************************************************
*************                              STATIC CART SUMMARY                                *****************
/************************************************************************************************************/
$.ajax({
	url: '/AjaxCart.asp',
	cache: false,
	dataType: 'text',
	success: function(data) {
		var json = eval('(' + data + ')');
		var quantity;
		var cartTotal;
		quantity = json.Totals[0].Quantity;
		cartTotal = json.Totals[0].CartTotal;
		$("#mini_cart_summary").text("Items in Cart: " + quantity + " | Total: " + cartTotal);
	},
  error: function() {
    return false;
  }
});

/************************************************************************************************************
*************                              MAKE   AN   OFFER                                *****************
/************************************************************************************************************/
// The following code will create a "Make an Offer" button provided the sample html is included on a page:
//   Sample HTML:
//     <input type="image" id="make_an_offer_button" alt="Make An Offer" src="/v/vspfiles/templates/140/images/buttons/btn_makeanoffer.gif" />
  var makeAnOffer = {
    init: function() {
      // locate the make an offer button
      var $makeAnOfferButton = $("#make_an_offer_button");
      // if it exists then initialize it, otherwise exit.
      if (0 == $makeAnOfferButton.length)
      {
        debug.write("Make an offer button not found; skipping");
        return;
      }
      
      var $anchor = $("[name=btnaddtowishlist]");
      // if it exists then initialize it, otherwise exit.
      if (0 == $anchor.length)
      {
        debug.write("'Add To Wishlist' button not found; cannot add 'Make an Offer' button.");
        return;
      }

      // update the product code and name
      var productName = $(".productnamecolorLARGE.colors_productname").text();
      var productCode = $(".product_code").text();

      // set the make an offer click handler to show a dialog
      $makeAnOfferButton.insertAfter($anchor);
      $makeAnOfferButton.click(function() {
        var referrer = window.location.pathname.replace(".htm", "_htm");
        window.location.href = "http://" + site.config.domainname + "/articles.asp?ID=257&pn=" + productName + "&pc=" + productCode + "&referrer=" + referrer;
        return false;
      });
    }
  };
  makeAnOffer.init();
  // end "Make an Offer"

/************************************************************************************************************
*************                              Copy email address to "from" field                                *****************
/************************************************************************************************************/
// The following code will copy a user's entered email address to the sender field so staff can "reply" without having to manually
// copy the user's email address.
// REQUIREMENTS:
//    1. All fields should be contained in a form named "eMail", e.g. "<form name="eMail" ...>"
//    2. The textbox that users enter their email address into is named "Email Address"
  var copyUserEmail = {
    $form : $("form[name=eMail]"),
    execute : function() {
      var sourceAddress = $("input[name='Email Address']", copyUserEmail.$form).val();
      if ("undefined" != sourceAddress)
        $("input[name=email_From]", copyUserEmail.$form).val(sourceAddress);
    },
    init : function() {
      if (0 < copyUserEmail.$form.length)
      {
        copyUserEmail.$form.submit(copyUserEmail.execute);
      }
    }
  };
  copyUserEmail.init();
}); // End jQuery's "Document Ready" function
