var bDebug = true;

//console.log("IN DEBUGGER")

Debugger = {	
	on: bDebug,
	Firebug: null,
	DebugType: null,
	FirebugVersion: null,
	LiteLoaded: false,
	toFile: false,
	LogPath: null,
	DebugCount: 0,
	RevertToAlert: false,  //if firebug lite cannot be loaded do we revert to alerts ?
	AddMessagesToCache : false,
	Clearing : false,
	MessageCache : [],
	LoadFirefoxLite: function(){
		if(document.createElement){
			//alert("load firefox lite script document.body = " + document.body)
			var self = this;			
			//OnBody( function(){ScriptOnDemand.LoadScript("/jobboard/scripts/JS/firebug/firebug.js",Debugger.SetProperties()) });			
			addLoadEvent( function(){ScriptOnDemand.LoadScript("/jobboard/scripts/JS/firebug/firebug.js",Debugger.SetProperties()) });			
		}else if(this.RevertToAlert==true){ //do we use alerts if we cannot get a firebug lite object?
			//alert("no support for create element so use alerts")
			window.console = {
				debug: function(msg){alert(msg);},
				log: function(msg){alert(msg);}
			}
			//create empty functions for all other firebug functions
			var names = ["debug", "info", "warn", "error", "assert", "dir", "dirxml","group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
			for (var i = 0; i < names.length; ++i){
				window.console[names[i]] = function() {}
			}
			this.Firebug = true;
			this.DebugType = "alert";
			this.FirebugVersion = "0";
		}
	},
	SetUp: function(){
		//work out the type of debugging we can do firebug, firebug lite or opera or alerts?
		if(typeof(window.console)=="undefined"){
			if(!this.LiteLoaded){
				this.AddMessagesToCache = true; //add any messages to cache whilst firebuglite is loaded
				//alert("no window.console object and not already loaded lite object so lock n load")		
				this.DebugType = "FirebugLite";
				this.LoadFirefoxLite();
			}else{
				alert("Firefox Lite should be loaded however window.console does not exist.");
			}
		}else{
			this.Firebug = true;
			this.DebugType = "Firebug";
			this.FirebugVersion = console.firebug;
		}	
	},
	SetProperties: function(){
		//alert("In SetProperties")
		//make sure we can reference object before confirming its loaded
		if(typeof(window.console)!="undefined"){
			Debugger.Firebug = true;		
			Debugger.FirebugVersion = 1.0//window.console.firebuglite;
			Debugger.LiteLoaded = true;
			//alert("FirefoxLite should be loaded console = " + window.console);
			if(Debugger.MessageCache.length>=0){Debugger.ClearCache();}
		}else{
			setTimeout(Debugger.SetProperties,250);
		}	
	},
	ClearCache: function(){
		//alert("in clear cache window.console = " + window.console + " do we have cached msg = " + Debugger.MessageCache.length);
		if(this.MessageCache.length>=0){
			this.Clearing = true;
			//alert("empty cache")
			for(var c=0;c<this.MessageCache.length;c++){
				console.log(this.MessageCache[c]); //add all cached messages to debug
			}	
			this.Clearing = false;
		}
	},
	ShowDebug: function(msg){		
		if(!this.on){return;}
		if(Jobboard.ServerType!="dev"){return;}
		//alert("this.DebugType = " + this.DebugType +  " console = " + window.console);
		this.DebugCount++;
		msg=this.DebugCount+": " + msg;
		if(!this.DebugType){ 
			//alert("run setup")
			this.SetUp(); 	
		}else if(this.DebugType=="FirebugLite" && !this.LiteLoaded){
			//alert("firebuglite not loaded yet do we add msg to cache?")
			if(this.AddMessagesToCache){				
				this.MessageCache.push(msg);
				//alert("yes add " + msg + " length = " + this.MessageCache.length)
			}
		}else if(this.Firebug && window.console){	
			//alert("output to console = " + msg)
			this.Show(msg);
		}else if(this.DebugType=="alert"){
			//alert("this.Firebug = "+ this.Firebug + " window.console = " + window.console + " == " + msg)
			alert(msg);
		}
	},
	Show : function(msg){
		//alert("in Show are we still clearing cache = " + this.Clearing);
		if(this.Clearing){
			setTimeout(function(){Debugger.Show(msg);},200);
		}else{			
			console.log(msg);
		}	
	}
}


if(bDebug && Jobboard.ServerType=="dev"){
	Debugger.SetUp();	
	ShowDebugger();
}

function ShowDebugger(){
	var str="";
	for(var i in Debugger){
		str+="Debugger["+i+"] = " + Debugger[i] + "\n";
	}
	//console.log(str);
}

function ShowDebug(msg){
	
	if(typeof(Debugger)=="object"){		
		Debugger.ShowDebug(msg);		
	}
	
}

