$(function() {
	$('a.downloadLink').qtip(
	   {
	      content: {
	         title: {
	            text: 'Download AppStar',
	            button: '×'
	         },
	         text: $('#downloadFormDiv')
	      },
	      position: {
	         target: $(document.body), // Position it via the document body...
	         corner: 'center' // ...at the center of the viewport
	      },
	      show: {
	         when: 'click', // Show it on click
	         solo: true // And hide all other tooltips
	      },
	      hide: false,
	      style: {
	         width: { max: 350 },
	         padding: '14px',
	         border: {
	            width: 9,
	            radius: 9,
	            color: '#666666'
	         },
	         name: 'light'
	      },
	      api: {
	         beforeShow: function()
	         {
	            // Fade in the modal "blanket" using the defined show speed
	            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
	         },
	         beforeHide: function()
	         {
	            // Fade out the modal "blanket" using the defined hide speed
	            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
	         },
	         onHide: function()
	         {
	         	$('.qtip .downloadFormContent').html("<p><b>You have already downloaded AppStar 2.0</b><br/>If you wish to re-download the demo, <a href='http://www.damabia.com/download/appstar.zip'>click here</a>.</p>");
	         }
	      }
	   });
	
	   // Create the modal backdrop on document load so all modal tooltips can use it
	   $('<div id="qtip-blanket">')
	      .css({
	         position: 'absolute',
	         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
	         left: 0,
	         height: $(document).height(), // Span the full document height...
	         width: '100%', // ...and full width
	
	         opacity: 0.7, // Make it slightly transparent
	         backgroundColor: 'black',
	         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
	      })
	      .appendTo(document.body) // Append to the document body
	      .hide(); // Hide it initially
}); 



$(function() {
	$(".downloadSubmitButton").click(function() {
		if (!$(".qtip .downloadSubmitButton").hasClass("activeDownloadSubmitButton")) { return false; }
		var email = $(".qtip input.downloadEmail").val();
		if (email == "") {  
			$("input#downloadEmail").addClass("formError");  
			$("input#downloadEmail").focus();  
			error = true;  
    	}
		var dataString = 'email=' + email;
		//alert (dataString);return false;
		$.ajax({
			type: "POST",
			url: "bin/process_download.php",
			data: dataString,  
			success: function() {    
				$('.qtip .downloadFormContent').html("<p><b>Thanks for downloading AppStar 2.0!</b><br/>Your download should start shortly. If it doesn't, <a href='http://www.damabia.com/download/appstar.zip'>click here</a>.</p>")   
				.hide()  
				.fadeIn(1500, function() {
					window.location.href = "http://www.damabia.com/download/appstar.zip"; 
				});
			}
		});
		return false;
	}); // onclick
	
	$("input.downloadEmail").keyup(function(){
		var filter = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		if (filter.test($(".qtip input.downloadEmail").val())) {
			$(".qtip .downloadSubmitButton").addClass("activeDownloadSubmitButton");
		} else {
			$(".qtip .downloadSubmitButton").removeClass("activeDownloadSubmitButton");
		}
	}); // keyup
});
