/*
 Popup form class
 Requires PopupBlock.js, jquery.js, jquery_form.js
 Author: Stepan Reznikov (stepan@design.ru)
 08.09.2008
 */
function PopupForm(spec) {
	
	// вызов конструктора базового класса PopupBlock
	PopupForm.baseConstructor.call(this, spec.container.get(0), spec.link.get(0), spec.fader && spec.fader.get(0));

	this.form = spec.container.find("form");
	this.input = this.form.find("input[type='text']").eq(0);
    this.progress = spec.container.find(".progress"); 
    this.message = spec.container.find(".message");
    this.close = spec.container.find(".close");

    var me = this;

    var options = {
        target: this.message,
        beforeSubmit: function () {
            me.startSubmit();
        },
        success: function () {
            me.showResponse();
        }
    };

    if (spec.url && typeof spec.url === 'string') {
        options.url = spec.url;
    }
    
    // jQuery Form Plugin
    this.form.ajaxForm(options);

    this.close.click(function (event) {
        event.preventDefault();
        event.stopPropagation(); 
        me.hide(event);
    });

}

// наследуем от PopupBlock
PopupForm.inheritFrom(PopupBlock, {

	show: function (event) {
		PopupForm.superClass.show.call(this, event);
		this.input.focus();
	},
	
	// pre-submit callback
    startSubmit: function () {
        this.progress.removeClass('hidden');
        return true;
    },
    
    // post-submit callback 
    showResponse: function () {
        var me = this;
        setTimeout(function () {
            me.progress.addClass('hidden');
            me.message.removeClass('hidden');
            me.form.addClass('hidden');
        }, 1000);
    }
});