// Javascript Log v0.5 - Log javascript to floating div - based on jQuery 1.3
// c) 2009 Ronny Sherer - www.hoojima.com - ronny@hoojima.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//
// Usage: $.fn.jslog(objectToLog);
//

(function ($)
{
	var jslog = 'jslog', count = 0, logDiv = null, content = null, thisPublic, debug = false;

	thisPublic = $.fn.jslog = function (objectToLog, options)
	{
		if (logDiv == null)
			thisPublic.init(options);
		if (!debug)
			return;

		count++;
		content.append('<div class="debug'+(count%2)+'"><b>'+count+':</b> '+objectToLog+'</div>');
		logDiv.show();
	}

	thisPublic.init = function(options)
	{
		var debug_jslog = getCookie("debug_jslog");
		if (debug_jslog)
			$.fn.jslog.defaults.debug = (debug_jslog == true || debug_jslog == 'true');
		if (options && options.debug != undefined)
			setCookie("debug_jslog", options.debug);
        var options = $.extend({}, $.fn.jslog.defaults, options);
		debug = options.debug;
		if (!debug)
			return;

		logDiv=document.createElement('div');
		$('body').append(logDiv);
		logDiv.id='jslog';
		logDiv = $(logDiv);
		logDiv.html('<div id="jslogHeader">Messages<div id="jslogMiniMax" onclick="MiniMaxDebugWindow()">-</div><div id="jslogHide" onclick="$.fn.jslog.hide()">X</div><div id="jslogClear" onclick="$.fn.jslog.clear()">clear</div></div><div id="jslogContent"></div>');

		content = logDiv.find('#jslogContent');
		logDiv.hide();

		if (!options.notDraggable && typeof logDiv.draggable == 'function')
			logDiv.draggable({ handle: '#jslogHeader', opacity: 0.85 });
		logDiv.css({color: options.textColor});
		if (options.width)
			logDiv.width(options.width);
		if (options.resizable)
			logDiv.resizable({ alsoResize: '#jslogContent' });

		content.css('max-height', $(window).height()-60);
		$(window).resize( function()
		{
			content.css('max-height', $(window).height()-60);
		});
	}

	thisPublic.clear = function()
	{
		count = 0;
		content.empty();
	}

	thisPublic.hide = function()
	{
		if (debug)
			logDiv.hide();
	}

	thisPublic.show = function()
	{
		if (debug)
			logDiv.show();
	}

	this.MiniMaxDebugWindow = function()
	{
		var me = logDiv.find('div:first div:first');
		if (me.text() == '-')
		{
			content.hide();
			me.text('+');
		}
		else
		{
			content.show();
			me.text('-');
		}
	}

	this.getCookie = function(name)
	{
		if (document.cookie && document.cookie != '')
		{
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++)
			{
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '='))
					return unescape(cookie.substring(name.length + 1));
			}
		}
		return '';
	}
	this.setCookie = function(name, value)
	{
        document.cookie = name+'='+escape(value);
	}

	$.fn.jslog.defaults = {
		notDraggable: false,
		resizable: false,
		debug: false,
		width: 0
	};

}(jQuery));

