// bcNav - to establish navigating context for category menu
// version: 1.6.0 (concept, major, minor, including bug fixes)

( function($) {
	$.fn.bcNav = function(options) {

		if( this.length === 0 ) {
			return;
		}

		var settings = {
			'followurl'     	: true,
			'replaceheader' 	: true,
			'headingtype'   	: 'h2',
			'context'       	: null,
			'parentclass'   	: 'SideCategoryListClassic',
			'keepclass'     	: false,
			'removeheading' 	: false
		};
		options = $.extend( settings, options );
		options.parentclass = '.' + options.parentclass;
		
		// Globals
		var bc_globals = {};
			bc_globals.level = 2;
			bc_globals.menu_id = this.attr('id');
		
		return this.each(function(i, e) {        
			// check if URI has valid location, if this returns 1 we exit (not valid)
			if( checkLevel() ) return;
			
			var menu_element = findTarget(bc_globals.level);
			//menu_element instanceof $);
			var _menu = $(menu_element);
			
			// Exit if no match (error, this shouldn't happen however does on Safari)
			if( bc_globals.match == false ) { 
				return; 
			} else if( _menu.children('ul').length == 0 ) {
				// if this is true we've reached the end of the tree
				// if root is up one level do nothing
				if( bc_globals.current_level == 3 ) { 
					return; 
				} else { 
					menu_element = menu_element.parentNode.parentNode; 
				}
				_menu = $(menu_element);
			}
			
			_menu = _menu.clone(true);
			var header_txt = _menu.find("a:first").html();
			_menu.children('ul:first').css({'display':'block','visibility':'visible'});
			_menu.find("a:first").remove();
		
			if( options.keepclass ) {
				$(this).find(options.parentclass + ' ul').attr('class');
				_menu.children("ul:first").addClass(
					$(this).find(options.parentclass + ' ul').attr('class')
				);
			}
		
			if( options.replaceheader ) {
				$(this).find(options.headingtype + ':first').empty();
				$(this).find(options.headingtype + ':first').html(header_txt);
			}
			if( options.removeheading ) {
				$(this).find(options.headingtype + ':first').remove();
			}
	
			// Insert new menu
			$(this).find(options.parentclass + ' ul').remove();
			$(this).find(options.parentclass).prepend(_menu.html());
	
			//_menu.css({'display':'block','visibility':'hidden'});
			//$(this).css({'display': 'block', 'visibility':'visible'});
			_menu = null;

    		});

		function checkLevel() {
			var _loc = getLocation();
			var tmp_location = _loc[_loc.length - bc_globals.level];
		
			// if we are on the root category page, not subcategory, exit
			// also if the location object doesn't a URI from the category or product pages, exit
			if( tmp_location == _loc[1] || _loc.length - bc_globals.level < bc_globals.level ) return 1;
		}

		function format( format_str ) {
			//var tmpstring = encodeURIComponent(format_str);
			//var tmpstring2 = decodeURIComponent(format_str);
			//return format_str.replace(/-/g," ");
			//return decodeURIComponent(format_str);
			return format_str;
		}
		
		function getLocation() {
		    if( options.context ) {
				var tmp_loc = options.context;
			} else {
				// create category page, or product page loc object (product page based off breadcrumb)
				var tmp_loc = "";
				if( location.pathname.split("/")[1] == "products" ) {
					// products page - derive location object via breadcrumb
					if( $("#ProductBreadcrumb ul").length == 0 ) {
						// product breadcrumb isn't enabled
						tmp_loc = "/0123456789abcdef";
					} else {
						var href = $("#ProductBreadcrumb ul:first li:nth-child(2) a").attr('href');
						strip_txt = getURLHostString(href);
						href = href.replace(strip_txt, "");
						tmp_loc = href
					}
				} else if( location.pathname.split("/")[1] == "categories" ) {
					// category page
					tmp_loc = location.pathname;
				} else {
					// couldn't match a product or category page, insert a mock URL
					tmp_loc = "/0123456789abcdef";
				}
			}
			return tmp_loc.split("/");
		}
		function getURLHostString(a) {
			var proto = a.substring(0, a.indexOf("/")) + "//";
			var host = a.replace(proto, "");
				host = host.substring(0, host.indexOf("/"));
			var strip_txt = proto + host;
			
			return strip_txt;
		}
		function formatHREF( href, level ) {
			var strip_txt = getURLHostString(href);
			
			var sub = href.replace(strip_txt, "");
			var sub_split = sub.split("/");
			return sub_split[level];
		}
		// find <a> element who's innerHTML is equal to the location
		function findTarget( level ) {
		
			bc_globals.match = false;
			bc_globals.current_level = level;
			var loc_array = getLocation();
			var target_el = document.getElementById(bc_globals.menu_id);
			var supportsNodeIterator = typeof document.createNodeIterator == "function";
			if( supportsNodeIterator ) {
			
				var filter = function(node){
					return node.tagName.toLowerCase() == "a" ?
						NodeFilter.FILTER_ACCEPT :
						NodeFilter.FILTER_SKIP;
					};
				for( a=level; a <= loc_array.length - level; a++ ) {

					//var re = new RegExp('^' + format( loc_array[a] ) + '$');
					var re = new RegExp('^' + format( loc_array[a] ) + '$');
					var iterator = document.createNodeIterator(target_el, NodeFilter.SHOW_ELEMENT,filter, false);
					var node = iterator.nextNode();
					while (node !== null) {
						var href_txt = formatHREF( node.href, bc_globals.current_level );
						//var txt = ( typeof node.innerText == "undefined" ) ? node.textContent : node.innerText; //node.innerHTML;
						//var a_href = node.href;
						if( href_txt.match(re) ) {
							bc_globals.match = true;
							target_el = node.parentNode;
							break;
						} else {
							node = iterator.nextNode();
						}
					}
					bc_globals.current_level++;
				}
			} else { // NodeFilter and NodeIterator not supported by IE. support IE here by using jquery
				var $_target_el = $(target_el);
				// tmp_match is to prohibit further matching, see below
				var tmp_match = false;
				for( a = level; a <= loc_array.length - level; a++ ) {
					var re = new RegExp('^' + format( loc_array[a] ) + '$');
					current_context_level = bc_globals.current_level;
					
					// remember, every single occurance of an 'a' tag will run this function, so when first match is found, prohibit further matching.
					$_target_el.find('a').each( function() {
						var href_txt = formatHREF($(this).attr('href'), bc_globals.current_level);
						if( href_txt.match(re) ) {
							// tmp_match is to prohibit further matches for the this function.
							// we are assuming first match is always the correct one, for the current level
							if( !tmp_match ) {
								$_target_el = $(this).parent();
								target_el = this.parentNode;
								tmp_match = true;
							}
							// if we are at the end of the loop with a match, set the global match variable to true
							if( a == loc_array.length - level ) {
								bc_globals.match = true;
							}
						}
					});
					bc_globals.current_level++;
					tmp_match = false;
				}
			}
			return target_el;
		};
	};
})(jQuery);



