/*
 * Fix IE background image flicker (via http://www.mister-pixel.com/)
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

/*
 * methods to run on DOM ready
 */
$(document).ready(function() {
	var verticalAlignHomepage = function()
	{
		if ($("body.home").length > 0)
		{
			var screenHeight = document.documentElement.clientHeight;
			var wrapperHeight = $("body.home div#wrapper").height();
			
			if (screenHeight > wrapperHeight)
			{
				var topMargin = (screenHeight)/2 - 303;
				$("body.home div#container").css("margin-top", topMargin);
			}
		}
	}();
	
	var configureAdmin = function() {
		// add alternate row color to "text" archive table
		$("table#admin tbody tr:odd").addClass("odd");
	}();
	
	var configureDVDArchive = function() {
		// vertical align DVD title bottom
		$("div#latest h4 strong, div#archive h4 strong").each(function(){
			$(this).children().css("padding-top", $(this).height() - $(this).children().height());
		});
		
		// add alternate row color to "text" archive table
		$("table#text tbody tr:odd").addClass("odd");
	}();
	
	var configureExitLinks = function() {
		// add "exit" link click events
		$("a.exit").click(function() {
			window.open($(this).attr("href"));
			
			return false;
		});
	}();

	var mailingListValidation = function() {
		var errorMessages = {
			email: "Please enter a valid Email Address.",
			dob_month: "Please select a Date of Birth Month.",
			dob_day: "Please select a Date of Birth Day.",
			dob_year: "Please select a Date of Birth Year."
		};
		
		var validateEmail = function(email)
		{
			if (email.search(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/) == -1)
			{
				return false;
			}
			return true;
		};
		
		var checkFieldValues = function(fields)
		{
			var errors = [];
			for (i = 0; field = fields[i]; i++)
			{
				if (field.name == "email" && !validateEmail(field.value))
				{
					errors.push(field.name);
					continue; // skip next check
				}
				if (field.value == "")
				{
					errors.push(field.name);
				}
			}
			return errors;
		};
		
		var generateErrors = function(errors)
		{
			// clear previous errors
			$("p#required").css("font-weight", "normal");
			$("ul#errors").remove();
			
			if (errors.length == 0)
			{
				return;
			}
			// display error messages
			$("p#required").css("font-weight", "bold");
			$("form#mailing-list").before("<ul id=\"errors\"></ul>");
			for (i = 0; i < errors.length; i++)
			{
				$("ul#errors").append("<li>" + errorMessages[errors[i]] + "</li>");
			}
			return false;
		};
		
		// validate mailing list form onsubmit
		$("form#mailing-list").submit(function() {
			var errors = checkFieldValues($("input[@type=text], select", $(this)));
			
			return generateErrors(errors);
		});
	}();
});

/*
 * detach all handlers
 */
$(window).unload(function() {
    $("*").unbind();
});

/*
 * Flash show/hide drop-down functionality for homepage
 */
function showHomeNav(show, i)
{

	if (i >606) {
		$("div#container").height((show) ? i + "px" : "606px");
	}
}

/*
 * Flash show/hide drop-down functionality for subpage header
 */
function showNav(show, i)
{
	$("div#header").height((show) ? i + "px" : "187px");
}

/*
 * Flash drop-down thumbnail click functionality for subpage header
 */
function showFilm(i)
{
	window.location.href = "../index.php?filmid=" + i;
}

/*
 * For Sony Flash Tracking
 */
function changestate(playStatus) { }
