 /*
 * @author Klaus Lokmann Knudsen klaus@lokmann.dk 2010-01-22
 */
        if ($.browser.msie && parseInt($.browser.version)<7 ) {// skjuler select elems
        //$.getScript('js/jquery/plugins/jquery.random.js'/*, function() {alert('Random');}*/);
        $.getScript('js/jquery/plugins//bgiframe/jquery.bgiframe.min.js'/*, function() {alert('bgiframe_2.1.1 load was performed.');}*/);
    }

    /*
     * disse to lukkes ikke korrekt ved eks. escape
     */
    function closeHack() {
        $(".ac_results, .helpbox").hide();
    }

    function alertDialog(ajax,url,data,showTitle,title,width,height,okCaption,callBackOk){
         loadDialog(ajax,url,data,showTitle,title,width,height,true,okCaption,callBackOk,'','',callBackOk);
    }
    function confirmDialog(ajax,url,data,showTitle,title,width,height,okCaption,callBackOk,cancelCaption,callBackCancel){
        loadDialog(ajax,url,data,showTitle,title,width,height,true,okCaption,callBackOk,cancelCaption,callBackCancel,callBackCancel);
    }
    function confirmDialog(ajax,url,data,showTitle,title,width,height,okCaption,callBackOk,cancelCaption,callBackCancel,escapeFunction){
        loadDialog(ajax,url,data,showTitle,title,width,height,true,okCaption,callBackOk,cancelCaption,callBackCancel,escapeFunction);
    }
    
    function loadDialog(ajax,url,data,showTitle,title,width,height,closeOnEscape,okCaption,callBackOk,cancelCaption,callBackCancel,escapeFunction){
        var id = createID_dialog();
        var btns = {};
        var hasButtons=false;
        if (okCaption!=""){
            hasButtons=true;
            if(callBackOk!=''){ 
                // vigigt at overrule default funtionalitet på close!
                btns[okCaption] = function(){
                    closeHack();
                    if(callBackOk()) {
                        $(this).dialog("option","close",null );
                        $(this).dialog("close");
                        $('#'+id).remove();
                    }
                };
            }else{
                btns[okCaption] = function(){ closeHack();$(this).dialog("option","close",null );$(this).dialog("close");$('#'+id).remove();};
            }
        } 
        if (cancelCaption!=""){
            hasButtons=true;
            if(callBackCancel!=''){
                // vigigt at overrule default funtionalitet på close!
                btns[cancelCaption] = function(){ closeHack(); $(this).dialog("option","close",null );$(this).dialog("close");$('#'+id).remove();callBackCancel();};
            }else{
                btns[cancelCaption] = function(){ closeHack(); $(this).dialog("option","close",null );$(this).dialog("close");$('#'+id).remove();};
            }
        }
    
        var esc;
        if (escapeFunction!=''){
            esc = function(){ closeHack(); $(this).dialog("option","close",null );$(this).dialog("close");$('#'+id).remove();escapeFunction();};
        }
        else{
            esc = function(){ closeHack(); $('#'+id).remove();};//empty
        }
        loadDialog_private(id,ajax,url,data,showTitle,title,width,height,closeOnEscape,btns,hasButtons,esc);
    }
        
    function loadDialog_private(id,ajax,url,data_,showTitle,title,width_,height_,closeOnEscape,btns,hasButtons,esc){
        //$('#overlay').show();
        var divContent = '<div id="'+id+'" style="display:hidden; overflow:auto"></div>';
//        if(!ajax){
//        	divContent = '<div style="display:hidden; /*background-color:#e2ebb5;*/ overflow:auto /*text-align:center;*/">';
//           //divContent = '<div style="display:hidden; background-color:#e2ebb5; /*overflow:auto text-align:center;*/"><span class="tekst"><br>'+url+'</span></div>';
//          //divContent = '<div id="dialog" style="display:hidden"><span align="center" class="tekst">'+url+'</span></div>';					
//        }
        var dialog = $(divContent).appendTo('body').dialog({
            bgiframe: true,
            modal:true,			
            resizable: true,
            draggable: false,
            stack: true,
            width: width_,
            height: height_,
            title: title,
            buttons: btns,
            closeOnEscape: closeOnEscape,
            close: esc,
            autoOpen: false
//          position: 'center',
//          open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }, // skjul kryds til at lukke		    
        });
        //hack - bug i dialog. når man åbner en dialog ovenpå en dialog, og derefter lukker den yderste.
        // så vil den 1. dialog ikke kunne bruge sine links
        // http://bugs.jqueryui.com/ticket/4800
        $(document).unbind('.dialog-overlay');
        //evt. flyt ind i succes/error
        if(showTitle){
            $(".ui-dialog-titlebar").show();
        }else{
            dialog.css(".hide-title-bar.ui-dialog-titlebar { display: none; }");
            dialog.dialog({dialogClass: "hide-title-bar"});
            dialog.dialog().parents(".ui-dialog").find(".ui-dialog-titlebar").remove(); 
        }
        //$(".ui-dialog-titlebar-close").hide();// skjul close icon
        //TODO bug - results hid'er ikke altid på autocompletes - fjern når nye aotucomplete tages i brug
        $("div .ac_results").hide();

        if(ajax){
            $.ajax({
            type: 'POST', // vigtig for at undgå http err 122 i ie5-7 : kan max modtage ca 2030 tegn i get via ajax
            //contentType: "text/html; charset=windows-1252",
            data:data_,
            url: url,
            async:false,
            success: function(data) {
                dialog.html(data);
                dialog.dialog("open").focus();
//              if(hasButtons){
//                  //Hack - da buttons tilføjer ekstra størrelse, som gør at scrolls kommer på
//                  //skyldes nok fejl i styling
//		    var hh =dialog.dialog( "option", "height" );
//		   dialog.dialog("option","height", hh+10 );
//            	}
            },
            error:function (xhr, ajaxOptions, thrownError){
                var s = xhr.status+","+xhr.responseText;
                $(this).dialog("option","title","ERROR" );
                dialog.html(s);//TODO errorhtml
                dialog.dialog("open").focus();
            }
            //error(XMLHttpRequest, textStatus, errorThrown)
            //beforeSend(XMLHttpRequest, settings)
            });
        }else{
            dialog.html('<br>'+url);
//          if(hasButtons){
//              //Hack - da buttons tilføjer ekstra størrelse, som gør at scrolls kommer på
//              //skyldes nok fejl i styling
//              var hh =dialog.dialog( "option", "height" );
//              dialog.dialog("option","height", hh+10 );
            dialog.dialog("open").focus();            		  
        }

        $(window).resize(function() {$("#"+id).dialog("option", "position", "center");});
    }
    
    function createID_dialog(){
        var names = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r');
        var id =names[$.randomBetween(0, names.length)];
        id +=names[$.randomBetween(0, names.length)];
        id += (new Date().valueOf());
        return id;
    }


