function init() {
	quirksExtLinks();
	BrowserDetect.init();
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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);
	}
	return null;
}

function quirksExtLinks() {
	if (!document.getElementsByTagName) {
		return;
	}

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "off-site") {
			anchor.target = "_blank";
		}
	}
}

// Browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

// Fixing document.write (http://ejohn.org/blog/xhtml-documentwrite-and-adsense/)
// but only if not IE
if (BrowserDetect.browser != 'Explorer') {
	document.write = function(str){
	    var moz = !window.opera && !/Apple/.test(navigator.vendor);
       
	    // Watch for writing out closing tags, we just
	    // ignore these (as we auto-generate our own)
	    if ( str.match(/^<\//) ) return;
 
	    // Make sure & are formatted properly, but Opera
	    // messes this up and just ignores it
	    if ( !window.opera )
	        str = str.replace(/&(?![#a-z0-9]+;)/g, "&amp;");
 
	    // Watch for when no closing tag is provided
	    // (Only does one element, quite weak)
	    str = str.replace(/<([a-z]+)(.*[^\/])>$/, "<$1$2></$1>");
       
	    // Mozilla assumes that everything in XHTML innerHTML
	    // is actually XHTML - Opera and Safari assume that it's XML
	    if ( !moz )
	        str = str.replace(/(<[a-z]+)/g, "$1 xmlns='http://www.w3.org/1999/xhtml'");
       
	    // The HTML needs to be within a XHTML element
	    var div = document.createElementNS("http://www.w3.org/1999/xhtml","div");
	    div.innerHTML = str;
       
	    // Find the last element in the document
	    var pos;
       
	    // Opera and Safari treat getElementsByTagName("*") accurately
	    // always including the last element on the page
	    if ( !moz ) {
	        pos = document.getElementsByTagName("*");
	        pos = pos[pos.length - 1];
               
	        // Mozilla does not, we have to traverse manually
	    } else {
	        pos = document;
	        while ( pos.lastChild && pos.lastChild.nodeType == 1 )
	            pos = pos.lastChild;
	    }
       
	    // Add all the nodes in that position
	    var nodes = div.childNodes;
	    while ( nodes.length )
        pos.parentNode.appendChild( nodes[0] );
	}
}
