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;
			var catID = c.replace(/ /g, '_');
			catID = catID.replace(/\(/g,'');
			catID = catID.replace(/\)/g,'');
			category.id = catID;
			category.href = '/_catalog_' + thisVal + '/' + category.id;
			category.parent = '';
			category.generation = '';

			//if there were 6 hypens this means that it was a subcategory
			if(x == 6 || x == 9){
				if(x == 6)
					category.generation = 2;
				else if(x == 9)
					category.generation = 3;
				//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 != ''){
					if(categories[categories.length - 1].generation == category.generation)
						category.parent = categories[categories.length - 1].parent;
					else if(categories[categories.length - 1].generation == category.generation - 1)
						category.parent = categories[categories.length - 1].id;
					else if(categories[categories.length - 1].generation == category.generation + 1){
					
						for(j=categories.length -1; j>0; j--){
							
							if(categories[j].generation == category.generation){
								category.parent = categories[j].parent;
								break;
							}
						}
					}
				}
				//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
			//add the category to the array (just supporting 2 levels of navigation for now)
			if(x <= 9)
				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,'_');
				matchthis = (matchthis).replace(/\(/g,'');
				matchthis = (matchthis).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
				if(categories[k].parent != ''){
					
					var parentLi = jQuery('#' + targetObj + ' ul #' + categories[k].parent + ' ul.' + categories[k].generation);
					
					if(jQuery(parentLi).length == 0)
						jQuery('#' + targetObj + ' ul #' + categories[k].parent).append('<ul class="' + categories[k].generation + '"><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.' + categories[k].generation).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');
		}
	}
		   
});

