// Galerie-Function
function Slide(img_id, slideshowname, maxWidth, maxHeight)
{
	this.ms_filter  = 'progid:DXImageTransform.Microsoft.Fade(duration=2)';
	this.imgarray   = new Array();
	this.imgpaths   = new Array();
	this.img	    = document.getElementById(img_id);
	this.intervall  = 3000;
	this.ar_pointer = 0;
	this.param      = "";
	this.name 		= slideshowname;
	this.running    = false;
	this.runforward = true;
	this.max_width  = maxWidth;	
	this.max_height = maxHeight;	
	var obj         = this;
	this.use_filter	= (typeof this.img.filters != 'undefined' && typeof this.img.filters[0] != 'undefined') ? true : false;
	this.use_moz	= (this.img.style.MozOpacity != 'undefined') ? true : false;

	this.StartStop = function()
	{
		if (this.running)
		{
			window.clearInterval(this.param);
			if (document.getElementById("StartStop_Slide") != undefined) document.getElementById("StartStop_Slide").value = "Start";
			this.running    = false;
		}
		else
		{ 
			this.param = setInterval(this.name+".changePic()",this.intervall);
			if (document.getElementById("StartStop_Slide") != undefined) document.getElementById("StartStop_Slide").value = "Stop";
			this.running    = true;
		}
	}

	this.SetIntervall = function(value)
	{
		this.intervall = value;
		
	}

	this.ChangeIntervall = function()
	{
		this.intervall = 1000 * document.getElementById("Intervall_Slide").value;
		if (this.running)
		{
			window.clearInterval(this.param);
			this.param = setInterval(this.name+".changePic()",this.intervall);
		}
		
	}

	this.runBackwards = function()
	{
		this.runforward = false;
	}

	this.runForwards = function()
	{
		this.runforward = true;
	}

	
	//Lade alle Pfade in ein array 	
	this.addPics = function(picpath)
	{
		this.ar_pointer++;
		this.imgpaths[this.ar_pointer] = picpath;			
	}
	
	/* warten bis das Bild geladen ist*/
	this.onComplete = function()
	{
		if(this.imgarray[this.current].complete == true)
		{
			this.show();
		}
		else
		{
			setTimeout(this.name+".onComplete()",500);
		}
	}

	this.init	= function()	{		this.max_pic = this.ar_pointer;

		//Lade erstes Bild vor		this.current         = 1;
		this.imgarray[this.current]     = new Image;
		this.imgarray[this.current].src = this.imgpaths[this.current];
		//Warte bis das Bild geladen ist
		setTimeout(this.name+".onComplete()",500);
	}
	
	/* Skaliert das Bild und zeigt es an */
	this.show	= function()	{		act_height = this.imgarray[this.current].height;
		act_width  = this.imgarray[this.current].width;
		if (act_width < act_height)
		{
			this.img.height  = this.max_height;
			this.img.width   = act_width / (act_height/this.max_height);
			if (this.img.width > this.max_width)
			{
				this.img.width  = this.max_width;
				this.img.height = act_height / (act_width/this.max_width);
			}
		}
		else
		{
			this.img.width  = this.max_width;
			this.img.height = act_height / (act_width/this.max_width);
			if (this.img.height > this.max_height)
			{
				this.img.height  = this.max_height;
				this.img.width   = act_width / (act_height/this.max_height);
			}
		}
		
		if(this.use_moz) 		{			this.opacity = 0.04;			this.img.style.MozOpacity = this.opacity;			for(i=0;i<1000;i++);			this.fader = self.setInterval(moz_fadein,50);		}		if(this.use_filter)		{			this.img.filters[0].Apply();			this.img.src = this.imgarray[this.current].src;			this.img.filters[0].Play();		}		if(!this.use_filter) this.img.src = this.imgarray[this.current].src;

	}

	//lädt das nächste Bild 
	this.changePic = function()
	{
		if (this.runforward)
		{
			this.current++;
			if(this.current > this.max_pic) this.current = 1;
		}
		else
		{
			this.current--;
			if(this.current < 1) this.current = this.max_pic;
		}
		// Wenn es dieses imagearray nicht gibt, dann befülle es
		if (this.imgarray[this.current] == undefined)
		{
			this.imgarray[this.current]     = new Image;
			this.imgarray[this.current].src = this.imgpaths[this.current];
		}
	
		//warte bis das Bild geladen ist
		setTimeout(this.name+".onComplete()",500);
	}
	var moz_fadein = function()	{		if (obj.img.style.MozOpacity < 0.99)		{			obj.opacity += .05;			obj.img.style.MozOpacity = obj.opacity;		}		else window.clearInterval(obj.fader);	}
	
}




