$(function() {

	// Navigation
	$("#nav ul a").hover(function(){
			$(this).children("img.nav-off").fadeOut(0, function(){
				// Callback
			});
		}, function(){
			$(this).children("img.nav-off").fadeIn(0, function(){
				// Callback
			});
	});
	
	// Navigation on blog
	$("ol.nav a").hover(function(){
			$(this).children("img.nav-off").fadeOut(0, function(){
				// Callback
			});
		}, function(){
			$(this).children("img.nav-off").fadeIn(0, function(){
				// Callback
			});
	});
	
	// Gloabl variables for the search
	var searchStr = "";
	var searchCleared = false;
	
	// Search box methods
	$("#search-box").focus(function(){
		if (!searchCleared)
		{
			searchStr = $(this).val();	
			$(this).val("");
			searchCleared = true;
		}
	});	
	$("#search-box").blur(function(){
		if ($(this).val() == "")
		{
			$(this).val(searchStr);
			searchCleared = false;
		}
	});
	
	// Trigger search
	$("#search-button").click(function(){
		if ($("#search-box").val() == searchStr)
		{
			$("#search-box").val("");
		}
		$("#search-form").submit();
	});
	
	// Clients nav
	$("#clients li").hover(function(){
		$(this).children().children("img.mono").fadeOut(75, function(){
			$(this).parent().parent().children("div.popup").fadeIn(75);
		});
	});
	$("#clients div.popup").hover(function(){}, function(){
		$(this).fadeOut(150);
		var id = $(this).attr("id");
		var split = id.split("_");
		$("#"+split[0]).children("img.mono").fadeIn(75, function(){
			// Callback
		});
	});
	$("#clients div.popup").click(function(){
		parent.location.href = $(this).parent().children("a").attr("href");
	});

	// Gloabl clicked variable
	var clicked;
	
	// People tabbed nav
	$("#people-tabbed-nav div.tabbed-table-nav a").click(function(){
		// Set the clicked variable
		clicked = $(this).attr("id");
		// Fade out all the ones we don't need
		for (var i=1; i<=2; i++)
		{				
			var tabbed = "tabbed"+i;
			if (clicked != tabbed) {
				$("#people-tabbed-table").children("#"+tabbed+"-text").fadeOut(200);
			}
			$("#"+tabbed).children("img.tabbed-table-nav-off").fadeIn(200);
		}
		// Bring in the ones we want
		$(this).children("img.tabbed-table-nav-off").fadeOut(200);
		setTimeout(function() {			
			$("#people-tabbed-table").children("#"+clicked+"-text").fadeIn(200);
		}, 200);
	});
	
	// Case study galleries
	$("a.case-study-gallery-link").click(function(){
		var gallery = $(this).attr("rel");
		$("#"+gallery+"-start").trigger("click");
	});
	
	// Tweets
	$("#social-tweets").load("/get/latest/tweets", function(){
		// Fade out the loading div
		$("#social-tweets").fadeOut(0, function(){
			$("#social-tweets-loading").fadeOut(400, function() {		
				$("#social-tweets").fadeIn(400, function(){
					var tweet = 0;
					var tweets = $("#social-tweets").children().size();
					if (tweets > 1)
					{
						window.setInterval(function(){
							tweet = (tweet + 1) % tweets;
							for (var i=0; i<tweets; i++)
							{
								if (tweet != i)
								{
									$("#social-tweets").children("#tweet"+i).fadeOut(200);
								}
							}
							$("#social-tweets").children("#tweet"+tweet).fadeIn(200);
						}, 6000);
					}
				});
			});
		});
	});
	
});	
