// JavaScript Document
/*my subscribe js 03/12/10*/

$(function(){

function subscribe() {
	
		if ($("#subscribeContainer").is(":hidden")){
			$("#subscribeContainer").fadeIn("slow");
			
			//fade out contact form
			$("#myContent").hide('fast');
			$('#flashHolder').hide("fast");
			//$('#myContent').css("opacity", 0);
			
			$("#page-wrap").fadeOut('slow');
			
		//	$("#backgroundPopup").css({"opacity": "0.7"});
		//	$("#backgroundPopup").fadeIn("slow"); 
		}
		else{
			$("#subscribeContainer").fadeOut("fast");
		//	$("#backgroundPopup").fadeOut("slow"); 
		
		//fade in contact form
			$("#myContent").show();
			$("#flashHolder").show();
			//$('#myContent').css("opacity", 1);
		}
	}
	//run contact form when any contact link is clicked
	$(".subscribe").click(function(){subscribe()});
	



$(function(){	
$('#subscribeButton').click(function() {
		//console.log('submit pressed');
	var username = $('#username').val().replace(/[^\w\d ]+/gi, ''),
		email = $('#email').val().match(/^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i) ? $('#email').val() : null
		
		if(username.length < 2 ||  email === null) {	
		if($('.error').length === 0) {
			//$('form').append('<p id="message" class="error">Please enter your name and a valid email address.</p>');
			// $('#message').fadeTo("slow",  0.01, function() {	
			$('#message').html('<div class="error">Please enter your name and a valid email address.</div>');
			$('.error').fadeIn("slow");
			//console.log('finished')		
		//});
		
		}
		/*get rid of the error on focus  
		$("input[type=text]").focus(function(){
			 // $(this).blur();
			 $('.error').fadeTo("slow", 0.01);

			});
		*/


	//$('form').append('<p class="error">Please fill out each field.</p>');
	return false;
		}
		
		
		$.post(
		'dlAjaxSubscribe.php', {
	
		'username' : username, 
		'email' : email, 
		//'comment' : comment 
		}, 
		
		function(data) {			
			$('.error').fadeOut(300);
			
			$('#subscribeContainer h4').slideUp('slow');
			
			
			$('#message').html('<p id="message">Thanks, '+ username +'! Look for your email confirmation.</p>');
			//$('.error').fadeIn("slow");
			
			//$('#message').animate({opacity: 1.0}, 3000);
			
			
			$('#message').delay(2500).slideUp('slow');
			
			$('#message').delay(2500).fadeOut('slow').slideUp('slow');;
			$('#formHolder').delay(3500).fadeOut('slow').slideUp('slow');
			
			//$('#formHolder').animate({opacity: 0}, 1200);
			
			
			//$('#formHolder').delay(1500).fadeOut('slow');
			
			//$('#formHolder').fadeTo("slow", 0.01);
			
			//fade out the thanks message
		//	$('#message').animate({opacity: 0}, 100);
			
			
		//	$('#message').slideUp('slow');
		//	$('#subscribeContainer h4').slideUp('slow');
			
			
			//console.log('success function');
			
			
			
			
		});
		return false;
		});
		   });
		
		   });

