var maxPics = 0;
var curPhoto = 0;
var togglevals = [];
var homeTimer;
var autoHomePhotos = false;

$(document).ready(function() {
	/* HANDLE HOVER IMAGE CHANGES */
	$(".flip").hover(function() {
		$(this).attr({"src":$(this).attr("src").replace("images/","images/images/")});
	},function() {
		$(this).attr({"src":$(this).attr("src").replace("images/images/","images/")});
	});
	
	/* HANDLE TOGGLE VALUES IN INPUT BOXES */
	var x = 0;
	$(".toggle").each(function() {
		togglevals[x] = $(this).val();
		$(this).attr({"togglenum":x});
		x++;
	});
	
	$(".toggle").click(function() {
		var togglenum = $(this).attr("togglenum");
		if ($(this).val() == togglevals[togglenum]) $(this).val("");
	});
    
    $(".toggle").bind("blur",function() {
		var togglenum = $(this).attr("togglenum");
		if ($(this).val() == '') $(this).val(togglevals[togglenum]);
	});
	/* END TOGGLE BOX HANDLING */
	
	$("a[rel='nofollow']").each(function() {
		var q = $(this).attr("href").split("/");
		var e = q[2] + "@" + q[3];
		$(this).attr({"href":"mailto:"+e});
	});
	
	if ($(".colorme").length > 0) $(".colorme").colorbox({width:"550px",height:"350px", iframe:true});
	if ($(".colorme2").length > 0) $(".colorme2").colorbox({width:"550px",height:"450px", iframe:true});

	if ($("#photoBox").length > 0)
	{
		homeTimer = setTimeout(function() { moveToPhoto(1); },5000);
		autoHomePhotos = true;
	}
	
	

});



function swapPhoto(id)
{
	clearTimeout(homeTimer);
	var tmpId = curPhoto;
    curPhoto = id;
    $("#g"+tmpId).fadeOut(400,null);
    $("#g"+id).fadeIn(1200,null);
	
	//update current #
	var showNum = id+1;
	$("#col2.homeTop #photoBox #gCaptionBar #gNav #gCur").html(showNum);
	
	//update caption
	var caption = $("#g"+id).attr("alt");
	$("#col2.homeTop #photoBox #gCaptionBar #gCaption").html(caption);
	
	if (autoHomePhotos) homeTimer = setTimeout(function() { moveToPhoto(1); },5000);
}

function moveToPhoto(dir)
{
	if (maxPics == 0) maxPics = $("#col2.homeTop #photoBox img.g").length;
	var newId = (curPhoto*1)+(dir*1);
    if (newId == maxPics) newId = 0;
    if (newId < 0) newId = maxPics-1;
    swapPhoto(newId);
	return false;
}

function checkForm(frmId)
{
	var validated = true;
	$("#"+frmId+" .req").each(function() {
		var v = $(this).val();
		if (v == '' || v == 'Subject' ||  v == 'First Name' || v == 'Last Name' || v == 'Email Address') validated = false;			   
	});
	if (validated == false) 
	{
		alert("Please fill in all fields");
		return false;
	}
	else return true;
}

function loadMoreJobs()
{
	var x = 0;
	var firstEle = "";
	$(".jobChunk.hidden").each(function() {
		if (x < 5)
		{
			if (firstEle == "") firstEle = $(this).offset().top - 10;
			$(this).removeClass("hidden");
			x++;
		}
	});
	$('html,body').animate({scrollTop: firstEle}, 500);
	if (x < 5) $("#loadMoreJobs").remove();
	return false;
}

function submitMailingForm()
{
	var e = $("#email").val();
	var f = $("#firstname").val();
	var l = $("#lastname").val();
	if (e == '' || e == 'Email Address' || f == '' || f == 'First Name' || l == '' || l == 'Last Name')
	{
		alert("Please fill in your First and Last Name and your Email Address.");	
	}
	else
	{
		$.ajax({
			type: "POST",
			url: "/index.php",
			data: "action=addNewsletterSignup&email="+e+"&first="+f+"&last="+l,
			success: function(resp)
			{
				alert("Thank you! You have  been successfully added to our mailing list.");
				$("#firstname").val("First Name");
				$("#lastname").val("Last Name");
				$("#email").val("Email Address");
			}
		});
	}
	return false;
}
