/*********************************************************************************
*	System object
*	created: 04/06/06
*	last modified: 02/01/07 - Tom Raab
*	System.out.println(string) will print string to a screen layer of the page
*	out(string);// exposed function for the above method
* 	Alpha channel only effective in IE (not that it matters)
*********************************************************************************/
System = {ready:false,ison:false};
system = System;
// exposed System functions for code brevity
function out(txt,lines){
	system.out.println(txt,lines);
}
System.init = function(){
	System.ready = true;
	if(System.out.scrlog.length > 0 && System.cookie.test(System.cookie.sysActive)){
		System.out.output();
		System.ison = true;
	}
	if(System.cookie.test('metaData=true')){
		System.metaData();
	}
}
System.on = function(){
	// check cookie
	if(System.ison){
		System.ison = false;
		System.off();
		return;
	}
	else{
		System.ison = true;
	}
	System.cookie.setsys();
	System.out.scrDiv.style.visibility  = 'visible';
}
System.off = function(){
	System.cookie.del(System.cookieName);
	System.out.scrDiv.style.visibility = 'hidden';
}
// browser detection is a bad thing - should instead test for functionality
// but in the inevitable case that the browser type branching is needed
System.out = {
	//System.out is used to create javascript output for debugging purposes
	scrDiv  : document.createElement('div'),
	scrInc : {}, // time interval
	cookieName : "showScreenData",
	scrlog : [],
	scrDepth : 1,
	tableName : 'contentTable',
	showData : function(val){
		document.cookie = this.cookieName+"=true";
		this.print();
	},
	setData : function(val){
		if(val==true){
			document.cookie = this.cookieName+"=true";
		}
		else{
			document.cookie = this.cookieName+"=false";
		}
	},
	print : function(){
		// appends the last item of the scrlog with the argument
		txt = arguments[0];
		lines = arguments[1]
		if(this.scrlog.length>0)
			this.scrlog[this.scrlog.length-1] += txt;
		else
			this.scrlog[this.scrlog.length] = txt;
		if(System.ready)
		this.output(lines);
	},
	println : function(){
		txt = arguments[0];
		lines = arguments[1]
		this.scrlog[this.scrlog.length] = System.util.encodeHTML(txt);
		if(System.ready)
		this.output(lines);
	},
	output : function(lines){
		var zdepth;
		if(this.scrDepth)
			zdepth = 50000;
		else
			zdepth = 0;
		textToStyle  = 'position:absolute; '
					 + 'z-index:50001; '
					 + 'top:0px; '
					 + 'left:0px; '
					 + 'width:350px; '
					 + 'height:50px; '
					 + 'color:#ffffff; '
					 + 'font-family:helvetica; '
					 + 'font-size:12px;';
		textToOutput = '<table name="swapDepthButton" id="swapDepthButton" style="'+textToStyle+'" width="100%" height="22" border="0">';
		textToOutput += '<tr><td align="left" valign="top"><a onclick="System.out.swapDepth();" style="cursor:pointer;">|X|</a></td></tr>';
		textToOutput += '</table>';
		textToStyle  = 'position:absolute; '
					 + 'z-index:'+zdepth+'; '
					 + 'top:0px; '
					 + 'left:0px; '
					 + 'width:350px; '
					 + 'height:50px; '
					 + 'color:#ffffff; '
					 + 'background-color:#333333; '
					 + 'Filter: Alpha(Opacity=70); '
					 + 'opacity: .80; '
					 + 'font-family:helvetica; '
					 + 'font-size:12px;';
		textToOutput += '<table name="'+this.tableName+'" id="contentTable" style="'+textToStyle+'" >';
		textToOutput += '<tr><td height="33">&nbsp;</td></tr>';
		var start = 0;
		if(lines)
			start = this.scrlog.length-lines;
		if(start < 0)
			start = 0;
		for(i=start; i<this.scrlog.length; i++){
			textToOutput += '<tr><td align="left" valign="top">';
			textToOutput += this.scrlog[i];
			textToOutput += '</td></tr>';
		}
		textToOutput += '</table>';
		if(document.cookie.indexOf(System.cookie.sysActive)>-1){
			this.scrDiv.innerHTML = textToOutput;
			var new_div = document.getElementsByTagName('body')[0].appendChild(this.scrDiv);
			new_div['id'] = 'srcDiv';
		}
	},
	swapDepth : function(){
		if(this.scrDepth == 1){
			this.scrDepth = 0;
			document.getElementById(this.tableName).style.zIndex = -1;
		}
		else{
			this.scrDepth = 1;
			document.getElementById(this.tableName).style.zIndex = 50000;
		}
	}
}
System.cookie = {
	cookieArray : [],
	sysActive   : "systemcookie=active",
	setsys : function(){
		document.cookie = this.sysActive;
	},
	// must have name and value // expires will default to session
	// expires format "Thu, 01-Jan-1970 00:00:01 GMT"
	set : function(name,value,expires,path,domain){
		document.cookie = name + "=" + value +((path) ? ";path=" + path : "") +((domain) ? ";domain=" + domain : "") + ((expires) ? ";expires="+ expires : "");
	},
	get : function(name){
		this.parsecookies();
		return this.cookieArray[name];
	},
	// this deletes the cookie when called
	del : function(name, path, domain){
		if (this.cookieArray[name]){
			System.out.println('delete '+name+' test');
			document.cookie = name + "=" +((path) ? ";path=" + path : "") +((domain) ? ";domain=" + domain : "") +";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	},
	parsecookies : function(){
		// parse through cookies and create name value pairs in the cookieArray
		this.cookieArray.length = 0;
		var cookies = document.cookie;
		var namevalues = cookies.split(';');
		for(i=0; i<namevalues.length; i++){
			// the cookieParams will have multiple = signs
			if(namevalues[i].indexOf('cookieParams')>-1){
				var splitAt = namevalues[i].indexOf('=');
				var pairs = new Array();
				pairs[0] = namevalues[i].substring(0,splitAt);
				pairs[1] = namevalues[i].substring(splitAt+1);
			}
			else{
				var pairs = namevalues[i].split('=');
			}
			this.cookieArray[System.util.trimString(pairs[0])] = pairs[1];
		}
	},
	test : function(cookieNameAndValue){
		if(document.cookie && document.cookie.indexOf(cookieNameAndValue)>-1)
			return true;
		else
			return false;
	}
}// end System.cookie
System.metaData = function(){
	System.on();
	textToOutput = '<table>';
	metel = document.getElementsByTagName('meta');
	if(metel.length==0)
		textToOutput += '<tr><td> NO META TAGS ON THIS PAGE </td></tr>';
	for(i=0; i<metel.length; i++){
		textToOutput += '<tr><td align="right" valign="top" width="30">';
		textToOutput += metel[i].name.toUpperCase().substring(0,3)+':</td><td align="left" valign="top"> '+metel[i].content+'</td></tr>';
	}
	textToOutput += '</table>';
	System.out.println(textToOutput);
}
System.util = {
	encodeHTML : function(strValue){
		strValue=strValue.replace(/\&/g,'&amp;');
		strValue=strValue.replace(/\</g,'&lt;');
		strValue=strValue.replace(/\>/g,'&gt;');
		strValue=strValue.replace(/\"/g,'&quot;');
		return strValue
	},// end encodeHTML
	trimString : function(str){
		if(typeof str != 'string')
			return str;
		var retString = str;
		// check begining of string for white spaces
		var character = retString.substring(0,1);
		while(character.charCodeAt(0) == 9 || character.charCodeAt(0) == 32 || character.charCodeAt(0) == 10){
			retString = retString.substring(1,retString.length);
			character = retString.substring(0,1);
		}
		// check end of string for white spaces
		character = retString.substring(retString.length-1,retString.length);
		while(character.charCodeAt(0) == 9 || character.charCodeAt(0) == 32 || character.charCodeAt(0) == 10){
			retString = retString.substring(0,retString.length-1);
			character = retString.substring(retString.length-1,retString.length);
		}
		return retString;
	},// end trimString
	stripWhitespace : function(str){
		var result = str;
    	result = result.split("\t").join(" ");
    	result = result.split("\r").join(" ");
    	result = result.split("\n").join(" ");
    	return result;
	}
}
if(window.attachEvent){
	window.attachEvent('onload',System.init);
}
else if(document.addEventListener){
// safari
	if (/WebKit/i.test(navigator.userAgent)){
		var safari_timer = setInterval(function(){
			if (/loaded|complete/.test(document.readyState)){
				clearInterval(safari_timer);
				System.init();
			}
		}, 10);
	}
	else // not safari
		document.addEventListener('DOMContentLoaded',System.init,null);
}



