/**
* Script contains all wrapper functions for the Ext Window Library.
*/

/**
 * Creates a class called WindowUtils.
 */
var WindowUtils = {
    version : "1.0.0"
};

/**
 * Opens please wait overlay.
 *
 * @param message to render
 * @param width of the window
 * @param height of the window
 */
WindowUtils.windowShowPleaseWait = function(message, width, height) {
    Ext.MessageBox.show({
        msg: message,
        modal: true,
        width:width,
        height:height,
        wait:true,
        waitConfig: {interval:200}
    });

}

/**
 * Opens a new Ext Window with a title and text content inside.
 *
 * @param titleText for the window title
 * @param contentText for the window body
 * @param width for the window
 * @param height for the window
 */
WindowUtils.windowOpenContentWindow = function(titleText, contentText, width, height) {
    var myWnd = new Ext.Window({
        id:'myWin_' + (Math.floor(Math.random()*1000))
        ,modal: false
        ,autoScroll: true
        ,width: width
        ,height: height
        ,title: titleText
        ,html: '<div style=\"margin:10px;\">' + contentText + '</div>'
    });

    myWnd.show();

    return myWnd;
}

/**
 * Opens a new Ext Window with a title and text content inside.
 *
 * @param titleText for the window title
 * @param contentText for the window body
 * @param width for the window
 * @param height for the window
 * @param modal is window modal
 */
WindowUtils.windowOpenContentWindow = function(titleText, contentText, width, height, modal) {
    var myWnd = new Ext.Window({
        id:'myWin_' + (Math.floor(Math.random()*1000))
        ,modal: modal
        ,autoScroll: true
        ,width: width
        ,height: height
        ,title: titleText
        ,html: '<div style=\"margin:10px;\">' + contentText + '</div>'
    });

    myWnd.show();

    return myWnd;
}

/**
 * Opens a new Ext Window with a title and text content inside.
 *
 * @param titleText for the window title
 * @param contentText for the window body
 * @param width for the window
 * @param height for the window
 * @param modal is window modal
 * @param listeners for window
 */
WindowUtils.windowOpenContentWindow = function(titleText, contentText, width, height, modal, listeners) {
    var myWnd = new Ext.Window({
        id:'myWin_' + (Math.floor(Math.random()*1000))
        ,modal: modal
        ,autoScroll: true
        ,width: width
        ,height: height
        ,title: titleText
        ,html: '<div style=\"margin:10px;\">' + contentText + '</div>'
        ,listeners: listeners
    });

    myWnd.show();

    return myWnd;
}

/**
 * Opens a new Ext Window with a URL for the content
 *
 * @param url to be rendered within the window
 * @param titleText for the window title
 * @param width for the window
 * @param height for the window
 * @param listeners for the window (except show)
 */
WindowUtils.windowOpenUrl = function(url, titleText, width, height, iframeWindowName, listeners) {
    var _iframeWindowName;
    if (iframeWindowName == undefined || StringUtils.isWhitespace(iframeWindowName)) {
        _iframeWindowName = "myFrame_" + (Math.floor(Math.random()*11));
    } else {
        _iframeWindowName = iframeWindowName;
    }

    var onShow = function() {
            this.loadMask = new Ext.LoadMask(this.body, {
                msg:'Loading. Please wait...'
            });
        };

    if (listeners == undefined) {
        listeners = {show:onShow};
    } else {
        listeners.show = onShow;
    }

    var myWnd = new Ext.Window({
        id:'myWin_' + (Math.floor(Math.random()*1000))
        ,modal: true
        ,width: width
        ,height: height
        ,title: titleText
        ,html: '<iframe name="' + _iframeWindowName + '" frameborder="0" src="' + url + '" style="width:100%;height:100%;border:none;"></iframe>'
        ,listeners: listeners
    });

    myWnd.show();

    return myWnd;
}


/**
 * Closes the please wait window.
 */
WindowUtils.windowClosePleaseWait = function() {
    Ext.MessageBox.hide();
}


WindowUtils.openLargeImage = function(largeImage) {
    var myWnd = new Ext.Window({
        id:'myWin_' + (Math.floor(Math.random()*1000))
        ,modal: true
        ,width: 450
        ,height: 350
        ,html: '<br><center><img src="' + largeImage + '"></center>'
    });
    myWnd.show();

    return myWnd;
}

