YAHOO.namespace('zoeyui');

YAHOO.zoeyui.overlay_manager = null;

YAHOO.zoeyui.Panel = function(element_id, header) {
    this.element_id = element_id;
    this.header = typeof(header) != 'undefined' ? header : '&nbsp;';
    this.yui_panel = null;
    this.createPanel();
};

YAHOO.zoeyui.Panel.prototype.createPanel = function() {
    if (this.yui_panel == null || this.yui_panel == undefined) {
        this.yui_panel = new YAHOO.widget.Panel(this.element_id);
    }
    this.yui_panel.setHeader(this.header);
    this.yui_panel.cfg.queueProperty('constraintoviewport', true);
};

YAHOO.zoeyui.Panel.prototype.showLoading = function() {
    this.yui_panel.setHeader("Loading, please wait...");
    this.yui_panel.setBody("<img src='http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif' />");
    this.show();
};

YAHOO.zoeyui.Panel.prototype.fetchContent = function(url) {
    var self = this;
    this.showLoading();
    YAHOO.util.Connect.asyncRequest('GET', url, {
            success: self.onRecvContent,
            failure: self.onRecvContentFail,
            scope: this
    });
};

YAHOO.zoeyui.Panel.prototype.onRecvContent = function(req) {
    this.yui_panel.setHeader(this.header);
    this.yui_panel.setBody(req.responseText);

    this.show();
};

YAHOO.zoeyui.Panel.prototype.onRecvContentFail = function(req) {
    this.onRecvContent(req);
}

YAHOO.zoeyui.Panel.prototype.show = function() {
    this.yui_panel.render(document.body);
    this.yui_panel.show();
    this.yui_panel.center();
    if (YAHOO.zoeyui.overlay_manager != null) {
        YAHOO.zoeyui.overlay_manager.register(this.yui_panel);
    }
};

YAHOO.zoeyui.Panel.prototype.hide = function() {
    this.yui_panel.render(document.body);
    this.yui_panel.hide();
};


YAHOO.zoeyui.Dialog = function(element_id, header) {
    YAHOO.zoeyui.Dialog.superclass.constructor.call(this, element_id, header);
};

YAHOO.extend(YAHOO.zoeyui.Dialog, YAHOO.zoeyui.Panel, {
    createPanel: function() {
        var self = this;
        if (this.yui_panel == null || this.yui_panel == undefined) {
            this.yui_panel = new YAHOO.widget.Dialog(this.element_id);
        }
        this.yui_panel.cfg.queueProperty('constraintoviewport', true);
        this.yui_panel.cfg.queueProperty('close', false);
        this.yui_panel.callback = {
            success: self.onSubmitSuccess,
            failure: self.onRecvContentFail,
            scope: self
        };
    },
    showLoading: function() {
        this.yui_panel.cfg.queueProperty('buttons', []);
        YAHOO.zoeyui.Dialog.superclass.showLoading.call(this);
    },
    onSubmitSuccess: function(req) {
        this.yui_panel.cfg.queueProperty('buttons', this.buildSuccessButtonList());
        YAHOO.zoeyui.Dialog.superclass.onRecvContent.call(this, req);
    },
    buildSuccessButtonList: function() {
        var button_list = [
            {
                text: 'Close',
                isDefault: true,
                handler: this.yui_panel.cancel
            }
        ];
        return button_list;
    },
    buildButtonList: function() {
        var button_list = [
            {
                text: 'Cancel',
                isDefault: false,
                handler: this.yui_panel.cancel
            }, {
                text: 'Submit',
                isDefault: true,
                handler: this.yui_panel.submit
            }
        ];
        return button_list;
    },
    onRecvContent: function(req) {
        this.yui_panel.cfg.queueProperty('buttons', this.buildButtonList());
        YAHOO.zoeyui.Dialog.superclass.onRecvContent.call(this, req);
    }
});


YAHOO.zoeyui.UploadDialog = function(element_id, header) {
    YAHOO.zoeyui.UploadDialog.superclass.constructor.call(this, element_id, header);
};
YAHOO.extend(YAHOO.zoeyui.UploadDialog, YAHOO.zoeyui.Dialog, {
    submit: function() {
        this.yui_panel.submit();
        this.showLoading();
    },
    buildButtonList: function() {
        var button_list = [
            {
                text: 'Cancel',
                isDefault: false,
                handler: this.yui_panel.cancel
            }, {
                text: 'Upload',
                isDefault: true,
                handler: {fn: this.submit, scope: this}
            }
        ];
        return button_list;
    },
    createPanel: function() {
        YAHOO.zoeyui.UploadDialog.superclass.createPanel.call(this);
        var self = this;
        this.yui_panel.callback = {
            upload: self.onRecvContent,
            scope: self
        };
    }
});
