var targetObj;
var baseSSL;


jQuery(document).ready(function() {
	
	
	baseSSL = baseSSL.replace("https:","http:");
	
	//all the options in the dropdown
	var categoryOptions = jQuery('#CAT_ProductCatalogue option');
		
	var categories = new Array();
	
	for(i = 0; i < categoryOptions.length; i++){
		//iterate through the categories

		var thisName = (jQuery(categoryOptions[i]).html());
		var thisVal = (jQuery(categoryOptions[i]).val());
		//this is any sub category of the 'Gifts Category' main parent
		if(thisName.indexOf('---') != -1){

			var category = {};
			var c = thisName;
			var x = 0;
			//strip out the hyphens while counting them
			while (c.charAt(0)=='-'){
				c = c.substring(1,c.length);
				x++;
			}
			//create a hyper link to the catalogue
			category.name = c;
			category.id = c.replace(/ /g, '_');
			category.href = '/_catalog_' + thisVal + '/' + category.id;

			//if there were 6 hypens this means that it was a subcategory
			if(x == 6){
				//if the last item pushed to the array is also a sub category
				//then inherit it's parent value
				if(categories[categories.length - 1].parent != '')
					category.parent = categories[categories.length - 1].parent;
				//otherwise get the parent value from the last item's ID
				else
					category.parent = (categories[categories.length - 1].id);
			}
			//this category is not a subcategory and has no parent
			else if(x == 3)
				category.parent = '';
			//add the category to the array (just supporting 2 levels of navigation for now)
			if(x <= 6)
				categories.push(category);
		}
	}
	
	
	//now we have all the categories with their names links and parent child relations
	//let's build the navigation control...

	//this breadcrumb trail will tell us where we are in the navigation
	var selectedLevels = jQuery('#ShoppingBreadcrumb a');
	
	for(k=0;k<categories.length;k++){
		//this is the object which the navigation items will be added to
		var NavMenu = jQuery('#' + targetObj + ' ul');
		//the currrent category is a top level category and can be added directly as a list item
		if(categories[k].parent == '')
			jQuery(NavMenu[0]).append('<li id="' + categories[k].id + '" class="off"><a href="' + baseSSL + categories[k].href + '">' + categories[k].name + '</a></li>');
		//this is a sub category so we need to decide whether or not to expand it
		else{
			//set the flag
			var isDisplay = false;
			//go through the breadcrumb links
			for(m = 0; m < selectedLevels.length; m++){
				var matchthis = (jQuery(selectedLevels[m]).html()).replace(/ /g,'_');
				//see if the breadcrumb link matches the current category's parent
				//if it does then it means that the category's parent has been selected
				//so we can expand the sub categories
				if(matchthis == categories[k].parent)
					isDisplay = true;
					
				if(m == selectedLevels.length -1){
					lastSelectedObj = jQuery('#' + targetObj + ' ul').find('#' + matchthis);
					jQuery(lastSelectedObj).attr('class','selected');
					jQuery(lastSelectedObj).parents('li').attr('class','selected');
				}
			}

			if(isDisplay){
				//check if a subcategory list has already been created
				var parentLi = jQuery('#' + targetObj + ' ul #' + categories[k].parent + ' ul');
				//if not create one and add the first item to it
				if(jQuery(parentLi).length == 0)
					jQuery('#' + targetObj + ' ul #' + categories[k].parent).append('<ul><li id="' + categories[k].id + '" class="off"><a href="' + baseSSL + categories[k].href + '">' + categories[k].name + '</a></li></ul>');
				//a list has alredy been created to add an item to it
				else
					jQuery('#' + targetObj + ' ul #' + categories[k].parent + ' ul').append('<li id="' + categories[k].id + '" class="off"><a href="' + baseSSL + categories[k].href + '">' + categories[k].name + '</a></li>');
			
			}
		}
	}
	//finally set the selected classes on the selected item and it's parent links
	for(m = 0; m < selectedLevels.length; m++){
		if(m == selectedLevels.length -1){
			lastSelectedObj = jQuery('#' + targetObj + ' ul').find('#' + matchthis);
			jQuery(lastSelectedObj).attr('class','selected');
			jQuery(lastSelectedObj).parents('li').attr('class','selected');
		}
	}
		   
});
