/**
 * @author andrew
 * 
 * Functions to allow debugging - disable on live sites
 */

var jsDebug = false;	//set true" to enable debugging  

if (typeof jsDebug != 'undefined' && jsDebug) {

	//pops up an alert message if it finds a message in the alert cookie (set server-side)
	jQuery(document).ready(function(){
		if (message = readCookie('alert')) {
			alert(message);
		}
		
		sessionLog();
	});
	
	
	function readCookie(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') 
				c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) 
				return c.substring(nameEQ.length, c.length).myDecode().replace('+', ' ');
		}
		return false;
	}
}	


	
	//javascript logger
	function log(key, val){
		//set "debug=true" at the top of this page to enable this log
		if (typeof jsDebug == 'undefined' || !jsDebug) {
			return false;
		}
		  
		var log = jQuery('#debugLog');
		if (!log.length) {
			jQuery('body').append('<div id="debugLog" style="opacity:0.8; background-color:white; position:absolute; marging:0; top:0; right:0; padding:5px"><h3>Debug Log: <span class="small" onclick="jQuery(\'#debugLog\').css(\'display\', \'none\');">hide</span></h3></div>')
			log = jQuery('#debugLog');
		}
		
		log.append('<br /><strong>' + key + ': </strong>' + val);
	}
	
	
	
	//displays live session info using periodic ajax polling to help debugging
	function sessionLog() {
		//set "debug=true" at the top of this page to enable this log
		if (typeof jsDebug == 'undefined' || !jsDebug) {
			return false;
		}

		busyStart();	//start the busy cursor indicator 
		  
		var sessionLog = jQuery('#sessionLog');
		if (!sessionLog.length) {
			jQuery('body').append('<div id="sessionLog" style="opacity:0.8; background-color:white; position:absolute; marging:0; top:0; left:30%; padding:0 5px 0 5px"><h3>Session Log: <span class="small" onclick="jQuery(\'#sessionLog\').css(\'display\', \'none\');">hide</span></h3></div>')
			sessionLog = jQuery('#sessionLog');
		}
		
		//load the updated session log window
		sessionLog.load(randomiseUrl(base_url+'login/sessionLog'), false, function() {
			setTimeout('sessionLog()', 5000);		//poll the server for session info every 10 seconds
			busyStop();	//stop the busy animation now that the server has responded
		});
	}
	