// kolamon dialog

function SocialDialog(myId){
    var _this = this;
    _this.id = myId;
    _this.dialogId = 'socialDialog' + myId;
    _this.mainId = 'socialDialogMainTD' + myId;
    _this.transparentDivId = 'transparentDiv' + myId;
    _this.bodyId = 'socialDialogBody' + myId;
    _this.textId = 'socialDialogText' + myId;
    _this.footerId = 'socialDialogFooter' + myId;
    _this.closeId = 'socialDialogCloseDiv' + myId;
    _this.width = null;
    _this.defaultWidth = 300;
    _this.height = null;
    _this.isBorder = false;
    _this.isClosable = false;

    $('dialogTemplateDiv').innerHTML += dialogTemplateHtml.replace(/_MYID/g, myId);

    // paramsObj.autoHideDelay - in miliseconds
    // paramsObj.autoHideAction - the callback
    _this.show = function(paramsObj){
        var dialogCount = dialogsStack.size();
        $(_this.transparentDivId).style.zIndex = dialogCount*2+11;
        $(_this.dialogId).style.zIndex = dialogCount*2+12;
        $(_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);
            }
        }
        dialogsStack.push(_this);
    }

    _this.setWidth = function(width){
        _this.width = width;
    }

    _this.repaint = function(){
        $(_this.dialogId).width = _this.width;
        $(_this.dialogId).style.right = (document.body.clientWidth - _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.clientHeight - $(_this.mainId).clientHeight) / 2);
            $(_this.dialogId).style.height = $(_this.mainId).clientHeight;
            $(_this.bodyId).style.border = "";
        } else {
            $(_this.dialogId).style.top = document.body.scrollTop + ((document.body.clientHeight - _this.height) / 2);
            $(_this.bodyId).style.height = _this.height;
            $(_this.mainId).style.height = _this.height;
            $(_this.dialogId).style.height = $(_this.mainId).clientHeight;
        }
        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(){
        // !!! don't use here the word '_this'. it will refer the caller button !!!
        $(_this.dialogId).hide();
        $(_this.transparentDivId).hide();
        _this.getBody().innerHTML = "";
        dialogsStack.pop();
    }

        // 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;
            }
            if (isNotNull(button.disabled)){
                buttonElement.className = 'disabled';
                buttonElement.disabled = 'disabled';
            }
            new Element.update(buttonElement, button.caption);
            $(_this.footerId).appendChild(buttonElement);
        }
    }

    _this.getButtonsHtml = function(){
        return $(_this.footerId)
    }

    _this.clearFooter = function(){
        $(_this.footerId).update();
    }    
}


var dialogsStack = new Array();
var errorMessageDialog = new SocialDialog("errorMessageDialog");
var identificationDialog = new SocialDialog("identificationDialog");
var submitLinkDialog = new SocialDialog("submitLinkDialog");
var selectImageDialog = new SocialDialog("selectImageDialog");
var selectTagsDialog = new SocialDialog("selectTagsDialog");
var datesDialog = new SocialDialog("datesDialog");
var profileDialog = new SocialDialog("profileDialog");
var moreActionsDialog = new SocialDialog("moreActionsDialog");
var addEventDialog = new SocialDialog("addEventDialog");
var thankyouDialog = new SocialDialog("thankyouDialog");
var contactUsDialog = new SocialDialog("contactUsDialog");
var aboutUsDialog = new SocialDialog("aboutUsDialog");
var generalDialog = new SocialDialog("generalDialog");
var itemAdditionalLinksDialog = new SocialDialog("itemAdditionalLinksDialog");
var forgotPasswordDialog = new SocialDialog("forgotPasswordDialog");
var unsubscribeDialog = new SocialDialog("unsubscribeDialog");

