// required functions: setOpacity, newelement


var picturegalleries = new Array();
var loaded_pictures = new Array();

function init_picturegal() {
	var uls = document.getElementsByTagName('UL');
	for (i=0; i<uls.length; i++) {
		if (uls[i].className == 'picturegal') {
			var picturegal = uls[i];
			var anchors = picturegal.getElementsByTagName('A');
			for (j=0; j<anchors.length; j++) {
				anchors[j].onclick = function() {
					display_galpic (this.href, this.title);
					return false;
				}

				picturegalleries[j] = anchors[j].href + ";;" + anchors[j].title;
			}
		}
	}
}

function display_galpic (url, title) {

	if (!document.getElementById('picturegal_box')) {newelement('picturegal_box');}
	if (!document.getElementById('picturegal_background')) {newelement('picturegal_background');}

	var picturebox = document.getElementById('picturegal_box');
	picturebox.style.display = 'none';

	var picturebackground = document.getElementById('picturegal_background');
	var pic_values;

	for (i=0; i<picturegalleries.length; i++) {
		if (picturegalleries[i].indexOf(url) > -1) {
			pic_values = picturegalleries[i];
		}
	}

	var picture_url = pic_values.split(';;');
	var picture_height = parseInt(picture_url[1]) + 20;
	var picture_width = parseInt(picture_url[2]) + 10;
	var picture_title = picture_url[3];
	var picture_url = picture_url[0];

	picturebox.innerHTML = "<div id='picturegal_content' style='margin-top:-" + Math.round(picture_height/2) + "px; margin-left:-" + Math.round(picture_width/2) + "px'><img src='" + picture_url + "' alt='' title='' /><br /><p>" + picture_title + "</p><a id='picturegal_close' href='#' onclick='hide_galpic (this)'>x</a><a id='picturegal_next' href='#' onclick='getNextPic (this, 0)'>></a><a id='picturegal_prev' href='#' onclick='getNextPic (this, 1)'><</a></div>";

	setOpacity (picturebackground, 0);
	picturebackground.max_opacity = 75;
	picturebackground.opacity = 0;
	picturebackground.interval = window.setInterval ("fadeIn(document.getElementById('picturegal_background'))", 75);
	picturebackground.style.display = 'block';

	picturebox.interval = window.setInterval ("showGalpic(document.getElementById('picturegal_box'))", 50);
}

function getNextPic (element, direction) {
	var picturegal_content = element.parentNode;
	var actual_image = picturegal_content.getElementsByTagName('IMG')[0];
	var actual_url = actual_image.src.split(";;")[0];
	var next_image;

	for (i=0; i<picturegalleries.length; i++) {
		if (picturegalleries[i].indexOf(actual_url) > -1) {
			if (direction == 1) {
				next_image = i-1;
				if (next_image < 0) {
					next_image = picturegalleries.length-1;
				}
			} else {
				next_image = i+1;
				if (next_image >= picturegalleries.length) {
					next_image = 0;
				}
			}
		}
	}


	var picture_url = picturegalleries[next_image].split(';;');
	var picture_height = parseInt(picture_url[1]) + 20;
	var picture_width = parseInt(picture_url[2]) + 10;
	var picture_title = picture_url[3];
	var picture_url = picture_url[0];

	var nextpic_loader = new Image();
	nextpic_loader.src = picture_url;

	if (nextpic_loader.complete == true) {       // IE6 und 7 fuehren onload nur aus, wenn's bild neu geladen wird
		actual_image.src = picture_url;
		picturegal_content.style.marginTop = "-"+picture_height/2+"px";
		picturegal_content.style.marginLeft = "-"+picture_width/2+"px";
		picturegal_content.getElementsByTagName('P')[0].innerHTML = picture_title;
	} else {
		nextpic_loader.onload = function () {
			actual_image.src = picture_url;
			picturegal_content.style.marginTop = "-"+picture_height/2+"px";
			picturegal_content.style.marginLeft = "-"+picture_width/2+"px";
			picturegal_content.getElementsByTagName('P')[0].innerHTML = picture_title;
		}
	}
}

function hide_galpic(element) {
	element.parentNode.parentNode.style.display = 'none';
	document.getElementById('picturegal_background').interval = window.setInterval("fadeOut(document.getElementById('picturegal_background'))", 75);
}

function fadeIn (element) {

	element.opacity += 10;

	if (element.opacity >= element.max_opacity) {
		element.opacity = element.max_opacity;
		window.clearInterval(element.interval);
	}
	setOpacity (element, element.opacity);
}

function fadeOut (element) {

	element.opacity -= 10;

	if (element.opacity <= 0) {
		element.opacity = 0;
		element.style.display = 'none';
		window.clearInterval(element.interval);
	}
	setOpacity (element, element.opacity);
}

function showGalpic (element) {
	var picturebackground = document.getElementById('picturegal_background');

	if (picturebackground.opacity == picturebackground.max_opacity) {
		element.style.display = 'block';
		window.clearInterval(element.interval);
	}
}
