// JavaScript Document

// Make sure the document is ready
$(document).ready(function() {
	
	/**
	* Swaps the content for the home selection
	*/
	function swap_home_selection(selection) {
		
		for (c = 1; c <= $("#home-selection-content").children().size(); c++) {
			$("#home-selection-" + c).css("display", "none");
		}
		
		$("#home-selection-" + selection).css("display", "block");
		$("#home-selection-" + selection).fadeIn(100);
		
		selection_count = selection;
		
		$(document).stopTime("home_selection_loop");
		set_home_selection_loop(selection);
		
	}
	
	
	
	/**
	* Change the content on the home content selector
	*/
	$("#home-selection ul li a").hover(function(event) {
		
		event.preventDefault();
		
		selection = $(this).attr("id");
		selection = selection.substr(20, (selection.length - 20));
		
		swap_home_selection(selection);
		
	});
	
	
	
	/**
	* Loop through the home selection every x seconds (for the homepage only)
	*/
	function set_home_selection_loop(selection_count) {
		
		if ($("#home-selection").attr("id")) {
			
			$(document).everyTime("7s", "home_selection_loop", function() {
												 
				selection_count ++;
				if (selection_count >= $("#home-selection-content").children().size()) selection_count = 1;
				
				swap_home_selection(selection_count);
				
			}, 0);
			
		}
		
	}
	
	
	
	/**
	* Hide all tabs and reveals the first available
	*/
	function hide_tabs() {
		
		if ($(".tab-selected").attr("id")) {
			
			$(".tab-selected").addClass("tab");
			$(".tab-selected").removeClass("tab-selected");
			
			hash = location.hash.substring(1);
			if (hash) {
				
				$("ul.tab-menu li:first").removeClass("selected");
				$("ul.tab-menu li a[href^='#"+hash+"']").parent().addClass("selected");
				$("#"+hash).addClass("tab-selected");
				
			}
			
			else {
				
				$(".tab:first").addClass("tab-selected");
				
			}
			
		}
		
	}
	
	
	
	/**
	* Tab system
	*/
	$("ul.tab-menu li a").click(function(event) {
		
		event.preventDefault();
		
		loc = $(this).attr("href");
		loc = loc.substr(1);
		
		$("ul.tab-menu li").removeClass("selected");
		$(this).parent().addClass("selected");
		
		$(".tab").removeClass("tab-selected");
		$("#" + loc).addClass("tab-selected");
		
	});
	
	
	
	// Start the home selection loop
	set_home_selection_loop(0);
	hide_tabs();
	
});
