function Control(element){
	var self = this;
	this.root = element;
	if (this.root.find('#Emailer').hasClass('Emailer'))  {
		this.nav = new Emailer(this.root.find('#Emailer'));
	}
	if (this.root.find('#newsletter').hasClass('newsletter'))  {
		this.newsletter = new NewsLetter(this.root.find('#newsletter'));
	}
}

function Emailer(element)  {
	var self = this;
	this.root = element;
	this.ajaxed = null;
	
	this.root.click(function() {
		jQuery.ajax({
			url: "/contact/refer",
			async: false,
			success: function(html) {
				jQuery('#container').after(html);
				self.ajaxed = jQuery('#container').siblings('.refer');
				self.ajaxed.dialog({
					title: "Email a Friend",
					modal: true,
					overlay: {
						backgroundColor: "#005698",
						 opacity: '.85'
					 },
					height: 550,
					width: 450,
					buttons: {
						send: function()  {
							var formData = self.ajaxed.find('form').serialize();
							self.ajaxed.find('#errorExplanation').remove();
							jQuery.ajax({
								url: "/contact/refer",
								type: "POST",
								data: formData,
								async: false,
								success: function(html) {
									var tempHtml = jQuery(html).find('#errorExplanation');
									if (tempHtml.hasClass('errorExplanation')) {
										self.ajaxed.find('h2').after(tempHtml);
									}
									else  {
										self.ajaxed.html("<h3> succsess </h3>");
										self.ajaxed.fadeOut(500, function() {self.ajaxed.dialog('close')});
									}
								}
							});
							//self.ajaxed.dialog('close');
						}	
					},
					close: function()  {
						self.ajaxed.dialog('destory').remove();
					}
				});
			}
		});
	});
}

function NewsLetter(element)  {
	var self = this;
	this.root = element;
	this.root.click( function() {
		jQuery('#container').after('<form action="http://copiousinc.createsend.com/t/1/s/pdtht/" method="post" class="newsletterSingup"><div><label for="mb-name">Name:</label><br /><input type="text" name="mb-name" id="mb-name" /><br /><label for="mb-pdtht-pdtht">Email:</label><br /><input type="text" name="mb-pdtht-pdtht" id="mb-pdtht-pdtht" /><br /><input type="submit" style="margin-bottom:10px;margin-left:0;margin-right:0;margin-top:10px;" value="Subscribe" /><br /><p style="">Thanks for joining our list of supporters. <br /><br /></div></form>');
		self.ajaxed = jQuery('#container').siblings('.newsletterSingup');
		self.ajaxed.dialog({
			title: 'Sign Up for the Banfield Charitable Trust Newsletter',
			modal: true,
			overlay: {
				backgroundColor: "#005698",
				opacity: '.85'
			},
			height: 300,
			width: 500,
			close: function()  {
				self.ajaxed.dialog('destory').remove();
			}
		});
	});
}