jQuery(document).ready(function(){
	textbox_click_clear(".mod-globals li.search form input.cat_textbox-1");
	textbox_click_clear("form.mod-search-1 input.cat_textbox-1");
	textbox_click_clear("form.mod-search-2 input.cat_textbox-1");
	
	set_selected_menu_item(".mod-menu-1");
	
	hide_empty_catalogue_table();
	
	initialize_tabs({
		css_selector: ".mod-tabs",
		css_navigation_selector: ".mod-tabs-nav",
		css_tab_content_selector: ".mod-tabs-tab"
	});
	
	jQuery("#home-slideshow").orbit({
		animation: 'fade',
		bullets: true,
		directionalNav: false,
		timer: true
	});
});

function textbox_click_clear(css_selector){
	jQuery(css_selector).attr("defaultValue",jQuery(css_selector).val());
	
	jQuery(css_selector).focus(function(){ 
		if(jQuery(this).val() == jQuery(this).attr("defaultValue")){
			jQuery(this).val("");
		}
	});
	jQuery(css_selector).blur(function(){
		if(jQuery(this).val() == ""){
			jQuery(this).val(jQuery(this).attr("defaultValue"));
		}
	});
}

function hide_empty_catalogue_table(){
	jQuery("table.catalogueTable").each(function(){
		if(jQuery(this).find("td.catalogueItemNotFound").length > 0){
			jQuery(this).remove();
		}
	});
}

function set_selected_menu_item(menu_css_selector){
	var current_url = document.location.pathname;
	
	jQuery(menu_css_selector).find("a").each(function(){
		if(jQuery(this).attr("href") == current_url){
			jQuery(this).parents("li").addClass("selected");
		}
	});
}

function initialize_tabs(settings){
	jQuery(settings.css_selector).each(function(){
		var $tab_panel = jQuery(this);
		var $tab_panel_navigation = jQuery(this).find(settings.css_navigation_selector);
		var $tab_panel_content = jQuery(this).find(settings.css_tab_content_selector);
		
		// Unselect any selected tabs
		$tab_panel_navigation.find("li").each(function(i){
			jQuery(this).removeClass("selected");
		});
		
		// Select the first tab
		$tab_panel_navigation.find("li").eq(0).addClass("selected");

		// Hide all of the content sections
		$tab_panel_content.each(function(){
			jQuery(this).hide();
		});
		
		// She the first content section
		$tab_panel_content.eq(0).show();

		$tab_panel_navigation.find("li a").each(function(){
			// Cache the jQuery object
			$anchor = jQuery(this);
			
			// Bind the event to the anchor
			$anchor.click(function(e){
				// Prevent default action
				e.preventDefault();
		
				// Hide all of the content sections
				$tab_panel_content.each(function(j){
					jQuery(this).hide();
				});

				// Unselect any selected tabs
				$tab_panel_navigation.find("li").each(function(i){
					jQuery(this).removeClass("selected");
				});

				// Show the content section that correlates with the anchor clicked
				jQuery(jQuery(this).attr("href")).show();
				
				// Set the tab class to selected
				jQuery(this).parents("li").addClass("selected");
			});
		});
	});
}
