//currently displayed image
cImg = "";
/* ************************************************************************* */
window.onload = function () {
	// remove the focus rectangle on clicked anchor elements
	var allAnchors = document.getElementsByTagName("a");
	for (var i in allAnchors) {
		addMyEvent(document.getElementsByTagName("a")[i],'click',clickBlur);
	}
	// ad the up/down movement to the image-thumbs
	var thumbCount = 0;
	while (document.getElementById('imgthumb'+thumbCount)) {
		addMyEvent(document.getElementById('imgthumb'+thumbCount),'mouseover',objMover,['up','imgthumb'+thumbCount,0,2,16,1,0.5]);
		addMyEvent(document.getElementById('imgthumb'+thumbCount),'mouseout',objMover,['down','imgthumb'+thumbCount,64,2,16,1,0.5]);
		thumbCount++;
	}
	// add the movement to the thumbnails-bar
	if (document.getElementById('less') || document.getElementById('more')) {
		addMyEvent(document.getElementById('less'),'mouseover',objMover,['left','thumbnails',0,1,4,20]);
		addMyEvent(document.getElementById('less'),'mouseout',stopMove,['thumbnails']);
		addMyEvent(document.getElementById('more'),'mouseover',objMover,['right','thumbnails',-(document.getElementById('thumbnails').offsetWidth - winSize()[0]),1,4,20]);
		addMyEvent(document.getElementById('more'),'mouseout',stopMove,['thumbnails']);
	}
	// add the movement to the exchange gallery
	if (document.getElementById('up') || document.getElementById('down')) {
		addMyEvent(document.getElementById('up'),'mouseover',objMover,['down','fileContainer',0,1,4,20]);
		addMyEvent(document.getElementById('up'),'mouseout',stopMove,['fileContainer']);
		addMyEvent(document.getElementById('down'),'mouseover',objMover,['up','fileContainer',-(document.getElementById('fileContainer').offsetHeight - winSize()[1]),1,4,20]);
		addMyEvent(document.getElementById('down'),'mouseout',stopMove,['fileContainer']);
	}
	// disable event bubbeling for IE
	if (window.attachEvent) {
		window.event.cancelBubble = true;
	}
	// empty the eventcache on unload
	addMyEvent(window,'unload',EventCache.flush, false);
	//
	// hide 'more' and 'less' if the thumbnails do not fill the window
	if (document.getElementById('thumbnails')) {
		if (document.getElementById('thumbnails').offsetWidth < winSize()[0]) {
			document.getElementById('less').style.visibility = "hidden";
			document.getElementById('more').style.visibility = "hidden";
			document.getElementById('thumbnails').style.left = ((winSize()[0] - document.getElementById('thumbnails').offsetWidth) / 2)+"px";
		}
	}
	// show 'up' and 'down' if the gallery does not fill the window
	if (document.getElementById('files')) {
		if ((document.getElementById('files').offsetHeight - 1) < winSize()[1]) {
			document.getElementById('fileNav').style.visibility = "hidden";
		}
	}
}
/* ************************************************************************* */
window.onresize = function() {
	// watch for window size-changes and correct the position of the thumbnails
	if (document.getElementById('thumbnails')) {
		// checke whether the thumbnails fill up the screnn width
		if (document.getElementById('thumbnails').offsetWidth > winSize()[0]) {
			document.getElementById('less').style.visibility = "visible";
			document.getElementById('more').style.visibility = "visible";
			document.getElementById("more").mouseoverprops[2] = -(document.getElementById('thumbnails').offsetWidth - winSize()[0]);
			if ((document.getElementById('thumbnails').offsetWidth + document.getElementById('thumbnails').offsetLeft) < winSize()[0]) {
				var newPos = document.getElementById('thumbnails').offsetLeft + (winSize()[0] - (document.getElementById('thumbnails').offsetWidth + document.getElementById('thumbnails').offsetLeft));
				if (newPos > 0) newPos = 0;
				document.getElementById('thumbnails').style.left = newPos + "px";
			}
		} else {
			document.getElementById('less').style.visibility = "hidden";
			document.getElementById('more').style.visibility = "hidden";
			document.getElementById('thumbnails').style.left = "0px";
			document.getElementById('thumbnails').style.left = ((winSize()[0] - document.getElementById('thumbnails').offsetWidth) / 2)+"px";
		}
	}
}
/* ************************************************************************* */
function mouseSpy() {
	//alert("OUT")
}
/* ************************************************************************* */
function winSize() {
	var winW, winH;
	winW = window.innerWidth ? window.innerWidth : document.body.clientWidth;
	winH = window.innerHeight ? window.innerHeight : document.body.clientHeight;
	return [winW, winH];
}
/* ************************************************************************* */
// remove the focus rectangle on clicked anchor elements, the actual function
function clickBlur() {
	this.blur();
}
/* ************************************************************************* */
/*	Cross-browser event addition
	obj = object
	evt = event
	fn  = function
	opt = options (array), optional (attach additional options to the target object; the only way to deliver optinins this way)
*/
function addMyEvent(obj,evt,fn,opt) {
	if (obj.addEventListener) { // Standard
		obj.addEventListener(evt,fn,true);
		if (opt) {
			obj[evt+'props'] = new Array();
			for (var i in opt) {
				obj[evt+'props'].push(opt[i]);
			}
		}
		return true;
	} else if (obj.attachEvent) { // IE
		obj.attachEvent('on'+evt,fn);
		if (opt) {
			obj[evt+'props'] = new Array();
			for (var i in opt) {
				obj[evt+'props'].push(opt[i]);
			}
		}
		return true;
	} else {
		return false;
	}
}

function getMyEventSrc(evt) {
	if (!evt) evt = window.event;
	if (evt.originalTarget) {
		return evt.originalTarget;
	} else if (evt.srcElement) {
		return evt.srcElement;
	}
}

function removeMyEvent(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler, bCapture) {
			listEvents.push(arguments);
		},
		flush : function() {
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1) {
				item = listEvents[i];
				if(item[0].removeEventListener) {
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on") {
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent) {
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

/* ************************************************************************* */
function objPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function objOffset(obj) {
	var ofsleft = document.getElementById(obj).offsetLeft;
	// an obvious correction for non IE-browsers?!?
	if (navigator.userAgent.indexOf('MSIE') == -1 && obj.indexOf('imgthumb') != -1) {
		var ofstop = document.getElementById(obj).offsetTop - 85;
	} else {
		var ofstop = document.getElementById(obj).offsetTop;
	}
	return [ofsleft,ofstop];
}
/* ************************************************************************* */
//myLoopIsRunning = false;
//myTimeoutID = null;

function objMover(evt) {
	// props[0] = direction
	// props[1] = div-id (to move)
	// props[2] = target position
	// props[3] = 0|1|2 - jump|move|tween
	// props[4] = tween steps
	// props[5] = intervals
	// props[6] = powr
	if (evt.target) {
		var props = this[evt.type+'props'];
	} else {
		var targ = window.event.srcElement;
		while (targ.nodeName != 'DIV') {
			targ = targ.parentNode;
		}
		var props = targ[evt.type+'props'];
	}
	if (props[3] == 1) {
		linearMove(props[1],props[0],props[2],props[4],props[5]);
	} else if (props[3] == 2) {
		tweenMove(props[1],props[0],props[2],props[4],props[5],props[6]);
	} else {
		document.getElementById(props[1]).style.left = props[2]+"px";
	}
}

function linearMove(obj,stle,tgt,steps,intervals) {
	if (intervals == '') intervals = 1;
	var myObj = document.getElementById(obj);
	if (myObj.myTimeoutID) window.clearInterval(myObj.myTimeoutID);
	// exit when the inner object is smaller than the outer
	if (myObj.offsetWidth < myObj.parentNode.offsetWidth) return;
	switch (stle) {
		case "left":
			myObj.myTimeoutID = window.setInterval(
				function() {
					var pos = objOffset(obj)[0];
					myObj.newPos = pos + steps;
					if (myObj.newPos > tgt) {
						myObj.newPos = tgt;
						window.clearInterval(myObj.myTimeoutID);
					}
					myObj.style.left  = myObj.newPos+"px";
				}
				,intervals);
			break;
		case "up":
			myObj.myTimeoutID = window.setInterval(
				function() {
					var pos = objOffset(obj)[1];
					myObj.newPos = pos - steps;
					if (myObj.newPos < tgt) {
						myObj.newPos = tgt;
						window.clearInterval(myObj.myTimeoutID);
					}
					myObj.style.top  = myObj.newPos+"px";
				}
				,intervals);
			break;
		case "right":
			myObj.myTimeoutID = window.setInterval(
				function() {
					var pos = objOffset(obj)[0];
					myObj.newPos = pos - steps;
					if (myObj.newPos < tgt) {
						myObj.newPos = tgt;
						window.clearInterval(myObj.myTimeoutID);
					}
					myObj.style.left  = myObj.newPos+"px";
				}
				,intervals);
			break;
		case "down":
			myObj.myTimeoutID = window.setInterval(
				function() {
					var pos = objOffset(obj)[1];
					myObj.newPos = pos + steps;
					if (myObj.newPos > tgt) {
						myObj.newPos = tgt;
						window.clearInterval(myObj.myTimeoutID);
					}
					myObj.style.top  = myObj.newPos+"px";
				}
				,intervals);
			break;
	}
}
function tweenMove(obj,stle,tgt,steps,intervals,powr) {
	if (intervals == '') intervals = 1;
	if (powr == '') powr = 0.5;
	var myObj = document.getElementById(obj);
	if (myObj.myTimeoutID) window.clearInterval(myObj.myTimeoutID);
	var actStep = 0;
	switch (stle) {
		case "left":
			var pos = objOffset(obj)[0];
			myObj.myTimeoutID = window.setInterval(
				function() {
					myObj.currentPos = easeInOut(pos,tgt,steps,actStep,powr);
					myObj.style.left  = myObj.currentPos+"px";
					actStep++;
					if (actStep > steps) window.clearInterval(myObj.myTimeoutID);
				}
				,intervals);
			break;
		case "up":
			var pos = objOffset(obj)[1];
			myObj.myTimeoutID = window.setInterval(
				function() {
					myObj.currentPos = easeInOut(pos,tgt,steps,actStep,powr);
					myObj.style.top  = myObj.currentPos+"px";
					actStep++;
					if (actStep > steps) window.clearInterval(myObj.myTimeoutID);
				}
				,intervals);
			break;
		case "right":
			var pos = objOffset(obj)[0];
			myObj.myTimeoutID = window.setInterval(
				function() {
					myObj.currentPos = easeInOut(pos,tgt,steps,actStep,powr);
					myObj.style.left  = myObj.currentPos+"px";
					actStep++;
					if (actStep > steps) window.clearInterval(myObj.myTimeoutID);
				}
				,intervals);
			break;
		case "down":
			var pos = objOffset(obj)[1];
			myObj.myTimeoutID = window.setInterval(
				function() {
					myObj.currentPos = easeInOut(pos,tgt,steps,actStep,powr);
					myObj.style.top  = myObj.currentPos+"px";
					actStep++;
					if (actStep > steps) window.clearInterval(myObj.myTimeoutID);
				}
				,intervals);
			break;
	}
}

function stopMove(evt) {
	if (evt.target) {
		var props = this[evt.type+'props'];
	} else {
		var targ = window.event.srcElement;
		while (targ.nodeName != 'DIV') {
			targ = targ.parentNode;
		}
		var props = targ[evt.type+'props'];
	}
	window.clearInterval(document.getElementById(props[0]).myTimeoutID);
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

// simple linear tweening - no easing
// t: current time, b: beginning value, c: change in value, d: duration
function linearTween(t, b, c, d) {
	t = new Date().getTime();
	return c*t/d + b;
}
function loadImage(w) {
	alert(this + ", " + w);
}
/* ************************************************************************* */
function showImg(wImg) {
	cImg = wImg;
	// reset the currently displayed image
	document.getElementById("lrgImg").src = "./files/space.gif";
	// show the "larger"-button
	document.getElementById('larger').style.visibility = "visible";
	var newImg = "./index.php?rex_resize=480h__"+wImg+"&amp;rex_filter[]sharpen";
	document.getElementById("lrgImg").src = newImg;
}

function hideImg() {
	cImg = "";
	// reset the currently displayed image
	document.getElementById("lrgImg").src = "./files/space.gif";
	// hide the "larger"-button
	document.getElementById('larger').style.visibility = "hidden";
}

function bigImg() {
	//alert(cImg);
	var imgURL = "./files/"+cImg;
	biggerImg = window.open(imgURL, "Bigger", "location=0, menubar=0, status=0, toolbar=0, scrollbars=1");
	biggerImg.resizeTo(screen.availWidth,screen.availHeight);
	biggerImg.moveTo(0,0);
	biggerImg.focus();
}
/* ************************************************************************* */
showhideOpen = "";

function showhide(w) {
	if (showhideOpen != "" && showhideOpen == w) {
		document.getElementById("txt"+showhideOpen).style.display = "none";
		document.getElementById("ellip"+showhideOpen).style.display = "inline";
		showhideOpen = "";
	} else if (showhideOpen != "") {
		document.getElementById("txt"+showhideOpen).style.display = "none";
		document.getElementById("ellip"+showhideOpen).style.display = "inline";
		document.getElementById("txt"+w).style.display = "block";
		document.getElementById("ellip"+w).style.display = "none";
		showhideOpen = w;
	} else {
		document.getElementById("txt"+w).style.display = "block";
		document.getElementById("ellip"+w).style.display = "none";
		showhideOpen = w;
	}
}
/* ************************************************************************* */
