var NewsLetterBanner = {
	openedHeight: 85,
	steps: 10,
	initialize: function () {
		this.element = $('newsletter_form');
		this.btn = $('newsletter_banner');
		if (!this.element || !this.btn) return;
		this.visible = false;
		this.btn.onclick = this.showHideForm.bindAsEventListener(this);
		this.form = this.element.getElementsByTagName('form')[0];
		this.inpName = this.form.name;
		this.inpName.onfocus = this.onFocusName.bindAsEventListener(this);
		this.inpName.onblur = this.onBlurName.bindAsEventListener(this);
		this.inpEmail = this.form.email;
		this.inpEmail.onfocus = this.onFocusEmail.bindAsEventListener(this);
		this.inpEmail.onblur = this.onBlurEmail.bindAsEventListener(this);
		this.inpSubmit = this.element.getElementsByClassName('b_send')[0];
		this.inpSubmit.onclick = this.submitData.bindAsEventListener(this);
		Form.enable(this.form); // if by any chance the user refreshes the page while fields are disabled things get shitty :(
	},
	showHideForm: function () {
		this.direction = (this.visible?'hide':'show');
		this.element.style.display = 'block';
		if (this.direction == 'show')
			this.animShowFormStep(1);
		else
			this.animHideFormStep(this.steps - 1);
	},
	animShowFormStep: function (step) {
		this.element.style.height = (this.openedHeight * step / this.steps) + "px";
		if (step < this.steps)
			setTimeout(this.animShowFormStep.bind(this, step + 1), 40);
		else
			this.visible = true;
	},
	animHideFormStep: function (step) {
		this.element.style.height = (this.openedHeight * step / this.steps) + "px";
		if (step > 0)
			setTimeout(this.animHideFormStep.bind(this, step - 1), 40);
		else {
			this.visible = false;
			this.element.style.display = 'none';
		}
	},
	nameDefValue: 'Your name',
	onFocusName: function () {if (this.inpName.value == this.nameDefValue) this.inpName.value = '';},
	onBlurName: function () {if (this.inpName.value == '') this.inpName.value = this.nameDefValue;},
	emailDefValue: 'Your e-mail',
	onFocusEmail: function () {if (this.inpEmail.value == this.emailDefValue) this.inpEmail.value = '';},
	onBlurEmail: function () {if (this.inpEmail.value == '') this.inpEmail.value = this.emailDefValue;},

	submitData: function () {
		var email = this.inpEmail.value, name = this.inpName.value;
		if (!name || !email || name == this.nameDefValue || email == this.emailDefValue) {
			alert ('Please fill both your name and email address!');
			if (!name || name == this.nameDefValue) this.inpName.focus(); else this.inpEmail.focus();
			return;
		}
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-\.])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)) {
			alert ('The email address provided is not valid!');
			this.inpEmail.focus();
			return;
		}
		Form.disable(this.form);
		new Ajax.Request ('/newsletter_signup.php', {method:'post', onSuccess: this.submitData_handler.bind(this),
			onFailure:this.submitData_error.bind(this), onComplete: this.enableForm.bind(this),
			parameters: {name:name, email:email} });
	},
	enableForm: function () { Form.enable(this.form); },
	submitData_handler: function (ajaxRequest) {
		var rt = ajaxRequest.responseText;
		if (rt == 'OK')
			alert ("Thank you for signing up for Kangaroo Partners newsletter\n" +
				"You will recieve confirmation email. Follow the instructions described there" );
		else
		if (rt == 'er1')
			alert ("You are already registered for our newsletter!");
		else
			this.submitData_error();
	},
	submitData_error: function () {
		alert ("An error was encountered while signing you up. We are sorry for the inconvenience\n" +
			"Please try again later");
	}
}
