(function() {
    if (typeof cmCreatePageviewTag === "function") {
        try {
            var fns = []; // hold references to original functions with newer APIs

			fns["cmCreateRegistrationTag"]	 = cmCreateRegistrationTag;
            fns["cmCreateManualPageviewTag"] = cmCreateManualPageviewTag;
            fns["cmCreateManualPageviewTag"] = cmCreateManualPageviewTag;
            fns["cmCreatePageviewTag"]       = cmCreatePageviewTag;
            fns["cmCreateProductviewTag"]    = cmCreateProductviewTag;
            fns["cmCreateShopAction5Tag"]    = cmCreateShopAction5Tag;
            fns["cmCreateShopAction9Tag"]    = cmCreateShopAction9Tag;
            fns["cmCreateOrderTag"]          = cmCreateOrderTag;

            // Override Coremetrics functions to pass the bnCustomerID (cookie:userid) and WL store id

            var setupCoremetricsProxy = function() {
            
				var getCookie = function(name) { // Same as in bnnav.js but indented
					var search = name + "=";
					if (document.cookie.length > 0) { // if there are any cookies 
						offset = document.cookie.indexOf(search);
						if (offset != -1) {								// if cookie exists		  
							offset += search.length;					// set index of beginning of value 
							end = document.cookie.indexOf(";", offset);	// set index of end of cookie value		 
							if (end == -1) {
								end = document.cookie.length;
							}
							return unescape(document.cookie.substring(offset, end));
						}	
					}
				};

				var getStoreID = function() {
					var id = "0001";					
					try {
						id = BN.Environment.Store.getStoreId();
					}
					catch(e) {}
					return id;
				};
				
				var getStoreCode = function() {
					var code = "bn";					
					try {
						code = BN.Environment.Store.getStoreCode();
					}
					catch(e) {}
					return code.toUpperCase();
				};
				
				var bnCustomerID = getCookie("userid");
				var storeID = getStoreID();
				var storeCode = getStoreCode();
				var bnURL = document.URL;

				var extraAttr = bnCustomerID + "-_-" + storeID;

				var getPrefixedStoreAttributes = function(attributes) {
					// If no attributes, return the [bnCustomerID-_-storeID]
					// else prefix the attributes: [bnCustomerID-_-storeID]-_-attributes
					var empty = (typeof attributes === "undefined");
					var result = (empty)? storeID : storeID + "-_-" + attributes;
					return result;
				};
				
				var getCategory = function(category) {
					var result = "";

					if (storeCode === "BN") {
						result = category;
					}
					else {
						result = "WL:" + storeCode;
					}
					return result;
				};
				
				var getPageId = function(pageId) {
					var result = "";
					
					if (storeCode === "BN") {
						result = pageId;
					}
					else {
						result = storeCode + "-" + pageId;
					}
					result = result.substr(0, 50); // no more than 50 chars.
					return result;
				};

				cmCreateRegistrationTag = function(customerID, customerEmail, customerCity, customerState, customerZIP, attributes) {
					var empty = (typeof attributes === "undefined");
					var revisedAttributes = (empty)? ("-_--_--_--_--_-" + extraAttr) : (attributes + "-_-" + extraAttr);
					fns["cmCreateRegistrationTag"](customerID, customerEmail, customerCity, customerState, customerZIP, revisedAttributes);
					
					if(typeof cmCreateCustomRegTag !== "undefined"){
						cmCreateCustomRegTag(bnCustomerID);
					}
				};

				cmCreateManualPageviewTag = function(pageID, categoryID, destinationURL, referringURL) {
					pageID = getPageId(pageID);
					categoryID = getCategory(categoryID);
					fns["cmCreateManualPageviewTag"](pageID, categoryID, destinationURL, referringURL, bnCustomerID, bnURL, storeID);
				};

				cmCreatePageviewTag = function(pageID, categoryID, searchString, searchResults, attributes) {
					var customAttributes = getPrefixedStoreAttributes(attributes);
					pageID = getPageId(pageID);
					categoryID = getCategory(categoryID);
					fns["cmCreatePageviewTag"](pageID, categoryID, searchString, searchResults, customAttributes, bnCustomerID, bnURL, storeID);
				};

				cmCreateProductviewTag = function(productID, productName, categoryID, attributes) {
					var customAttributes = getPrefixedStoreAttributes(attributes);
					categoryID = getCategory(categoryID);
					fns["cmCreateProductviewTag"](productID, productName, categoryID, customAttributes, bnCustomerID, storeID);
				};

				cmCreateShopAction5Tag = function(productID, productName, productQuantity, productPrice, categoryID, attributes) {
					var customAttributes = getPrefixedStoreAttributes(attributes);
					categoryID = getCategory(categoryID);
					fns["cmCreateShopAction5Tag"](productID, productName, productQuantity, productPrice, categoryID, customAttributes, bnCustomerID, storeID);
				};

				cmCreateShopAction9Tag = function(productID, productName, productQuantity, productPrice, customerID, orderID, orderTotal, categoryID, attributes) {
					var customAttributes = getPrefixedStoreAttributes(attributes);
					categoryID = getCategory(categoryID);
					fns["cmCreateShopAction9Tag"](productID, productName, productQuantity, productPrice, customerID, orderID, orderTotal, categoryID, customAttributes, bnCustomerID, storeID);
				};

				cmCreateOrderTag = function(orderID, orderTotal, orderShipping, customerID, customerCity, customerState, customerZIP, attributes) {
					var customAttributes = getPrefixedStoreAttributes(attributes);
					fns["cmCreateOrderTag"](orderID, orderTotal, orderShipping, customerID, customerCity, customerState, customerZIP, customAttributes, bnCustomerID, storeID);
				};
            };

            try {
                setupCoremetricsProxy();
            }
            catch (e1) {
                if (typeof console !== "undefined") {
                    var c = console.log;
                    c("Error: " + e1.message);
                }
                throw e1;
            }
        }
        catch(e2) {
            if (typeof console !== "undefined") {
                var c = console.log;
                c("Error: " + e2.message);
            }
            throw e2;
        }
    }
})();

