﻿var download;
var resume;
var share;
var notification;

function reposition() {
	var l = (resume.offset().left - 94) + "px";
	download.css("margin-left", l);
	share.css("margin-left", l);
}

function processClick(hr) {
	$.get("ProcessClick.aspx", { "hr": hr });
}

function validateEmail(val) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(val);
}

function showNotification(msg) {
	notification.text(msg).css({
		left: (resume.offset().left + ((resume.width() - notification.width()) / 2)) + "px",
		top: ($(window).height() + $(window).scrollTop() - notification.height() - 50) + "px"
	}).fadeIn();

	window.setTimeout(function () { notification.fadeOut(); }, 3000);
}


$(document).ready(function () {
	// Write the address
	var fn = "arash";
	var ln = "motamedi";
	var de = ".com";
	var as = "@";
	var mt = "mailto:";
	var e = fn + as + fn + ln + de;

	var t1 = 408;
	var t2 = 329;
	var t3 = 8415;

	var t = t1 + "-" + t2 + "-" + t3;

	$("#e").html("<a href=\"" + mt + e + "\">" + e + "</a>");
	$("#t").html("<a href=\"tel:+1-" + t + "\">" + t + "</a>");

	download = $("#download");
	resume = $("#resume");
	share = $("#share");
	notification = $("#notification");

	$(window).resize(reposition);
	$(window).load(function () {

		// Ok, reposition things!
		reposition();

		// And fade them in!
		window.setTimeout(function () { download.fadeIn(); }, 800);
		window.setTimeout(function () { share.fadeIn(); }, 900);

	});

	// Get a hold of all links on the page
	$("a").click(function () {
		// Report to the server
		processClick($(this).attr("href"));
	});

	// Set up functionality for email send
	$("#cancelEmail").click(function () {
		$.colorbox.close();
	});

	$("#submitEmailRequest").click(function () {
		// Gather values
		var sendername = $.trim($("#sendername").val());
		var recipient = $.trim($("#recipient").val());
		var attach = $("#attach").attr("checked");
		var sendermessage = $.trim($("#sendermessage").val());

		// Make sure the sender has introduced herself
		if (sendername == "") {
			$("#sendername").focus();
			$("#sendererror").css({ "display": "inline" });
			window.setTimeout(function () { $("#sendererror").fadeOut(); }, 1000);
			return;
		}

		// Make sure there's a valid recipient email address
		if (!validateEmail(recipient)) {
			$("#recipient").focus().select();
			$("#recipienterror").show();
			window.setTimeout(function () { $("#recipienterror").fadeOut(); }, 1000);

			return;
		}

		// Close the form
		$.colorbox.close();

		// Everything seems to be fine! so, send the email!
		$.post("EmailResume.aspx",
				{ "sendername": sendername,
					"recipient": recipient,
					"attach": attach,
					"sendermessage": sendermessage
				},
				function () {
					showNotification("Email sent to " + recipient);
				});

	});

	$("#sendEmail").click(function () {
		// Empty the values
		$("#sendername,#recipient,#sendermessage").val("");

		// Open up the window!
		$.colorbox({ href: "#emailForm", inline: true, scrolling: false, opacity: 0.6, transition: "none", close: null, initialWidth: 600, width: 600, initialHeight: 400, height: 400 });

		// Set the focus
		$("#sendername").focus();

		// Do not allow navigation off of this page
		return false;
	});
});

