var RAF = {};

RAF.contentHeight = function (id) {
	var pane = $(window).height();
	var foot = $(id).offset().top + $(id).height();
	var height = (foot > pane) ? $(document).height() : pane;
	var overflow = (foot > pane) ? "auto" : "hidden";

	$("#container").css({"height": height});
	$("body").css({"overflow": overflow});
};
RAF.contentHeight("#littoral");

RAF.augmentLinks = function () {
	$("a").each(function () {
		switch($(this).attr("rel")) {
		case "external":
		  $(this).attr("target","_new");
		  break;
		case "internal":
		  $(this).attr("target","_new");
		  break;
		default:	
		}
	});
};

RAF.augmentNavigation = function () {
	$("#section-projects li.projects ul, #section-services li.services ul, #section-about li.about ul, #section-home li.home ul").fadeIn();

	$("li.tab li a").each(function(){
		if ($(this).attr("href") == document.location.pathname) {
			$(this).parent().addClass("current");
		};
	});
/*	$("li.tab").hover(function(){
		$(this).css({
			height:"2.5em",
			"z-index": 100
		});
	},function (){
			$(this).css({
				height:"1.5em",
				"z-index": 0
			});
	});*/
};

RAF.augmentOverlays = function () {
	$("#footer .credits").click(function(){
		RAF.overlayOpen("#credits");
	});
	$("#footer .privacy").click(function(){
		RAF.overlayOpen("#privacy");
	});
	$("#overlay .close").click(function(){
		$("#overlay").css("display","none");
	});
};

RAF.overlayOpen = function (which) {
	$("#overlay .overlay").css("display","none");
	$(which).css("display","block");
	$("#overlay").css("display","block");
}

RAF.init = function () {
	RAF.augmentLinks();
	RAF.augmentOverlays();
	$.timer(500, function(){
		RAF.augmentNavigation();
	});
};

RAF.resize = function () {
	RAF.contentHeight("#littoral");
};


$(document).ready(function(){
	RAF.init();
	RAFE.init();
});

$(window).resize(function(){
	RAF.resize();
});

var RAFE = {};
RAFE.messages = [];
RAFE.messages["partial"] = "<h6>Please check your email</h6>";
RAFE.messages["none"] = "<h6>Please add your email</h6>";
RAFE.messages["success"] = "<h5>Thank you for subscribing</h5>";

RAFE.timer = 0;

RAFE.focusInput = function (which) {
	$("#mailing-feedback").fadeOut("slow");

	if ($(which).val() === "") {
		$(which).val($(which).attr("alt"));
		$(which).removeClass("focus");
	}
	else if($(which).val() === $(which).attr("alt")) {
		$(which).val("");
		$(which).addClass("focus");
	}
	$(which).attr("rel","invalid");
}

RAFE.validateEmail = function (which) {
	var address = $(which).val();
	if (address.indexOf("@") != -1) {// test for an @
		if (address.split("@")[1].indexOf(".") != -1) {// test for a . after the @
			$(which).attr("rel","valid");
			RAFE.feedback("success");
			return true;
		}
		else {
			$(which).attr("rel","invalid");
			RAFE.feedback("partial");
			return false;
		}	
	}
	else {
		$(which).attr("rel","invalid");
		RAFE.feedback("none");
		return false;
	}
}

RAFE.feedback = function (message) {
	if (RAFE.timer) {
		clearTimeout(RAFE.timer);
	}
	if (!message) {
		$("#mailing-feedback").fadeOut("slow");
	}
	else {
		$("#mailing-feedback").fadeIn("slow").html(RAFE.messages[message]);
	}
	if (message != "success") {
		RAFE.timer = setTimeout('RAFE.feedback()',6500);
	}
}

RAFE.submit = function () {
	var valid = true;
	$("#mailing-form input[type=text]").each(function(i){
		valid = RAFE.validateEmail(this);
	});
	
	if (valid) {
		$("#mailing-form")
			.attr("action","/_includes/contact_mailer.php")
			.slideUp("slow")
			.fadeOut("slow")
			.submit();
		$("#mailing-form button").focus();
		return true;
	}
	return false;
}

RAFE.init = function () {
	$("#mailing-form input")
		.focus(function(){
			RAFE.focusInput(this);
		})
		.blur(function(){
			RAFE.focusInput(this);
		})
		.attr("rel","invalid");

	$("#mailing-form button").click(function() {
		RAFE.submit();
		return false;
	});
	
	$("#mailing-form").attr("target","mailing-ajax");
}


Image1= new Image(296,320);
Image1.src = "/_assets/home2_left.jpg";

Image2 = new Image(566,320);
Image2.src = "/_assets/home2_right.jpg";


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var pageTracker = _gat._getTracker("UA-2934472-4");
pageTracker._trackPageview();