

var EmailAFriend = {
	sliderHeight: 265,
	slideduration: 0.3,
	isOpen: false,
	effect: {state:"finished"},
	initialize: function() {
		if (emailAFriendPostURL === undefined) throw 'ERROR, emailAFriendPostURL was not set beforehand';
		var elm = this.elm = $('emailAFriend');
		
		var opener		= this.opener 	= $('openEmailAFriend');	
		var slider		= this.slider	= $$('#emailAFriend .slider')[0].setStyle({height:'0px'});
		var form		= this.form		= Form.disable($$('#emailAFriend form')[0]).hide();
		var error		= this.error	= $$('#emailAFriend .error')[0].hide();
		var success		= this.success	= $$('#emailAFriend .success')[0].hide();
		
		error.update = success.update = function(update){ this.firstDescendant().update(update); return this; };
		
		opener.observe('click', function(event) {
			Event.element(event).blur();
			(this.isOpen)? this.close() : this.showForm();
			Event.stop(event);
		}.bindAsEventListener(this));

		form.observe('submit', function(event) {
			Event.stop(event);
			this.sendEmail();

		}.bindAsEventListener(this));


		elm.observe('click', function(event) { (event||window.event).cancelBubble = true;  });
		Event.observe(document.body, 'click', this.close.bind(this));
	},
	sendEmail: function() {
		this.close();
		// http://www.brightir.com/

		new Ajax.Request(emailAFriendPostURL, {
			method: 'post',
			postBody: this.form.enable().serialize()+'&url='+encodeURI(window.location)+'&ajah=true&schema=friend',
			onSuccess: function(response) {
				var f = function() {
					this.form.reset();
					this.showSuccess(response.responseText)
				}.bind(this);
				(this.isOpen)? this.slider.observeOnce('slider:afterClose', f) : f();
			}.bind(this),
			onFailure: function(response) { 
				var f = function() {this.showError(response.responseText)}.bind(this);
				(this.isOpen)? this.slider.observeOnce('slider:afterClose', f) : f();
			}.bind(this),
			onException: function(r, e) {alert(e);}
		});
		this.form.disable();
	},
	showForm: function() {
		if (this.isOpen){
			this.slider.observeOnce('slider:afterClose', this.showForm.bind(this)); 
			this.close();
			return;
		}
		this.slider.observeOnce('slider:afterOpen', function(){ this.form.enable().focusFirstElement() }.bind(this))
		this.slider.observeOnce('slider:beforeClose', function(){ this.form.disable() }.bind(this))
		this.form.show();
		this.open();
	},
	showSuccess: function(message) {
		this.success.update(message);
		this.success.show();
		this.open(this.success.clientHeight+40);
		clearTimeout(this.timeout)
		this.timeout = setTimeout(this.close.bind(this),6000)
	},
	showError: function(message) {
		this.error.update(message);
		this.error.show();
		this.open(this.error.clientHeight+40);
		clearTimeout(this.timeout)
		this.timeout = setTimeout(this.close.bind(this),6000)
	},
	
	open: function(size) {
		Event.fire(this.slider, 'slider:beforeOpen'); 
		this.isOpen = true;
		this.cancelEffect();
		this.effect = new Effect.Morph( this.slider, { 
      style:'height: '+(size||this.sliderHeight)+'px;',
      transition: Effect.Transitions.EaseTo,
      duration: this.slideduration,
			afterFinish: function() { Event.fire(this.slider, 'slider:afterOpen'); }.bind(this)
    });
	},
	close: function() {
		if(!this.isOpen) return;
		Event.fire(this.slider, 'slider:beforeClose'); 
		this.cancelEffect();
		this.effect = new Effect.Morph( this.slider, { 
      style:'height: 0px;',
      transition: Effect.Transitions.EaseFrom,
      duration: this.slideduration,
			afterFinish: function() { 
				[this.form,this.error,this.success].invoke('hide');
				this.isOpen = false; Event.fire(this.slider, 'slider:afterClose'); 
			}.bind(this)
    });
	},
	cancelEffect: function() {
		this.timeout = clearTimeout(this.timeout);
		if (typeof this.effect.state != 'undefined' & this.effect.state != "finished") return this.effect.cancel();		
	}
};

Event.observe(window,'load',function() {EmailAFriend.initialize()});
