var preloadImages = [];
MM_preloadImages = function(preloadImages) {
		preloaded = new Array();

		if(typeof(preloadImages) == 'object' && preloadImages.length > 0) {
				for(index in preloadImages) {
						preloaded[index] = new Image;
						preloaded[index].src = preloadImages[index];
				}
		}
}


function jsgalerie(element,fotos,imgpath,imgsuffix) {
// variablen deklaration
	var win = window,
		doc = document,
		caller = this,
		fotoakt = 0,

		// fading
		fader,
		fadeopacity = 1,

		// slideshow
		sliderInterval = null,


// function deklaration
		// slideshow

		// bildwecheln/faden
		fadeout,
		fadein,
		complete;


	if (typeof(element) != "object") {
		element = doc.getElementById(element);
	}

	this.setFotoakt = function(bild) {
		fotoakt = bild;
	};

	fadeout = function($src)
	{
		fadeopacity -= 0.1;
		if (fadeopacity < 0) {
			fadeopacity = 0;
		}
		fadeopacity = Math.round(fadeopacity * 10) / 10;
		element.style.filter = "alpha(opacity=" + parseInt(fadeopacity * 100) + ")";
		element.style.opacity = fadeopacity;
		if (fadeopacity > 0)
		{
				fader = setTimeout(function() {fadeout($src);},25);
		}
		else
		{
			oldImg = element;
			newImg = new Image();
			newImg.src = $src;
			newImg.id = oldImg.id;
			newImg.border = oldImg.border;
			//newImg.maxHeight = oldImg.maxHeight;
			newImg.className = oldImg.className;
			newImg.style.filter = "alpha(opacity=0)";
			newImg.style.opacity = "0";

			//alert(newImg.height + " " + oldImg.height);


			oldImg.parentNode.appendChild(newImg);
			oldImg.parentNode.removeChild(oldImg);
			element = newImg;
			fader = setTimeout(function() {complete(fadein);},5);
		}
	};

	fadein = function (timeout,callback)
	{
		if (typeof(timeout) == "undefined")
		{
				timeout = 25;
		}
		fadeopacity += 0.1;
		if (fadeopacity > 1) {
			fadeopacity = 1;
		}
		fadeopacity = Math.round(fadeopacity * 10) / 10;
		
		element.style.filter = "alpha(opacity=" + (fadeopacity * 100) + ")";
		element.style.opacity = fadeopacity;
		//alert(fadeopacity + " " + (fadeopacity < 1));
		if (fadeopacity < 1)
		{
				fader = setTimeout(function() {fadein(timeout);},timeout);
		} else if(typeof(callback) == "function") {
			callback();
		}

	};


	complete = function (callback)
	{
			if (element.complete == false)
			{
					fader = setTimeout(function() {complete(callback);},5);
			}
			else
			{
					fader = setTimeout(function() {callback();},25);
			}
	};


	this.swap_image = function ($src) {
			if (typeof(slider) != "undefined")
					this.stop_slide();

			fotoakt = $src;
			if (doc.getElementById("aktuellesbild"))
				doc.getElementById("aktuellesbild").innerHTML = parseInt(fotoakt) + 1;
			if (typeof(imgsuffix) == "undefined")
			{
					imgsuffix = "";
			}
			if(fotos) {
					$src = imgpath + fotos[$src] + imgsuffix;
			}
			preloadImages.push($src);

			clearTimeout(fader);
			fader = setTimeout(function() {fadeout($src);},25);

	};

	this.swap = function ($src) {
		caller.swap_image($src);
	};
	this.change = function ($src) {
		caller.swap_image($src);
	};
	this.changeImage = function($src) {
		caller.swap_image($src);
	};

	this.swap_big = function ($richtung) {
			if (typeof($richtung) == "undefined")
			{
					$richtung=false;
			}
			if ($richtung == false)
			{
					fotoakt++;
			}
			else
			{
					fotoakt--;
			}
			if (fotoakt > fotos.length - 1){
					fotoakt = 0;
			}
			if (fotoakt < 0){
					fotoakt = fotos.length -1;
			}
			if (typeof(imgsuffix) == "undefined")
			{
					imgsuffix = "";
			}
			if (doc.getElementById("aktuellesbild"))
			{
				doc.getElementById("aktuellesbild").innerHTML = parseInt(fotoakt) +1;
			}
			$src = imgpath + fotos[fotoakt] + imgsuffix;
			preloadImages.push($src);
			clearTimeout(fader);
			fader = setTimeout(function() {fadeout($src)},25);

	};

	this.next = function () {
		caller.swap_big();
	};

	this.prev = function () {
		caller.swap_big(1);
	};


	this.start_slide = function(interval) {
		if (typeof(interval) == "undefined") {
			interval = 5000;
		}


		var slideButton = doc.getElementById("bigimage_slideshow"),
			slideOnOff = doc.getElementById("onoff");


		if (sliderInterval != null) {
				caller.stop_slide();
		}
		else {
			if (typeof(slideButton) != "undefined" && slideButton != null && element.id == "bigimage") {
				slideButton.getElementsByTagName('img')[0].src = "images/icons/icon_stop.png";
			}
			if (typeof(slideOnOff) != "undefined" && slideOnOff != null && element.id == "bigimage") {
				slideOnOff.innerHTML = 'ON';
			}
			sliderInterval = setInterval(function() {caller.swap_big();},interval);
		}
		if (fotos.length <= 1) {
			caller.stop_slide();
		}
	};

	this.stop_slide = function()
	{
		var slideButton = doc.getElementById("bigimage_slideshow"),
			slideOnOff = doc.getElementById("onoff");

		if (sliderInterval != null)	{
			if (typeof(slideButton) != "undefined" && slideButton != null && element.id == "bigimage") {
				slideButton.getElementsByTagName('img')[0].src = "images/icons/icon_play.png";
			}
			if (typeof(slideOnOff) != "undefined" && slideOnOff != null && element.id == "bigimage") {
				slideOnOff.innerHTML = 'OFF';
			}
			clearInterval(sliderInterval);
			sliderInterval = null;
		}
	};
}
