/***************************************************************************************************************
	This code used to in /xsl/common/ultraTransform.xsl.  It was removed: June 2010
	** see LeroyV or Carleigh Romeis if you have any questions.
****************************************************************************************************************/					

function cmSpFromCmRE(href, cmRe, linkName) {
	// Turns the value a generated manual_cm_re attribute into a cm_sp impression on links where it's been added by hand
	//usage examples:
	// onClick="cmSpFromCmRE($(this).attr('href'), $(this).attr('manual_cm_re'), 'Description');"
	// onClick="cmSpFromCmRE($(this).attr('href'), $(this).attr('manual_cm_re'), $(this).text().replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, ''));"
	href = href.replace(/[#']/g, '');
	
	if(href.indexOf('?') > -1){
		cmCreateManualLinkClickTag(href + '&cm_sp=' + cmRe, linkName);
		naf.omni.sendSpEvent(cmRe);
	}
	else{
		cmCreateManualLinkClickTag(href + '?cm_sp=' + cmRe, linkName);
		naf.omni.sendSpEvent(cmRe);
	}
}





/*####		Moved these back to ultraTransform.xsl 	#####*/
/*
var windowOnloadDone = false;
function origOnload() {
}

origOnload = window.onload;
function init() {
	windowOnloadDone = true;
}

window.onload = function () {
	origOnload();
	init();
}


function setCookie(name, value) {
	var expdate = "Fri, 31-Dec-99 11:59:59 GMT"
	document.cookie = name + "=" + escape(value) + "; expires=" + expdate + "; path=/";
}

var currentUrl = window.location.href;

function logout() {
	setCookie("SESSION_ID", "");
	setCookie("loggedin", "0");
	setCookie("rei_cart", "0");
	setCookie("GiftRegistrySetup", "0");
}

var multipleItems = "s";
if (numOfItems == 1) multipleItems = "";

*/




var makeCmReRun = false;

makeCmRe = function(){

	if (windowOnloadDone == true) {
	
		if (makeCmReRun == false) {
			makeCmReRun = true;
			
			$('#pageContent a, #sidebarWrapper a') .one('mouseover', function () {
				var parentId1 = $(this) .parents('[id = "pageContent"],[id = "sidebarWrapper"]') .get(0) .id;
				var parentId2 = $(this) .parents('[id]') .get(0) .id;
				var linkContent;
				if ($(this) .children('img') .length > 0 && $(this) .children('img') .attr('alt') && $(this) .children('img') .attr('alt') .length > 0) {
					linkContent = $(this) .children('img') .attr('alt') .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				} 
				else if ($(this) .children('img') .length > 0) {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				else {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				
				var sidewrapper_val = parentId1 + '-_-' + parentId2 + '-_-' + linkContent;				
				$(this) .attr('manual_cm_re', sidewrapper_val);
				
				//for omniture
				$(this).click(function() {
					if( $('#omni_re').length > 0 ){
						naf.omni.sendReEvent(sidewrapper_val);
					}
				});
			});
			
			$('#bannerZone1 a, #bannerZone2 a, #bannerZone3 a') .one('mouseover', function () {
				var store = "REI";
				if (storeId == "8001") {
					store = "Outlet";
				}
				var parentId = $(this) .parents('[id]') .get(0) .id;
				var param1 = 'banner-_-' + parentId + '_' + store + '-_-';
				var linkContent;
				if ($(this) .children('img') .length > 0 && $(this) .children('img') .attr('alt') && $(this) .children('img') .attr('alt') .length > 0) {
					linkContent = $(this) .children('img') .attr('alt');
					linkContent = linkContent.replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				} else if ($(this) .children('img') .length > 0) {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				} else {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				
				var banner_val = param1 + linkContent;
				$(this).click(function () {
					//for omniture
					if( $('#omni_re').length > 0 ){
						naf.omni.sendReEvent(banner_val);
					}		
					//for coremetrics
					cmSpFromCmRE($(this).attr('href'), banner_val, '');
				});
			});
			
			$('#siteWideMsg a') .one('mouseover', function () {
				var store = "REI";
				if (storeId == "8001") {
					store = "Outlet";
				} 
				else if (storeClass == "adv") {
					store = "ADV"
				}
				var param1 = 'banner-_-sitewide_' + store + '-_-';
				var linkContent;
				if ($(this) .children('img') .length > 0 && $(this) .children('img') .attr('alt') && $(this) .children('img') .attr('alt') .length > 0) {
					linkContent = $(this) .children('img') .attr('alt');
					linkContent = linkContent.replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				else if ($(this) .children('img') .length > 0) {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				else {
					linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
				}
				
				var other_val = param1 + linkContent;
				$(this) .attr('manual_cm_re', other_val);
				
				$(this).click(function() {
					//for omniture
					if($('#omni_re').length > 0){
						naf.omni.sendReEvent(other_val);
					}
					
					//for coremetrics
					cmSpFromCmRE($(this).attr('href'), param1 + linkContent, '');
				});
			});
		}
	}
};

var makeCmSpDone = {};
function makeOneCmSp(o) {
	if (windowOnloadDone == true) {
		if (! makeCmSpDone[o]) {
			makeCmSpDone[o] = true;
			cmSpMouseover.apply($(o));
		}
	}
}



function cmSpMouseover() {
	var parentId1 = $(this) .parents('[id = "headerWrapper"],[id = "footerWrapper"],[id = "socialFtr"]') .get(0) .id;
	var parentId2 = $(this) .parents('[id]') .get(0) .id.replace('hunt3', 'subNav') .replace('hunt2', 'navTabs') .replace('hunt1', 'utilityLinks');
	var linkContent;
	if ($(this).hasClass('catList')) {
		var index = $(this) .attr('class') .indexOf(' ');
		linkType = $(this) .attr('class') .substring(index + 1);
		linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
	} else if ($(this).parent('li').parent('ul').siblings('a').hasClass('catList')) {
		var catListObj = $(this).parent('li').parent('ul').siblings('a.catList');
		var index = catListObj.attr('class') .indexOf(' ');
		linkType = catListObj.attr('class') .substring(index + 1) + ':' + $(this).parent('li').parent('ul').children('.subTitle') .text();
		linkType = linkType.replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:]/g, '');
		linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
	} else if ($(this) .children('img') .length > 0) {
		linkType = 'image';
		linkContent = $(this) .children('img') .attr('alt');
		linkContent = linkContent.replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
	} else {
		linkType = 'text';
		linkContent = $(this) .text() .replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
	}
	var store = "REI_";
	if (storeId == "8001") {
		store = "Outlet_";
	}
	
	var cm_sp_val = store + parentId1 + ':' + parentId2 + '-_-' + linkType + '-_-' + linkContent;	
	$(this) .attr('manual_cm_sp', cm_sp_val);
	
	$(this).click(function() {
		naf.omni.sendSpEvent(cm_sp_val);
	});
	
	
} 

$(document) .ready(function () {
	if (! cm_page_name) {
		var cm_page_name;
		if ($('body') .attr('id')) {
			cm_page_name = $('body') .attr('id');
			cm_page_name = cm_page_name.replace(/ /g, ':') + '::' + $('h1') .text();
			cm_page_name = cm_page_name.replace(/[^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]/g, '');
		} else if ($('body') .attr('class') .length > 1) {
			cm_page_name = storeClass + ':' + sectionClass + ':' + pageClass;
		} else {
			cm_page_name = window.pathname;
		}
	}
	if (! cm_page_cat) {
		var cm_page_cat;
		if (sectionClass) {
			cm_page_cat = sectionClass;
		}
	}
});
