function get_breadcrumb_value() {
	var breadcrumb_value = '';
	if($('cat_id')) {
		breadcrumb_value += $('cat_id').value;
	}
	breadcrumb_value += '_';
	if($('subcat_id')) {
		breadcrumb_value += $('subcat_id').value;
	}
	breadcrumb_value += '_';
	if($('pg_id')) {
		breadcrumb_value += $('pg_id').value;
	}
	breadcrumb_value += '_';
	if($('brand_id') && $('brand_id').type == 'hidden') {
		breadcrumb_value += $('brand_id').value;
	}
	breadcrumb_value += '_';
	if($('brand_cat') && $('brand_cat').value) {
		breadcrumb_value += 'BC';
	}
	var location = window.location.href;
	if(location.match('/sale/') || location.match('/on_sale.html')) {
		breadcrumb_value += 'S';
	}
	return breadcrumb_value;
}
function set_nav_onclicks() {
	var breadcrumb_value = get_breadcrumb_value();
	var path, breadcrumb_fxn;
	var cross_sell = ['ASO', 'BD', 'BS', 'FP', 'PWAB', 'PWAG', 'RI'];

	var aryOmnDiv = ['header_tracking', 'left_nav_tracking', 'footer', 'homepagecategories', 'subcats', 'bread_crumb', 'subcat_wrapper', 'prod_group_wrapper']; //container divs
	for(var outside_i in aryOmnDiv) {
		var div	= $(aryOmnDiv[outside_i]);
		if(div && div.getElementsByTagName) {
			var anchors = div.getElementsByTagName('a'); //anchors inside divs
			var track_category = aryOmnDiv[outside_i];
			track_category = track_category.replace(/_tracking/g, ''); //taking out postfix
			for (var i = 0; i < anchors.length; i++) { 
				if(anchors[i].getAttribute('name')) {
					var anchor_name= anchors[i].getAttribute("name");
					var anchor_onclick = anchors[i].getAttribute("onclick") || '';
					var split_result = anchor_name.split('__');
					var nav_path_var = split_result[1];
					var set_tracking = '';
					if(catalog == 'store') {
						var tr_template_var = split_result[0].replace(/'/g, '');
						if(track_category == 'homepagecategories') {
							track_category = 'Home Category';
						}
						else if(track_category == 'subcats') {
							track_category = 'Category';
						}
						else if(track_category == 'subcat_wrapper') {
							track_category = 'Subcat';
						}
						else if(track_category == 'prod_group_wrapper') {
							track_category = 'Product Group';
						}

						var set_tracking = "template_links_cookie('tr_template', '" + track_category + ": " +  tr_template_var + "');";
					}
					//setting onclicks
					if(!nav_path_var) {
						anchors[i].setAttribute('onclick', set_tracking + anchor_onclick);
					}
					else {
						path = '/' + catalog + '/' + nav_path_var + '/';
						path = path.replace(/_/g,'/');
						breadcrumb_fxn = "_setCookie('nav_path', '" + breadcrumb_value + "', null, '" + path + "');";
						if(track_category == 'left_nav') {
							breadcrumb_fxn = "_setCookie('nav_path', '____', null, '" + path + "');";
						}
						anchors[i].setAttribute('onclick', set_tracking + breadcrumb_fxn + anchor_onclick);
					}
					if(navigator.appName == 'Microsoft Internet Explorer') {
						anchors[i].parentNode.innerHTML = anchors[i].parentNode.innerHTML;
					}
				}
			}
		}
	} 

	for(var i in cross_sell) {
		var csp_elements = document.getElementsByName(cross_sell[i]);
		for(var j=0; j<csp_elements.length; j++) {
			var sku = csp_elements[j].className;
			path = '/' + catalog + '/' + sku + '/';
			var track_fxn = "_setCookie('tr_cross_sell', '" + cross_sell[i] + '_' + sku + "', '1', '" + path + "'); _setCookie('nav_path', '" + breadcrumb_value + "', null, '" + path + "');";   //1 is day expires
			csp_elements[j].setAttribute('onclick', track_fxn);
			if(navigator.appName == 'Microsoft Internet Explorer') {
				csp_elements[j].parentNode.innerHTML = csp_elements[j].parentNode.innerHTML;
			}
		}
	}
	var nav_elements = document.getElementsByName('nav_link');
	for(i=0; i<nav_elements.length; i++) {
		path = '/' + catalog + '/' + nav_elements[i].className + '/';
		path = path.replace(/_/g,'/');
		breadcrumb_fxn = "_setCookie('nav_path', '" + breadcrumb_value + "', null, '" + path + "');";
		nav_elements[i].setAttribute('onclick', breadcrumb_fxn);
		if(navigator.appName == 'Microsoft Internet Explorer') {
			nav_elements[i].parentNode.innerHTML = nav_elements[i].parentNode.innerHTML;
		}
	}
	var pdp_elements = document.getElementsByName('pdp_link');
	for(i=0; i<pdp_elements.length; i++) {
		path = '/' + catalog + '/' + pdp_elements[i].className + '/';
		path = path.replace('_','/');
		breadcrumb_fxn = "_setCookie('nav_path', '" + breadcrumb_value + "', null, '" + path + "');";
		pdp_elements[i].setAttribute('onclick', breadcrumb_fxn);
		if(navigator.appName == 'Microsoft Internet Explorer') {
			pdp_elements[i].parentNode.innerHTML = pdp_elements[i].parentNode.innerHTML;
		}
	}

	// QAndA GearQuestions
	if ($('questions')) {
		// Get all questions listed on the page
		var prop44, // All questions shown 'q:316,q:450,q:618'
			prop28, // 'QA:Men's Clothing:316:flag'
			eVar35, // Question the user clicked on.  'q:450'
			association,
			association_type;

		var questions = [];
		yd.getElementsBy( function(o) { return o.name === 'question_id' }, 'input', 'questions', function(o) {
			questions.push('q:' + o.value);
		});

		// Adds a nav_path cookie, and a special cookie for qanda
		var addQandACookies = function(el, page) {
			var breadcrumb = breadcrumb_value;
			ye.addListener(el, 'click', function(e) {
				var question = yd.getAncestorByClassName( el, 'question' );
				var question_id = yd.getElementsBy( function(o) { return o.name === 'question_id' }, 'input', question)[0].value;
				var association = yd.getElementsBy( function(o) { return o.name === 'default_association' }, 'input', question)[0].value;
				var pagename = s_sc.pageName.split(':')[1];
				eVar35 = 'q:' + s_sc.pageName + ':' + question_id;
				prop28 = ['q', pagename, question_id, page].join(":");

				path = '/' + catalog + '/' + association + '/';
				_setCookie('nav_path', breadcrumb, null, path);
				_setCookie('tr_template', prop28, null, path);
				omn_cookie('eVar35', eVar35);
				omn_cookie('eVar2', 'QA');
			});
		};
		// Title links
		yd.getElementsBy( function(o) { return o.parentNode.nodeName.toLowerCase() === 'h4' }, 'a', 'questions', function(o) {
			addQandACookies(o, 'title');
		});
		// 'Answer' link
		yd.getElementsBy( function(o) { return o.parentNode.className.toLowerCase() === 'answer' }, 'a', 'questions', function(o) {
			addQandACookies(o, 'answer');
		});
		// 'Flag' link
		yd.getElementsBy( function(o) { return o.parentNode.className.toLowerCase() === 'flag' }, 'a', 'questions', function(o) {
			addQandACookies(o, 'flag');
		});
		// Text-link
		yd.getElementsByClassName( 'text_link', 'a', 'questions', function(o) {
			addQandACookies(o, 'text');
		});
		// More link
		yd.getElementsBy( function(o) { return o.innerHTML === 'More &gt;' }, 'a', 'questions', function(o) {
			addQandACookies(o, 'more');
		});
	}

	breadcrumb_value += 'R';
	pdp_elements = document.getElementsByName('review_pdp_link');
	for(i=0; i<pdp_elements.length; i++) {
		path = '/' + catalog + '/' + pdp_elements[i].className + '/';
		path = path.replace('_','/');
		breadcrumb_fxn = "_setCookie('nav_path', '" + breadcrumb_value + "', null, '" + path + "');";
		if(pdp_elements[i].id == 'flytool_review') {
			breadcrumb_fxn += " return AddSwatchToURL(this, '');";
		}
		pdp_elements[i].setAttribute('onclick', breadcrumb_fxn);
		if(navigator.appName == 'Microsoft Internet Explorer') {
			pdp_elements[i].parentNode.innerHTML = pdp_elements[i].parentNode.innerHTML;
		}
	}
	var promo_elements = document.getElementsByName('promo_link');
	for(i=0; i<promo_elements.length; i++) {
		breadcrumb_fxn = "_setCookie('promo_nav', '" + promo_elements[i].id + "', null, '/');";
		promo_elements[i].setAttribute('onclick', breadcrumb_fxn);
		if(navigator.appName == 'Microsoft Internet Explorer') {
			promo_elements[i].parentNode.innerHTML = promo_elements[i].parentNode.innerHTML;
		}
	}
	return;
}


function template_links_cookie(name, value) {
		_setCookie(name, value, '1', '/');
		return true;
}

function template_links_tl(name, value) {
		var disableTrackEvents = ['PDP:view_large_image', 'PDP:swatch', ':large_image_scroll_left', 
			':large_image_scroll_right', 'PDP:di'];

		for (var i=0; i<disableTrackEvents.length; i++)
			if(disableTrackEvents[i] == value) return false;
		clearVars('pageName');
		var s_sc = s_gi(sc_account);
		s_sc.linkTrackVars = 'prop28';
		s_sc.prop28 = value;
		s_sc.tl(this,'o',"Filter Type");
		return true;
}

function track_mini_profile(name, value) {
		clearVars('pageName');
		var s_sc = s_gi(sc_account);
		s_sc.linkTrackVars = 'events,prop28';
		s_sc.linkTrackEvents = 'event56';
		s_sc.events = 'event56';
		s_sc.prop28 = value;
		s_sc.tl(this,'o',"Filter Type");
		return true;
}

function track_receipt_change_password() {
		clearVars();
		var s_sc = s_gi(sc_account);
		s_sc.linkTrackVars = 'events,eVar49';
		s_sc.linkTrackEvents = 'event51';
		s_sc.events = 'event51';
		s_sc.eVar49 = 'New User Password Change';
		s_sc.tl(this,'o',"Filter Type");
		return true;
}

// Unlike the above, sets a single cookie for multiple values.
// name is something like 'eVar35', 'prop44', etc
var omn_cookie_cache = [];
function omn_cookie(name, value) {
	var path = '/';
	var cookie_value = name + ':' + value;
	var cookie = omn_cookie_cache;
	// If the value exists, replace it
	var added;
	for (var i = 0; i < cookie.length; i++) {
		if (!cookie[i]) continue;
		var val = cookie[i].split(':')[0];
		if (val === name) {
			cookie[i] = cookie_value;
			added = true;
		}
	}
	if (!added) {
		cookie.push(cookie_value);
	}
	omn_cookie_cache = cookie;
	_setCookie('omn_cookie', cookie.join('|'), null, path || '/');
}

ye.addListener(window, 'load', set_nav_onclicks);
