jQuery(document).ready(function() {
	function show_hide_lit_items(country, show_or_hide) {
		jQuery('.order_form_items_table .item.' + country.toLowerCase())[(show_or_hide == 'show' ? 'show' : 'hide')]();
		jQuery('.order_form_items_table .item.' + country.toLowerCase() + ' input').each(function() {
			this.disabled = show_or_hide != 'show';
		});
	}
	
	jQuery('.country_select input').each(function() {
		if(this.value == 'USA') {
			jQuery(this).click(function() {
				if(this.checked) {
					show_hide_lit_items('US', 'show');
					show_hide_lit_items('non-US', 'hide');
				} 
			});
			
			this.checked = true;
			jQuery(this).click();
		} else {
			jQuery(this).click(function() {
				if(this.checked) {
					show_hide_lit_items('non-US', 'show');
					show_hide_lit_items('US', 'hide');
				} 
			});
		}
	});
	
	if(jQuery('#tournament-tool-selection-options').length > 0) {
		jQuery('#tournament-tool-selection-options .option').each(function() {
			this.toggle = function() {
				if(this.toggled() && !arguments[0]) {
					jQuery(this).removeClass('toggled');
					jQuery(this).find('.engine_content').hide();
					
					if(jQuery('#tournament-tool-selection-options .toggled').length == 0)
						jQuery('#tournament-tool-selection-options').removeClass('has_toggled');
				} else {
					jQuery('#tournament-tool-selection-options .engine_content').hide();
					jQuery('#tournament-tool-selection-options .option').removeClass('toggled');
					
					jQuery(this).addClass('toggled');
					jQuery(this).find('.engine_content').show();
					
					jQuery('#tournament-tool-selection-options').addClass('has_toggled');
				}
			};
			
			this.toggled = function() {
				return jQuery(this).hasClass('toggled');
			}
			
			var option = this;
			
			jQuery(this).find('.select_engine').each(function() {
				this.option = option;
			});
			
			jQuery(this).find('.select_engine').unbind('click');
			jQuery(this).find('.select_engine').click(function() {
				this.option.toggle();
				return false;
			});
			
			var selected_option = jQuery(this).find('.selected_option')[0];
			
			if(selected_option) {
				jQuery(this).find('select')[0].selected_option = selected_option;
				jQuery(this).find('select')[0].option = option;
				jQuery(this).find('select').change(function() {
					jQuery(this.selected_option).html(jQuery(this).val());
					this.option.toggle(true);
				});
				jQuery(this).find('select').change();
			}
			
			jQuery('#tournament-tool-selection-options .engine_content').hide();
			jQuery('#tournament-tool-selection-options .option').removeClass('toggled');
			
			if(jQuery('#tournament-tool-selection-options .toggled').length == 0)
				jQuery('#tournament-tool-selection-options').removeClass('has_toggled');
		});
	}
	
	if(jQuery('#wp-realtime-sitemap-pages').length > 0) {
		var	first_ul	= jQuery('#wp-realtime-sitemap-pages ul')[0],
			lis			= jQuery(first_ul).find('> li'),
			second_ul	= jQuery('<ul></ul>'),
			third_ul	= jQuery('<ul></ul>');
		
		jQuery(first_ul).addClass('first');
		jQuery(second_ul).addClass('second');
		jQuery(third_ul).addClass('third');
		
		jQuery(first_ul).after(third_ul);
		jQuery(first_ul).after(second_ul);
		
		var found_products = false, found_support = false;
		
		jQuery(lis).each(function() {
			if(jQuery(this).find('a').html() == 'Products')
				found_products = true;
			
			if(jQuery(this).find('a').html() == 'Support')
				found_support = true;
			
			if(found_support) {
				jQuery(this).appendTo(third_ul);
			} else if(found_products) {
				jQuery(this).appendTo(second_ul);
			}
		});
		
	}
});
