/* Bild-Funktionen fr Artikel */

function img_change(name,url,besch) {
	document.images[name].src= "../archiv/medium_"+url;
	document.images[name].alt = url;
	document.images[name].title = besch;
}

function zoom(name) {
	var datei = document.images[name].src;
	var rx = /^(.*\/(large_|medium_|small_)?)(.*)$/;
	if (rx.test(datei)) datei = RegExp.$3;
	var zoom_win = window.open("/templates/zoom.php?img="+datei,"Zoom","location=0,dependent=1,resizable=0,scrollbars=0,status=0,toolbar=0,height=500,width=700");
}

function activate(obj) {
	var nodes = document.getElementsByTagName("DIV");
	for (var elt in nodes) {
		if (nodes[elt].className == 'active') nodes[elt].className = 'kat';
	}
	var nodes = document.getElementsByTagName("A");
	for (var elt in nodes) {
		if (nodes[elt].className == 'active') nodes[elt].className = 'artikel';
	}
	if (obj.className == 'artikel') obj.className = 'active';
	obj.parentNode.className = 'active';
}

/* Mouseover-Effekte */

function button_mouseout() {
	if (this.img != document.mousedownimage) this.img.src = this.img.outsrc;
}

function button_mouseover() {
	if (this.img != document.mousedownimage) this.img.src = this.img.oversrc;
}

function button_mousedown() {
	if (document.mousedownimage) document.mousedownimage.src = document.mousedownimage.outsrc;
	this.img.src = this.img.downsrc;
	document.mousedownimage = this.img;
}

function button_preload() {
	document.preloaded_images = new Array();
	var res = "";
	var rx = /^(.*\/button-[^\/.]*)(\.[a-z0-9_-]+)$/i;
	for (var i = 0; document.images[i]; i++) {
		if (!rx.test(document.images[i].src)) continue;
		var name = RegExp.$1;
		var ext = RegExp.$2;
		if (!document.preloaded_images[name+"-over"]) {
			document.preloaded_images[name+"-over"] = new Image();
			document.preloaded_images[name+"-over"].src = name+"-over"+ext;
			document.preloaded_images[name+"-down"] = new Image();
			document.preloaded_images[name+"-down"].src = name+"-down"+ext;
		}
		if (!document.images[i].name) {
			document.images[i].img = document.images[i];
			document.images[i].onmouseover = button_mouseover;
			document.images[i].onmouseout = button_mouseout;
			document.images[i].onclick = button_mousedown;
		}
		document.images[i].outsrc = name+ext;
		document.images[i].oversrc = name+"-over"+ext;
		document.images[i].downsrc = name+"-down"+ext;
	}
	var linkrx = /^link-(.*)$/;
	for (var i = 0; document.anchors[i]; i++) {
		if (!linkrx.test(document.anchors[i].name)) continue;
		var imgname = "img-"+RegExp.$1;
		if (document.images[imgname]) {
			document.anchors[i].img = document.images[imgname];
			document.anchors[i].onmouseover = button_mouseover;
			document.anchors[i].onmouseout = button_mouseout;
			document.anchors[i].onclick = button_mousedown;
		}
	}
}
setTimeout("button_preload();",200);

var sponsoren_data;
function sponsoren(dat) {
	sponsoren_data = dat;
	setInterval(sponsoren_change,15000);
	sponsoren_change();
}

function sponsoren_change() {
	var elt = document.getElementById("sponsoren");
	var img = document.createElement("IMG");
	var a = document.createElement("A");
	var sponsor = sponsoren_data[Math.floor(Math.random()*sponsoren_data.length)];
	img.src = "/archiv/small_"+sponsor[0];
	a.href = sponsor[1];
	a.appendChild(img);
	if (elt.firstChild) elt.removeChild(elt.firstChild);
	elt.appendChild(a);
}

function animate(firstimg,step,time) {
	var first = firstimg.parentNode;
	var left = first.offsetLeft+step;
	var width = first.offsetWidth+2;
	var last = null;

	for (var elt = first; elt != null;) {
		elt.style.left = left+"px";
		elt.prev = last;
		left += width;
		last = elt;
		elt = elt.nextSibling;
		while (elt != null && elt.nodeType != 1) elt = elt.nextSibling;
		last.next = elt;
	}
	last.next = first;
	first.prev = last;
	if (step > 0) first = first.prev;
	setTimeout(function(){animate_run(first,step,time);},time);
}

function animate_run(first,step,time) {
	var i;
	var width = first.offsetWidth+2;

	if (step < 0 && first.offsetLeft < -width) {
		first.style.left = (first.prev.offsetLeft+width)+"px";
		first = first.next;
	} else if (step > 0 && first.offsetLeft >= first.offsetParent.offsetWidth) {
		first.style.left = (first.next.offsetLeft-400)+"px";
		first = first.prev;
	}

	var elt = first;
	var left = first.offsetLeft+step;
	do {
		elt.style.left = left+"px";
		left += width;
		elt = elt.next;
	} while (elt != first);
	setTimeout(function(){animate_run(first,step,time);},time);
}

var scroll_elt;
function scrollTimer() {
	var elt1 = scroll_elt.firstChild;
	var elt2 = elt1.nextSibling;
	var pos = elt1.offsetLeft;
	var width = elt1.offsetWidth;
	elt1.style.left = (pos-1)+"px";
	if (-pos > width) elt1.style.left = (pos-1+2*width)+"px";
	if (pos > 0) elt2.style.left = (pos-1-width)+"px";
	else elt2.style.left = (pos-1+width)+"px";
}
function scrollTimerV() {
	var elt1 = scroll_elt.firstChild;
	var elt2 = elt1.nextSibling;
	var pos = elt1.offsetTop;
	var height = elt1.offsetHeight;
	elt1.style.top = (pos-1)+"px";
	if (-pos > height) elt1.style.top = (pos-1+2*height)+"px";
	if (pos > 0) elt2.style.top = (pos-1-height)+"px";
	else elt2.style.top = (pos-1+height)+"px";
}
function scrollSetup(id,vertical) {
	scroll_elt = document.getElementById(id);
	var elt1 = scroll_elt.firstChild;
	var elt2 = elt1.nextSibling;
	var pos = elt1.offsetLeft;
	elt1.style.display = "block";
	elt2.style.display = "block";
	elt1.style.position = "absolute";
	elt2.style.position = "absolute";
	if (vertical) {
		var height = elt1.offsetHeight;
		elt1.style.top = (pos)+"px";
		elt2.style.top = (pos+height)+"px";
		setInterval(scrollTimerV,40);
	} else {
		var width = elt1.offsetWidth;
		elt1.style.left = (pos)+"px";
		elt2.style.left = (pos+width)+"px";
		setInterval(scrollTimer,40);
	}
}
