Emv.Dialog = function () {

    // Set defaults.
    this.dialogName  = 'jq-dialog';
    this.resizable   = false;
    this.dialogObj   = null;
    this.dialogClass = '';

    /**
     * Set the dialog name.
     *
     * @param string dialogName
     */
    this.setDialogName = function(dialogName) {

        this.dialogName = dialogName;
    };

    /**
     * Get the dialog name.
     *
     * @return string dialogName
     */
    this.getDialogName = function() {

        return this.dialogName;
    };

    /**
     * Set the dialog class.
     *
     * @param string dialogClass
     */
    this.setDialogClass = function(dialogClass) {

        $("#" + this.dialogName).dialog("option", "dialogClass", dialogClass);
    };

    /**
     * Set the 'resizable'-flag.
     *
     * @param boolean resizable
     */
    this.setResizable = function(resizable) {

        this.resizable = resizable;
    };

    /**
     * Set the dialog object, used by jquery.
     *
     * @param string object
     */
    this.setDialogObject = function(object) {

        this.dialogObj = $('#' + this.dialogName);
    };

    /**
     * Shows a dialog box. The content of the dialog is given by the id of a html tag.
     *
     * @param string imageSrc Full image path.
     * @param string imageSubtitle The subtitle for the image (text under image).
     * @param string imgAltText The text for the alt tag of the image.
     * @param string dialogTitle The title of the dialog.
     * @return boolean
     */
    this.showImagePopup = function(imageSrc, imageSubtitle, imgAltText, dialogTitle) {

        if ('' === imageSrc) {

            return false;
        }

        this.setDialogObject(this.dialogName);
        var dialogName = this.dialogName;

        // Get the window height and width
        var winWidth  = $(window).width();
        var winHeight = $(window).height();

        // Get images size
        var imageUrlArr      = imageSrc.split("/");
        var imageSizeArr    = imageUrlArr[6].split("x");
        var imageHeightArr    = imageSizeArr[1].split(".");
        var imageWidth        = imageSizeArr[0].substring(1);
        var imageHeight        = imageHeightArr[0];

        //Set pop up window to center
        var popUpWidth  = winWidth/2 - imageWidth/2 - 15;
        var popUpHeight = winHeight/2 - imageHeight/2 - 30;

        this.dialogObj.dialog({
            title    : dialogTitle,
            modal    : true,
            hide     : ('fade', 'fast'),
            draggable: true,
            resizable: this.resizable
        });

        loaderImg = new Image();
        loaderImg.src = '/jslib/int/ajax-loader.gif';

        this.dialogObj.html(loaderImg);
        this.dialogObj.dialog('open');

        imageObj = new Image();
        $(imageObj).load(function() {

            img = $('#' + dialogName).find('img');

            if ('undefined' != typeof(imgAltText)) {

                img.alt = imgAltText;
            }

            img.attr('src', this.src);

            $('#' + dialogName).append('<p class="image-subtitle">' + imageSubtitle + '</p>');

            // Set width for dialog box (depends on image width)
            $('#' + dialogName).dialog({

                width   : this.width + 31,
                position: [popUpWidth,popUpHeight]
            });

            // Workaroud for IE 6 (without this fix the image would not appear correctly)
            img.hide();
            img.show();

            this.onload = null;
        });

        imageObj.src = imageSrc; // Triggers onload if image is cached

        // Close dialog if image is clicked
        $(this.dialogObj).find('img').bind('click', function() {

            $('#' + dialogName).dialog('close');
        });

        // Close dialog if image overlay is clicked
        $('.ui-widget-overlay').bind('click', function() {

            $('#' + dialogName).dialog('close');
        });

        return false;
    };

    /**
     * Shows a dialog box filled with a static image.
     *
     * @param string imageSrc Full image path.
     * @param integer imageWidth
     * @param integer imageHeight
     * @param string imageSubtitle The subtitle for the image (text under image).
     * @param string imgAltText The text for the alt tag of the image.
     * @param string dialogTitle The title of the dialog.
     * @return boolean
     */
    this.showStaticImagePopup = function(imageSrc, imageWidth, imageHeight, imageSubtitle, imgAltText, dialogTitle) {

        if ('' === imageSrc) {

            return false;
        }

        this.setDialogObject(this.dialogName);
        var dialogName = this.dialogName;

        // Get the window height and width
        var winWidth  = $(window).width();
        var winHeight = $(window).height();

        //Set pop up window to center
        var popUpWidth  = winWidth/2 - imageWidth/2 - 15;
        var popUpHeight = winHeight/2 - imageHeight/2 - 30;

        // Failover if image bigger than window: do not start above view scope
        if (popUpWidth<0) {

            popUpWidth=0;
        }
        if (popUpHeight<0) {

            popUpHeight=0;
        }

        this.dialogObj.dialog({
            title    : dialogTitle,
            modal    : true,
            hide     : ('fade', 'fast'),
            draggable: true,
            resizable: this.resizable
        });

        loaderImg = new Image();
        loaderImg.src = '/jslib/int/ajax-loader.gif';

        this.dialogObj.html(loaderImg);
        this.dialogObj.dialog('open');

        imageObj = new Image();
        $(imageObj).load(function() {

            img = $('#' + dialogName).find('img');

            if ('undefined' != typeof(imgAltText)) {

                img.alt = imgAltText;
            }

            img.attr('src', this.src);
            img.attr('height', imageHeight);
            img.attr('width', imageWidth);

            $('#' + dialogName).append('<p class="image-subtitle">' + imageSubtitle + '</p>');

            // Set width for dialog box (depends on image width)
            $('#' + dialogName).dialog({

                width   : this.width + 31,
                position: [popUpWidth,popUpHeight]
            });

            // Workaroud for IE 6 (without this fix the image would not appear correctly)
            img.hide();
            img.show();

            this.onload = null;
        });

        imageObj.src = imageSrc; // Triggers onload if image is cached

        // Close dialog if image is clicked
        $(this.dialogObj).find('img').bind('click', function() {

            $('#' + dialogName).dialog('close');
        });

        // Close dialog if image overlay is clicked
        $('.ui-widget-overlay').bind('click', function() {

            $('#' + dialogName).dialog('close');
        });

        return false;
    };

    /**
     * Opens a jquery dialog with the content of the given url, which is loaded via ajax (json!).
     *
     * @param string  url
     * @param integer dialogWidth
     */
    this.openDialogWithAjaxContent = function(url, dialogWidth) {

        // Set default width if none is given to 500.
        if (!dialogWidth) {
            dialogWidth = 500;
        }

        var request = new Emv.Ajax();

        // Avoid client cache
        var _now = new Date().getTime();

        request.setDialogWidth(dialogWidth);

        _uri = url + '?ts=' + _now;

        request.requestType = "GET";
        request.setCallback(this.ajaxCallback);
        request.request(_uri, '');
    };

    this.ajaxCallback = function (response) {

        var jqDialog = $("#jq-dialog");

        if ('undefined' == typeof(response) || 'success' != response.status) {

            jqDialog.dialog( "option", "buttons", "");

            jqDialog.dialog( "option", "title", "Es ist ein Fehler aufgetreten" );

            // Invalid status return!
            if ('undefined' !== typeof(response.errorMessage)) {

                jqDialog.html(response.errorMessage);
            } else {

                jqDialog.html("<p>Undefinierter Fehler beim Senden der Anfrage</p>");
            }
            jqDialog.dialog("open");

            return false;
        }

        jqDialog.dialog({ title: '',
                          width: this.dialogWidth,
                          modal: true,
                          hide: ('fade', 'slow'),
                          draggable: true,
                          resizable: false
                         });
        jqDialog.html(response.htmlContent);
        jqDialog.dialog("open");

        return true;
    };


    /**
     * Opens a jquery dialog with the content of the given div.
     *
     * @param string  divId
     * @param integer dialogWidth
     */
    this.openDialogWithDivContent = function(divId, dialogWidth) {

        // Set default width if none is given to 500.
        if (!dialogWidth) {
            dialogWidth = 500;
        }

        var jqDialog = $("#jq-dialog");

        jqDialog.dialog({ title: '',
            width: this.dialogWidth,
            modal: true,
            hide: ('fade', 'slow'),
            draggable: true,
            resizable: false
           });

        jqDialog.html(jQuery.extend(true, {}, $('#' + divId)));

        jqDialog.dialog("open");

        return true;
    };
};
