
Ext.ns("LSI.Widgets");

LSI.Widgets.AdInfoRequestForm = Ext.extend(Object, {

		anims : {
		},

		constructor : function(config) {

			Ext.apply(this, config);

			this.el = Ext.get(this.el);

			if (!this.el) throw "El not found";

			if (!this.ad_id) throw "Ad ID not given";

		},

		init : function() {

			this.form = this.el;
			if (!this.form.is('form'))
				this.form = this.el.child("form");

			if (!this.form)
				throw "No Form Found";

			this.el.on('click', this.onClick, this);

			this.button = this.el.child("button.submitBtn");

			this.button.on("click", this.onButtonClick, this);

		},

		onClick : function(e) {

			if ((linkEl = e.getTarget('.showPhoneLink'))) {
				// Show phone number, please !

				e.stopEvent();

				try {
					_gaq = _gaq || [];
					_gaq.push(['_trackEvent', 'Annonce', 'Click', 'PhoneNumber']);
				} catch (ex) {
					// Autruche
				}

				try {

					e.stopEvent();

					el = Ext.fly(linkEl).next('.realPhoneNumber');
					Ext.fly(linkEl).fadeOut({
							remove   : true,
							callback : function() {
								el.hide();
								el.removeClass("x-hidden");
								el.fadeIn().highlight("#7777ff");
							}
						});
				} catch(ex) {
					// Autruche
				}
				return;

			}

		},

		toggleLoading : function(show) {

			if (show) {
				this.button.dom.disabled = true;
				this.form.addClass("loading");
			} else {
				this.button.dom.disabled = false;
				this.form.removeClass("loading");
			}

		},

		onButtonClick : function(e) {

			e.stopEvent();
			this.toggleLoading(true);


			Ext.Ajax.request({
					form     : this.form,
					params   : {ad_id: this.ad_id},
					url      : "/lsi-ajax/sendAdInfoRequestMail.php",
					scope    : this,
					callback : function(options, success, response) {
						this.toggleLoading(false);
						if (!success) {
							this.showError(response.responseText || response.statusText);
						} else {
							this.showSuccess();
							try {
								_gaq.push(['_trackEvent', 'Annonce', 'Demande']);
							} catch(e) { }
						}
					}
				});

		},

		showError : function(msg) {
			alert(msg);
		},

		showSuccess : function() {

			var el, newEl;
			
			el = this.el;
			newEl = el.parent().createChild({
					html : "Votre message a été envoyé avec succès.",
					style: "text-align:center; padding: 50px 0; font-weight: bold;"
				});
			newEl.hide();
			el.enableDisplayMode().fadeOut();
			newEl.fadeIn();
		}


	});


