// kolamon dialog

function KolamonDialog(){
    this.dialogId = 'kolamonDialog';
    this.mainId = 'kolamonDialogMainTD';
    this.transparentDivId = 'transparentDiv';
    this.bodyId = 'kolamonDialogBody';
    this.textId = 'kolamonDialogText';
    this.footerId = 'kolamonDialogFooter';
    this.closeId = 'kolamonDialogCloseDiv';
    this.width = null;
    this.defaultWidth = 300;
    this.height = null;
    this.isBorder = false;
    this.isClosable = false;

    // paramsObj.autoHideDelay - in miliseconds
    // paramsObj.autoHideAction - the callback
    this.show = function(paramsObj){

        $(this.transparentDivId).show();
        $(this.dialogId).show();
        this.repaint();

        if (isNotNull(paramsObj) && isNotNull(paramsObj.autoHideDelay)){
            setTimeout(this.hide, paramsObj.autoHideDelay);
            if (isNotNull(paramsObj.autoHideAction)){
                setTimeout(paramsObj.autoHideAction, paramsObj.autoHideDelay);
            }
        }
    }

    this.repaint = function(){
        $(this.dialogId).width = this.width;
        $(this.dialogId).style.right = (document.body.offsetWidth - this.width) / 2;
        $(this.dialogId).style.width = this.width;
        if (this.height == null){
            $(this.dialogId).style.height = "";
            $(this.mainId).style.height = "";
            $(this.bodyId).style.height = "";
            $(this.dialogId).style.top = document.body.scrollTop + ((document.body.offsetHeight - $(this.mainId).offsetHeight) / 2);
            $(this.dialogId).style.height = $(this.mainId).offsetHeight;
            $(this.bodyId).style.border = "";
        } else {
            $(this.dialogId).style.top = document.body.scrollTop + ((document.body.offsetHeight - this.height) / 2);
            $(this.bodyId).style.height = this.height;
            $(this.mainId).style.height = this.height;
            $(this.dialogId).style.height = $(this.mainId).offsetHeight;
        }
        if (this.isBorder){
            $(this.bodyId).style.border = "1px solid #CFCFC5";
        } else {
            $(this.bodyId).style.border = "";
        }
        $(this.transparentDivId).style.height = document.body.scrollHeight;
    }

    this.hide = function(delayInMillis){
        // !!! don't use here the word 'this'. it will refer the caller button !!!
        $('kolamonDialog').hide();
        $('transparentDiv').hide();
    }

        // myParam is the text itself, or json: {text, width, height, align}
    this.setBody = function(myParam){
        if (isNotNull(myParam.text)){
            $(this.textId).innerHTML = myParam.text;
            if (isNotNull(myParam.width)){
                this.width = myParam.width;
            } else {
                this.width = this.defaultWidth;
            }
            if (isNotNull(myParam.height)){
                this.height = myParam.height;
            } else {
                this.height = null;
            }
            if (isNotNull(myParam.isBorder)){
                this.isBorder = myParam.isBorder;
            } else {
                this.isBorder = false;
            }
            if (isNotNull(myParam.align)){
                $(this.bodyId).style.textAlign = (myParam.align == 'side' ? getLanguageAlignment() : 'center');
            } else {
                $(this.bodyId).style.textAlign = '';
            }
            if (isNotNull(myParam.isClosable)){                
                if (myParam.isClosable){
                    $(this.closeId).show();
                }else{
                    $(this.closeId).hide();
                }
            } else {
                $(this.closeId).hide();
            }
        } else {
            $(this.textId).innerHTML = myParam;
            this.width = this.defaultWidth;
            this.height = null;
            this.isBorder = false;
            $(this.closeId).hide();
            $(this.bodyId).style.textAlign = '';
        }
    }

    this.getBody = function(){
        return $(this.textId).innerHTML;
    }

    this.getBodyElement = function(){
        return $(this.textId);
    }

    this.setButtons = function(){
        this.clearFooter();
        if (arguments.length==0 || arguments[0]==""){
            return;
        }
        for(var i=0; i<arguments.length; i++){
            var button = arguments[i];
            var buttonElement = document.createElement("button");
            buttonElement.className = "kolamonControl";
            buttonElement.onclick = button.onclick;
            buttonElement.style.margin = "5px";
            buttonElement.style.minWidth = "30px";
            if (isNotNull(button.id)){
                buttonElement.id = button.id;
            }
            new Element.update(buttonElement, button.caption);
            $(this.footerId).appendChild(buttonElement);
        }
    }

    this.clearFooter = function(){
        $(this.footerId).update();
    }

    this.showLoading = function(text){
        if (isNull(text)){
            text = "";
        }
        this.setBody("<div align='center'>" + $('loadingDiv').innerHTML + text + "</div>");
        $(this.footerId).update();
        this.show();
    }
}


var kolamonDialog = new KolamonDialog();
