/*

Create a summary view popup window for the hovered element.

How it works:

The element must have a unique ID. If there is an element on the page with the
ID of "hover_" concatenated with the unique ID, then the info from that element
will be inserted into the hover div.

*/



function RT_Summary() {
    // Unique ID for the summary window
    var id = 'RTSummaryWindow';
    
    // Private variables for the summary window element objects
    var summaryWindow = null;
    var summaryHeader = null;
    var summaryContent = null;
    
    // Private method to create the window
    var createWindow = function() {
        // Create summary window element (outer container)
        summaryWindow = document.createElement('DIV');
        summaryWindow.setAttribute('id', id);
        document.body.appendChild(summaryWindow);
        
        // Create middle container element
        var middleContainer = document.createElement('DIV');
        summaryWindow.appendChild(middleContainer);
        
        // Create inner container element
        var innerContainer = document.createElement('DIV');
        middleContainer.appendChild(innerContainer);
        
        // Create header div
        summaryHeader = document.createElement('DIV');
        innerContainer.appendChild(summaryHeader);
        
        // Create contents div
        summaryContent = document.createElement('DIV');
        innerContainer.appendChild(summaryContent);
        
        // Add class names for summary
        try {
            $(summaryHeader).addClassName('header');
            $(summaryContent).addClassName('content');
        } catch(e) {
            //RT.log.error("Unable to add class names. " + e);
        }
    };
    
    // Protected method to set the header and body in one pass
    this.set = function(header, body) {
        if(!summaryWindow) createWindow();
        summaryHeader.innerHTML = header;
        summaryContent.innerHTML = body;
    }
    
    this.getWindow = function() {
        if(!summaryWindow) createWindow();
        return summaryWindow;
    }
    
    // Protected method to show or hide the window
    this.visible = function(bool) {
        if(!summaryWindow) createWindow();
        summaryWindow.style.display = bool ? "block" : "none";
        return;
    }
    
    this.setTop = function(value) {
        if(!summaryWindow) createWindow();
        try{
            var scrolloffset = document.viewport.getScrollOffsets();
            var maxvalue = scrolloffset['top'] + document.viewport.getHeight() - $(summaryWindow).getHeight();
            value = value > maxvalue ? maxvalue : value;
        } catch(e) {
            // Ignore
        }
        summaryWindow.style.top = value + 'px';
    }
    
    this.setLeft = function(value) {
        if(!summaryWindow) createWindow();
        summaryWindow.style.left = value + 'px';
    }
}


RT_Summary.prototype = {
    write:          function(header, body) {
                        this.set(header, body);
                    },
                    
    writeFrom:      function(id) {
                        var elem = null;
                        if(!(elem = $(id))) return false;
                        this.getWindow().innerHTML = elem.innerHTML;
                    },

    hide:           function() {
                        this.visible(false);
                    },
    
    show:           function() {
                        this.visible(true);
                    },
                    
    position:       function(top, left) {
                        this.setTop(top);
                        this.setLeft(left);
                    }
}

if(!window.RT) { window['RT'] = {}; }
window['RT']['summary'] = new RT_Summary();