/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

(function() {
    if (!window.PGATOUR) {
        window['PGATOUR'] = {};
    }

    function myLogger(_id) {
        id = _id || 'PGATOURLogWindow';
        var logWindow = null;
        var createWindow = function() {

            // Get the left and top position for the new window
            // so it's centered in the browser
            var browserWindowSize = PGATOUR.getBrowserWindowSize();
            var top = ((browserWindowSize.height - 200) / 2) || 0;
            var left = ((browserWindowSize.width - 200) / 2) || 0;

            // Create the DOM node for the log window using the
            // protected logWindow property to maintain a reference
            logWindow = document.createElement('UL');

            // Assign an ID so you can identify it in the DOM tree if necessary
            logWindow.setAttribute('id', id);

            // Position it centered on the screen
            logWindow.style.position = 'absolute';
            logWindow.style.top = top + 'px';
            logWindow.style.left = left + 'px';

            // Give it a fixed size and allow scrolling;
            logWindow.style.width = '400px';
            logWindow.style.height = '200px';
            logWindow.style.overflow = 'scroll';

            // Add some style to make it look a l itter nicer
            logWindow.style.padding = '0';
            logWindow.style.margin = '0';
            logWindow.style.border = '1px solid black';
            logWindow.style.backgroundColor = 'white';
            logWindow.style.listStyle = 'none';
            logWindow.style.font = '10px/10px Verdana, Tahoma, Sans';
			logWindow.style.zIndex = '99999999';


            // Append it to the body
            document.body.appendChild(logWindow);
        };

        this.writeRaw = function(_message) {
            // If the initial window doesn't exist, create it.
            if (!logWindow) createWindow();
			
            // Create the list item and style it appropriately
            var li = document.createElement('LI');
            li.style.padding = '2px';
            li.style.border = '0';
            li.style.borderBottom = '1px dotted black';
            li.style.margin = '0';
            li.style.color = '#000';
            li.style.font = '9px/9px Verdana, Tahoma, Sans';
			
            // Add the message to the log node
            if (typeof _message == 'undefined') {
                li.appendChild(document.createTextNode('Message was undefined'));
            } else if (typeof li.innerHTML != undefined) {
                li.innerHTML = _message;
            } else {
                li.appendChild(document.createTextNode(_message));
            }
			
            // Append this entry to the log window
            logWindow.appendChild(li);
			
            return true;
        };
    }
	
    myLogger.prototype = {
        write: function(_message) {
            // Warn about null messages
            if (typeof _message == 'string' && _message.length == 0) {
                return this.writeRaw('PGATOUR.log: null message');
            }
			
            // If the message isn't a string try to call the toString()
            // method, if it doesn't exist simply log the type of the object
            if (typeof _message != 'string') {
                if (_message.toString) return this.writeRaw(_message.toString());
                else return this.writeRaw(typeof _message);
            }
            
            // Transform < and > so that .innerHTML doesn't parse
            // the message as HTML
            message = _message.replace(/</g, "&lt;").replace(/>/g, "&gt;");
				
            return this.writeRaw(message);
        },
        header: function(_message) {
            message = '<span style="color:white; background-color:black; font-weight:bold; padding:0px 5px;">' + _message + '</span>';
            return this.writeRaw(message);
        }
    };
	
    window['PGATOUR']['log'] = new myLogger();
})();


