/**
* Agathons Dialog Boxes 
* Versjon: 0.1
*
* Dette er et skript som gjør det mulig å vise dialogbokser på en side.
*/
var dialogProducer = new DialogProducer();
var OK_PRESSED     = 1;
var CANCEL_PRESSED = 2;
var EXIT_PRESSED   = 3

function DialogProducer() {
    
    var documntObj;
    var okAction;
    var cancelAction;
    var exitAction;
    
    var dialogWidth;
    var dialogHeight;
    var dialogTop;
    var dialogLeft;
    
    this.showConfirmDialog  = showConfirmDialog;
    this.showMessageDialog  = showMessageDialog;
    this.showCustomDialog   = showCustomDialog;
    this.showProgressDialog = showProgressDialog;
    this.eventHappened      = eventHappened;
    this.setVisible         = setVisible;
    this.placeDialog        = placeDialog;
    this.clearActions       = clearActions;
    
    function showConfirmDialog(title, text, action) {
        dialogObj         = findObj('dialog_close');
        dialogTitleObj    = findObj('dialog_title');
        dialogTextObj     = findObj('dialog_text');
        dialogButtonsObj  = findObj('dialog_button_div');
        
        dialogTitleObj.innerHTML   = title;
        dialogTextObj.innerHTML    = text;
        dialogTextObj.style.textAlign = "center";
        dialogButtonsObj.innerHTML = "<input type='submit' value='Ok' class='dialog_buttons' onMouseUp='dialogProducer.eventHappened(OK_PRESSED)'><input type='submit' value='Cancel' class='dialog_buttons' onMouseUp='dialogProducer.eventHappened(CANCEL_PRESSED)'>";
        
        this.okAction     = action;
        this.cancelAction = new CloseAction();
        this.exitAction = new CloseAction();
        
        this.placeDialog();
        this.setVisible(true);
    }
    
    function showMessageDialog(title, text) {
        dialogObj         = findObj('dialog_close');
        dialogTitleObj    = findObj('dialog_title');
        dialogTextObj     = findObj('dialog_text');
        dialogButtonsObj  = findObj('dialog_button_div');
        
        this.setVisible(false);
        
        dialogTitleObj.innerHTML   = title;
        dialogTextObj.innerHTML    = text;
        dialogTextObj.style.textAlign = "center";
        dialogButtonsObj.innerHTML = "<input type='submit' value='ok' class='dialog_buttons' onMouseUp='dialogProducer.eventHappened(OK_PRESSED)'>";
                
        this.okAction = new CloseAction();
        this.exitAction = new CloseAction();
        
        this.placeDialog();
        this.setVisible(true);
    }
    
    function showCustomDialog(title, contentElementName, okAction, cancelAction, exitAction) {
        dialogObj         = findObj('dialog_close');
        dialogTitleObj    = findObj('dialog_title');
        dialogTextObj     = findObj('dialog_text');
        contentElementObj = findObj(contentElementName);
        
        if (contentElementObj == null) { return; }
        
        this.setVisible(false);
        
        dialogTitleObj.innerHTML      = title;
        dialogTextObj.innerHTML       = contentElementObj.innerHTML;
        dialogTextObj.style.textAlign = "center";
        
        this.okAction = okAction;
        this.cancelAction = cancelAction;
        this.exitAction = exitAction;

        this.placeDialog();
        this.setVisible(true);       
    }
    
    function showProgressDialog(title, text) {
        dialogObj         = findObj('dialog_close');
        dialogTitleObj    = findObj('dialog_title');
        dialogTextObj     = findObj('dialog_text');
        dialogButtonsObj  = findObj('dialog_button_div');
        
        this.setVisible(false);
        
        dialogTitleObj.innerHTML   = title;
        dialogTextObj.innerHTML    = text;
        dialogTextObj.style.textAlign = "center";
        dialogButtonsObj.innerHTML = "<img src='external/dialogs/images/dialog_progress.gif'>";
                
        this.placeDialog();
        this.setVisible(true);
    }
    
    function eventHappened(eventCode) {
        if (eventCode == OK_PRESSED && this.okAction != null) {
            this.okAction.doAction();
        }
        else if (eventCode == EXIT_PRESSED && this.exitAction != null) {
            this.exitAction.doAction();
        }
        else if (eventCode == CANCEL_PRESSED && this.cancelAction != null) {
            this.cancelAction.doAction();
        }
    }
    
    function setVisible(visible) {
        dialogObj = findObj('dialog_close');
        modalObj  = findObj('dialog_modal_div');
        
        if (visible) {
            dialogObj.style.visibility = "visible";
            modalObj.style.visibility = "visible";
        } 
        else {
            dialogObj.style.visibility = "hidden";
            modalObj.style.visibility = "hidden";
        }
    }
    
    function placeDialog() {
        dialogObj = findObj('dialog_close');
        modalObj  = findObj('dialog_modal_div');
        
        var innerTop    = get_inner_top();
        var innerLeft   = get_inner_left(); 
        var innerWidth  = get_inner_width();
        var innerHeight = get_inner_height();
        
        if (innerTop == null)  { innerTop = 0; }
        if (innerLeft == null) {innerLeft = 0; }
        
        //alert(innerTop + ", " + innerLeft + ", " + innerWidth + ", " + innerHeight);
        
        modalObj.style.top    = innerTop; //    + "px";
        modalObj.style.left   = innerLeft; //   + "px";
        modalObj.style.width  = innerWidth; //  + "px";
        modalObj.style.height = innerHeight;// + "px";

        dialogObj.style.top  = (innerTop  + (innerHeight / 2) -  50);// + "px";
        dialogObj.style.left = (innerLeft + (innerWidth  / 2) - 200);// + "px";
    
        setTimeout('dialogProducer.placeDialog()', 100);
    }
    
    function clearActions() {
        this.okAction     = null;
        this.cancelAction = null;
        this.exitAction   = null;
    }
}

function CloseAction() {
        
    this.doAction = doAction;
    
    function doAction() {
        dialogProducer.setVisible(false);
        dialogProducer.clearActions();
    }
}

function RedirectAction(URL) {
    var redirectURL;
    this.redirectURL = URL;

    this.doAction = doAction;
    
    function doAction() {
        dialogProducer.setVisible(false);
        dialogProducer.clearActions();
        window.location=this.redirectURL;
    }
}