﻿// JScript File

function ituma_yesno(text,action) {
    var dlg = new ituma_dlg({
        text : text,
        onFinished : function(dlg,result) {
            if (dlg.result == "no")
                return;
            window.location.href = action;
        }
    });
}

var ituma_dlg = Class.create({
    divMain : null,
    divInner : null,
    divText : null,
    form : null,
    buttons : null,
    config : null,
    result : null,
    initialize : function(config) {
        this.config = new Object();
        this.buttons = new Array();
        this.config.mode = "yesno";
        this.config.text = "dummytext";
        this.config.pos = new Object();
        Object.extend(this.config, config || { });
        this.divMain = new Element('div',{'id':'itumaDlgMain'});
        $$('body')[0].insert({'top':this.divMain});

        this.config.pos.x = (document.viewport.getDimensions().width - this.divMain.getWidth()) / 2;
        this.config.pos.y = (document.viewport.getDimensions().height - this.divMain.getHeight()) / 2;
        this.divMain.setStyle({'left':this.config.pos.x+'px','top':this.config.pos.y+'px'});
        
        this.divInner = new Element('div');
        this.divInner.addClassName('itumaDlgInner');
        this.divMain.insert({'bottom':this.divInner});
        
        this.form = new Element('form');
        this.divInner.insert({'bottom':this.form});
        
        this.divText = new Element('div');
        this.divText.addClassName('itumaDlgText');
        this.divInner.insert({'top':this.divText});
        this.divText.update(this.config.text);

        var button;
        if (this.config.mode == "yesno") {
            button = new Element('button');
            button.update("Ja");
            button.observe('click',this.buttonClick.bindAsEventListener(this,"yes"));
            this.buttons.push(button);
            
            button = new Element('button');
            button.update("Nein");
            button.observe('click',this.buttonClick.bindAsEventListener(this,"no"));
            this.buttons.push(button);
        }
        var i;
        for (i=0;i<this.buttons.length;i++) {
            this.form.insert({'bottom':this.buttons[i]});
        }
    },
    buttonClick : function(event) {
        event.stop();
        var args = $A(arguments);
        args.shift();
        this.result = args[0];
        this.hide();
    },
    hide : function() {
        this.divMain.remove();
        if (this.config.onFinished) {
            this.config.onFinished(this,this.result);
        }
    }
});
