
   ////  ////   ////// //
 //     //  // ///    //
//     //  //     // //
 //// //  // ////// //////

/////////////////////////////////////////////////
////// DEPENDS ON ///////////////////////////////
//////// JQUERY.COOKIE.JS ///////////////////////
//////// JQUERY.UI.JS ///////////////////////////
/////////////////////////////////////////////////
////// CNSL TYPES ///////////////////////////////
//////// BUILDUP //// build /////////////////////
//////// BEHAVIOUR // behave ////////////////////
//////// UPDATE ///// update ////////////////////
//////// TEMP /////// NULL //////////////////////
///// (ALL WITH AN OPTIONAL /////////////////////
////// LOG PARAMETER) ///////////////////////////
/////////////////////////////////////////////////




var nfcnfg, dev_debug, dev_cnsl, dev_log;

/////////
// SETUP
$(document).ready(function(){

	dev_debug = (nfcnfg)? nfcnfg.debug : false;
	dev_cnsl = (nfcnfg)? nfcnfg.cnsl : false;
	dev_log = (nfcnfg)? nfcnfg.log : true;
	
	//////////////////////////////
	// FIRE UP CNSL IF STANDALONE
	
	if(!nfcnfg&&dev_debug){
		cnsl.init();
		// setTimeout(cnsl.init, 300);
	}
})




///////////////////
// NEW FOLDER CNSL

var cnsl = {

	init: function(){

		if(!dev_debug) return false;

		cnsl.getState()
		cnsl.style.add(cnsl.style.css());
		cnsl.create();
		cnsl.moveble();
		cnsl.show.init();
	},

	create: function(){

		cnsl.element =
		$('<div/>').attr('id', 'nf_cnsl').append(
			$('<div/>').addClass('cnsl_titlebar').append(
				$('<div/>').addClass('cnsl_title').text('nf_cnsl'),
				$('<div/>').addClass('cnsl_close').text('x')
			),
			$('<div/>').addClass('cnsl_linkbar').append(
				$('<a/>').addClass('all').text('all'),
				$('<a/>').addClass('debug').text('temp'),
				$('<a/>').addClass('info').text('update'),
				$('<a/>').addClass('warn').text('behaviour'),
				$('<a/>').addClass('error').text('build'),
				$('<a/>').addClass('fatal').text('fatal'),
				$('<a/>').addClass('erase').text('erase')
			),
			$('<div/>').attr('id', 'nf_cnsl_log')
		)

		$('body').append(cnsl.element);
	},

	log: function(type,var_string){

		if(!dev_debug) return false;
		if(type)type = type.toString();
		else return false;
		if(dev_cnsl){
			if(type.match('build')){
				error(var_string)
			}
			if(type.match('behave')){
				warn(var_string)
			}
			if(type.match('update')){
				info(var_string)
			}
			if(!var_string){
				var var_string = type;
				debug(var_string)
			}
		}

		if(dev_log&&type.match('log')){
			$.post("/editor/system/log.php", {
				post_string: var_string
			});
		}
	},

	moveble: function(){
		cnsl.element.draggable({
			handle: cnsl.element.find('.cnsl_titlebar:first'),
			stop: cnsl.storePos
			// containment: 'parent'
		});
	
		// ENABLE RESIZABILITY OF LIVEUSER ITEMS
		cnsl.element.resizable({
			handles: 'se',
			stop: cnsl.storeSize,
			minWidth: 217,
			minHeight: 55
		});
	},

	show: {

		init: function(){
			cnsl.element.find('a').click(function(){
				cnsl.show[$(this).attr('class')]()
			})
		},

		all: function(){ FVL_showMessages() },

		debug: function(){ FVL_showMessages(FVL_DEBUG); },

		info: function(){ FVL_showMessages(FVL_INFO); },

		warn: function(){ FVL_showMessages(FVL_WARN); },

		error: function(){ FVL_showMessages(FVL_ERROR); },

		fatal: function(){ FVL_showMessages(FVL_FATAL); },

		erase: function(){ eraseLog(true) }
	},

	write: function(){
		
	},

	style: {

		add: function(css){
			$('head').append(
				$('<style/>').attr('type', 'text/css').text(css)
			)
		},
		
		css: function(){
			css =
			"#nf_cnsl{\
				position: fixed;\
				padding: 6px;\
				top: "+(cnsl.dmnsn.t||50)+"px;\
				left: "+(cnsl.dmnsn.l||650)+"px;\
				width: "+(cnsl.dmnsn.w||125)+"px;\
				height: "+(cnsl.dmnsn.h||250)+"px;\
				background: #414141 url(/editor/images/high_gradient_white.png) repeat-x 0 top;\
				font: 9px 'nfscreen9';\
				color: rgb(220, 220, 220);\
				-webkit-box-shadow: 0px 1px 2px black;\
				z-index: 9999;\
				overflow: hidden:\
				}\
			.cnsl_titlebar{\
				width: 100%;\
				height: 17px;\
				background: #414141 url(/editor/images/toolbar_gradient_white.png) repeat-x 0 top;\
				-webkit-box-shadow: 0px 1px 2px rgb(40,40,40);\
				}\
			.cnsl_titlebar div{\
				position: absolute;\
				padding: 3px;\
				}\
			.cnsl_close{\
				right: 8px;\
				cursor: pointer;\
				}\
			.cnsl_linkbar{\
				position: relative;\
				width: 100%;\
				height: 15px;\
				margin-top: 4px;\
				margin-bottom: 5px;\
				padding: 3px 0 0px 0;\
				-webkit-box-shadow: 0px 1px 2px rgb(40,40,40);\
				background: #414141 url(/editor/images/toolbar_gradient_white.png) repeat-x 0 top;\
				z-index: 100;\
				}\
			.cnsl_linkbar a{\
				position: relative;\
				cursor: pointer;\
				height: 17px;\
				display: inline-table;\
				margin-left: 2px;\
				padding: 1px 1px 1px 1px;\
				z-index: 999;\
				}\
			.cnsl_linkbar .debug { color: lime; }\
			.cnsl_linkbar .info  { color: lightblue; }\
			.cnsl_linkbar .warn  { color: yellow; }\
			.cnsl_linkbar .error { color: #ED791F; }\
			.cnsl_linkbar .fatal { color: gray; }\
			#nf_cnsl_log{\
				position: absolute;\
				overflow: auto;\
				top: 44px;\
				bottom: 6px;\
				left: 6px;\
				right: 6px;\
				padding: 2px;\
				z-index: -1;\
				}\
			#nf_cnsl_log p{\
				margin: 0;\
				}\
			#nf_cnsl p.debug { color: lime;  }\
			#nf_cnsl p.info  { color: lightblue; }\
			#nf_cnsl p.warn  { color: yellow; }\
			#nf_cnsl p.error { color: #ED791F; }\
			#nf_cnsl p.fatal { color: black; }";
			return css;
		}
	},

	storePos: function(e, ui){
		var h = cnsl.element.height();
		var w = cnsl.element.width();
		var t = ui.position.top;
		var l = ui.position.left;
		cnsl.log('behave','nf_cnsl_state: '+ h+'/'+w+'/'+t+'/'+l)
		$.cookie('nf_cnsl_state', h+'/'+w+'/'+t+'/'+l, { path: '/', expires: 100 })
	},

	storeSize: function(e, ui){
		var h = ui.size.height;
		var w = ui.size.width;
		var t = cnsl.element.offset().top;
		var l = cnsl.element.offset().left;
		cnsl.log('behave','nf_cnsl_state: '+ h+'/'+w+'/'+t+'/'+l)
		$.cookie('nf_cnsl_state', h+'/'+w+'/'+t+'/'+l, { path: '/', expires: 100 })
	},

	getState: function(e, ui){

		var dimens = $.cookie('nf_cnsl_state');
		if(dimens){ 
			dimens = dimens.split('/');
			var h = dimens[0];
			var w = dimens[1];
			var t = dimens[2];
			var l = dimens[3];
		}
		cnsl.dmnsn = {
			h: h || 250,
			w: w || 217,
			t: t || 20,
			l: l || 550
		}
	},

	scrollDown: function(){
		$('#nf_cnsl_log').each(function(){this.scrollTop=this.scrollHeight});
	}
}






//-------------------------------------------------
//	FVLOGGER SCRIPT
//
//	fvlogger v1.0
//	(c) 2005 davidfmiller
//	http://www.fivevoltlogic.com/code/fvlogger/
//	see readme.txt for documentation
//-------------------------------------------------

// version number
var FVLOGGER_VERSION = '1.0';

// turn logging on or off;
var FVL_LOG_ON = true;

// all logging statements that whose level is greater than or equal to FVL_DEFAULT_LOG_LEVEL will be processed;
// all others will be ignored
var FVL_DEFAULT_LOG_LEVEL = FVL_DEBUG;

// the id of the node that will have the logging statements appended to it
var FVL_LOG_ID = 'nf_cnsl_log';

// the element that should be wrapped around the log messages
var FVL_LOG_ELEMENT = 'p';

/* the code that follows is */

// constants for logging levels
var FVL_DEBUG = 0;
var FVL_INFO = 1;
var FVL_WARN = 2;
var FVL_ERROR = 3;
var FVL_FATAL = 4;

// the css classes that will be applied to the logging elements
var FVL_LOG_CLASSES = new Array("debug","info","warn","error","fatal");

/* */

// retrieves the element whose id is equal to FVL_LOG_ID
function getLogger(id) {
	if (arguments.length == 0) { id = FVL_LOG_ID; }
	return document.getElementById(id);
}

function showDebug() { FVL_showMessages(FVL_DEBUG); }
function showInfo() { FVL_showMessages(FVL_INFO); }
function showWarn() { FVL_showMessages(FVL_WARN); }
function showError() { FVL_showMessages(FVL_ERROR); }
function showFatal() { FVL_showMessages(FVL_FATAL); }
function showAll() { FVL_showMessages(); }

// removes all logging information from the logging element
function eraseLog() {
	var debug = getLogger();
	if (! debug) { return false; }

	// if (ask && ! confirm("Are you sure you wish to erase the log?")) {
	// 		return false;
	// 	}

	var ps = debug.getElementsByTagName(FVL_LOG_ELEMENT);
	var length = ps.length;
	for (var i = 0; i < length; i++) { debug.removeChild(ps[length - i - 1]); }
	return true;
}
// function debug(message){alert(message)}
function debug(message) { FVL_log("" + message, FVL_DEBUG); }
function warn(message) { FVL_log("" + message, FVL_WARN); }
function info(message) { FVL_log("" + message, FVL_INFO); }
function error(message) { FVL_log("" + message, FVL_ERROR); }
function fatal(message) { FVL_log("" + message, FVL_FATAL);}

function windowError(message, url, line) {
	FVL_log('Error on line ' + line + ' of document ' + url + ': ' + message, FVL_FATAL);
	return true; //
}

// only override the window's error handler if we logging is turned on
if (FVL_LOG_ON) {
	window.onerror = windowError;
}

//
function FVL_showMessages(level, hideOthers) {

//	alert('showing ' + level);

	var showAll = false;
	// if no level has been specified, use the default
	if (arguments.length == 0) { level = FVL_DEFAULT_LOG_LEVEL; showAll = true; }
	if (arguments.length < 2) { hideOthers = true; }

	// retrieve the element and current statements
	var debug = getLogger();
	if (! debug) { return false; }
	var ps = debug.getElementsByTagName("p");
	if (ps.length == 0) { return true; }

	// get the number of nodes in the list
	var l = ps.length;

	// get the class name for the specified level
	var lookup = FVL_LOG_CLASSES[level];

	// loop through all logging statements/<p> elements...
	for (var i = l - 1; i >= 0; i--) {

		// hide all elements by default, if specified
		if (hideOthers) { hide(ps[i]); }

		// get the class name for this <p>
		var c = getNodeClass(ps[i]);
//		alert(c);
//		alert("Node #" + i + "'s class is:" + c);
		if (c && c.indexOf(lookup) > -1 || showAll) { show(ps[i]); }
	}
}

// appends a statement to the logging element if the threshold level is exceeded
function FVL_log(message, level) {

	// check to make sure logging is turned on
	if (! FVL_LOG_ON) { return false; }

	// retrieve the infrastructure
	if (arguments.length == 1) { level = FVL_INFO;}
	if (level < FVL_DEFAULT_LOG_LEVEL) { return false; }
	var div = getLogger();
	if (! div) { return false; }

	// append the statement
	var p = document.createElement(FVL_LOG_ELEMENT);

	// this is a hack work around a bug in ie
	if (p.getAttributeNode("class")) {
		for (var i = 0; i < p.attributes.length; i++) {
			if (p.attributes[i].name.toUpperCase() == 'CLASS') {
				p.attributes[i].value = FVL_LOG_CLASSES[level];
			}
		}
	} else {
		p.setAttribute("class", FVL_LOG_CLASSES[level]);
	}
	var text = document.createTextNode(message);
	p.appendChild(text);
	div.appendChild(p);
	cnsl.scrollDown();
	return true;
}

function exampleLogs() {
	// describe the four types of logging messages
	debug('Scatter debug messages throughout your code to provide a high-level overview of what your code is doing, such as which function is currently in scope and the values of loop counters.');
	info('Information messages are the meat and potatoes of logging messages; sprinkle them around to reveal more detailed information about your script\'s execution, such as the values of variables and function/method return values.');
	warn('Warning messages are used to indicate potentially hazardous situations, such as missing function arguments...');
	error('While error messages are used to indicate that something bad is about to happen; note that these kinds of errors are considered to be run-time errors, which are a different type of beast from the parse errors mentioned below.');

// generate an error to demonstrate the fatal error in ie and mozilla browsers
	a
}

// show a node
function show(target) {
	target.style.display = "";
	return true;
}

// hide a node
function hide(target) {
	target.style.display = "none";
	return true;
}

// returns the class attribute of a node
function getNodeClass(obj) {
	var result = false;

	if (obj.getAttributeNode("class")) {
		result = obj.attributes.getNamedItem("class").value;
	}
	return result;
}














////////////////////////
// MEASURE BUILD UP TIME

var time = {

	init: new Date(),

	set : {

		start: function(){
			time.start = time.init.getTime();
		},

		load: function(){
			var date = new Date();
			time.load = date.getTime();
		}
	},

	get : {

		ready: function(){ 
			var date = new Date();
			time.ready = date.getTime();
			load = time.load - time.start;
			ready = time.ready - time.load;
			cnsl.log('build', 'buildtime: ' + ready + ' ms' );
		}
	}
}

time.set.start();
