﻿var labels = [];
var jqSelectedTutorial = null;

function playVimeo(jqIFrame) {
	if (jqIFrame.length == 0) return;
	jqIFrame.get(0).contentWindow.postMessage({ "method": "play" }, "*");
}

function pauseVimeo(jqIFrame) {
	if (jqIFrame.length == 0) return;
	jqIFrame.get(0).contentWindow.postMessage({ "method": "pause" }, "*");
}

$(function () {

	var n = 0;
	$("input.with-label").each(function () {
		$(this).attr("id", "WithLabel" + n);
		labels[n++] = $(this).val();

		if ($.queryString.hasKey($(this).attr("name"))) {
			$(this).val($.queryString.get($(this).attr("name")));
			$(this).removeClass("with-label");
		}

	}).focus(function () {
		if ($(this).val() == labels[parseInt($(this).attr("id").substr(9))]) $(this).removeClass("with-label").val("");
	}).blur(function () {
		if ($.trim($(this).val()).length == 0) $(this).addClass("with-label").val(labels[parseInt($(this).attr("id").substr(9))]);
	});

	$("input.submit-on-enter").keypress(function (e) {
		if (e.which == 13) {
			$(this).parents("form").submit();
			e.preventDefault();
			return false;
		}
	});

	$(".frame:has(p)").mouseenter(function () {
		$(this).addClass("frame-mouseenter");
	}).mouseleave(function () {
		$(this).removeClass("frame-mouseenter");
	}).click(function () {
		var jqTutorial = $("#Tutorial" + $(this).attr("id").substr("Thumbnail".length));

		$(this).addClass("frame-selected");

		var jqCode = jqTutorial.find("div[code]");
		var code = jqCode.attr("code");
		var heightAttr = /height=\"\d+\"/.exec(code).toString();
		if (heightAttr != null) {
			var height = heightAttr.substr(8, heightAttr.length - 9);
			jqCode.css("height", height + "px");
			jqTutorial.find(".script").css("height", height + "px");
		}

		jqCode.html(code);
		jqTutorial.overlay({
			click: function () {
				jqCode.html("&nbsp;");
				jqTutorial.unoverlay();
			}
		});

	});

	$(".tutorial").find("a").click(function () {
		$(this).parents(".tutorial").find("div[code]").html("&nbsp;");
		$(this).parents(".tutorial").unoverlay();
	});

	$("#NewsletterSubmit").click(function () {
		if (!$.validation.isEmail($.trim($("#NewsletterEmailInput").val()))) {
			alert("Please enter a valid email address.");
			$("#NewsletterEmailInput").focus();
			return;
		}

		$("#NewsletterSubmit").hide();
		$("#NewsletterStatus").html("Please wait ...").show();

		$.ajax({
			type: "POST",
			url: "/NewsletterSignUp.ashx",
			data: "name=" + $("#NewsletterNameInput").val() + "&email=" + encodeURIComponent($("#NewsletterEmailInput").val())
		}).done(function (msg) {
			setTimeout(function () { $("#NewsletterStatus").html("Thanks!"); }, 500);
			
		}).fail(function (jqXHR, textStatus) {
			alert("An error occurred. Please check you have an active internet connection and try again.");
			$("#NewsletterStatus").hide();
			$("#NewsletterSubmit").show();
		});
	});

});

function scrollToAnchor(id) {
	$("html,body").animate({ scrollTop: $("a[name=" + id + "]").offset().top }, 600);
	return false;
}

function overlayFeature(url) {
	$("#FeatureOverlay img.target").attr("src", "/i/_.gif");
	setTimeout(function () {
		$("#FeatureOverlay img.target").attr("src", url);
		$("#FeatureOverlay").overlay({ click: function () { $("#FeatureOverlay").unoverlay(); } });
	}, 50);
}
