$().ready(function () {
    $("a.fancybox-link").colorbox({
        opacity: 0.65, transition: 'elastic', initialWidth: 0, initialHeight: 0, maxHeight: '90%', maxWidth: '80%'
    });

    $("#primarynav-contact").click(function () {
		showContact();
    });
    
    $(".contact-form .send").click(function() {
    	sendHello();
    });
    
    $(".contact-form .send").click(function() {
    	sendHello();
    });

    $("#contact .contact-bg").click(function() {
		hideContact(false);
    });

	$(document).keydown(function(e) { 
	    if (e.which == 27) {
	    	hideContact();
	    }
	});
	
	$(".contact-close").click(function() {
	   hideContact();
	});
	
    
});

function showContact() {
	
	// clean up previous animations
	$('#contact-form').stop(true, true);
	clearTimeout(hideTimer);
	
    $("#contact").fadeIn(0);
    $('#contact-form').css("marginLeft", -2000);

    setTimeout(function () {
        $('#contact-form').animate({ marginLeft: -330 }, { duration: 500, easing: "easeOutCirc", queue: false });
    }, 300);
}

var hideTimer;

function hideContact(showSent) {

	var fadeWait = 0;

	if (showSent) {
    	fadeWait = 2500;
        $('.contact-form .sent').animate({ top: -300 }, { duration: 0, queue: true });
        $('.contact-form .sent').animate({ top: 0 }, { duration: 300, queue: false });
		$(".contact-form .sent").fadeIn(300);
    	$('#contact-form').delay(2000).animate({ marginLeft: 2000 }, { duration: 400, easing: "easeInBack", queue: true });
	}

	hideTimer = setTimeout(function() {
		$('#contact').fadeOut();
	    $('.contact-form .error').fadeOut();
	    $('.contact-form .sent').fadeOut();
	}, fadeWait)

}

function showError() {
	$(".contact-form .error").fadeIn(300);
}

function sendHello() {

	var name = $(".contact-form .from").val();
	var email = $(".contact-form .email").val();
	var message = $(".contact-form .message").val();
	var dataString = "name=" + name + "&email=" + email + "&message=" + message;
	
	$.ajax({
	  type: "GET",
	  url: t() + "/sendmail.php",
	  data: dataString,
	  success: function (msg) {
		if (msg === "SUCCESS") {
			hideContact(true);
		}
		else {
			showError();
		}
	  }
	});
	
	return false;
};



