 /*
 * 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, $WhichMenu; 
	// 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();
        var $breadcrumbPenultimate2 = $('.Breadcrumb ul:first-child li:nth-last-child(3)').text();
	
	// uncomment the line below to test what you're actually getting
	 // alert(" Penultimate2 Item = " + $breadcrumbPenultimate2 + ", Penultimate Item = " + $breadcrumbPenultimate + ",  BreadcrumbLast =" + $breadcrumbLast);
	
	// 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;
                $lastPage = $breadcrumbPenultimate2;
		$WhichList = '#SideCategoryList';
                $WhichMenu = '#Menu';
	}
	else if ($path.match('/categories')){
		// if it's category page, get the last breadcrumb item 
		$currentPage = $breadcrumbLast;
        $lastPage = $breadcrumbLast;
		// target the category list
        if ($path.match('/categories/')){
		   // if it's category page, get the last breadcrumb item 
                  if ($breadcrumbPenultimate != "Home") {
                    $currentPage = $breadcrumbLast;
					$lastPage = $breadcrumbPenultimate;
                  }
				  
				  if ($breadcrumbPenultimate == "Home") { $lastPage = $breadcrumbPenultimate;}
				  if ($breadcrumbPenultimate2 == "Home") { $currentPage = $breadcrumbLast; $lastPage = $breadcrumbPenultimate;}
				  
				  
                }

		$WhichList = '#SideCategoryList';
        $WhichMenu = '#Menu';
	}
       
       // else if ($path.match('/categories/Gifts/')){
		// if it's category page, get the last breadcrumb item 
		//$currentPage = $breadcrumbLast;
                //$lastPage = $breadcrumbPenultimate;
		// target the category list
		//$WhichList = '#SideCategoryList';
                //$WhichMenu = '#Menu';
	//}

	else if ($path.match('/brands/')){
		// if it's brand page, get the last breadcrumb item
		$currentPage = $breadcrumbLast
		// target the shopbybrand list
		$WhichList = '#SideShopByBrand';
	}

        
	
	// 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)) && ($currentPage != $lastPage))  {
			// Add a class to the list item if you find a text match and return
			return $(this).addClass('ActiveSection');
		}
	})

	// go through every item in the chosen list
	$($WhichMenu+' ul li').each(function() {
		// get the text for the list item 
		text = $(this).text();
		if ((text.match($currentPage)) || (text.match($lastPage)))  {
			// Add a class to the list item if you find a text match and return
			return $(this).addClass('ActiveSection');
		}
	})
	
       	
});

