function AttachOnload(MyFunction) {
	if (window.attachEvent) { window.attachEvent("onload",MyFunction); }
	else if (window.addEventListener) { window.addEventListener("load",MyFunction,false); }
}

// Check for targetblank
function makeLinksPopup() {
	convertLinks("a");	// Links
	convertLinks("area");	// Image Maps
}


function convertLinks(tagType) {
	var targets = document.getElementsByTagName(tagType); 
	if (targets) {
		for (var i = 0; i < targets.length; i++) {
			if (targets[i].rel) {
				if ((targets[i].rel == "targetblank") || (targets[i].rel == "lightbox")) {
					targets[i].target = "_blank";
				}
			}
		}
	}
}

function splitBrowsers(id) {
	if (typeof(document.styleSheets[id].cssRules) == "object") {
		return document.styleSheets[id].cssRules;
	}
	else {
		return document.styleSheets[id].rules;
	}
}

function getStyleBySelector( selector ) {
   var sheetList = document.styleSheets;
   var ruleList;
   var i, j;

   /* look through stylesheets in reverse order that they appear in the document */
   for (i=sheetList.length-1; i >= 0; i--) {
	   ruleList = splitBrowsers(i);
	   for (j=0; j<ruleList.length; j++) {
		   if (ruleList[j].selectorText == selector) {
				return ruleList[j].style;
		   }   
	   }
   }
   return null;
}

// Alternative to m.offsetLeft as some browsers take their offsets from the body while others take it from the parent element
function FindPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) { curleft += obj.x; }
	return curleft;
}

// Alternative to m.offsetTop as some browsers take their offsets from the body while others take it from the parent element
function FindPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) { curtop += obj.y; }
	return curtop;
}

// get the height of the window
function GetWindowHeight() {
	if (window.innerHeight) 
		var window_height = window.innerHeight;
	else {
		if (document.documentElement.clientHeight)
			var window_height = document.documentElement.clientHeight;
		else
			var window_height = document.body.clientHeight;
	}
	return window_height;
}

// get the position of the scroll bar
function GetScrollTop() {
	if (window.pageYOffset)
		var window_top = window.pageYOffset;
	else {
		if (document.documentElement.scrollTop)
			var window_top = document.documentElement.scrollTop;
		else
			var window_top = document.body.scrollTop;
	}
	return window_top;
}

function writeThickboxCSS() {
	var obj1=getStyleBySelector("#TB_overlay");
	if (obj1!=null) {
		obj1.Opacity="0.6";
		obj1.MozOpacity="0.6";
		obj1.filter="alpha(opacity=60)";
	}

	var obj2=getStyleBySelector("#TB_HideSelect");
	if (obj2!=null) {
		obj2.Opacity="0";
		obj2.MozOpacity="0";
		obj2.filter="alpha(opacity=0)";
	}

	var obj3=getStyleBySelector(".clearfix");
	if (obj3!=null) obj3.Display="inline-block";
}

// Attaches a style to the page e.g. AttachStyle("#wrapper", "width:500px");
function AttachStyle(to, what) {

	// IE of course needs its own version
		if (document.createStyleSheet) {
			var obj = document.createStyleSheet();
			return obj.addRule(to, what);
		}

	// for the good browsers
		var head = document.getElementsByTagName("head")[0];
		var obj = document.createElement("STYLE");

		if (obj && head) {
			obj.setAttribute("type", "text/css");

			var entry = document.createTextNode(to + " { " + what + " }");

			if (obj.appendChild && head.appendChild) {
				obj.appendChild(entry);
				head.appendChild(obj);
			}
		}
}

// attaches a stylesheet to the page
function AttachStyleSheet(src) {

	var head = document.getElementsByTagName("head")[0];
	var obj = document.createElement("link");

	if (obj && head) {
		obj.setAttribute("type", "text/css");
		obj.setAttribute("rel", "stylesheet");
		obj.setAttribute("href", src);

		if (head.appendChild) head.appendChild(obj);
	}
}

AttachOnload(makeLinksPopup);
AttachOnload(writeThickboxCSS);