Clientcide.setAssetLocation('js/assets/');
window.addEvent('domready', function(){
	var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var dialog = {
		'alert': new StickyWin({zIndex: 10000}),
		'invite': new StickyWin({zIndex: 0})
	};
	var ajaxInvite = new Request.JSON({url: 'invite.php', onComplete: function(status){
		if(status){
			dialog.alert.setContent(StickyWin.ui('Invites Sent!', 'Your invitations were sent!', {buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
			dialog.alert.position({'offset':{'x':0,'y':0}}).show();
			dialog.invite.hide();
		}else{
			dialog.alert.setContent(StickyWin.ui('Error!', 'Unable to send invitations at this time. Please try again later.', {buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
			dialog.alert.position({'offset':{'x':0,'y':0}}).show();
		}
	}});
	var ajaxOptin = new Request.JSON({url: 'optin.php', onComplete: function(status){
		if(status){
			dialog.alert.setContent(StickyWin.ui('E-mail Saved!', 'Your email address has been saved and you will be notified as soon as i-Vita goes live and your personalized vitamins are available', {'width': 395, buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
			dialog.alert.position({'offset':{'x':0,'y':0}}).show();
			$$('form')[0].email.value = '';
		}else{
			dialog.alert.setContent(StickyWin.ui('Error!', 'Unable to save e-mail. Please try again later.', {buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
			dialog.alert.position({'offset':{'x':0,'y':0}}).show();
		}
	}});


	$('invite-a-friend').addEvent('click', function(){
		dialog.invite.setContent(StickyWin.ui('Invite A Friend', $('invite').get('html'), {width: 317}))
		.position({'offset':{'x':0,'y':0}})
		.show();
		$$('.StickyWinInstance form').addEvent('submit', function(){
			friends = this.elements['friends[]'];
			if(regex.test(this.youremail.value) && this.yourname.value != ''){
				ajaxInvite.post({
					'youremail': this.youremail.value,
					'yourname': this.yourname.value,
					'friends': [friends[0].value, friends[1].value, friends[2].value, friends[3].value],
					'message': this.message.value
				});
			}else{
				dialog.alert.setContent(StickyWin.ui('Invite Error', 'Please enter your name and a valid e-mail address', {'width': 348, buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
				dialog.alert.position({'offset':{'x':0,'y':0}}).show();
			}
			return false;
		});
		return false;
	});

	$$('form')[0].addEvent('submit', function(){
		if(regex.test(this.email.value)){
			ajaxOptin.post({'email': this.email.value});
		}else{
			dialog.alert.setContent(StickyWin.ui('Invalid E-mail', 'Please enter a valid e-mail address', {'width': 257, buttons: [{text: 'Close', onClick: function(){dialog.alert.hide()}}]}));
			dialog.alert.position({'offset':{'x':0,'y':0}}).show();
		}
		return false;
	});


});