var omnPageTitlePrefix, omnPageTitlePostfix;
var staticPageName;
var omnProductNames, omnProductIDs, omnStateCode, omnPostalCode, omnCountry, omnLocale, omnSearchResultPageNumber, omnQuickView, omnRefinementType;
var tempHierarchy, tempPageType, tempPageName, tempProducts, tempProductID;
var metricsAutoInit;

(function(app){ 
if (app) {
	
	/*
	 * app.metrics
	 * Namespace intended to handle all metrics / analytics for the app (including Omniture, Google Analytics, etc)
	 */
	app.metrics = {
		pageType: null,
		pageName: null,
		pageNameSeparator: ":",
		usePageTitle: true,
		platform: "web",
		sitePrefix: "",
		eVar3Cookie: null,		
		searchCookie: null,
		
		/*
		 * app.metrics.init
		 * Initialize the metrics object
		 */
		init: function (options) {
			//console.log('app.metrics.init', options);

			if (typeof options != 'undefined') {
				if (typeof options.usePageTitle == 'boolean') 
					this.usePageTitle = options.usePageTitle;
				if (options.platform)
					this.platform = options.platform;
			}
			
			if (app.resources["SITE_ID"])
				this.sitePrefix = app.resources["SITE_ID"].replace('ToryBurch_', '');

			if (!app.currentPage.categoryHierarchy) {
				app.currentPage.categoryHierarchy = [];
			}
			if (!app.currentPage.contentHierarchy) {
				app.currentPage.contentHierarchy = [];
			}
			if (!app.currentPage.breadcrumbs) {
				app.currentPage.breadcrumbs = [];
			}
			
			this.cookies.init();
		},
		
		/*
		 * app.metrics.autoSetPageName
		 * tries to set the page name automatically
		 */
		autoSetPageName: function () {
			//console.log("app.metrics.autoSetPageName");

			this.setPageNameFromContent();
			this.setPageNameFromCategories();
		
			if (!this.pageName) {
				this.setPageNameFromBreadcrumbs();
			}
			
			if (!this.pageName) {
				this.setDefaultPageName();
			}			
		},			
		
		/*
		 * app.metrics.setDefaultPageName
		 * sets the page name from staticPageName or the page title
		 */		
		setDefaultPageName: function () {
			if (staticPageName) {
				this.pageName = staticPageName;					
			} else {
				var pgt = this.getPageTitleParts();
				for (var i = 0; i < pgt.length; i++) 
					this.addToArray(pgt[i], app.currentPage.contentHierarchy);
				this.pageName = app.currentPage.contentHierarchy.join(this.pageNameSeparator);
			}
		},	
		
		/*
		 * app.metrics.getPageTitle
		 * Utility function to get and clean the document/page title
		 * @param: strip (Boolean) whether or not to strip special characters from the title
		 * @return: String
		 */
		getPageTitle: function (strip) {
			var t = document.title;
			//console.log("original page title = " + t);
			t = jQuery.trim(t.replace(/Version([^\""]+)-/, "")); // remove dev/staging version number
			//console.log("version removed = " + t);
			
			t = jQuery.trim(t.replace(omnPageTitlePrefix, "")); // remove prefix
			t = jQuery.trim(t.replace(omnPageTitlePostfix, "")); // remove postfix
			
			t = jQuery.trim(t.replace(omnPageTitlePrefix.replace('|', '-'), "")); // remove prefix
			t = jQuery.trim(t.replace(omnPageTitlePostfix.replace('|', '-'), "")); // remove postfix
			
			
			//console.log("prefix/postfix removed = " + t);
			t = jQuery.trim(t.replace(/Checkout Step([^\""]+)\|/, "")); // remove checkout step number
			//console.log("checkout step removed = " + t);
			//t = t.replace(/^\s+|\s+$/g, ''); // trim whitespace
			//console.log(t);
			return (strip) ? this.stripChars(t) : t;
		},
		
		/*
		 * app.metrics.getPageTitleParts
		 * Utility function to split the title into an array
		 * @param: t (Optional String) the title to split, if not supplied app.metrics.getPageTitle is called
		 * @return: Array of Strings
		 */
		getPageTitleParts: function (t) {
			if (!t) t = this.getPageTitle(false);
			var tt = t.split("|");
			for (var i = 0; i < tt.length; i++)
				tt[i] = this.stripChars(tt[i].replace(/^\s+|\s+$/g, ''));
			return tt;
		},
		
		/*
		 * app.metrics.setPageNameFromContent
		 * Build the page name from app.currentPage.contentHierarchy
		 */
		setPageNameFromContent: function () {
			//console.log("setPageNameFromContent");
			if (app.currentPage.contentHierarchy) {
				
				// add the parent as the first in the array
				if (app.currentPage.contentParent)
					if (jQuery.inArray(app.currentPage.contentParent, app.currentPage.contentHierarchy) == -1)
						app.currentPage.contentHierarchy.unshift(app.currentPage.contentParent);
				
				if (app.currentPage.contentHierarchy.length > 0) {

					if (app.currentPage.contentHierarchy.length == 1)
						this.addToArray("Home", app.currentPage.contentHierarchy);
					
					this.pageName = app.currentPage.contentHierarchy.join(this.pageNameSeparator);
				}
			}
			//console.log("app.metrics.pageName = " + this.pageName);
		},
	
		/*
		 * app.metrics.resetPageNameFromContent
		 * Clears and repopulates app.currentPage.contentHierarchy to build the page name
		 */
		resetPageNameFromContent: function () {
			//console.log("resetPageNameFromContent");
			app.currentPage.contentHierarchy = [];
			if (app.currentPage.contentParent)
				this.addToArray(app.currentPage.contentParent, app.currentPage.contentHierarchy);
			if (app.currentPage.contentName)
				this.addToArray(app.currentPage.contentName, app.currentPage.contentHierarchy);
			if (app.currentPage.contentHierarchy.length == 1)
				this.addToArray("Home", app.currentPage.contentHierarchy);
					
			this.pageName = app.currentPage.contentHierarchy.join(this.pageNameSeparator);	
			//console.log("app.metrics.pageName = " + this.pageName);
		},
	
		/*
		 * app.metrics.rejoinPageNameFromContent
		 * build the page name from app.currentPage.contentHierarchy
		 */
		rejoinPageNameFromContent: function () {
			//console.log("resetPageNameFromContent");
			this.pageName = app.currentPage.contentHierarchy.join(this.pageNameSeparator);	
			//console.log("app.metrics.pageName = " + this.pageName); 
		},
		
		/*
		 * app.metrics.setPageNameFromCategories
		 * Build the page name from app.currentPage.categoryHierarchy (which is populated in components/header/htmlhead.isml)
		 */
		setPageNameFromCategories: function () {
			if (app.currentPage.categoryHierarchy) {
				//console.log("setPageNameFromCategories " + app.currentPage.categoryHierarchy);
				
				if (app.currentPage.categoryHierarchy.length > 0) {
					
					//this.removeFromArray("Hidden Category", app.currentPage.categoryHierarchy);
					
					var lastItem = app.currentPage.categoryHierarchy[app.currentPage.categoryHierarchy.length-1];
					//console.log('lastItem=' + lastItem + ' ' + (lastItem.indexOf("Page ") === 0));
					if (lastItem) {
						if ((lastItem.indexOf("Page ") === 0) || lastItem == "Home") {
							this.removeFromArray(lastItem, app.currentPage.categoryHierarchy);
						}
					}
					lastItem = (parseInt(omnSearchResultPageNumber) > 1) ? ("Page " + omnSearchResultPageNumber) : "Home";
	
					switch (app.currentPage.categoryHierarchy.length) {
						case 1:
							this.pageType = "Category Page";
							this.addToArray(lastItem, app.currentPage.categoryHierarchy);
							break;
						case 2:
							this.pageType = "Sub Section";
							this.addToArray(lastItem, app.currentPage.categoryHierarchy);
							break;
						case 3:
							this.pageType = "Sub Subsection";
							this.addToArray(lastItem, app.currentPage.categoryHierarchy);
							break;
					}
					this.pageName = app.currentPage.categoryHierarchy.join(this.pageNameSeparator);	
				}							
			}		
			//console.log("app.metrics.pageName = " + this.pageName);	
		},
		
		/*
		 * app.metrics.setPageNameFromBreadcrumbs
		 * Uses app.currentPage.breadcrumbs (which is populated in components/breadcrumbs.isml)
		 * to build the page name parent and then gets the rest from the page title
		 */
		setPageNameFromBreadcrumbs: function () {
			//console.log("setPageNameFromBreadcrumbs");
			if (app.currentPage.breadcrumbs) {
				
				//console.log('app.metrics.usePageTitle = ' + app.metrics.usePageTitle);
				if (app.metrics.usePageTitle == true) {
					var pgt = this.getPageTitleParts();
					for (var i = 0; i < pgt.length; i++) 
						this.addToArray(pgt[i], app.currentPage.breadcrumbs);
				}
				
				// remove numeric (such as order history #)
				for (var i = 0; i < app.currentPage.breadcrumbs.length; i++) {
					if (!isNaN(parseInt(app.currentPage.breadcrumbs[i]))) 
						this.removeFromArray(app.currentPage.breadcrumbs[i], app.currentPage.breadcrumbs);				
				}
				
				if (app.currentPage.breadcrumbs.length == 1) 
					this.addToArray("Home", app.currentPage.breadcrumbs);
				
				this.pageName = app.currentPage.breadcrumbs.join(this.pageNameSeparator)				
			}
			//console.log("app.metrics.pageName = " + this.pageName);
		},
		
		/*
		 * app.metrics.setPageNameFromProduct
		 * Appends the Product Name to app.currentPage.categoryHierarchy and sets app.metrics.pageType
		 * @param: productName (String) the name of the product to add to app.currentPage.categoryHierarchy
		 */
		setPageNameFromProduct: function (productName) {
			//console.log("setPageNameFromProduct");
			
			if (this.pageType == "Product page") {
				// opening quick view for a product on a product page
				app.currentPage.categoryHierarchy = app.quickView.categoryHierarchy;
			}
			
			if (productName) {
				productName = jQuery.capitalizeWords(this.stripChars(productName));
				
				if (app.currentPage.categoryHierarchy.length > 0) {
					//this.removeFromArray('Home', app.currentPage.categoryHierarchy);
					var lastItem = app.currentPage.categoryHierarchy[app.currentPage.categoryHierarchy.length-1];
					//console.log('lastItem=' + lastItem + ' ' + (lastItem.indexOf("Page ") === 0));
					if ((lastItem.indexOf("Page ") === 0) || lastItem == "Home") {
						this.removeFromArray(lastItem, app.currentPage.categoryHierarchy);
					}
				}
				
				this.addToArray(productName, app.currentPage.categoryHierarchy);
				this.pageType = "Product page";
				this.pageName = app.currentPage.categoryHierarchy.join(this.pageNameSeparator);	
			}
			//console.log("app.metrics.pageName = " + this.pageName);
		},
		
		/*
		 * app.metrics.setOmnitureForShopping
		 * Finalizes omniture settings	for Merchandising pages
		 */
		setOmnitureForShopping: function (pageNameParts) {
			if (s) {
				//console.log("setOmnitureForShopping");
				
				switch (this.pageType) {
					case "Category Page":
						this.set_eVar4(pageNameParts);
						break;
					case "Sub Section":
						s.prop2 = pageNameParts[1];
						this.set_eVar4(pageNameParts);
						break;
					case "Sub Subsection":
						s.prop2 = pageNameParts[1];
						s.prop3 = pageNameParts[2]; 
						this.set_eVar4(pageNameParts);
						break;
					case "Product page":
						s.prop4 = pageNameParts[pageNameParts.length-1];						
						s.prop2 = (pageNameParts.length >= 3 && pageNameParts[1] != s.prop4) ? pageNameParts[1] : "";
						s.prop3 = (pageNameParts.length >= 4 && pageNameParts[2] != s.prop4) ? pageNameParts[2] : "";
						s.products = ";" + omnProductIDs;
						s.events = (omnQuickView == true) ? "prodView,event4" : "prodView,event3";
						
						if (!tempProductID) {
							// save current settings
							tempProducts = this.stripChars(omnProductNames);
							tempProductID = omnProductIDs;
						}
						
						break;
				}

				s.eVar3 = "browse";				
				
				if (omnRefinementType && omnRefinementName && omnRefinementValue) {	
					omnRefinementValue = this.stripChars(omnRefinementValue);
					if (omnRefinementType == 'product') {
						s.events = 'event1';
						s.prop18 = omnRefinementName;
						s.prop19 = omnRefinementName + this.pageNameSeparator + omnRefinementValue;
						s.eVar34 = s.prop18;
						s.eVar35 = s.prop19;
					} else if (omnRefinementType == 'search') {
						s.events = 'event1';
						s.prop16 = omnRefinementName;
						s.prop17 = omnRefinementName + this.pageNameSeparator + omnRefinementValue;
						s.eVar32 = s.prop16;
						s.eVar33 = s.prop17;
					}
				}
				
			}
		},

		/*
		 * app.metrics.setOmniture
		 * Finalizes omniture settings	
		 */
		setOmniture: function (isShoppingPage) {
			if (s) {
				//console.log("setOmniture " + this.pageName);

				var pageNameParts = this.pageName.split(this.pageNameSeparator);				
				for (var i = 0; i < pageNameParts.length; i++) 
					pageNameParts[i] = this.stripChars(pageNameParts[i]);				
				
				if (!this.pageType && pageNameParts.length > 0)
					this.pageType = pageNameParts[0];
				
				if (isShoppingPage == true)
					this.setOmnitureForShopping(pageNameParts);
				
				//console.log(pageNameParts);
				s.pageName = pageNameParts.join(this.pageNameSeparator);
				
				if (!s.channel && pageNameParts.length > 0)
					s.channel = (pageNameParts[0] != s.prop4) ? pageNameParts[0] : "";
				
				if (!s.prop1 && pageNameParts.length > 0)
					s.prop1 = (pageNameParts[0] != s.prop4) ? pageNameParts[0] : "";
				
				if (!s.prop2 && pageNameParts.length > 1) {
					if (pageNameParts[1] != "Home" && pageNameParts[1] != s.prop4)
						s.prop2 = pageNameParts[1];
				}
				if (!s.prop3 && pageNameParts.length > 2) {
					if (pageNameParts[2] != "Home" && pageNameParts[2] != s.prop4)
						s.prop3 = pageNameParts[2];
				}
				//console.log('pageType = ' + this.pageType);
				s.prop5 = this.pageType;
				
				//s.state = omnStateCode;
				//s.zip = omnPostalCode;
								
				this.setOmniturePlatform();
				this.setOmnitureSitePrefix();
				//console.log('s.pageName=' + s.pageName);
				
				if (s.hier) {
					if (s.hier1.toLowerCase() != s.pageName.toLowerCase())
						s.hier1 = "";
				}
				
				var cookieval = this.eVar3Cookie.get();									
				if (cookieval) {
					if (cookieval == 'cross-sell')
						s.eVar3 = cookieval;
				}
				this.eVar3Cookie.clear();
				
				
				if (!tempPageName) {
					// save current settings
					tempHierarchy = app.currentPage.categoryHierarchy.slice();
					tempPageType = this.pageType;
					tempPageName = this.pageName;
				}
			}
		},
		
		/*
		 * app.metrics.setOmniturePlatform
		 * Finalizes omniture platform	
		 */
		setOmniturePlatform: function () {
			if (s) {
				if (this.platform) { 
					
					s.eVar15 = this.platform;
					s.prop15 = this.platform;
					if (this.platform != "web") {
						
						var p = this.platform + this.pageNameSeparator;
						function addPlatform(val) {
							if (val) return p + val;
							return val;
						}
						
						s.pageName	= addPlatform(s.pageName);
						s.channel 	= addPlatform(s.channel);
						s.prop1 	= addPlatform(s.prop1);
						s.prop2 	= addPlatform(s.prop2);
						s.prop3 	= addPlatform(s.prop3);
						s.prop4 	= addPlatform(s.prop4);
						s.prop5 	= addPlatform(s.prop5);
					}
				}
			}
		},
		
		/*
		 * app.metrics.setOmnitureSitePrefix
		 * Finalizes omniture site prefix	
		 */
		setOmnitureSitePrefix: function () {
			if (s) {
				
				if (!this.sitePrefix || this.sitePrefix == "") 
					this.sitePrefix = "US";
					
				s.eVar36 = this.sitePrefix;
				s.prop20 = this.sitePrefix;
				
				if (this.sitePrefix != "US") {
					
					var p = this.sitePrefix + this.pageNameSeparator;
					function addPrefix(val) {
						if (val) return p + val;
						return val;
					}
					
					s.pageName 	= addPrefix(s.pageName);
					s.channel 	= addPrefix(s.channel);
					s.prop1 	= addPrefix(s.prop1);
					s.prop2 	= addPrefix(s.prop2);
					s.prop3 	= addPrefix(s.prop3);
					s.prop4 	= addPrefix(s.prop4);
					s.prop5 	= addPrefix(s.prop5);
				}				
			}
		},
		
		/*
		 * app.metrics.set_eVar4
		 * Set Merchandising Category for Omniture
		 */
		set_eVar4: function (pageNameParts) {
			var evar4temp = pageNameParts.slice(); 				
			var lastitem = evar4temp[evar4temp.length-1];
			if ((lastitem.indexOf("Page ") === 0) || lastitem == "Home") {
				this.removeFromArray(lastitem, evar4temp);
			}
			s.eVar4 = evar4temp.join(" > ").toLowerCase();	
			
			if (this.platform) { 
				if (this.platform != "web") 
					s.eVar4 = this.platform + this.pageNameSeparator + s.eVar4;
			}
			if (this.sitePrefix) { 
				if (this.sitePrefix != "" && this.sitePrefix != "US") 
					s.eVar4 = this.sitePrefix + this.pageNameSeparator + s.eVar4;
			}
		},
		
		/*
		 * app.metrics.trackAddToCart
		 * Track product add to cart	
		 * @param: productID (String) the ID of the product being added to the cart
		 */
		trackAddToCart: function (productID) {			
			var event = (jQuery('.minicartcontent').length == 0) ? "scOpen,scAdd" : "scAdd";	
			if (this.platform.toLowerCase() == 'mobile')
				event = (jQuery('#minicart .cartqty').text().toLowerCase().indexOf('empty') > -1) ? "scOpen,scAdd" : "scAdd";
			this.trackCustomLink({ linkTrackVars: "events,products", linkTrackEvents: event, events: event, products: ";" + productID  }, "Add to Cart", true);
		},
		
		/*
		 * app.metrics.trackCartRemove
		 * Track product removal from cart	
		 * @param: target (Object) the button or link that was clicked
		 * @param: productID (String) the ID of the product being removed from the cart
		 */
		trackCartRemove: function (target, productID) {
			this.trackCustomLink({ linkTrackVars: "events,products", linkTrackEvents: "scRemove", events: "scRemove", products: ";" + productID  }, "Cart Removal", target);
		},
		
		/*
		 * app.metrics.trackQuickView
		 * Track product quick view
		 */
		trackQuickView: function () {
			
			// update Omniture for Quick View
			s.hier1 = '';
			s.eVar4 = '';
			s.eVar19 = '';
			s.eVar20 = '';
			this.setPageNameFromProduct(omnProductNames);
			this.setOmniture(true);
			s.t();
			
			setTimeout(function () {				
				// revert original settings
				app.currentPage.categoryHierarchy = tempHierarchy.slice();
				this.pageType = tempPageType;
				this.pageName = tempPageName;
				s.pageName = this.pageName;
				omnProductNames = tempProducts;
				omnProductIDs = tempProductID;
				s.products = ";" + omnProductIDs;
		 		omnQuickView = false;	
			}, 500);
		},		
		
		/*
		 * app.metrics.trackModalPageView
		 * Track page view of modal popup 
		 */
		trackModalPageView: function (pageName) {
			this.trackNewPageView(pageName + this.pageNameSeparator + "Popup");
		},
		
		/*
		 * app.metrics.trackNewPageView
		 * Track a new page view (not custom link)
		 */
		trackNewPageView: function (pageName, isShoppingPage) {
			if (s) {
				var tempPageType = this.pageType;
				var tempPageName = this.pageName;
				
				this.clearOmniture();
				if (pageName) this.pageName = pageName;
				if (!(isShoppingPage == true)) this.pageType = null;
				this.setOmniture(isShoppingPage);
				s.t();
				
				this.pageType = tempPageType;
				this.pageName = tempPageName;
			}
		},
		
		/*
		 * app.metrics.trackCustomLink
		 * Custom link tracking
		 * @param: linkOptions (Object) { linkTrackVars: '', linkTrackEvents: '', events: '', products: ''  }
		 * @param: linkName (String) Name of the link for reporting
		 * @param: linkTarget - (Optional) the target of the click event, or true to disable the delay
		 */
		trackCustomLink: function (linkOptions, linkName, linkTarget) {
			if (s) {					
				for (var propName in linkOptions) {
					s[propName] = linkOptions[propName];
				}
				s.tl(linkTarget, 'o', linkName);
			}
		},
		
		/*
		 * app.metrics.trackSearchSubmit
		 * Track keyword search from form submit
		 * @param: keyword (String) the search keyword
		 * @param: target (Object) the button or link that was clicked
		 */
		trackSearchSubmit: function (keyword, target) {
			//console.log('app.metrics.trackSearchSubmit');
			this.eVar3Cookie.set('search');
			this.searchCookie.set(keyword);			
			//this.trackCustomLink({ linkTrackVars: "events,prop6,eVar1,eVar3", linkTrackEvents: "event1", events: "event1", prop6: keyword, eVar1: keyword, eVar3: "search" }, "Site Search Submit", target);
		},
		
		/*
		 * app.metrics.trackCrossSell
		 * Track cross sell click
		 */
		trackCrossSell: function () {
			//console.log('app.metrics.trackCrossSell');
			app.metrics.eVar3Cookie.set('cross-sell');
			s_crossSell();
		},
		
		/*
		 * app.metrics.cookie
		 * Constructor for metrics cookies
		 * @param: cookieName (String) the name of the cookie
		 */
		cookie : function (cookieName) {
			return {
				name: cookieName,
				get: function () {
					return (jQuery.cookie) ? jQuery.cookie(this.name) : null;
				},
				
				set: function (value) {
					if (jQuery.cookie)
						jQuery.cookie(this.name, value, { path: '/' });
				},
				clear: function () {
					if (jQuery.cookie)
						jQuery.cookie(this.name, null, { path: '/', expires: -1 });
				}				
			}
		},
		
		/*
		 * app.metrics.cookies
		 * initialize metrics cookies
		 */
		cookies: {
			init: function () {			
				app.metrics.eVar3Cookie = new app.metrics.cookie('s_eVar3');
				app.metrics.searchCookie = new app.metrics.cookie('s_eVar1');
			}
		},

		
		/*
		 * app.metrics.addToArray
		 * Utility function to add to array while preventing duplicates	
		 * @param: str (String) the string to add to the supplied array
		 * @param: arr (Array) the array	to add the supplied string to
		 */
		addToArray: function (str, arr) {
			if (str.length > 0) {
				if (jQuery.inArray( str, arr ) == -1) {
					arr.push(str);
					//console.log('addToArray ' + str);
				}
			}			
		},
		
		/*
		 * app.metrics.removeFromArray
		 * Utility function to remove from array	
		 * @param: str (String) the string to remove from the supplied array
		 * @param: arr (Array) the array	to remove the supplied string from	
		 */
		removeFromArray: function (str, arr) {
			var remInd = jQuery.inArray( str, arr );
			//console.log('app.metrics.removeFromArray('+str+') index=' + remInd);
			if (remInd > -1) {
				arr.splice(remInd, 1);
			}
		},	
		
		/*
		 * app.metrics.stripChars
		 * Utility function to strip special characters from a supplied string	
		 * @param: a (String) the string to strip
		 * @return: String
		 */
		stripChars: function(a) {
			a = a.split("|").join("");
			a = a.split("/").join("");
			a = a.split("!").join("");
			a = a.split("&").join("and");
			a = a.split("'").join("");
			a = a.split("#").join("");
			a = a.split("$").join("dollars");
			a = a.split("%").join("percent");
			a = a.split("^").join("");
			a = a.split("*").join("");
			a = a.split(":").join("");
			a = a.split("~").join("");
			a = a.split(";").join("");
			return a;
		},
		
		/*
		 * app.metrics.clearOmniture
		 * Clears Omniture settings	
		 */
		clearOmniture: function() { 
			if (s) {
				for(var i = 0; i < 100; i++) { 
					s['prop' + i] = ''; 
					s['eVar' + i] = ''; 
				} 
				s.products = ''; 
				s.events = ''; 
				s.linkTrackVars="None"
				s.linkTrackEvents="None"
				s.server = ''; 
				s.channel = ''; 
				s.pageName = ''; 
				s.pageType = ''; 
				s.campaign = ''; 
				s.hier1 = '';
			}
		}
		
	}; // end metrics definition
}
else {
	alert("app is undefined!");
}
})(app);


//omnitureStrip moved here from omniture_asset_include.isml use app.metrics.stripChars instead
function omnitureStrip(a) {
	a = a.split("|").join("");
	a = a.split("/").join("");
	a = a.split("!").join("");
	a = a.split("&").join(" and ");
	a = a.split("'").join("");
	a = a.split("#").join("");
	a = a.split("$").join(" dollars ");
	a = a.split("%").join(" percent ");
	a = a.split("^").join("");
	a = a.split("*").join("");
	a = a.split(":").join("");
	a = a.split("~").join("");
	a = a.split(";").join("");
	a = a.split(" ").join("+");
	//a = a.toLowerCase();
	return a;  
}
//getEpTag moved here from omniture_asset_include.isml
function getEpTag() {
	var url = window.location.toString();
	url.match(/\?(.+)$/);
	var params = RegExp.$1;
	var params = params.split("&");
	var queryStringList = {};
	for (var i=0;i<params.length;i++) {
		var tmp = params[i].split("=");
		if(tmp[0] == "ep_tag")
			return eptag = tmp[1];
	}
	return "";
}


if (metricsAutoInit != false)
	app.metrics.init();

jQuery(document).ready(function() {
	
	var isCtrl = false; 
	jQuery(document).keydown(function (e) { 
		if(e.which == 17) isCtrl=true; 
		if(e.which == 81 && isCtrl == true) { 
			//run code for CTRL+Q 
			var dbg = window.open("", "dp_debugger", "width=600,height=800,location=0,menubar=0,status=1,toolbar=0,resizable=1,scrollbars=1");
			dbg.document.write("<script language=\"JavaScript\" id=dbg src=\"http://www.digitalpulse.omniture.com/dp/debugger.js\"></" + "script>");
			dbg.focus();
			return false; 
		} 
	}).keyup(function (e) { 
		if(e.which == 17) isCtrl=false; 
	});	
	
	// catch submit for all search forms and track omniture click
	jQuery('form[action$="Search-Show"]').one('submit', function(e) {
		e.preventDefault();
		e.stopImmediatePropagation();
		
		var form = this;
		var q = jQuery('input[name="q"]', this).val();
		var target = jQuery('button[type="submit"]', this);
		app.metrics.trackSearchSubmit(q, target[0]);
		
		// slight delay to make sure the omniture tracking went through
		setTimeout(function () {
			jQuery(form).submit();
		}, 200);
		
		return false;
	});
});
