/* 
    #########################################
    Author : JEREMY BUELER www.jbueler.com
    Company : www.summitprojects.com
    Date: 3/12/08
    * 
    * 
    #########################################
	
    THIS IS DEBUG FUNCTION - use trace() this instead of console.log() or alert() -- trace() will be 
     						 ignored if your project is not on DEV so stage and live servers will not execute 
     						 trace() and there will not be signs of debugging.
     						  
     						 if firebug is installed it will use the console for all output
     						 if it isn't installed it will create an overlay and output all trace into the overlay
	 * 
	 * 
	 * 
	 * 
	 * 
*/

function createConsoleLog(){
	if(location.href.indexOf('.dev.summitprojects.com') != -1){
		if(!window.console){
			window.console = {
				log: function(contents){
					trace(contents)
				}
			};
		}
	}else{
		window.console = {
			log: function(){
				
			}
		}
	}
}
if(typeof RegisterFunctions != 'undefined'){
	Onload.add(createConsoleLog);
}else{
	window.onload = createConsoleLog;
}

if( typeof MooTools != "undefined" ){
	
	/**
	 * trace
	 * @param {mixed} object or string that will be logged/traced 
	 */
	 function trace(content) {
	 	traceWindow.inject($(document.body));
	 	var entry = new Element('div',{
	 		styles:{
	 			'margin':'10px',
	 			'padding':'5px',
	 			'border':'1px solid red',
	 			'background':'#66CCCC'
	 		}
	 	});
	 	var type = $type(content);
	 	if(type == 'object'){
	 		content = JSON.encode(content);
	 	}
	 	entry.inject(traceWindow.getElement('.traceContent'));
	 	entry.set('html',content);
	 	traceWindow.makeDraggable({handle:traceWindowUtility});
	 }
	
	/**
	 * clearConsole
	 * @param {null}  
	 */
	 function clearConsole(e) {
	 	new Event(e).stop();
	 	var console = this;
	 	console.getElement('.traceContent').empty();
	 }
	
	/**
	 * minimizeConsole
	 * @param {null}  
	 */
	 function minimizeConsole(e) {
	 	new Event(e).stop();
	 	var console = this[0];
	 	var minimizeBtn = this[1];
	 	
	 	if(console.getStyle('height').toInt() > 30){
	 		console.setStyles({
	 			'height':'30px'
 			});
 			e.target.set('text','+');
	 	}
	 	else{
	 		console.setStyles({
	 			'height':'500px'
 			});
 			e.target.set('text','-');
	 	}
	 	
	 }
	 
	/**
	 * closeConsole
	 * @param {null}  
	 */
	 function closeConsole(e) {
	 	new Event(e).stop();
	 	var consoleWindow = this[0];
	 	var closeBtn = this[1];
	 	
	 	consoleWindow.remove();
	 	
	 }
	
	
	var traceWindow = new Element('div',{
		'id':'traceWindow',
		'styles':{
			'display':'block',
			'position':'absolute',
			'overflow':'hidden',
			'top':'0',
			'left':'0',
			'z-index':'5000',
			'background-color':'#FFFFFF',
			'color':'#000000',
			'border':'2px solid black',
			'text-align':'left',
			'width':'500px',
			'height':'500px'
		}
	});
	var traceWindowUtility = new Element('div',{
		'styles':{
			'height':'30px',
			'width':'100%',
			'cursor':'move',
			'background':'#E5E5E5',
			'border-bottom':'1px solid black'
		},
		events:{
			
		}
	});
	
	var clear = new Element('a',{
		id:'clearConsole',
		href:'#',
		events: {
			'click': clearConsole.bindWithEvent(traceWindow)
		},
		styles:{
			'float':'left',
			'color':'#FFFFFF',
			'background':'#747474',
			'background':'#747474',
			'text-decoration':'none',
			'padding':'3px',
			'margin':'5px 5px 0 5px'
		}
	});
	clear.set('text','CLEAR');
	clear.inject(traceWindowUtility);
	

	var closeTrace = new Element('a',{
		id:'closeConsole',
		href:'#',
		events: {
			'click': closeConsole.bindWithEvent([traceWindow,minimizeTrace])
		},
		styles:{
			'float':'right',
			'color':'#FFFFFF',
			'background':'#747474',
			'background':'#747474',
			'text-decoration':'none',
			'padding':'1px 3px 1px 3px',
			'margin':'5px 5px 0 5px'
		}
	});
	closeTrace.set('text','x');
	closeTrace.inject(traceWindowUtility);
	
	var minimizeTrace = new Element('a',{
		id:'minimizeConsole',
		href:'#',
		events: {
			'click': minimizeConsole.bindWithEvent([traceWindow,minimizeTrace])
		},
		styles:{
			'float':'right',
			'color':'#FFFFFF',
			'background':'#747474',
			'background':'#747474',
			'text-decoration':'none',
			'padding':'1px 3px 1px 3px',
			'margin':'5px 5px 0 5px'
		}
	});
	minimizeTrace.set('text','-');
	minimizeTrace.inject(traceWindowUtility);
	
	traceWindowUtility.inject(traceWindow);
	
	var traceContent = new Element('div',{
		styles:{
			'width':'100%',
			'height':'93%',
			'overflow':'auto',
			'margin':'5px 0 5px 0'
		}
	});
	traceContent.addClass('traceContent');
	traceContent.inject(traceWindow);
}


/*
 * 
 * JQUERY PORTION OF THE TRACE CODE 
 * 
 * */
 
/* 
else if(typeof jQuery != "undefined"){
	var traceWindow = $('<div>');
	traceWindow.id = 'traceWindow';
	traceWindow.css({
		'display':'block',
		'position':'absolute',
		'overflow':'auto',
		'bottom':'25px',
		'left':'5%',
		'cursor':'move',
		'background-color':'#FFFFFF',
		'border':'2px solid black',
		'width':'90%',
		'height':'100px'
	});
	
	function trace(contents){
		var output = $('<div style="margin-left: 2px;margin-bottom: 3px; margin-top: 5px; border-bottom: 1px solid red;">');
		
		checkObj(contents,output,1);
		
		if(typeof traceWindow.draggable != "undefined"){
			traceWindow.draggable();
		}
		if(typeof traceWindow.resizable !="undefined"){
			traceWindow.resizable({minHeight:100,minWidth:200});
		}
		$('body').append(traceWindow);
	}
	
	function checkObj(contents,output,i){
		var objSepTop = $('<div style="font-size: 0; height: 1px; background-color: #CCCCCC; width:100%; margin: 2px 0;">&nbsp;</div>');
		var objSepBottom = $('<div style="font-size: 0; height: 1px; border-top:1px dashed #CCCCCC; width:100%; margin: 2px 0;">&nbsp;</div>');
		
		if(typeof contents == "object"){
			output.append(objSepTop);
			for(name in contents){
				var el = $('<div style="margin-left: 10px;">');
				el.append(name +" :: "+contents[name]+" :: "+ typeof contents[name]);
				output.append(el);
				
				if(typeof contents[name] == 'object'){
					i += 1;
					checkObj(contents[name],output,i)
				}
				
			}
			output.append(objSepBottom);		
			traceWindow.append(output);	
		}
		else{
			var el = $('<div style="margin-left: 10px;">');
			el.append(contents);
			output.append(el);
			traceWindow.append(output);
		}
	}
}*/