/* TurtleHawk Productions : turtlehawk.com : 2010 */

var _gaq = [['_setAccount', 'UA-18311476-4'], ['_trackPageview']]; //Google Analytics
var THP = {}; //TurtleHawk namespace

THP.page = function() {
	
	var _info = {};	//ssl: encryption (boolean); host: hostname (string); port: port (string); path: pathname (string); patharray: pathname (array);
					//querystring: query (string); query: query (object); referrer: referring url (string); live: live server (boolean);
	
	var onInit = function() {
		loadInfo();
		loadOPCs();
	}
	
	var onLoad = function() { //window.onload
		var objLink = document.getElementById('conlink');
		objLink.href = [[['rallyheads','lto'].reverse().join(':'),['com','prroadrally'].reverse().join('.')].join('@'),'mai'].reverse().join('');
	}
	
	var loadOPCs = function() { //loads "other people's code" sources
		var strFile;
		
		//Google Analytics
		if(_info.live) {
			strFile = _info.ssl ? 'https://ssl' : 'http://www'; strFile += '.google-analytics.com/ga.js';
			THP.utils.loadJSFile(strFile, true, 'head');
		}
	}
	
	var loadInfo = function() { //loads "info" object
		var strPath; var arrPath;
		var strQuery; var arrQuery; var objQuery;
		var i; var arrTemp;
		
		//window object properties
		_info.ssl = (window.location.protocol == 'https:') ? true : false;
		_info.host = window.location.hostname;
		_info.port = window.location.port;
		
		strPath = window.location.pathname;
		arrPath = strPath.slice(1).split('/');
		if(arrPath[(arrPath.length - 1)] == '') {arrPath[(arrPath.length - 1)] = 'index.html'; strPath += 'index.html';}
		_info.path = strPath; _info.patharray = arrPath;
		
		strQuery = window.location.search; objQuery = {};
		if(strQuery != '') {
			strQuery = strQuery.slice(1); arrQuery = strQuery.split('&');
			for(i=0; i<arrQuery.length; i++) {
				arrTemp = arrQuery[i].split('='); objQuery[arrTemp[0]] = arrTemp[1];
			}
		}
		_info.querystring = strQuery; _info.query = objQuery
		
		//document object properties
		_info.referrer = document.referrer;
		
		//live server?
		_info.live = (_info.host.indexOf('prroadrally.com') != -1) ? true : false;
	}
	
	var getInfo = function() { //gets copy of "info" object
		var objTemp = {}; var x;
		for(x in _info) {objTemp[x] = _info[x];}
		return(objTemp);
	}
	
	return {
		init: onInit, onLoad: onLoad,
		getInfo: getInfo
	}
	
}();

THP.utils = function() {
	
	var loadJSFile = function(file, asyn, node) { //loads external js file
		file = (typeof(file) == 'string') ? file : '';
		asyn = (typeof(asyn) == 'boolean') ? asyn : false;
		node = (asyn && (typeof(node) == 'string')) ? node : 'head';
		
		var objFile; var objNode;
		
		if(asyn) {
			objFile = document.createElement('script');
			objFile.type = 'text/javascript';
			objFile.loaded = false;
			
			if(objFile.readyState) { //IE & Opera
				objFile.onreadystatechange = function() {
					if((objFile.readyState == 'loading' || objFile.readyState == 'complete') && !objFile.loaded) {objFile.loaded = true; onJSFileLoad();}
				}
			} else {
				objFile.onload  = function() {
					if(!objFile.loaded) {objFile.loaded = true; onJSFileLoad();}
				}
			}
			
			objFile.src = file;
			objFile.async = true;
			
			objNode = document.getElementsByTagName(node)[0];
			objNode = (objNode == null) ? document.getElementById(node) : objNode;
			if(objNode != null) {objNode.appendChild(objFile);}
		} else {document.write('<script type="text/javascript" src="' + file + '"><\/script>');}
	}
	
	var onJSFileLoad = function() {} //callback: "loadJSFile" (asynchronous)
	
	var loadCSSFile = function(file, asyn, node) { //loads external css file
		file = (typeof(file) == 'string') ? file : '';
		asyn = (typeof(asyn) == 'boolean') ? asyn : false;
		node = (asyn && (typeof(node) == 'string')) ? node : 'head';
		
		var objFile; var objNode;
		
		if(asyn) {
			objFile = document.createElement('link');
			objFile.rel = 'stylesheet';
			objFile.type = 'text/css';
			objFile.href = file;
		
			objNode = document.getElementsByTagName(node)[0];
			objNode = (objNode == null) ? document.getElementById(node) : objNode;
			if(objNode != null) {objNode.appendChild(objFile);}
		} else {document.write('<link rel="stylesheet" type="text/css" href="' + file + '" />');}
	}
	
	return {
		loadJSFile: loadJSFile, loadCSSFile: loadCSSFile
	}
	
}();

var trace = function(text) {alert(text);} //allows "trace" to be used in place of "alert"
window.onload = THP.page.onLoad;
THP.page.init();
