$(document).ready(function(){
	//Search Field - default value
	$("#search").attr("value", "Search or #Tag");
	
	//Search Field - on focus 
	$("#search").focus(function() {
		if (this.value == "Search or #Tag"){  
             this.value = '';  
        } 
		if(this.value != "Search or #Tag"){  
			this.select();  
		}
	});
	
	//Search Field - on blur
	$("#search").blur(function() {  
		if ($.trim(this.value) == ''){  
			this.value = "Search or #Tag";
		}  
	});
	
	//Empty value of search submit button
	$("#search_go").attr("value", "");
	
	//Slideshow
	$("#slideshow").cycle({
		timeout: 5000,
		prev: "#slideshow-prev",
		next: "#slideshow-next"
	});
	
	$("#slideshow-pause").click(function() {
		$("#slideshow").cycle('pause');
	});
	
	$("#slideshow-resume").click(function() {
		$("#slideshow").cycle('resume', true);
	});
	
	//Interior Tabbed Content
	
	
	//If Tabbed Content is present
	if ($('div.tabs').length > 0) {	
		$('#tab2, #tab3, #tab4, #tab5').hide();
	
		$('a.open').click(function(event) {
		event.preventDefault();
		// mark current link as selected and unmark all others
		$(this)
		.addClass('selected')
		.siblings('a.open').removeClass('selected');  
		
		$(".tabBlock:visible").hide();
		var index = $("a.open").index(this);
		$(".tabBlock:eq("+index+")").show();
		});
		
		var tab = getUrlVars()["tabID"];
		if (tab != null) {
			$('#tab1, #tab2, #tab3, #tab4, #tab5').hide();
			$('#tab'+tab).show();
			$('a.open:eq('+(tab-1)+')').addClass('selected');
			$('a.open:eq('+(tab-1)+')').siblings().removeClass('selected');
		}
	}
	
	//Dynamically set the width of the footer-nav2 UL
	var totalWidth = 0;
	$("#footer-nav2 > li").each(function() {
		var marginL = parseInt($(this).css("marginLeft").replace("px",""));
		var marginRight = parseInt($(this).css("marginRight").replace("px",""));
		totalWidth += $(this).outerWidth() + marginL + marginRight;
	});
	$("#footer-nav2").css("width", totalWidth);
});

// Read a page's GET URL variables and return them as an associative array.
//http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

//Contact Form Validation
function isEmail(strValue) { return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue); } 

function validateContact(objForm)
{
	var strReqMsg = "";
	var strValidationMsg = "";
	
	if(objForm.whoYouAre.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Who you are\n"; }
	
	if(objForm.name.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\n"; }
	
	if(objForm.email.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Email\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.comments.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Comments\n"; }
	
	// Assemble all of the error messates together to display to the user
	if(strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }

		if(strValidationMsg.length)
		{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}
}

