
/************************************************************************
 globals.js
 jpl 5/08/08
 
	Suite of global JavaScript functions specific to the Coach Accountable
	system.
	
					setSessionKeepAlive()
					AJAXKeepAlive()
					submitFeedback()
					toggleLiquidLayout()
	String			getCoachFilePath(CoachID)
					switchToLiquidLayoutMode()
					switchToFixedLayoutMode()
			
************************************************************************/


	dbug.enable();
	

	// the CoachAccountable namespace object
	CA = { 
		showLoadCount : 0,
		SSLIsAvailable : false, // overwritten in context
		thisPage : location.href.substring(location.href.lastIndexOf('/')+1).split('.asp')[0] + '.asp'
	};
	
	CA.roar = new Roar({
		duration: 3000,
		position: 'upperRight',
		margin: {x: 30, y: 20}
	});

	function showLoad(alternateMessage) {
		$E('#loadingMessage div').setText($pick(alternateMessage, 'Loading...'))
		CA.showLoadCount++;
		top.$('loadingMessage').style.display = 'block';
	}
	function hideLoad() {
		CA.showLoadCount = Math.max(CA.showLoadCount-1, 0);
		if(CA.showLoadCount == 0)
			setTimeout('window.top.$(\'loadingMessage\').style.display = \'none\'', 100);
	}
	
	if(location.href.indexOf('localhost') == -1) 
		window.onerror = reportJSError;

window.addEvent('domready', function() {
	$(document.body).addEvent('click', function(event) {
		CA.lastClickTarget = event.target;
	});
});

	function reportJSError(message, fileName, lineNumber) {
		
		if(!fileName  ||  (fileName + '') == ''  ||
		   fileName.match(/socialtwist/)  ||
		   fileName == 'http://www.google-analytics.com/ga.js'  ||
		   properInt(lineNumber, 0) == 0) // don't care.
			return;
		
		showLoad();
		new Request.HTML({ url: 'AJAXUser.asp?a=reportJSError',
			method: 'post',
			data: { message:	message,
					URL:		location.href,
					fileName:	fileName,
					lineNumber:	lineNumber,
					lastClick:	elementPathDescriptor(CA.lastClickTarget),
					browser:	navigator.userAgent },
			onComplete: function() {
				hideLoad();
				CA.roar.alert('An error has occurred.', 
					'This is inconvenient, and we know it.  The error has been logged ' +
					'and the details have been emailed to the CoachAccountable team!',
					{ duration : 7000 });
			}
		}).send();
		return true;
	}
	
	function elementPathDescriptor(el) {
		if(!$(el)) return '';
		var result = elementDescriptor(el)
		var parent = el.getParent();
		while(parent) {
			var thisNode = elementDescriptor(parent);
			if(thisNode)
				result = elementDescriptor(parent) + '\n  >> ' + result;
			parent = parent.getParent();
		}
		return result;
	}
	function elementDescriptor(el) {
		var boring = true;
		var result = '<' + el.tagName + ' ';
		['id', 'class', 'title', 'src', 'onclick', 'value'].each(function(property) {
			if(el[property]  &&  el[property].length) {
				result += property + '=\"' + el[property] + '\" ';
				boring = false;
			}
		});
		return boring ? false : result + '/>';
	}
