/******************************************************************************************/
/************************************ MTVM LOGGER *****************************************/
/*******************************************************************************************
USAGE:  MTVM.logger.log(YOUR_DEBUG_MESSAGE, DEBUG_LEVEL); OR MTVM.log(YOUR_DEBUG_MESSAGE, DEBUG_LEVEL);
	
	MTVM.logger.log("my debug message here"); 			//will render a LOG level message
	MTVM.logger.log("my debug message here", 0); 		//will render a LOG level message
	MTVM.logger.log("my debug message here", "LOG");	//will render a LOG level message
	
	MTVM.logger.log("my INFO message here", 1); 		//will render an INFO level message
	MTVM.logger.log("my INFO message here", "INFO");	//will render an INFO level message

	MTVM.logger.log("my INFO message here", 2); 		//will render an WARNING level message
	MTVM.logger.log("my INFO message here", "WARNING");	//will render an WARNING level message
	
NOTES:
	- CAN BE COMPLETELY ENABLED/DISABLED thru MTVM.config.debugLogEnabled
	- LOG Messages are avaiable to be viewed AFTER window.loaded event
	- On browsers w/o Firebug, able to capture console.log calls

DEPENDENCIES:
	- jQuery
	
TODO: Remove jquery dependency	

*******************************************************************************************/
/******************************************************************************************/

MTVM.logger = function()
{
	var private =
	{
		LOG_LEVEL:				["LOG", "INFO", "WARNING"],		
		initialized:			false,
		
		__init:	function(){
			if(this.initialized == false)
			{	
				if(document.getElementById(public.ELEMENT_ID_WRAPPER) == null)
				{
					this.__createNewDebugDiv();			
				}
				this.initialized = true;
			}
		}, //end __init
		

		__addEntryToDOM: function(tmpLogEntry)
		{
			public.CURRENT_LOG = public.CURRENT_LOG + tmpLogEntry;
					
			if(private.initialized == true)
			{
				document.getElementById(public.ELEMENT_ID_LOG).innerHTML = public.CURRENT_LOG;
				public.checkOptions();
				
			}
		},		
		
		__createNewDebugDiv: function()
		{
			var debugOptions ="";

			var wrapper = document.createElement("div");
			wrapper.id = public.ELEMENT_ID_WRAPPER;

			var debugButton = document.createElement("div");
			var debugButtonText = document.createTextNode("x");
			debugButton.appendChild(debugButtonText);
			debugButton.id = public.ELEMENT_ID_DEBUG_BUTTON;			
			debugButton.onclick = public.show;

			document.body.insertBefore(debugButton, document.body.firstChild);
			document.body.insertBefore(wrapper, document.body.firstChild);

			for(var i=0; i <private.LOG_LEVEL.length; i++)
			{
				debugOptions +=	'<input type="checkbox" id="'+public.ELEMENT_ID_OPTIONS+'-'+private.LOG_LEVEL[i]+'" onclick="MTVM.logger.checkOptions();"/> '+private.LOG_LEVEL[i]+' ';			
			}
			document.getElementById(public.ELEMENT_ID_WRAPPER).innerHTML ='<div id="mtvm-debug-options">'+
				debugOptions +
				'</div>'+				
				'<div id="'+public.ELEMENT_ID_LOG+'"></div></div>';
		}, //end __createNewDebug
				
				
		__saveOptions: function()
		{
			for(var i=0; i < private.LOG_LEVEL.length; i++)
			{	
				MTVM.util.setCookie(public.ELEMENT_ID_WRAPPER+private.LOG_LEVEL[i], document.getElementById("mtvm-debug-options-"+private.LOG_LEVEL[i]).checked, 3650)
			}
		}, //end __saveOptions


		__loadOptions: function()
		{
			for(var i=0; i <private.LOG_LEVEL.length; i++)
			{
				if(MTVM.util.getCookie(public.ELEMENT_ID_WRAPPER + private.LOG_LEVEL[i])== "true" || MTVM.util.getCookie(public.ELEMENT_ID_WRAPPER+private.LOG_LEVEL[i])== "")
				{
					document.getElementById("mtvm-debug-options-"+private.LOG_LEVEL[i]).checked = true;
				}		
			}
		}	//end __loadOptions			
		
	};
	
	
	var public = {	
		CURRENT_LOG:				"",
		ENABLE_LOG:					function(){return MTVM.config.debugLogEnabled;},
		
		ELEMENT_ID_LOG:				"mtvm-debug-log",
		ELEMENT_ID_WRAPPER:			"mtvm-debug",
		ELEMENT_ID_OPTIONS:			"mtvm-debug-options",
		ELEMENT_ID_DEBUG_BUTTON:	"mtvm-debug-button",		
		
		CSS_STYLE_ENTRY:			"mtvm-debug-entry",
		CSS_STYLE_DEBUGTYPE:		"mtvm-debugType",
		CSS_STYLE_TIMESTAMP:		"mtvm-debug-timestamp",
		CSS_STYLE_MESSAGE:			"mtvm-debug-message",
				
		log:					function(mesg, level)	
		{
			var timestamp = new Date();	
			var classname = public.CSS_STYLE_ENTRY;
			var tmpLogEntry = "";			
			var debugLevel;
			
			if(public.ENABLE_LOG())
			{
				if(level != null && level !="")
				{
					switch(isNaN(parseInt(level)))
					{
						case false:
							debugLevel	= level>=private.LOG_LEVEL.length?0:level;
							classname = public.CSS_STYLE_DEBUGTYPE+'-'+private.LOG_LEVEL[debugLevel];							
							break;
		
						case true:
			
							if(level in private.LOG_LEVEL)
							{
								classname = public.CSS_STYLE_DEBUGTYPE+'-'+level;
							}
							else
							{
								classname = public.CSS_STYLE_DEBUGTYPE+'-'+private.LOG_LEVEL[0];		
							}
							break;
					}
				}else
				{
					classname = public.CSS_STYLE_DEBUGTYPE+'-'+private.LOG_LEVEL[0];
				}
				
				tmpLogEntry = "<div class=\""+public.CSS_STYLE_ENTRY+" "+classname+"\">"+
						"<div class=\""+public.CSS_STYLE_TIMESTAMP+"\">"+timestamp+"</div>"+	
						"<div class=\""+public.CSS_STYLE_MESSAGE+"\">"+mesg+"</div>"+
					"</div>";
				
				private.__addEntryToDOM(tmpLogEntry);
		
			}
		}, //end log


		
		printToDom: function()
		{
			if(public.ENABLE_LOG())
			{
				if(private.initialized == false)
				{
					private.__init();
					document.getElementById(public.ELEMENT_ID_LOG).innerHTML = public.CURRENT_LOG;
				}
				
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.display = "block";		
				private.__loadOptions();
				public.checkOptions();
			}	
		}, //end printToDom

		show: function()
		{	
			if(document.getElementById(public.ELEMENT_ID_LOG).style.display == 'none' || document.getElementById(public.ELEMENT_ID_LOG).style.display == '')
			{
				document.getElementById(public.ELEMENT_ID_LOG).style.display='block';	
				document.getElementById(public.ELEMENT_ID_OPTIONS).style.display='block';	

				document.getElementById(public.ELEMENT_ID_WRAPPER).style.width='50%'; 
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.height='auto'; 
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.maxHeight='50%'; 
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.border="dashed #aaa 1px";
			}
			else
			{
				document.getElementById(public.ELEMENT_ID_OPTIONS).style.display='none';	
				document.getElementById(public.ELEMENT_ID_LOG).style.display='none';	
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.width='20px'; 
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.height='20px'; 
				document.getElementById(public.ELEMENT_ID_WRAPPER).style.border="0px";
			}
		},	//end show

		checkOptions: function()
		{
			for(var i=0; i < private.LOG_LEVEL.length; i++)
			{	
				switch(document.getElementById(public.ELEMENT_ID_OPTIONS+"-"+private.LOG_LEVEL[i]).checked)
				{
					case true:			
						$J('.'+public.CSS_STYLE_DEBUGTYPE+'-'+private.LOG_LEVEL[i]).css("display", "block");
						break;

					case false:
						$J('.'+public.CSS_STYLE_DEBUGTYPE+'-'+private.LOG_LEVEL[i]).css("display", "none");
						break;
				}
			}
			private.__saveOptions();
			
		}	//end checkOptions
	};

	return(public);

}();

MTVM.log = MTVM.logger.log;

if (typeof console == "undefined")
{
    console = {};	
	console.log = MTVM.logger.log;
}



