/**
 *
 * @package Printable Parties
 * @copyright (c) 2010 Funsational
 * @license Licensed for Funsational, Inc usage only.
 *
 * @author  (Ryan McIntyre)
 * @todo for use in combined js (when script created)
 *
 */

$(function(){

	var signedUp = false;
    $('#newsletter_signup').show();
    $('#newsletter_submit').show();
    $('.js-error').hide();
    
	$('.home_newsletter').validate({
		debug: true,
		submitHandler: function() {
			$.ajax({
				url: 'home/newsletterSignup',
				type: 'POST',
				dataType: 'json',
				data: {
						email : $("#txtNewsletterFooter").val()
					  },
				success: function(data) {
					if (data.result == 'Subscribed') 
					{	
						$("<div>You have <b>ALREADY</b> signed up for the <b>Printable Games Newsletter</b><br/><br/>"+
						"You should have received an e-mail in your inbox <b class=\"i\">("+$('#txtNewsletterFooter').attr('value')+")</b>.<br/><br/>"+
						"If you still have not received your e-mail confirmation you may contact support:<br/>"+
						"<b>Monday - Friday<br>	6am &ndash; 5pm <br>"+
						"9am &ndash; 8pm EST</b><br/><br/>Phone Number: 1-888-722-0306</div>").dialog({
							autoOpen: true,
							height:300,
							width:480,
							modal:true,
							title: 'Newsletter Sign-up',
							buttons: {
								"Okay": function() {$(this).dialog("close");}
							}
						});
						
					} 
					else if(data.result)
					{
						$("<div>You have been successfully signed up for the <b>Printable Games Newsletter</b>!<br/><br/>"+
						"You should receive an e-mail in your inbox <b class=\"i\">("+$('#txtNewsletterFooter').attr('value')+")</b>.</div>").dialog({
							autoOpen: true,
							height:150,
							width:480,
							modal:true,
							title: 'Newsletter Sign-up',
							buttons: {
								"Okay": function() {$(this).dialog("close");}
							}
						});
					}
				}					
			});
			PrintableParties.UIDialogCloseFix();
		},
		rules: {
			txtNewsletterFooter: {
				required: true,
				email: true
			}
		},
		messages: {
			txtNewsletterFooter: {
				required: "<b>* Email address: </b> We need your email address to send you an e-mail ",
				email: "<b>* Email address: </b> Invalid e-mail address (example: jDoe@example.com)"
			}
		},
		showErrors: function(errorMap, errorList) {
			this.defaultShowErrors(); // show the floating boxes
		},
		errorElement: "div",
		wrapper: "div",  // a wrapper around the error message
		errorPlacement: function(error, element) {
			PrintableParties.inlinePlacement(error,element);
		}
	});

	var defaultText = $("#txtNewsletterFooter").attr('value');
	$("#txtNewsletterFooter").bind({
		focus: function() {
			$(this).attr('value', '');
		},
		blur: function() {
			if($(this).attr('value') === '')
			{
				$(this).attr('value', defaultText);
			}
		}
	});
	$('.home_newsletter .btn').bind({
		click: function()
		{
			if($('.home_newsletter').valid())
			{
				$('.home_newsletter').submit();
			}
			else
			{
				$("#txtNewsletterFooter").focus();
			}
			return false;
		}
	});

	if(window.external && typeof window.external.AddFavorite !== "undefined") // ie bookmark
	{
		$('.home_newsletter .flicker').after('<a title="Bookmark this site" class="sprite_social bookmark" href=""></a>');

		$('.home_newsletter .bookmark').bind({
			click: function(e)
			{
				PrintableParties.bookmark(window.location.toString(), document.title);
				return false;
			}
		});

	}

});
