﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!	
//@adapted: Steven Lasch steven.lasch@gmail.com
/***************************/

/*
**************************************************
* STATUS
* 0 means disabled; 1 means enabled
**************************************************
*/

var popupStatus = 0;

/*
**************************************************
* LOADING FUNCTION
**************************************************
*/

function loadPopup(id){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#" + id).fadeIn("slow");
		popupStatus = 1;
	}
}

/*
**************************************************
* DISABLE FUNCTION
**************************************************
*/

function disablePopup(id){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#" + id).fadeOut("slow");
		popupStatus = 0;
	}
}

/*
**************************************************
* CENTERING FUNCTION
**************************************************
*/

function centerPopup(id){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#" + id).height();
	var popupWidth = $("#" + id).width();
	//centering
	$("#" + id).css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

/*
**************************************************
* ON READY FUNCTIONS
**************************************************
*/

$(document).ready(function(){
	$(".writerErgonomicsButton").click(function(){
		$('.error').hide();						
		centerPopup("writerErgonomics");
		loadPopup("writerErgonomics");

		$("#submitWriterErgonomics").click(function(){
			$('.error').hide();  
			var name = $("input#nameWriterErgonomics").val();  
			if (name == "") {  
				$("label#nameWriterErgonomics_error").show();  
				$("input#nameWriterErgonomics").focus();  
				return false;  
			}  
			var email = $("input#emailWriterErgonomics").val();  
			if (email == "") {  
				$("label#emailWriterErgonomics_error").show();  
				$("input#emailWriterErgonomics").focus();  
				return false;  
			}
			
			var file = $("input#fileWriterErgonomics").val();
	
			var dataString = 'name='+ name + '&email=' + email + '&file=' + file;  
			//alert (dataString);return false;  
			$.ajax({  
				type: "GET",  
				url: "/email_capture.php",  
				data: dataString,
				cache: false,
				success: function() {
					disablePopup("writerErgonomics");
					window.location.href = file;
				}
			});
		});//end submit click function
		//cancel the submit button default behaviours
        return false;
	});//end button click function

	$(".creativeCommercialButton").click(function(){
		$('.error').hide();						
		centerPopup("creativeCommercial");
		loadPopup("creativeCommercial");

		$("#submitCreativeCommercial").click(function(){
			$('.error').hide();  
			var name = $("input#nameCreativeCommercial").val();  
			if (name == "") {  
				$("label#nameCreativeCommercial_error").show();  
				$("input#nameCreativeCommercial").focus();  
				return false;  
			}  
			var email = $("input#emailCreativeCommercial").val();  
			if (email == "") {  
				$("label#emailCreativeCommercial_error").show();  
				$("input#emailCreativeCommercial").focus();  
				return false;  
			}
			
			var file = $("input#fileCreativeCommercial").val();
	
			var dataString = 'name='+ name + '&email=' + email + '&file=' + file;  
			//alert (dataString);return false;  
			$.ajax({  
				type: "GET",  
				url: "/email_capture.php",  
				data: dataString,
				cache: false,
				success: function() {
					disablePopup("creativeCommercial");
					window.location.href = file;
				}
			});
			//cancel the submit button default behaviours
            return false;
		});//end submit click function
	});//end button click function

	$(".editingGuideSampleButton").click(function(){
		$('.error').hide();						
		centerPopup("editingGuideSample");
		loadPopup("editingGuideSample");

		$("#submitEditingGuideSample").click(function(){
			$('.error').hide();  
			var name = $("input#nameEditingGuideSample").val();  
			if (name == "") {  
				$("label#nameEditingGuideSample_error").show();  
				$("input#nameEditingGuideSample").focus();  
				return false;  
			}  
			var email = $("input#emailEditingGuideSample").val();  
			if (email == "") {  
				$("label#emailEditingGuideSample_error").show();  
				$("input#emailEditingGuideSample").focus();  
				return false;  
			}
			
			var file = $("input#fileEditingGuideSample").val();
	
			var dataString = 'name='+ name + '&email=' + email + '&file=' + file;  
			//alert (dataString);return false;  
			$.ajax({  
				type: "GET",  
				url: "/email_capture.php",  
				data: dataString,
				cache: false,
				success: function() {
					disablePopup("editingGuideSample");
					window.location.href = file;
				}
			});
			//cancel the submit button default behaviours
            return false;
		});//end submit click function
	});//end button click function


	/*
	**************************************************
	* CLOSE FUNCTIONS
	**************************************************
	*/
	
	$("#closeWriterErgonomics").click(function(){
		disablePopup("writerErgonomics");
	});

	$("#closeCreativeCommercial").click(function(){
		disablePopup("creativeCommercial");
	});

	$("#closeEditingGuideSample").click(function(){
		disablePopup("editingGuideSample");
	});

});
