$(document).ready(function() {
	$('#email_signup_form #first_name').bind("focus",function(){
		if($(this).val() == "NAME"){
			$(this).val('');
		}
	});
	$('#email_signup_form #email_address').bind("focus",function(){
		if($(this).val() == "EMAIL"){
			$(this).val('');
		}
	});
	$('#email_signup_form #first_name').bind("blur",function(){
		if($(this).val() == ""){
			$(this).val('NAME');
		}
	});
	$('#email_signup_form #email_address').bind("blur",function(){
		if($(this).val() == ""){
			$(this).val('EMAIL');
		}
	});
	//signup email address
	$('#submit_womens').bind("click",function(e){
		e.preventDefault();
		gender = "w";
		newsletterSignup(gender);
	});
	$('#submit_mens').bind("click",function(e){
		e.preventDefault();
		gender = "m";
		newsletterSignup(gender);
	});
	
	// tabs starts
    $(function(){
        $("ul.tabs").tabs("div.panes > div");
    });
    // tabs ends
    
    //load the twitter and facebook content via ajax
    $('#facebook_box .socialise_inner_box').load(
 	   '/',
 	   {ajax: 1,
 		act: 'get_facebook_feed'},
 	   function(){
 		   $('#facebook_box .scroll-pane').jScrollPane(
 		    		{scrollbarWidth: 22,
 		    		dragMaxHeight: 45 }
 		    ); 
 	   }
    );
    $('#twitter_box .socialise_inner_box').load(
 	   '/',
 	   {ajax: 1,
 		act: 'get_twitter_feed'},
 	   function(){
 		   $('#twitter_box .scroll-pane').jScrollPane(
 		    		{scrollbarWidth: 22,
 		    		dragMaxHeight: 45 }
 		    ); 
 	   }
    );
	$('#wordpress_box .socialise_inner_box').load(
		   '/',
		   {ajax: 1,
			act: 'get_wordpress_feed'},
		   function(){
			   $('#wordpress_box .scroll-pane').jScrollPane(
			    		{scrollbarWidth: 22,
			    		dragMaxHeight: 45 }
			    ); 
		   }
	   );
});

var gender, firstName, email;
function newsletterSignup(gender){
	firstName = $('#first_name').val();
	email = $('#email_address').val();
	if(firstName == "NAME"){
		$('#subscribe_error').html("Please enter your name");
		$('#subscribe_error').fadeIn('fast');
		return;
	}
	if(email == "EMAIL"){
		$('#subscribe_error').html( "Please enter a valid email address.");
		$('#subscribe_error').fadeIn('fast');
		return;
	}
	$.getJSON( "/subscribe/", 
		{first_name: firstName,
		email: email,
		gender: gender,
		act: "submit",
		ajax: true}, 
		function(data){
			if(data.success){
				$('#email_signup_form').fadeOut();
				$('#signup_box').addClass('successful');
				$('#complete_registration').fadeIn();	
			}else{
				if(data.msg != ""){
					$('#subscribe_error').html(data.msg);
					$('#subscribe_error').fadeIn('fast');
				}
			}
	});
}
