 /*
 * jQuery Active Session 1.1
 * For use with BigCommerce hosted sites ONLY
 * Author: Ian Hallworth
 * http://www.hallworthdesign.co.uk
 * Date: 26 Oct 2010
 * Depends on: jQuery core - http://docs.jquery.com/Downloading_jQuery
 * Depends on jquery.ba-nth-last-child.min.js - http://benalman.com/projects/jquery-misc-plugins/
 */
 
 $(document).ready(function(){ 
	// get the url
	var $path = location.pathname; 
	// initialise variables 
	var $currentPage, $WhichList; 
	// Get the text of the last item in the first breadcrumb list
	var $breadcrumbLast = $('.Breadcrumb ul:first-child li:last').text();
	// Get the text of the penultimate item in the first breadcrumb list
	var $breadcrumbPenultimate = $('.Breadcrumb ul:first-child li:nth-last-child(2)').text();
	
	// uncomment the line below to test what you're actually getting
	// alert("Penultimate Item = " + $breadcrumbPenultimate + "Last Item=" + $breadcrumbPenultimate);
	
	// products always contain /products/ in the url string so look for it
	if ($path.match('/products/')) {
		// we have a match, get the *penultimate* list item from the first list
		// this will give you the brand or category name
		$currentPage = $breadcrumbPenultimate;
		$WhichList = '#SideCategoryList';
	}
	else if ($path.match('/categories/')){
		// if it's category page, get the last breadcrumb item 
		$currentPage = $breadcrumbLast;
		// target the category list
		$WhichList = '#SideCategoryList';
	}
	else if ($path.match('/brands/')){
		// if it's brand page, get the last breadcrumb item
		$currentPage = $breadcrumbLast
		// target the shopbybrand list
		$WhichList = '#SideShopByBrand';
	}
	
	else if ($path.match('/sitemap')){
		// if it's sitemap page, get the penultimate breadcrumb item
		$currentPage = 'Site Map';
		// target the HelpSubPageList list
		$WhichList = '#HelpSubPageList';
		
	}
	
	else if ($path.match('/pages/')){
		// get last breadcrumb item
		$currentPage = $breadcrumbLast;
		// If we are looking at a top level page
		if ( ($currentPage == "About")|| ($currentPage == "Help") ){
			$WhichList = '#'+$currentPage+'SubPageList';
		}
		// if we're on a sub page 
		else {
			// get the parent breadcrumb
			$currentParent = $breadcrumbPenultimate;
			$WhichList = '#'+$currentParent+'SubPageList';
			// alert($WhichList);
		}
	}
	else if ($path.match('/news/')){
		// if it's news page, get the penultimate breadcrumb item
		$currentPage = $breadcrumbPenultimate;
		// target the shopbybrand list
		$WhichList = '#AboutSubPageList';
	}
	
	else if ($path.match('/account.php')){
		// if it's account page, get the last breadcrumb item
		if ($breadcrumbLast){
		$currentPage = $breadcrumbLast;
		// target the SideAccountMenu list
		$WhichList = '#SideAccountMenu';
		}
	}
	
	else if ($path.match('/search.php')){
		// if it's account page, get the last breadcrumb item
		if ( $breadcrumbLast ){
		$currentPage = $breadcrumbLast;
		// target the HelpSubPageList list
		$WhichList = '#HelpSubPageList';
		}
	}
	
	// go through every item in the chosen list
	$($WhichList+' ul li').each(function() {
		// get the text for the list item 
		text = $(this).text();
		if (text.match($currentPage) ) {
			// Add a class to the list item if you find a text match and return
			return $(this).addClass('ActiveSection');
		}
	})	
});

