var email_signup = new function() {
	this.button_hash = '#newsletter-sign-up';
	
	this.name_default = "Name*";
	this.email_default = "Email*";
	
	this.style_error = {border: 'solid 1px red'};
	this.style_normal = {border: 'solid 1px white'};
	
	this.form_selector = "#emailsignup_form";
	this.message_selector = "#emailsignup_message";
	this.name_selector = "#emailsignup_name";
	this.email_selector = "#emailsignup_email";
	this.submit_selector = "#emailsignup_submit";

	this.ajax_url = "/wp-content/plugins/emailsignup/ajax.php";	
	
	this.setup = function() {
		jQuery("a[href=\"" + this.button_hash + "\"]").click(function() { email_signup_popup.show(); });
	};

	
	this.complete = function() {
		jQuery(this.form_selector).hide()
		jQuery(this.message_selector).fadeIn();		
	};
	
	this.process = function() {
		var str_email = jQuery(this.email_selector).val();
		
		var valid = true;
		
		if (str_email == "" || str_email == this.email_default) {
			jQuery(this.email_selector).focus();
			jQuery(this.email_selector).css(this.style_error);
			valid = false;
		} else {
			jQuery(this.email_selector).css(this.style_normal);
		}
		
		var str_name = jQuery(this.name_selector).val();
		
		if (str_name == "" || str_name == this.name_default) {
			jQuery(this.name_selector).focus();
			jQuery(this.name_selector).css(this.style_error);
			valid = false;
		} else {
			jQuery(this.name_selector).css(this.style_normal);
		}
		if (valid) {
			var data =  { txt_email: str_email, txt_name: str_name };
			jQuery.post(this.ajax_url, data, function() { email_signup.complete(); });
		}
	};
};
var email_signup_popup = new function() {
	this.title = "Newsletter Sign-Up";
	this.desc = "Want to stay informed of the latest news from Collins Construction? If you are interested in receiving these updates, sign-up by simply entering your email below.";
	this.label = "Your Information";
	
	this.overlay_animate_delay = 250;
	this.overlay_opacity = 0.78;
	this.popup_animate_delay = 250;

	this.popup_style = {background: 'White', position: 'absolute', top: 156, left: '50%', width: 600, 'margin-left': -315, 'z-index': 501, padding: 15};
	this.overlay_style = {background: '#5d5d5e', position: 'fixed', top: 0, left: 0, width: '100%', height: '100%', 'z-index': 500 };
	
	this.overlay = null;
	this.popup = null;


	this.show = function() {
		this.create_overlay();
		this.create_popup();
	};
	
	
	this.set_content = function() {
		var content = this.get_content();
		this.popup.html(content);

		jQuery(email_signup.submit_selector).click(function() { email_signup.process(); });
	};
	

	this.get_content = function() {	
		var title = "<div class=\"es_title\">" + this.title + "</div>";
		var desc = "<div class=\"es_desc\">" + this.desc + "</div>";
		var label = "<div class=\"es_label\">" + this.label + "</div>";
		
		var txt_name = "<input type=\"text\" id=\"emailsignup_name\" value=\"Name*\" onfocus=\"value=''\" onblur=\"if (value=='') value='Name*'\" title=\"Name\" />";
		var txt_email = "<input type=\"text\" id=\"emailsignup_email\" value=\"Email*\" onfocus=\"value=''\" onblur=\"if (value=='') value='Email*'\" title=\"Email\" />";
		var btn_submit = "<input type=\"button\" id=\"emailsignup_submit\" value=\"Submit\" />";
		
		var form = "<div id=\"emailsignup_form\">" + txt_name + txt_email + btn_submit + "</div>";
		var thankyou = "<div id=\"emailsignup_message\" style=\"display: none;\">Thank You!</div>";
	
		var close_bar = "<div class=\"es_close\"><a href=\"javascript:email_signup_popup.hide()\">Close</a></div>";
		
		return title + desc + label + form + thankyou + close_bar;
	};
	
	this.hide = function() {
		this.popup.stop().animate({opacity: 0}, this.popup_animate_delay,
			function() {
				email_signup_popup.popup.remove();
				email_signup_popup.overlay.stop().animate({opacity: 0}, email_signup_popup.overlay_animate_delay, 
					function() {
						email_signup_popup.overlay.remove(); 
					}
				);
		  	}
		);
		
	};
	
	this.create_popup = function() {
		this.popup = jQuery('<div />')
						.css(this.popup_style);
		
		jQuery('body').append(this.popup);
		this.set_content();


		var height = $(window).height();
		var scrollTop = $(window).scrollTop();
		var popup_height = this.popup.height();
		this.popup.css({top : (scrollTop + (height-popup_height)/2), opacity: 0});


		this.popup.delay(this.overlay_animate_delay).animate({opacity: 1}, this.popup_animate_delay);		
	};
	
	this.create_overlay = function() {
		this.overlay = jQuery('<div />')
							.css(this.overlay_style)
							.css('opacity',0)
							.click(function() {
								email_signup_popup.hide();				
							});
		jQuery('body').append(this.overlay);
		
		this.overlay.animate({opacity: this.overlay_opacity}, this.overlay_animate_delay);
	};
};

jQuery(document).ready(function() {
	email_signup.setup();								
});
