/* Copyright (c) 2009 Marak Squires - www.maraksquires.com
 
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.
*/

/*********************************************************************** 

  debug.js enables you to debug javascript safely in a production environment
  by seamlessly enabling/disabling debugging options as they are available 

  USAGE:

  debug.log(object);
  debug.log(string);
  debug.log(function);
  debug.log(string,string,object,function);

*************************************************************************/

/* 
  debuggers are listed in priority the in which they will be executed
  if a debugger fails on execution the next debugger will be processed
  until null (no debug output) is reached.
   
  additional debug tests can be built in the following fashion:
  
  debuggers.newBugger=function(){
    try{
      if(myDebugger.test()){return true;}	
    }
    catch(err){
      return false;
    }
  }
*/

var debuggers = {
	/* firebug and firebug lite */
	"firebug":function(args){
		try{
		  console.log(args);
		  return true;
		}
		catch(err){
		  return false;	
		}
	}
	,
	"null":function(){
		return false;
	},	
};

var debug = {};

debug._isRunning = false;
/* don't log anything by default, to enable debugging run debug.start() anytime */

debug.log=function(args){

  /* if debugger is not running, ignore all debug.log() statements */
  if(!debug._isRunning){return false;}	

  /* cycle through debuggers in order of priority */
  for(debugTest in debuggers)
    {
	  if(debuggers[debugTest](args)){
	  /* since we have passed the debugTest with args, stop trying to debug */
	  break;
	}
  }
};

debug.start=function(){
  debug._isRunning = true;
};

debug.stop=function(){
  debug._isRunning = false;	
};