$(document).ready(function () {

//UNIVERSAL FUNCTIONS AND CONSTANTS
var MIN_AGE_ALLOWED = 18;

$.clientCoords = function() {
	var dimensions = {width: 0, height: 0};
	if (document.documentElement) {
		dimensions.width = document.documentElement.offsetWidth;
		dimensions.height = document.documentElement.offsetHeight;
	} else if (window.innerWidth && window.innerHeight) {
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	}
	return dimensions;
}

$.validatePhone = function(pn) { return pn.match(/^\d{3}[-\ \.]?\d{3}[-\ \.]?\d{4}$/); }
$.validateZip = function(zip) {	return zip.match(/^\d{5}([-\ \.]?\d{4})?$/); }
$.validateEmail = function(eml) { return eml.match(/^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+\.([a-z]{2,4})$/i); }
$.validateName = function(name) { return name.match(/^\D+$/); }
$.validateAge = function(age) {	return age >= MIN_AGE_ALLOWED; }


//GA macro
$.doGA = function(val) { /*alert("GA VALUE: "+ val);*/ pageTracker._trackPageview(val); return false; }


//=======FADEUPS============================================
showFadeUp = function () {
	$.doGA("fadeup/"+$(this).attr("href"));
	
	//hide any other open fadeups.
	$("#fadeContainer").hide();
	$("#fadeContentDiv").hide();
	$("#fadeContainer").addClass("transparent");
	$("#fadeContainer").css("width", $.clientCoords().width);
	$("#fadeContainer").css("height", $.clientCoords().height);
	$("#fadeContainer").show();

	//load remote content
	$("#fadeContent").load($(this).attr('href'), registerChildHandlers);
	$("#fadeContentDiv").css("left", ($.clientCoords().width - $("#fadeContentDiv").width())/2);
	$("#fadeContentDiv").fadeIn("slow");
	return false;
}

hideFadeCtn = function () {
	$("#fadeContainer").hide();
	$("#fadeContentDiv").hide();
	return false;
}

registerChildHandlers = function () {
	//since this content has just been injected into the DOM, nothing has handlers.
	$("#fadeContent a").each(
		function() {
			var link = $(this).attr("href");
			if(link) {
				//off domain link, or specified to open in a new window
				if(link.indexOf("http://") == 0 || $(this).attr("target") === "new") {
					$(this).click(function() { window.open(link); return false });
				}
				//fadeup
				else if($(this).hasClass("fadeLink")) {
					$(this).click(showFadeUp);
				}
				//anything else except for #
				else if(link.indexOf("#") == -1) {
					$(this).click(function() { window.location.href = link; return false });
				} 
			}
		}
	);
}

//=======CASTING============================================
openCasting = function() {
	window.open("casting.html","","scrollbars=1,width=380,height=800,menubar=0,toolbar=0,location=1,status=0");
}

toggleEmpDiv = function () {
	($(this).attr("value") === "yes") ? $("#castingEmpSection").show("normal") : $("#castingEmpSection").hide("normal");
}

validateItem = function () {
	var name = $(this).attr("name");
	var val = $(this).val();
	var sib = $(this).siblings("label[for='"+name+"']");
	var isValid = false;
	switch(name) {
		case "phone":
			isValid = $.validatePhone(val);
			break;
		case "email":
			isValid = $.validateEmail(val);
			break;
		case "name":
			isValid = $.validateName(val);
			break;
		case "age":
			isValid = $.validateAge(val);
			break;
		default:
			break;
	}
	(!isValid) ? $(sib).addClass("error") : $(sib).removeClass("error");
}

processForm = function () {
	$("input").trigger("verify");
	if($("#CastingCallForm label").hasClass("error"))
		$("#errMsgSpan").show("normal");
	else {
		var castiframe = $( '<iframe name="castingIFrame" id="castingIFrame" style="display:none" src="about:blank" />' );
		$("#castingIFrameCtn").append( castiframe );
		$("#errMsgSpan").hide("normal");
		$("#CastingCallForm").attr("target","castingIFrame");
		$("#CastingCallForm").submit();
		$("#castingSubmit").remove();
		$("#processing").show(1000);
		$("#castingIFrame").load( function () {
			$("#processing").text("THANK YOU FOR YOUR SUBMISSION");
			return false;
		});
		$.doGA("casting/FormSubmit");
	}
	return false;
}

//=============POSTCARD==============
postcard = function ( name, email, msg, cardIndex ) {
	var iframe = $( '<iframe name="postframe" id="postframe" style="display:none" src="about:blank" />' );
	$("#iFrameCtn").append( iframe );
	$("#postCardForm input[name='fromEmail']").attr("value", name);
	$("#postCardForm input[name='toEmail']").attr("value", email);
	$("#postCardForm input[name='message']").attr("value", msg);
	$("#postCardForm input[name='image']").attr("value", cardIndex);
	$("#postCardForm").attr("target","postframe");
	$("#postCardForm").submit();
	return false;
 }

//display privacy fadeup.  called by postcard flash.
pcPrivacy = function () {
	$("#privLink").click();
}

//=============EVENT HANDLERS=============
//--footer nav fadeups--
$("#footerDiv .fadeLink").click(showFadeUp);
$("#closeFade").click(hideFadeCtn);
$("#fadeContainer").click(hideFadeCtn);
$("#fadeContentDiv").click(function () { return false; });
$("#footerDiv a[target='new']").click(function() { $.doGA("outbound/"+$(this).attr("href")); return true;});

//--casting--
$("input:radio[name='employee']").bind("click change select",toggleEmpDiv);
$("input:text[name='phone']").bind("blur verify", validateItem);
$("input:text[name='email']").bind("blur verify", validateItem);
$("input:text[name='name']").bind("blur verify", validateItem);
$("input:text[name='age']").bind("blur verify", validateItem);
$(".upld").click(function () { $.doGA("casting/"+$(this).attr("name")); return true;});
$("#castingSubmit").click(processForm);

});