$(function() {
	
	/* Function 1
	   Desc: This operates the tabs in the blog sidebar. */
	
	$(".blog-sidebar-panel").hide();
	$("#blog-sidebar-panel-1").show();
	
	$(".blog-sidebar-tabs a").click(function() {
		var panel_target = $(this).attr("href");
		
		$(".blog-sidebar-tabs a").removeClass("blog-sidebar-tab-active");
		$(this).addClass("blog-sidebar-tab-active");
		$(".blog-sidebar-panel").hide();
		$(panel_target).show();
		
		return false;
	});
	
	/* Function 2
	   Desc: Removes the search input value on focus.
	   Notes: Because the output is dynamically generated
	      Wordpress we need to add the initial input
	      value. (Otherwise we would have placed it in 
	      the HTML.) */
	      
	if ($("#searchform #s").attr("value") == "") {
		$("#searchform #s").attr("value","Search for:");
	}
	
	$("#searchform #s").focus(function() {
		var what_is_input_value = $("#searchform #s").attr("value");
		if (what_is_input_value == "Search for:") {
			$("#searchform #s").attr("value","");
		}
	});
				
	$("#searchform #s").blur(function() {
		var is_search_empty = $("#searchform #s").attr("value");
		if (is_search_empty == "") {
			$("#searchform #s").attr("value","Search for:");
		}
	});
	
	
	/* Function 3
	   Desc: Navigates the portfolio previews on the home page. */
	      
	var totalItems = $("#the-pf-preview-show section").size();
	var itemLabelNumber = 1;
	
	$("#the-pf-preview-show section").each(function() {
		$(this).animate({opacity: 0}, 0);
		$(this).addClass("pf-panel-"+itemLabelNumber);
		itemLabelNumber++;
	});
	
	$("#the-pf-preview-show section:first").animate({opacity: 1}, 0);
	$("#the-pf-preview-show section:first").addClass("current-pf-panel");
	
	$(".pf-nav-next").click(function() {
		$("#the-pf-preview-show section").each(function() {
			if ($(this).hasClass("current-pf-panel")) {
				$(this).removeClass("current-pf-panel");
				
				// Kind of hacky. Gets all the class names and then
				// trims it down to the number. Hackers gonna hack.
				var current_pf_panel_number = $(this).attr("class").replace("portfolio-preview wide-6 flow end pf-panel-","");
				
				if (current_pf_panel_number == totalItems) {
					var new_pf_panel_number = 1;
					var new_pf_panel_class = ".pf-panel-"+new_pf_panel_number;
					new_pf_panel_class_global = new_pf_panel_class;
				}
				else {
					var new_pf_panel_number = parseInt(current_pf_panel_number) + 1;
					var new_pf_panel_number = new_pf_panel_number;
					var new_pf_panel_class = ".pf-panel-"+new_pf_panel_number;
					new_pf_panel_class_global = new_pf_panel_class;
				}

				$(this).addClass("fade-me-please");
				
				return false;
			}
		});

		$(".fade-me-please").animate({opacity: 0}, 250);
		$(new_pf_panel_class_global).animate({opacity: 1.0}, 250);	
		
		$(new_pf_panel_class_global).addClass("current-pf-panel");
		$("#the-pf-preview-show section").removeClass("fade-me-please");
		
		return false;
	});
	
	$(".pf-nav-prev").click(function() {
		$("#the-pf-preview-show section").each(function() {
			if ($(this).hasClass("current-pf-panel")) {
				$(this).removeClass("current-pf-panel");
				
				// Kind of hacky. Gets all the class names and then
				// trims it down to the number. Hackers gonna hack.
				var current_pf_panel_number = $(this).attr("class").replace("portfolio-preview wide-6 flow end pf-panel-","");
				
				if (current_pf_panel_number == 1) {
					var new_pf_panel_number = 3;
					var new_pf_panel_class = ".pf-panel-"+new_pf_panel_number;
					new_pf_panel_class_global = new_pf_panel_class;
				}
				else {
					var new_pf_panel_number = parseInt(current_pf_panel_number) - 1;
					var new_pf_panel_number = new_pf_panel_number;
					var new_pf_panel_class = ".pf-panel-"+new_pf_panel_number;
					new_pf_panel_class_global = new_pf_panel_class;
				}

				$(this).addClass("fade-me-please");
				
				return false;
			}
		});

		$(".fade-me-please").animate({opacity: 0}, 250);
		$(new_pf_panel_class_global).animate({opacity: 1.0}, 250);	
		
		$(new_pf_panel_class_global).addClass("current-pf-panel");
		$("#the-pf-preview-show section").removeClass("fade-me-please");
		
		return false;
	});
});
