(function($) {
	var shown = false;
	var level1Expanded = false;
	var level2Expanded = false;
	var level3Expanded = false;
	var level4Expanded = false;
	var level5Expanded = false;
	$.fn.expandCollapseAll = function(options) {
		var opts = $.extend( {}, $.fn.expandCollapseAll.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend( {}, opts, $this.data()) : opts;
			initGroup(o, $(this));
		});
	}

	function getBaseName(href) {
		if (href) {
			var arr = href.split('/');
			return href.replace(/^http:\/\/[\w\.]+/, '');
		} else {
			return false;
		}
	}

	function initGroup(o, obj) {
		var hasCurrPage = false;

		//get the browser url
		var href = document.location.href ? document.location.href
				: document.location;
		//get the filepath (without domain)
		var basename = getBaseName(href);
		/*
		 * find the group with the selected page and make it visible 
		 */
		
		if (basename && basename.indexOf("/s/") == -1) {
			$kids = obj.next('ul').find('li a');
			$.each($kids, function() {
				$kid = $(this);
				var filename = getBaseName($kid.attr('href')).replace(' ','%20');
				
				//does the span contain the selected page?
					var match = false;
					if(basename.indexOf(";") != -1)
					{
						var shopMenu = basename.substr(basename.indexOf(";"), basename.length - basename.indexOf(";"));
						var tempBase = shopMenu;
						shopMenu = shopMenu.substr(shopMenu.lastIndexOf("/"), shopMenu.length - shopMenu.lastIndexOf("/"));

						var temp1 = filename.toLowerCase();
						var temp2 = shopMenu.toLowerCase();
						
						if((temp1.indexOf(temp2) != -1 ) && tempBase.indexOf('/') != tempBase.lastIndexOf("/"))
							match = true;
						
					}else
						if (filename.toLowerCase() == basename.toLowerCase()) 
							match = true;
					
					if (match) { //yes (open the group)
						$ancestors = $kid.parents('ul');
						count = 0;
						hasCurrPage = true;
						$.each($ancestors, function() {
							$ancestor = $(this);
							if ($ancestor.attr('id') == '') {
								$span = $ancestor.prev('span');
								//change the +/- icon
								$span.addClass(o.currGroupClass);
								//underline the select page
								$kid.addClass(o.currPageClass);
							}
						});
					}
				});
		}
		/*
		 * close all groups that don't contain the selected page
		 */
		if (!hasCurrPage) {
			//close the group
			obj.next('ul').hide();
			
			//bind mouseout (hover) event
			obj.mouseout(function() {
				$(this).removeClass(o.mouseoverClass);
			});
			//bind onclick event
			obj.click(function() {
				//change the +/- icon
					$(this).toggleClass(o.expandedClass);
					//toggle the ul visibility
					$(this).next('ul').toggle(o.toggleSpeed);
				});
		}
	}

	$.fn.expandCollapseAll.defaults = {
		mouseoverClass : 'mouseover',
		expandedClass : 'expanded',
		currGroupClass : 'on',
		currPageClass : 'underline',
		toggleSpeed : 'fast'
	}
})(jQuery);
