/**
 * @fileoverview
 * The Coedit Framework Javascript Library
 * 
 * @version 1.0
 * 
 * Copyright (c) 2007 Jeff Home and Dan Nye, Coedit UK Limited - http://www.coedit.co.uk
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

if (window['coedit'] === undefined)
{
	window['coedit'] = {};
}

coedit.version = function()
{
	return "1.0";
};

/**
 * Browser Helpers
 */
coedit.browser = {};
coedit.browser.fx = function()
{
	return navigator.userAgent.indexOf('Firefox') > 0;
};

/**
 * Logging Helpers
 */
coedit.debug = function(l_sDebugLevel)
{
	switch (l_sDebugLevel)
	{
		case 'todo':
			return true;
			break;
		case 'info':
			return false;
			break;
		case 'log':
			return false;
			break;
		case 'error':
			return true;
			break;
	}
};
coedit.log = function(l_sMessage)
{
	if (coedit.debug('log'))
	{
		if (coedit.browser.fx())
		{
			console.log(l_sMessage);
		}
		else
		{
			alert('Log:\n' + l_sMessage);
		}
	}
};
coedit.todo = function(l_sMessage)
{
	if (coedit.debug('todo'))
	{
		if (coedit.browser.fx())
		{
			console.info('TODO: ' + l_sMessage);
		}
		else
		{
			alert('ToDo:\n' + l_sMessage);
		}
	}
};
coedit.info = function(l_sMessage)
{
	if (coedit.debug('info'))
	{
		if (coedit.browser.fx())
		{
			console.info(l_sMessage);
		}
		else
		{
			alert('Info:\n' + l_sMessage);
		}
	}
};
coedit.error = function(l_sMessage)
{
	if (coedit.debug('error'))
	{
		if (coedit.browser.fx())
		{
			console.error(l_sMessage);
		}
		else
		{
			alert('Error:\n' + l_sMessage);
		}
	}
};

/**
 * Helper Methods
 */

/**
 * Removes leading and trailing white-space from a string and returns the result as a string.
 * If the supplied string was undefined, then it returns empty string.
 * 
 * @param {String} l_sData The string to trim leading and trailing white-space from
 * 
 * @return A string with leading and trailing white-space removed. If the supplied string was undefined, then return an emptry string.
 * @type String
 */
coedit.trim = function(l_sData)
{
	if (l_sData === undefined)
	{
		return "";
	}
	return l_sData.replace(/^\s*|\s*$/,"");
};




var debugWindowHandle = null;
var fireBugInstalled = false;
if (typeof(window.console) == 'object') {
	if (typeof(window.console.log) == 'function') {
		fireBugInstalled = true;
	}
}

function openDebugWindow() {
	if (!fireBugInstalled) {
		debugWindowHandle = window.open('', 'logWin', '');
		if (debugWindowHandle.document.getElementsByTagName('textarea').length == 0) {
			debugWindowHandle.document.open();
			debugWindowHandle.document.write('<html><body><form><textarea id="log" cols="120" rows="30"></textarea></form></body></html>');
			debugWindowHandle.document.close();
		}
	}
}
function logToDebugWindow(str) {
	if (!fireBugInstalled) {
		if (debugWindowHandle != null) {
			if (debugWindowHandle.closed !== true) {
				var logger = debugWindowHandle.document.getElementById('log');
				if (logger) logger.value += '\n' + str;
			}
		}
	} else {
		console.log(str);
	}
}
//openDebugWindow();
