(function() {
	if (typeof cmCreatePageviewTag === "function") {
		try {
			var fns = []; // hold references to original functions with newer APIs

			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)

			var setupBnCustomerIdFunctions = function() {
			
				var getCookie = function(Name) { // Same as in bnnav.js
					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 bnCustomerID = getCookie("userid");

				cmCreateManualPageviewTag = function(pageID, categoryID, destinationURL, referringURL) {
					fns["cmCreateManualPageviewTag"](pageID, categoryID, destinationURL, referringURL, bnCustomerID);
				};

				cmCreatePageviewTag = function(pageID, categoryID, searchString, searchResults, attributes) {
					fns["cmCreatePageviewTag"](pageID, categoryID, searchString, searchResults, attributes, bnCustomerID);
				};

				cmCreateProductviewTag = function(productID, productName, categoryID, attributes) {
					fns["cmCreateProductviewTag"](productID, productName, categoryID, attributes, bnCustomerID);
				};

				cmCreateShopAction5Tag = function(productID, productName, productQuantity, productPrice, categoryID, attributes) {
					fns["cmCreateShopAction5Tag"](productID, productName, productQuantity, productPrice, categoryID, attributes, bnCustomerID);
				};

				cmCreateShopAction9Tag = function(productID, productName, productQuantity, productPrice, customerID, orderID, orderTotal, categoryID, attributes) {
					fns["cmCreateShopAction9Tag"](productID, productName, productQuantity, productPrice, customerID, orderID, orderTotal, categoryID, attributes, bnCustomerID);
				};

				cmCreateOrderTag = function(orderID, orderTotal, orderShipping, customerID, customerCity, customerState, customerZIP, attributes) {
					fns["cmCreateOrderTag"](orderID, orderTotal, orderShipping, customerID, customerCity, customerState, customerZIP, attributes, bnCustomerID);
				};
			};

			try {
				setupBnCustomerIdFunctions();
			}
			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;
		}
	}
})();

