var allPreloadImages = new Object();
var isIE = ((!window.opera) && (navigator.userAgent.indexOf('MSIE') != -1));
var isIECSSCompat = ((document.compatMode && document.compatMode.indexOf('CSS1') >= 0) ? true : false);

// Little Helpers & DOM Stuff **********************************
function addEvent(elemRef, evType, func, useCapture) {
	var elem = getEl(elemRef);
	useCapture = (useCapture) ? useCapture : false;
	if (elem.addEventListener) { // DOM
		elem.addEventListener(evType, func, useCapture);
		return true;
	} else if (elem.attachEvent) { // IE
		var r = elem.attachEvent('on' + evType, func);
		return r;
	} else {
		// for IE/Mac, NN4, and older
		elem['on' + evType] = func;
	}
}

function ascendDOM(elemRef, target) {
	var elem = getEl(elemRef);
	while ((elem.nodeName.toLowerCase() != target) && (elem.nodeName.toLowerCase() != 'html'))
		elem = elem.parentNode;
	return (elem.nodeName.toLowerCase() == 'html') ? null : elem;
}

function attVal(element, attName) { // scrollImageListener
	return parseInt(element.getAttribute(attName));
}

function findPosX(elemRef) {
	var elem = getEl(elemRef);
	var curLeft = 0;
	if (elem.offsetParent) {
		do {
			curLeft += elem.offsetLeft;
		} while (elem = elem.offsetParent);
	}
	else if (elem.x) {
		curLeft += elem.x;
	}
	return curLeft;
}

function findPosY(elemRef) {
	var elem = getEl(elemRef);
	var curTop = 0;
	if (elem.offsetParent) {
		do {
			curTop += elem.offsetTop;
		} while (elem = elem.offsetParent);
	}
	else if (elem.y) {
		curTop += elem.y;
	}
	return curTop;
}

function getEl(elemRef) {
	if (typeof elemRef == 'string') {
		if (document.getElementById)
			return document.getElementById(elemRef);
		else if (document.all)
			return document.all(elemRef);
	}
	else
		return elemRef;
}

function preloadImage(strURL, iWidth, iHeight) {
	allPreloadImages[strURL] = new Image(iWidth, iHeight);
	allPreloadImages[strURL].src = strURL;
}

function winHeight() {
	var wH = 0;
	if (window.innerHeight) { // Mozilla
		wH = window.innerHeight;
	} else if (isIECSSCompat) { // IE7
		wH = document.body.parentElement.clientHeight;
	} else if (document.documentElement.clientHeight) {
		wH = document.documentElement.clientHeight;
	} else if (document.body.clientHeight) {
		wH = document.body.clientHeight;
	} else if (screen.availHeight) {
		wH = screen.availHeight;
	}
	return wH;
}

function winWidth() {
	var wW = 0;
	if (window.innerWidth) { // Mozilla
		wW = window.innerWidth;
	} else if (isIECSSCompat) { // IE7
		wW = document.body.parentElement.clientWidth;
	} else if (document.documentElement.clientWidth) {
		wW = document.documentElement.clientWidth;
	} else if (document.body.clientWidth) {
		wW = document.body.clientWidth;
	} else if (screen.availWidth) {
		wW = screen.availWidth;
	}
	return wW;
}


// Scroll Image Stuff **********************************
function scrollImageListener(e) {
	if (!e) var e = window.event;
	var t = e.target ? e.target : e.srcElement;
	var xPos, yPos;
	var pos = moveListener(e);
	xPos = pos.x - findPosX(t);
	yPos = pos.y - findPosY(t);

	if (t.nodeName.toLowerCase() == 'img')
		t = t.parentNode;
	if (t.nodeName.toLowerCase() == 'a') {
		var scaleFactorY = ((attVal(t, 'mainy') - attVal(t, 'thumby')) / attVal(t, 'thumby') * 0.985);
		var scaleFactorX = ((attVal(t, 'mainx') - attVal(t, 'thumbx')) / attVal(t, 'thumbx') * 0.985);
		t.style.backgroundPosition = (-parseInt(xPos * scaleFactorX)) + 'px ' + (-parseInt(yPos * scaleFactorY)) + 'px';
	}
}

function moveListener(e) {
	if (!e) var e = window.event;
	var xPos, yPos;
	if (e.pageX && e.pageY) {
		xPos = e.pageX;
		yPos = e.pageY;
	} else {
		xPos = e.clientX;
		yPos = e.clientY;
		if (isIE) {
			var pos = getScrollPosIE();
			xPos += pos.x;
			yPos += pos.y;
		}
	}
	return {x: xPos, y: yPos};
}

function getScrollPosIE() {
	var xPos = 0;
	var yPos = 0;

	if (document.body && document.body.scrollLeft)
		xPos = document.body.scrollLeft;
	else if (document.documentElement && document.documentElement.scrollLeft)
		xPos = document.documentElement.scrollLeft;

	if (document.body && document.body.scrollTop)
		yPos = document.body.scrollTop;
	else if (document.documentElement && document.documentElement.scrollTop)
		yPos = document.documentElement.scrollTop;

	return {x: xPos, y: yPos};
}


// String Handling **********************************
// extract front part of string prior to searchString
function getFront(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(0,foundOffset);
}

// extract back end of string after searchString
function getEnd(mainStr, searchStr) {
	var foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset == -1) return null;
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}

// extract middle part of string between strFront and strEnd
function getMiddle(mainStr, strFront,strEnd) {
	var front = getEnd(mainStr,strFront);
	if (front != null) return getFront(front,strEnd);
	return null;
}
