dojo.require("dijit.Dialog");

dojo.declare("BellaDialog", null,

{
    constructor: function( initParamsArray )
    {
        var initParams = (initParamsArray) ? initParamsArray : new Array();
        this.timestamp = new Date().getTime();
        initParams['id'] = 'dialogBox' + this.timestamp;
        this.dialog = new dijit.Dialog(initParams);
    },

    showInfoDialog:function( title, text, closeBtnTxt )
    {
        var closeSubTemplate = (closeBtnTxt == "") ? "" : "<a href=\"Javascript:void(0);\" onclick=\"dijit.byId('dialogBox${timestamp}').hide();return false; \" class=\"btnSubmit\"><span>${closeBtnTxt}</span></a>";
        var basicTemplate = "<div class=\"modalWindow\"><h1>${title}</h1><p>${text}</p>" + closeSubTemplate + "<br class=\"clearBoth\" /></div>";
        this.dialog.setContent(dojo.string.substitute(basicTemplate, {title:title,text:text,closeBtnTxt:closeBtnTxt,timestamp:this.timestamp}));
        this.dialog.show();
    },

    showErrorDialog:function( title, errors, closeBtnTxt )
    {
        var errorList = this.messageListToString(errors);
        this.showInfoDialog("Error", errorList, "Ok");
    },

    showLoadedDialog:function( src )
    {
        this.dialog.setHref(src);
        this.dialog.show();
    },
    
    showConfirmDialog:function( title, text, noBtnClick, noBtnText, yesBtnClick, yesBtnText )
    {
        var confirmTemplate = "<div class=\"modalWindow\"><h1>${title}</h1><p>${text}</p><div class=\"dottedRuleTop\"><a href=\"Javascript:void(0);\" class=\"btnSubmitGrey\" onclick=\"dijit.byId('dialogBox${timestamp}').hide();${noBtnClick}return false;\"><span>${noBtnText}</span></a><div><a href=\"Javascript:void(0)\" class=\"btnSubmit\" onclick=\"dijit.byId('dialogBox${timestamp}').hide();${yesBtnClick}return false;\"><span>${yesBtnText}</span></a><br class=\"clearBoth\"/></div><br class=\"clearBoth\"/></div></div>";
        this.dialog.setContent(dojo.string.substitute(confirmTemplate, {title:title,text:text,noBtnText:noBtnText,noBtnClick:noBtnClick,yesBtnText:yesBtnText,yesBtnClick:yesBtnClick,timestamp:this.timestamp}));
        this.dialog.show();
    },

    messageListToString: function( messageList )
    {
        if ( messageList.length == 0 )
        {
            return "";
        }

        var messages = "<ul>";
        for ( var i = 0; i < messageList.length; i++ )
        {
            messages += ("<li>" + messageList[i] + "</li>");
        }
        messages += "</ul>";
        return messages;
    }

}
        )

/*
 * FixedDialog is an implementation of Dialog that does not automatically vertically center itself, instead it is fixed
 * at the top of the browser. The DialogUnderlay has been removed because FireFox does not correctly display Flash movies
 * if they are on top of a transparent div.
 */
dojo.declare("FixedDialog", dijit.Dialog,
{
    _position: function()
    {
        // summary: position modal dialog in center of screen

        if ( dojo.hasClass(dojo.body(), "dojoMove") )
        {
            return;
        }
        var viewport = dijit.getViewport();
        var mb = dojo.marginBox(this.domNode);

        var style = this.domNode.style;
        style.left = Math.floor((viewport.l + (viewport.w - mb.w) / 2)) + "px";
        style.top = "8px";
    },

    _setup: function()
    {
        // summary:
        //		stuff we need to do before showing the Dialog for the first
        //		time (but we defer it until right beforehand, for
        //		performance reasons)

        if ( this.titleBar )
        {
            this._moveable = new dojo.dnd.TimedMoveable(this.domNode, { handle: this.titleBar, timeout: 0 });
        }

        this._underlay = new dijit.DialogUnderlay({
            id: this.id + "_underlay",
            "class": dojo.map(this["class"].split(/\s/), function( s )
            {
                return s + "_underlay";
            }).join(" ")
        });

        var node = this.domNode;
        this._fadeIn = dojo.fx.combine(
                [dojo.fadeIn({
                    node: node,
                    duration: this.duration
                }),
                    dojo.fadeIn({
                        node: this._underlay.domNode,
                        duration: this.duration,
                        onBegin: dojo.hitch(this._underlay, "show")
                    })
                ]
                );

        this._fadeOut = dojo.fx.combine(
                [dojo.fadeOut({
                    node: node,
                    duration: this.duration,
                    onEnd: function()
                    {
                        node.style.visibility = "hidden";
                        node.style.top = "-9999px";
                    }
                }),
                    dojo.fadeOut({
                        node: this._underlay.domNode,
                        duration: this.duration,
                        onEnd: dojo.hitch(this._underlay, "hide")
                    })
                ]
                );
    },
    
    hide: function()
    {
    	console.debug("Destroying the dialog on hide.")
        this.destroy();
    },

    layout: function()
    {
        // summary: position the Dialog and the underlay
        if ( this.domNode.style.visibility != "hidden" )
        {
            //this._underlay.layout();
            this._position();
        }
    }
}
        )

dojo.declare("FlashDialog", dijit.Dialog,
{
    constructor: function( initParamsArray )
    {
        var initParams = (initParamsArray) ? initParamsArray : new Array();
        this.timestamp = new Date().getTime();
        initParams['id'] = 'dialogBox' + this.timestamp;
        initParams['class'] = 'flashDialog';
        this.dialog = new FixedDialog(initParams);
    },

    showLoadedDialog:function( src )
    {
        this.dialog.setHref(src);
        this.dialog.show();
    },

    hide: function()
    {
        this.dialog.hide();
    }
}
        )
