$(document).ready(function() {
	//hide everything but .navItem
	//$('#NavigationSecondary .parent').hide();
	$('#NavigationSecondary .navChildren').hide();
	$('#NavigationSecondary .navChildren ul').hide();

	var urlKey = '';
	var currentURLArray;
	var parentDirectory;
	
	if (document.URL.indexOf('/dev/') != -1) {
		currentURLArray = document.URL.split('/dev/');
		urlKey = '/' + currentURLArray[1];
	} else {
		currentURLArray = document.URL.split('/');
		urlKey = '/';
		for (var i = 3; i < currentURLArray.length; i++) { // i=3 skipping http://www.domain.com. Assumption is that all links in the left nav are relative so we can properly compare
			if (i != (currentURLArray.length - 1)) { //don't want a trailing slash at the end of the url
				urlKey += currentURLArray[i] + '/';
			} else {
				urlKey += currentURLArray[i];
			}
		}
	}

	if(urlKey.indexOf('?') != -1) { //strip out any query string args so it doesn't mess up the match
		var tempArr= urlKey.split('?');
		urlKey = tempArr[0];
	}

	var splitUrlKey = urlKey.split('/');
	var parentDirectory = splitUrlKey[splitUrlKey.length - 2]; //parent directory
	var $link = $('#NavigationSecondary a[href="' + urlKey + '"]');

	if($link.length == 0) { //orphan!
		var parentDirectory = findParentDirectory(urlKey); //CHANGE
		$link = $('#NavigationSecondary #' + parentDirectory);
	}

	if((urlKey.indexOf('/products/') != -1) && (urlKey.indexOf('/accessories/') != -1)) {
		if(urlKey.indexOf('/products/voice/') != -1) {
			$('#voice_index').next('ul.parent').show(); //open up entire section
			$('#voice_index').parent().addClass('open'); //change arrow
			$('#voice_index').next('ul.parent').children('li').children('#accessories_index').addClass('active');
		} else if (urlKey.indexOf('/products/telepresence_video/') != -1) {

			if( (window.location.hostname == "www.polycom.com") || (window.location.hostname == "www.polycom.co.uk") || (window.location.hostname == "www.polycom.eu") || (window.location.hostname == "www.polycom.asia") ||(window.location.hostname == "www.polycom.co.in") ) {
				$('#hd_telepresence_video_index').next('ul.parent').show(); //open up entire section
				$('#hd_telepresence_video_index').parent().addClass('open'); //change arrow
				$('#hd_telepresence_video_index').next('ul.parent').children('li').children('#accessories_index').addClass('active');
			} else {
				$('#telepresence_video_index').next('ul.parent').show(); //open up entire section
				$('#telepresence_video_index').parent().addClass('open'); //change arrow
				$('#telepresence_video_index').next('ul.parent').children('li').children('#accessories_index').addClass('active');
			}
		}
	} else {
		if($link.length > 1) { //does the link show up multiple times in the left nav? BOTH as a child AND grandchild.
			if(($link.parents('.parent').length  > 0) && ($link.parents('.navChildren').length > 0) ) {
				$.each($link, function(key, value) {
					if( $(this).attr('rev') != 'yes') {
						$(this).parents('.parent').show(); //open up entire section
						$(this).parents('.parent').parent().addClass('open'); //change arrow
						$(this).parents('.navChildren').parent().addClass('open'); //change plus to minus
						$(this).parents().children('.navChildren').show(); //open up the grandchild node
						$(this).parent().children('.navChildren a').addClass('active'); //change the link to red
						/*
						$link.parents('.parent').show(); //open up entire section
						$link.parents('.parent').parent().addClass('open'); //change arrow
						$link.parent().addClass('open'); //change plus to minus
						$link.parents().children('.navChildren').show(); //open up the grandchild node
						$link.parent().children('.navChildren a').addClass('active'); //change the link to red
						*/
					}
				}); //end each
			} //end if
		} else {
			if ($link.parent('.navItem').length != 0) { //top level item i.e. 'About Us'		
				//alert('in if')
				if($link.parent().children('.parent').length != 0) { //top level item has children
					//$('a[href="' + urlKey + '"]+ul.parent').show();
					$link.parent().children('ul.parent').show();
					$link.parent().addClass('open'); //change arrow				
				}
				$link.addClass('active'); //change the link to red
			} else if($link.parent().children('.navChildren').length > 0) { //second level items that have children i.e. 'Executive Team'
				//alert('in else if')
				$link.parents('.parent').show(); //open up entire section
				$link.parent().children('.navChildren').show();
				$link.parents('.parent').parent().addClass('open'); //change arrow			
				$link.parent().addClass('open'); //change plus to minus
				$link.addClass('active'); //change the link to red
			} else {
				//alert('in else')
				$link.parents('.parent').show(); //open up entire section
				$link.parents('.navItem .navChildren').show();
				$link.parent().children('ul').show(); //4th level links
				if($link.children().length > 0) { //there are top level items (i.e. products A-Z that don't have children so don't change the arrow);
					$link.parents('.parent').parent().addClass('open'); //change arrow			
				}
				$link.parents().addClass('open'); //change plus to minus
				$link.addClass('active'); //change the link to red
			}
		} //end else
	} //end else

	function findParentDirectory(urlKey) {
		//alert('orpan!');
		var parentFound = false;
		var arraySearchIndex = 2; //start at -2 because -1 == filename, -2 == parent directory
		currentURLArray = urlKey.split('/');

		while( (parentFound != true) && (arraySearchIndex != currentURLArray.length) ){
			if($('#NavigationSecondary #' + (currentURLArray[currentURLArray.length - arraySearchIndex] + '_index')).length != 0) {
				parentFound = true;
				break;
			} else {
				arraySearchIndex = arraySearchIndex + 1;
			}
		}
		return currentURLArray[currentURLArray.length - arraySearchIndex] + '_index';
	}
	
}); // ###end document.ready###
