window.__requireJs = function(url) { document.write('<script src="'+url+'" type="text/javascript"></script>\n'); }
window.__requireCss = function(url) { document.write('<link href="'+url+'" type="text/css" rel="stylesheet" />'); }


// Usage

__requireJs("/_common/groundlogic/jquery/jquery.js");
__requireJs("/_common/groundlogic/rich/swfobject.js");


// Class

var GroundLogic = function() {


	var $this = this;


	// Variables
	
	
	// Functions


	// Settings

	this.getBooleanSetting = function(name,normal) {
		var boolean = normal;
		var query = __getQuery(name);
		var cookie = __cookieGet(name);
		if (cookie == "true" || cookie == "on") boolean = true;
		if (cookie == "false" || cookie == "off") boolean = false;
		if (query == "true" || query == "on") {
			boolean = true; __cookieSet(name,boolean);
		}
		if (query == "false" || query == "off") {
			boolean = false; __cookieSet(name,boolean);
		}
		if (boolean == normal) __cookieDelete(name);
		return boolean;
	}


	// Flash

	this.flashObject = function(flashName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[flashName] : document[flashName];
	}
	window.__flashObject = this.flashObject;


	// Page

	this.pageGetUrl = function(url, author) {
		if (author) {
			window.alert("href='"+unescape(url)+"'");
		} else {
			window.location.href = unescape(url);
		}
	}
	window.__pageGetUrl = this.pageGetUrl;

	this.pageRefresh = function() {
		window.location.href = window.location.href;
	}
	window.__pageRefresh = this.pageRefresh;


	// Hash

	this.pageSetHash = function(url) {
		if (!url || url == "null") var url = "";
		var hashValue = window.location.hash;
		if (url != hashValue) {
			if ($this.getBrowser() == "FireFox") url = "#" + url;
			window.location.hash = url;
		}
	}
	window.__pageSetHash = this.pageSetHash;

	this.pageGetHash = function() {
		var flashHash = "";
		var hashValue = window.location.hash;
		if (hashValue.indexOf("#/") != -1) {
			var split = hashValue.split('#/');
			flashHash = "/" + split[split.length-1];
		}
		return flashHash;
	}
	window.__pageGetHash = this.pageGetHash;

	this.pageSetTitle = function(title) {
		var titleValue = document.title;
		if (title != titleValue) {
			document.title = title;
		}
	}
	window.__pageSetTitle = this.pageSetTitle;
	

	// Query

	this.getQuery = function(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) return pair[1];
		}
	}
	window.__getQuery = this.getQuery;


	// Cookies

	this.cookieSet = function(name,value,days,path,domain,secure) {
		var expires = 0;
		if (days) {
			date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = date;
		}
		var cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "; path=/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
		document.cookie = cookie;
	}
	window.__cookieSet = this.cookieSet;
	
	this.cookieGet = function(variable) {
		var variable = variable + "=";
		var vars = document.cookie.split(';');
		for (var i=0;i<vars.length;i++) {
			var c = vars[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(variable) == 0) return c.substring(variable.length,c.length);
		}
	}
	window.__cookieGet = this.cookieGet;
	
	this.cookieDelete = function(name) {
		$this.cookieSet(name,"",-1);
	}
	window.__cookieDelete = this.cookieDelete;
	
	this.cookieCheck = function(name) {
		return $this.cookieGet(name);
	}
	window.__cookieCheck = this.cookieCheck;


	// History

	this.historySet = function(history) { 
		__cookieSet("richhistory", history);
	}
	window.__historySet = this.historySet;

	this.historyGet = function() { 
		return __cookieGet("richhistory"); 
	}
	window.__historyGet = this.historyGet;

	this.historyDelete = function() { 
		__cookieDelete("richhistory"); 
	}
	window.__historyDelete = this.historyDelete;

	this.historyAdd = function(add) {
		var history = $this.historyGet();
		$this.historySet(history + add + "|");
	}
	window.__historyAdd = this.historyAdd;


	// Legend
	
	this.legendSet = function(legend) { 
		__cookieSet("richlegend", legend); 
	}
	window.__legendSet = this.legendSet;
	
	this.legendGet = function(obj) { 
		__flashObject(obj+"_swf").returnLegend(__cookieGet("richlegend"));
	}
	window.__legendGet = this.legendGet;

	this.legendDelete = function() { 
		__cookieDelete("richlegend"); 
	}
	window.__legendDelete = this.legendDelete;	

		
	// Debug JS

	window.__debugJS = {}

	var $delimiterchar = "-";
	var $delimiterchars = 50;
		
	this.debugTrace = function(trc) {
		var trace = '';
		trace += '<span class="Trace">' + trc + '</span>';
		trace += '<br />';
		if ($debug) $this.debugOutput(trace);
	}
	__debugJS.trace = this.debugTrace;

	this.debugVariable = function(name, variable) {
		var split = "";
		var trace = '';
		trace += '<span class="Variable">' + "var " + name + ': ' + variable + ' ' + split + ' ' + '</span>';
		trace += '<br />';
		if ($debug) $this.debugOutput(trace);
	}
	__debugJS.variable = this.debugVariable;
	
	this.debugPart = function(part) {
		var char = $delimiterchar;
		var chars = $delimiterchars - (part.length) - 10;
		var trace = '';
		trace += '<span class="Delimiter">';
		trace += char + char + char + ' ' + part + ' ';
		var i = 0;
		do { trace += char; i++; } while (i < chars);
		trace += '</span>';
		trace += '<br />';
		if ($debug) $this.debugOutput(trace);
	}
	__debugJS.part = this.debugPart;
	
	this.debugOutput = function(output) {
		var input = $("#DebugInput_JS").html();
		if ($debug) $Debug.update('JS', input+output);
	}


	// Tracking
	
	this.uai = function(i) {
		var ua = navigator.userAgent.toLowerCase();
		return (ua.indexOf(i) + 1);
	}
	window.__uai = this.uai;
	
	this.getUA = function() {
		var ua = navigator.userAgent;
		return ua;
	}
		
	this.getOS = function() {
		var os = "";
		if (__uai("linux")) os = "Linux";
		if (__uai("mac") || __uai("ppc")) os = "Mac";
		if (__uai("win")) os = "Windows";
		return os;
	}

	this.getOSVer = function() {
		var os = $this.getOS().toLowerCase();
		var ver = "";
		if (os == "mac") {
			if (__uai("os x") && __uai("intel")) ver = "OS X (Intel)";
			if (__uai("os x") && __uai("ppc")) ver = "OS X (PowerPC)";
		}
		if (os == "windows") {
			if (__uai("windows nt") || __uai("winnt3.51")) ver = "NT";
			if (__uai("windows 95") || __uai("win95")) ver = "95";
			if (__uai("windows 98") || __uai("win98")) ver = "98";
			if (__uai("windows 2000") || __uai("nt 5.0")) ver = "2000";
			if (__uai("win32") || __uai("nt 5.1")) ver = "XP";
			if (__uai("win32") || __uai("nt 6.0")) ver = "Vista";
		}
		return ver;
	}

	this.getBrowser = function() {
		var browser = "";
		if (__uai("safari")) browser = "Safari";
		if (__uai("opera")) browser = "Opera";
		if (__uai("firefox")) browser = "FireFox";
		if (__uai("msie")) browser = "MSIE";
		if (__uai("netscape")) browser = "Netscape";
		return browser;
	}

	this.getBrowserVer = function() {
		var au = navigator.userAgent.toLowerCase();
		var id = $this.getBrowser().toLowerCase();
		if (id == "safari") id = "version";
		var start = au.substring( au.indexOf(id) + id.length + 1 );
		var end = start.indexOf(" "); if (end < 1) end = start.length;
		var ver = start.substring( 0, end );
		return ver;
	}

	this.getBrowserLang = function() {
		var lang = "";
		if (navigator.language) lang = navigator.language.toLowerCase();
		else if (navigator.systemLanguage) lang = navigator.systemLanguage.toLowerCase();
		return lang;
	}
	
	this.getCookies = function() {
		var enabled = false;
		if (document.cookie) enabled = true;
		return enabled;
	}	
	
	this.getFlash = function() {
		var flash = "";
		var fl = swfobject.getFlashPlayerVersion();
		flash += (fl["major"]) + "." + (fl["minor"]) + "." + (fl["release"]);
		return flash;
	}

	this.getScreen = function() {
		var scr = (screen.width) + "x" + (screen.height);
		return scr;
	}

	this.getWindow = function() {
		var win = "";
		win = ($(window).width()) + "x" + ($(window).height());
		return win;
	}	

	this.getReferrer = function() {
		var ref = "";
		if (parent.document.referrer) ref = parent.document.referrer;
		return ref;
	}

	this.getLocalTime = function() {
		var lt = "";
		var d = new Date();
		lt += "" + d.getFullYear();
		lt += "-"; if ((d.getMonth() + 1) < 10) lt += "0"; lt += (d.getMonth() + 1);
		lt += "-"; if (d.getDate() < 10) lt += "0"; lt += d.getDate();
		lt += " "; if (d.getHours() < 10) lt += "0"; lt += d.getHours();
		lt += ":"; if (d.getMinutes() < 10) lt += "0"; lt += d.getMinutes();
		lt += ":"; if (d.getSeconds() < 10) lt += "0"; lt += d.getSeconds();
		return lt;
	}	
	
	
}


// Create Instance

window.$GroundLogic = new GroundLogic();


