
function loadMenus(xml) {

	// Load the logo image
	jQuery(xml).find('topnav').children().each(function(index, item){
		if (jQuery(item).attr('class') == "bn-logo") {
			if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 6) {
				jQuery("#logo").css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + jQuery(item).attr('src') + "',sizingMethod='crop');");
			} else {
				jQuery("#logo").css('background-image', 'url(' + jQuery(item).attr('src') + ')');
			}
		}
	});

	// Populate main nav
	jQuery('#mainnav').empty();
	jQuery(xml).find('mainnav').each(function(index, item){
		text = jQuery(item).attr('label');
		url = jQuery(item).attr('url');
		target = jQuery(item).attr('target');
		li = jQuery('<li/>').attr('id', text).appendTo(jQuery('#mainnav'));
		a  = jQuery('<a/>').appendTo(li);
		a.text(text).attr('href', url).attr('target', target);
	});
	// Populate main nav quicklinks.
	jQuery('.quicklinks-menu ul').empty();
	jQuery(xml).find('quicklinks').children().each(function(index, item){
		text = jQuery(item).attr('label');
		url = jQuery(item).attr('url');
		target = jQuery(item).attr('target');
		li = jQuery('<li/>').attr('id', text).appendTo(jQuery('.quicklinks-menu ul'));
		if (index == 0) li.addClass('first');
		a  = jQuery('<a/>').appendTo(jQuery(li));
		a.html(text + ' <img src="/images/content/bg_pager_next.gif" alt="more" />').attr('href', url).attr('target', target);
	});
	
	// Populate footer
	jQuery('#footernav').empty();
	var prevElement;
	jQuery(xml).find('footernav').each(function(index, item){
		text = jQuery(item).attr('label');
		url = jQuery(item).attr('url');
		target = jQuery(item).attr('target');
		li = jQuery('<li/>').appendTo(jQuery('#footernav'));
		a  = jQuery('<a/>').appendTo(li);
		a.text(text).attr('href', url).attr('target', target);
		prevElement = jQuery(item);
	});
	jQuery('<li/>').text(prevElement.next().text()).appendTo(jQuery('#footernav'));
	/*
	jQuery(xml).find('footer').children().children().each(function(index, item){
		jQuery('<li/>').text(jQuery(item).text()).appendTo(jQuery('#footernav'));
	});*/

	// Once the XML is loaded we can hook events to menu items
	// TODO: This should be dynamic since menu items can change.
	jQuery('#HOME').bind('mouseover', function() { jQuery(this).addClass('home-hover');    });
	jQuery('#HOME').bind('mouseout',  function() { jQuery(this).removeClass('home-hover'); });
	
	jQuery('#GAMES').bind('mouseover', function() { jQuery(this).addClass('games-hover');    });
	jQuery('#GAMES').bind('mouseout',  function() { jQuery(this).removeClass('games-hover'); });
	
	jQuery('#COMMUNITY').bind('mouseover', function() { jQuery(this).addClass('comm-hover');    });
	jQuery('#COMMUNITY').bind('mouseout',  function() { jQuery(this).removeClass('comm-hover'); });
	
	jQuery('#SUPPORT').bind('mouseover', function() { jQuery(this).addClass('support-hover');    });
	jQuery('#SUPPORT').bind('mouseout',  function() { jQuery(this).removeClass('support-hover'); });
	
	jQuery('#COMPANY').bind('mouseover', function() { jQuery(this).addClass('company-hover');    });
	jQuery('#COMPANY').bind('mouseout',  function() { jQuery(this).removeClass('company-hover'); });
	
	jQuery('#SHOP').bind('mouseover', function() { jQuery(this).addClass('shop-hover');    });
	jQuery('#SHOP').bind('mouseout',  function() { jQuery(this).removeClass('shop-hover'); });
}


var gameFinderItem = function() {
	this.element  = null;
	this.selected = false;
	this.tag      = '';
	this.filters  = [];
	
	this.initialize = function(node, container) {
		this.element = jQuery('<li/>');

		this.element.text(node.attr('label'));
		this.element.attr('title',node.attr('tag'));
		this.select(node.attr('selected'));
		this.tag      = node.attr('tag');
		this.filters  = node.attr('filters')? node.attr('filters').split(',') : '';

		// Since there are multiple containers, element will become multiple elements
		this.element.addClass('gfadding');
		this.element.appendTo(container);
		this.element = container.find('.gfadding');
		this.element.removeClass('gfadding');

		return this;
	}
	
	this.select = function(selected) {
		this.selected = selected;
		if (this.selected)	this.element.addClass('selected');
		else				this.element.removeClass('selected');
	}

	this.hover = function(hover) {
		if (hover)	this.element.addClass('gfhover');
		else		this.element.removeClass('gfhover');
	}
}

var gameFinderController = {
	menus: null,
	itemsStep1: [],
	itemsStep2: [],

	load: function(xml) {
		var self = this;

		this.menus = jQuery('.gamefinder');
		this.menus.find('ul').empty();

		// Step 1
		xml.find('step1').children().each(function(){
			var item = new gameFinderItem().initialize(jQuery(this), self.menus.find('.step1 ul'));
			item.index = self.itemsStep1.length;
			self.itemsStep1.push(item);

			item.element.hover(
				function(){ item.hover(true);  },
				function(){ item.hover(false); }
			);

			item.element.click(function(event){
				event.stopPropagation();
				item.select(!item.selected);
				if (!item.selected) {
					self.itemsStep1[0].select(false);
					item.hover(false);
				}
				if (item.index == 0) {
					jQuery(self.itemsStep1).each(function(){
						this.select(item.selected);
					})
				}
				self.updateStep2();
			});
		});

		// Step 2
		xml.find('step2').children().each(function(){
			var item = new gameFinderItem().initialize(jQuery(this), self.menus.find('.step2 ul'));
			item.index = self.itemsStep2.length;
			self.itemsStep2.push(item);

			item.element.hover(
				function(){ item.hover(true);  },
				function(){ item.hover(false); }
			);

			item.element.click(function(event){
				event.stopPropagation();
				item.select(!item.selected);
				if (!item.selected) {
					self.itemsStep2[0].select(false);
					item.hover(false);
				}
				if (item.index == 0) {
					jQuery(self.itemsStep2).each(function(){
						this.select(item.selected);
					})
				}
			});
		});
		
		this.updateStep2();
	},
	
	updateStep2: function() {
		var self = this;

		jQuery(self.itemsStep2).each(function() { this.element.hide() });

		jQuery(self.itemsStep1).each(function() {
			var item1 = this;
			if (!item1.selected) return;
			jQuery(self.itemsStep2).each(function() {
				var select_all = false;
				if(this.index == 0 && this.selected){
					select_all = true;
				}
				for (var i=0, l=item1.filters.length; i<l; i++) {
					if (item1.filters[i] == this.tag) {
						this.element.show();
						if(select_all){
							this.select;
						}
						break;
					}
				}
			});
		})
	}
};

// Load the XML as soon as posible. Then, when DOM is ready, process it.
(function() {
	// Load and process the XML file for menu items
	jQuery.get("/home/xml/index", function(xml){ 
		jQuery(document).ready(function() {
			loadMenus(xml);
		});
	});
	// Load and process the XML file for game finder
	dummyDate = new Date() ; 
	dummyParameter = dummyDate.getTime()

	jQuery.get("/search/gamefinderxml/" + dummyParameter, function(xml){ 
		jQuery(document).ready(function() {
			gameFinderController.load(jQuery(xml));
			
			//bind click to next button to get value of checked items
			$(".step2 #gamefinder_next").click(function(){
				var platforms = '';
				var genres = '';
				loopcount = 0;
				$(".step1 li").each(function(i){
					//# platforms is hardcoded to 10 to prevent duplicates
					// not sure why loop goes over items twice
					//might need to prevent 'view all' from being added
					if ($(this).attr("title") == 'viewall'){
						loopcount++;
					}
					if (loopcount > 1) {
						return false;
					}
					if($(this).hasClass("selected")){
						platforms = platforms + $(this).attr("title") + '|';
					}
					
				});
				loopcount = 0;
				$(".step2 li").each(function(idx){
					//# genres is hardcoded to 14 to prevent duplicates
					// not sure why loop goes over items twice
					//this checks if it starts the 2nd time around
					//also prevents viewall from being added
					if ($(this).attr("title") == 'viewall'){
						loopcount++;
					} 
					if (loopcount > 1) {
						return false;
					}
					var display = $(this).css("display");
					//if($(this).hasClass("selected") && display == 'block'){
					if($(this).hasClass("selected")){
						genres = genres + $(this).attr("title") + '|';
					}
					
				});
				//Set to 0 if none are selected
				if (genres.length > 1){
					genres = genres.slice(0,-1);
				}else{
					genres = 0;
				}
				if (platforms.length > 1){
					platforms = platforms.slice(0,-1);
				}else{
					platforms = 0;
				}
				
				var next_link = $(".step2 #gamefinder_next").text();
				//alert('/search/gamefinder/' + next_link + "/" + platforms + "/" + genres);
				window.location = '/search/gamefinder/' + next_link + "/" + platforms + "/" + genres;
			});
		});
	});
})()

