/* for an example of how to work, see lib_ajax_filecloseup.inc.php, on or about line 626 */
ajaxWin = {

    contents:'',
    json:[],
    loaded: false,
    
    show: function(json) {
        ajaxWin.json = json;
        
        ajaxWin.buildContents();
        if(this.loaded){
            ajaxWin.activateWindow();
        }else{
            Event.observe(window, 'load', ajaxWin.activateWindow);
        }
    },
    
    
    buildContents: function() {
    
        json = ajaxWin.json;
    
        ajaxWin.contents = '<a onclick="' + (json.cnclFunc ? json.cnclFunc : '') + ' ajaxWin.cancelWindow(); return false" class="close_btn" title="'+(json.cnclText ? json.cnclText : 'Cancel')+'" ></a>';
            
        ajaxWin.contents += '<div class="hdr" id="ajaxWinHdr">';
            if (json.title) ajaxWin.contents += json.title;
        ajaxWin.contents += '</div>';
        
        
        
        ajaxWin.contents += '<div id="ajaxWinBody">';
            
            // only show the header if there is text to show
            if (json.hdrTxt || json.hdrIcon) {
                ajaxWin.contents += '<table class="ndnt">';
                    ajaxWin.contents += '<tr>';
                        if (json.hdrIcon) {
                            ajaxWin.contents += '<td class="m"><img src="'+json.hdrIcon+'" alt="" style="padding-right:6px" /></td>';
                        }
                        ajaxWin.contents += '<td class="m"><strong class="'+json.hdrTxtClass+'">'+json.hdrTxt+'</strong></td>';
                    ajaxWin.contents += '</tr>';
                ajaxWin.contents += '</table>';
            }

            ajaxWin.contents += json.cntnt;
        
        ajaxWin.contents += '</div>';

        if (json.footer) {        
            // div to ensure space above footer
            ajaxWin.contents += '<div class="clear"></div>';
            
            
            ajaxWin.contents += '<div id="ajaxWinFtr" class="ftr">';
                ajaxWin.contents += json.footer;
                //ajaxWin.contents += ' <a onclick="' + (json.cnclFunc ? json.cnclFunc : '') + ' ajaxWin.cancelWindow(); return false" class="btn_sq" >' +
                    //(json.cnclText ? json.cnclText : 'Cancel') + '</a>';
            ajaxWin.contents += '</div>';
        }
    },
    
    activateWindow: function() {
            
        json = ajaxWin.json;
        
        //create a new div to "white-out" the background & one to house the incoming info
        if ( !$('ajaxWin')){
            new Insertion.Bottom('wrapper', '<div id="ajaxWinBG" style="display:none;" ></div>');
            new Insertion.Bottom('wrapper', '<div id="ajaxWin" class="dropshadow" style="display:none;" ><div id = "ajaxWinWrapper">'+ajaxWin.contents+'</div></div>');
        } else {
            $('ajaxWin').hide();
            $('ajaxWinWrapper').update(ajaxWin.contents);
        }
        

        // if a width is specified, set the width that that dimension
        if (typeof(json.wdth) != 'undefined') {
            $('ajaxWin').style.width = json.wdth + 10 +'px';
        }

        // get information needed to position the divs (along with some cross-browser "fun")
        w = $('ajaxWin').getWidth();
        h = $('ajaxWin').getHeight();
        viewportDims = document.viewport.getDimensions();
        scrollOffsets = document.viewport.getScrollOffsets();
        
        // ensure whiteout covers everything (and then some)
        $('ajaxWinBG').style.width = (viewportDims['width']*3)+scrollOffsets['left']+'px';
        $('ajaxWinBG').style.height = (viewportDims['height']*3)+scrollOffsets['top']+'px';



        // position the custom-message div in centre of viewport
        $('ajaxWin').style.top = Math.max(0, parseInt((viewportDims['height']/2)-(h/2)+scrollOffsets['top']))+'px';
        $('ajaxWin').style.left = Math.max(0, parseInt((viewportDims['width']/2)-(w/2)+scrollOffsets['left']))+'px';
        
        // activate the select box beautifier
        //$$('#ajaxWin select').each(function(elm){
        //    new Autocompleter.SelectBox(elm, {'width':230});
       // });
        $('ajaxWinBG').show();
        $('ajaxWin').show();
        
        // make the window draggable + add appropriate CSS pointer
        draggable = new Draggable('ajaxWin',{handle:'ajaxWinHdr'});
        if (draggable) $('ajaxWinHdr').setStyle('cursor:move');

        // for usability, add a listener to the escape key and have it trigger closing the window
        Event.observe(document, 'keypress', function(e) {
            if (e.keyCode == 27) ajaxWin.cancelWindow();
        });
        
        //let everyone know this window is open and visible
        document.fire('ajax_widget_window:open');
    },
    
    cancelWindow: function() {
        if ($('ajaxWin')) $('ajaxWin').remove();
        if ($('ajaxWinBG')) $('ajaxWinBG').remove();

	//fire an event saying that window is closed, to whoever wants to catch it
        document.fire('ajax_widget_window:closed');
        
        // This will stop the keypress observer set when the window is displayed
        // Because this is a global observer (document) keypress was chosen instead
        // of the more-commonly used keyup. I hope this doesn't break stuff in the future)
        Event.stopObserving(document, 'keypress');
    }
}

Event.observe(window, 'load', function(){ajaxWin.loaded = true;});
try{
    document.fire('ajax_widget_window:loaded');
}catch(e){}
