var ENEWS_SIGNUP = {
	webservice_url: 'http://' + document.domain + '/webservices/enews_signup.php',
	initValues:	new Array(),

	//overlay to display processing status
	resetForm: function() {
		jQuery('#enews_dynamic_pane').remove();
		jQuery('#enews_initial_pane').show();
		jQuery('#enews_signup_form #lname').val(ENEWS_SIGNUP.initValues['lname']);
		jQuery('#enews_signup_form #fname').val(ENEWS_SIGNUP.initValues['fname']);
		jQuery('#enews_signup_form #email').val(ENEWS_SIGNUP.initValues['email']);
	},

	//overlay to display processing status
	showProcessing: function() {
		jQuery('#enews_initial_pane').hide();
		jQuery('#enews_signup').append(
		'<div id="enews_dynamic_pane">'
		+	'<p style="font-align:center">Processing your Request</p>'
		+	'<img src="images/loaders/ajax-loader-2.gif" class="enews_loader" />'
		+'</div>');
	},

	//overlay to display AJAX request outcome
	showResponse: function( response ) {
		jQuery('#enews_dynamic_pane').html(
		'<p>' + response.message + '</p>'
		+'<a id="enews_signup_close" href="#" onclick="jQuery(\'#enews_signup\').hide();ENEWS_SIGNUP.resetForm();return false;">Close</a>'
		);
	},

	//store initial messages values
	saveDefaults: function() {
	ENEWS_SIGNUP.initValues['lname'] = jQuery('#enews_signup_form #lname').val();
	ENEWS_SIGNUP.initValues['fname'] = jQuery('#enews_signup_form #fname').val();
	ENEWS_SIGNUP.initValues['email'] = jQuery('#enews_signup_form #email').val();
	},

	//fire off the AJAX request to deliver client message
	sendMessage: 	function() {
		valid = jQuery('#enews_signup_form').validate().form();
		if( valid )
		{
			this.showProcessing();
			jQuery.ajax({
				url: ENEWS_SIGNUP.webservice_url,
				global: false,
				type: "POST",
				data: jQuery('#enews_signup_form').serialize(),
				dataType: "json",
				success: function(response){
					ENEWS_SIGNUP.showResponse(response);
				},
				error: function(response){
					ENEWS_SIGNUP.showResponse(response);
				}
			   }
			);
		}
	}
};

//attach events on ready
jQuery(document).ready(function() {

//save defaults for detecting changes and reseting the form
ENEWS_SIGNUP.saveDefaults();

//submit form via ajax
jQuery('#enews_signup_form').submit(function(event){
	event.stopPropagation();
	ENEWS_SIGNUP.sendMessage();
	return false;
});

//make sure the values have been touched
jQuery.validator.addMethod("notInitValue",
	function(value, element) {
		return this.optional(element) || value != ENEWS_SIGNUP.initValues[element.id]
	},
	jQuery.format("Cannot use default")
);

//clear values upon focus if default value
jQuery('#enews_signup_form input').focus(function(){
	if(jQuery(this).val() == ENEWS_SIGNUP.initValues[this.id])
		jQuery(this).val('');
	});

jQuery('#enews_signup_form input').blur(function(){
	if(jQuery(this).val() == '')
		jQuery(this).val(ENEWS_SIGNUP.initValues[this.id]);
	});

//technically not enews related but very close in terms of layout
jQuery('#searchform input.inputField').focus(function(){
	if(jQuery(this).val() == 'Search')
		jQuery(this).val('');
	});

jQuery('#searchform input.inputField').blur(function(){
	if(jQuery(this).val() == '')
		jQuery(this).val('Search');
	});

});


