var allPreloadImages = {};
var myVars = {
	isIE : ((!window.opera) && (navigator.userAgent.indexOf('MSIE') != -1)),
	isIECSSCompat : ((document.compatMode && document.compatMode.indexOf('CSS1') >= 0) ? true : false),
	j : 0,
	popup : {
		elem : null,
		step : 10,
		delay : 4,
		topdimension : 0,
		tp : 0,
		leftdimension : 0,
		lt : 0,
		rightdimension : 0,
		rt : 0,
		bottomdimension : 0,
		bt : 0,
		intervalID : null
	},
	bismarck : {
		elem : null,
		opacity : 0,
		delay : 30,
		intervalID : null
	},
	pic : {
		elElo : null,
		elSmallImages : null,
		elIconLeft : null,
		elIconLeftText : null,
		elGoBack : null,
		elGoBackText : null,
		elIconRight : null,
		elIconRightText : null,
		elBigImage : null,
		elImage : null,
		elComments : null,
		winCenter : 0,
		numSmallImages : 0,
		fromThumbNail : false,
		direction : '',
		newImageName : '',
		newImageWidth : '',
		nextImageName : '',
		nextImageWidth : 0,
		nextImageHeight : 0,
		leftImageName : '',
		leftImageWidth : 0,
		leftImageHeight : 0,
		rightImageName : '',
		rightImageWidth : 0,
		rightImageHeight : 0,
		maxWidth : 0
	}
};
myVars.bismarck.step = (myVars.isIE) ? 3 : 4;

function addEvent(elemRef, event, listener, useCapture) {
	var elem = getEl(elemRef);
	useCapture = (useCapture) ? useCapture : false;
	if (elem.addEventListener) { // W3C
		elem.addEventListener(event, listener, useCapture);
		return true;
	} else if (elem.attachEvent) { // IE; function addon to make IE understand 'this'
		var r = elem.attachEvent('on' + event, function(){listener.call(elem)});
		return r;
	} else {
		// for IE/Mac, NN4, and older
		elem['on' + event] = listener;
	}
}

function addEventOLD(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) {
	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 getElementStyle(elemRef, CSSStyleProp) { // both are string values
	var elem = getEl(elemRef);
	var styleValue, camel;
	if (elem) {
		if (document.defaultView) {
			// W3C DOM version
			var compStyle = document.defaultView.getComputedStyle(elem, '');
			styleValue = compStyle.getPropertyValue(CSSStyleProp);
		} else if (elem.currentStyle) {
			// make IE style property camelCase name from CSS version
			var IEStyleProp = CSSStyleProp;
			var re = /-\D/;
			while (re.test(IEStyleProp)) {
				camel = IEStyleProp.match(re)[0].charAt(1).toUpperCase( );
				IEStyleProp = IEStyleProp.replace(re, camel);
			}
			styleValue = elem.currentStyle[IEStyleProp];
		}
	}
	return (styleValue) ? styleValue : null;
}

function getElementWidth(elemRef) {
	var result = null;
	var elem = getEl(elemRef);
	if (elem) {
		result = parseInt(getElementStyle(elemRef, 'width'));
		if (result == null || isNaN(parseInt(result))) {
			if (elem.offsetWidth) {
				if (elem.scrollWidth && (elem.offsetWidth != elem.scrollWidth)) {
					result = elem.scrollWidth;
				} else {
					result = elem.offsetWidth;
				}
			return parseInt(result);
			}
		}
	}
	return result;
}

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};
}

function moveListener(e) {
	var e = 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 (myVars.isIE) {
			var pos = getScrollPosIE();
			xPos += pos.x;
			yPos += pos.y;
		}
	}
	return {x: xPos, y: yPos};
}

function preloadImage(strURL, iWidth, iHeight) {
	allPreloadImages[strURL] = new Image(iWidth, iHeight);
	allPreloadImages[strURL].src = strURL;
}

function scrollImageListener(e) {
	var e = e || window.event;
	var t = 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 winHeight() {
	var wH = 0;
	if (window.innerHeight) { // Mozilla
		wH = window.innerHeight;
	} else if (myVars.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 (myVars.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;
}


// 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;
}

// globally replace searchString with replaceString
function replaceString(mainStr, searchStr, replaceStr) {
	var re = new RegExp(searchStr, 'g');
	return mainStr.replace(re, replaceStr);
}

// test if searchString is contained in mainString
function isInMainstring(mainStr, searchStr) {
	var re = new RegExp(searchStr, 'g');
	return mainStr.match(re) ? true : false;
}

