/****************************************************************
"bretteleben.de Simple Picture Slideshow" Plugin for Joomla 1.5 - Version 1.4.1
License: http://www.gnu.org/copyleft/gpl.html
Author: Andreas Berger - http://www.bretteleben.de
Copyright (c) 2009 Andreas Berger - andreas_berger@bretteleben.de
Project page and Demo at http://www.bretteleben.de
***Last update: 2009-07-20***
*****************************************************************/

function besps_slideshow(besps_slideid,besps_ftim,besps_stim,besps_steps,besps_startwhen,besps_emax,besps_caps,besps_preload){

//declarations
	var self = this;
	var slideid=besps_slideid;
	var ftim=besps_ftim;
	var stim=besps_stim;
	var steps=besps_steps;
	var startwhen=besps_startwhen;
	var emax=besps_emax;
	var preload=besps_preload;
	var stopit=1;
	var curimage=1;
	var lastimage=0;
	var noimage=-1;
	var u=0;
	var parr = new Array();
	var ptofade,pnext,factor,mytimeout;
	var caps=besps_caps;
	var run=0;

//--------------------------//daisychain onload-events
	this.daisychain = function(sl){
		if(window.onload){var ld=window.onload;window.onload=function(){ld();sl();};}
		else{window.onload=function(){sl();};}
	}

//--------------------------------//push images into array and get things going
	this.b_myfade = function(){
		var a,idakt,paktidakt,ie5exep;
		for(a=1;a<=emax;a++){
			idakt="img_"+slideid+"_"+a;
			paktidakt=document.getElementById(idakt);
			ie5exep=new Array(paktidakt);
			parr=parr.concat(ie5exep);
			if(preload&&a==emax){
				setTimeout(function(){self.b_preload();},10);
			}
		}
		if(startwhen){
			stopit=0;
 			mytimeout=setTimeout(function(){self.b_slide();},stim);
 		}
	}

//prepare current and next and trigger slide
	this.b_slide = function(){
		clearTimeout(mytimeout);
		u=0;
		ptofade=parr[lastimage];
		if(curimage<emax){
			pnext=parr[curimage];
		}else{
			pnext=parr[0];
		}
		pnext.style.zIndex=1;
		pnext.style.visibility="visible";
		pnext.style.filter="Alpha(Opacity=100)";
		pnext.style.MozOpacity=1;
		pnext.style.opacity=1;
		ptofade.style.zIndex=2;
		ptofade.style.visibility="visible";
		ptofade.style.filter="Alpha(Opacity=100)";
		ptofade.style.MozOpacity=1;
		ptofade.style.opacity=1;
		factor=100/steps;
		if(stopit=="0"){
			noimage=-1;
			this.b_slidenow();
		}
	}


	
//thumb click
	this.b_thumb = function(noimage){
		window.document.getElementById('startbtn').className='show';
		window.document.getElementById('stopbtn').className='hide';
		this.b_start();
		this.b_stop();
		pnext.style.visibility="hidden";
		stopit=1;
		clearTimeout(mytimeout);
		ptofade=parr[lastimage];
		pnext=parr[noimage];
		ptofade.style.visibility="hidden";
		ptofade.style.zIndex=1;
		pnext.style.visibility="visible";
		pnext.style.zIndex=2;
		lastimage=noimage;
		curimage=lastimage+1;
		//alert("paspaustas = "+noimage+"\nbuves = "+lastimage+"\nesamas = "+curimage);
		this.b_slide();
	}
	
//slide as said, then give back
	this.b_slidenow = function(){
		var check1,maxalpha,curralpha;
		check1=ptofade.style.MozOpacity;
		maxalpha=(100-factor*u)/100*105;
		if(check1<=maxalpha/100){u=u+1;}
		curralpha=100-factor*u;
		ptofade.style.filter="Alpha(Opacity="+curralpha+")";
		ptofade.style.MozOpacity=curralpha/100;
		ptofade.style.opacity=curralpha/100;
		
		if(u<steps){ //slide not finished
			mytimeout=setTimeout(function(){self.b_slidenow();},ftim);
		}
		else{ //slide finished


		//alert("paspaustas = "+noimage+"\nbuves = "+lastimage+"\nesamas = "+curimage);
			if(curimage<emax){
				ptofade.style.visibility="hidden";
				ptofade.style.zIndex=1;
				pnext.style.zIndex=2;
				lastimage=curimage;
				curimage=curimage+1;
				
				mytimeout=setTimeout(function(){self.b_slide();},stim);
			}
			else{

				ptofade.style.visibility="hidden";
				ptofade.style.zIndex=1;
				pnext.style.zIndex=2;
				curimage=1;
				lastimage=0;
				mytimeout=setTimeout(function(){self.b_slide();},stim);
			}
		}
	}

//----------------------------//manual start
	this.b_start= function(){
		if(stopit==1){
		
		noimage=-1;
 			stopit=0;
 			mytimeout=setTimeout(function(){self.b_slide();},stim);
 		}
	}

//-----------------------------//manual stop
	this.b_stop= function(){
		clearTimeout(mytimeout);
		noimage=-1;
		stopit=1;
		this.b_slide();
	}

//-----------------------------------//preload
	this.b_preload= function(noimage){
		var arrdelta,tempsrc,j;
		if(preload&&preload.length>=1){
			for (j=0;j<preload.length;j++){
				arrdelta=(emax-preload.length+j)*1;
				tempsrc=parr[arrdelta].getElementsByTagName("img")[0].src.toString();
				parr[arrdelta].getElementsByTagName("img")[0].src=tempsrc.replace(/plugins\/content\/plugin_besps\/besps.png$/g, preload[j]);
			}
		}
	}

//----------------------------------------//call autostart-function
	this.daisychain(this.b_myfade);
}
