fw.Libs.load([
	'http://link.members.freewebs.com/JS/checkLogin.jsp',
	'http://static.websimages.com/JS/mootools.js'
]);

var fwSiteSubscribe = new Class({

	type: 'siteSubscribe',

	initialize: function(ownerID, options){
		this.options = Object.extend({
			width:'300px',
			titleText: 'Subscribe To My Site',
			buttonText: 'Subscribe',
			infoLinkText: 'What\'s this?',
			helpText: 'Subscribing will allow you to receive site updates. Your email address will be kept private.',
			write: false
		}, options || {});
		
		this.ownerID = ownerID;
		this.containerID = 'siteSubscribe' + Math.round(Math.random() * 100000);
		
		this.prepare();
		if(this.options.write) this.write();
		this.instance = fw.Instances.add(this);
	},

	prepare: function(){
		this.hideParentButton = '<a class="fwssCloseLink" href="" onclick="this.parentNode.style.display=\'none\'; return false;">x</a>';
		
		this.HTML = '\
		<div id="'+this.containerID+'" class="fwSiteSubscribe fw-odd" style="width:'+this.options.width+'">\
		<div class="fwssHeader fw-secondary">'+this.options.titleText+'</div>\
		<div class="fwssMessageDiv fw-secondary" style="display:none;"></div>\
		<div class="fwssSubmitWrapper"><form action="http://link.members.freewebs.com/s/subscriptions/publicDispatcher"><table>\
		<tr>\
			<td>name:</td>\
			<td><input type="text" class="fwssTextInput"></td></tr>\
		</tr>\
		<tr>\
			<td>email:</td>\
			<td><input type="text" class="fwssTextInput"></td></tr>\
		</tr>\
		<tr>\
			<td>verify email:</td>\
			<td><input type="text" class="fwssTextInput"></td>\
		</tr>\
		<tr>\
			<td></td>\
			<td><input type="submit" class="fwssSubmit" value="'+this.options.buttonText+'"><a class="fwssHelpLink" href="">'+this.options.infoLinkText+'</a></td>\
		</tr>\
		</table></form></div>\
		<div class="fwssHelpDiv fw-secondary" style="display:none;">'+this.options.helpText+this.hideParentButton+'</div>\
		</div>';
		
		fw.Css.load('http://images.freewebs.com/Styles/fwSiteSubscribe.css');
	},

	write: function(){
		document.write(this.HTML);
		this.activate();
	},

	activate: function(){
		var self = this;
		
		this.container = $(this.containerID);
		this.form = this.container.getElement('form');
		this.submitWrapper = this.container.getElement('div.fwssSubmitWrapper');
		this.helpLink = this.container.getElement('a.fwssHelpLink');
		this.helpDiv = this.container.getElement('div.fwssHelpDiv');
		this.messageDiv = this.container.getElement('div.fwssMessageDiv');
		
		var inputs = this.container.getElements('input');
		this.name = inputs[0];
		this.email = inputs[1];
		this.email2 = inputs[2];
		this.statusContainer = this.container.getElement('input.fwssSubmit');
		
		this.helpLink.onclick = function(){
			self.helpDiv.style.display = self.helpDiv.style.display=='block' ? 'none' : 'block';
			return false;
		}
		
		this.form.onsubmit = function(){
			if(this.isProcessing) return false;
			var err = self.getValidateErrors();
			if(err){ 
				self.setMessage(err);
				return false;
			}
			
			this.isProcessing = true;
			self.dispatch('invite', 'crash', {
				callback: 'submitButtonCallback',
				data: {
					name: encodeURIComponent(self.name.value),
					email: encodeURIComponent(self.email.value),
					email2: encodeURIComponent(self.email2.value)
				}
			});
			return false;
		}
	},
	
	submitButtonCallback: function(response){
		if(response.error){
			alert(response.errorMsg)
			return;
		}
		this.container.innerHTML = '\
		<table class="fwssFinished"><tr><td>\
			<div class="fwssFinishedTitle">Thank you!</div>\
			<div>To complete the registration process, please click the link in the email we just sent to <b>'+this.email.value.makeSafe()+'</b>.</div>\
		</td></tr></table>\
		';
	},
	
	getValidateErrors: function(){
		if(!this.name.value){
			return 'Please enter your name.';
		}
		if(!this.email.value){
			return 'Please enter your email address.';
		}
		if(!(this.email.value+'').match(/[^@]+[@][^@.]+[.].+/i)){
			return 'Please enter a valid email address.';
		}
		if(this.email.value != this.email2.value){
			return 'Email addresses do not match.';
		}
		return false
	},
	
	setMessage: function(msg){
		this.messageDiv.innerHTML = msg + this.hideParentButton;
		this.messageDiv.style.display = 'block';
		var fx = new Fx.Style(this.messageDiv, 'marginLeft', {duration:600, transition:Fx.Transitions.bounceOut});
		fx.custom(40, 0);
	},
	
	dispatch: function(section, action, options){
		if(this.isBusy) return false;

		options = Object.extend({
			statusMsg: 'Please Wait...'
		}, options || {});
		
		this.setMessage(options.statusMsg);
		fw.jjax.req('http://link.members.freewebs.com/s/subscriptions/publicDispatcher', {
			appendTo: this.container,
			postBody: Object.extend({
				ownerID: this.ownerID,
				section: section,
				action: action,
				callback: 'fw.Instances.get("'+this.containerID+'").' + options.callback
			}, options.data || {})
		});
	}

});

