var _slideshowcount=0

var id;
var NextPos; // Need to fix this
var PrevPos; // Need to fix this
var slidedegree=10; // Slide degree (> is faster)
var slidedelay=30; // Delay between slide animation (< is faster)
var activecanvasindex=0; // Current "active" canvas- Two canvas DIVs in total
var curimageindex=0;
var zindex=100;
var imgArray=new Array();

var slideshowref;
var canvases=[];

var nextCanvas;
var prevCanvas;
var activeCanvas;

var animatetimer;
var direction;

var FinishedScroll = false;

var curImagepos = 0;
var curNextImagepos = 0;
var curPrevImagepos = 0;

function dropSlideShow(imgarray, w, h) {
	id="_dropslide"+(++_slideshowcount); //Generate unique ID for this slideshow instance (automated)
	NextPos=parseInt(h)*(-1); // Need to fix this
	PrevPos=parseInt(h); // Need to fix this
	CreateDiv(parseInt(w), parseInt(h));
	imgArray = imgarray;
	var preloadimages=[];
	for (var i=0; i<imgArray.length; i++){
		preloadimages[i]=new Image();
		preloadimages[i].src=imgArray[i];
	}

	init();
}
function CreateDiv(w,h) {
 	document.write('<div id="'+id+'" style="position:relative; width:'+w+'px; height:'+h+'px; overflow:hidden;">');
	document.write('<div style="position:absolute; width:'+w+'px; height:'+h+'px; top:0;text-align:center;"></div>');
	document.write('<div style="position:absolute; width:'+w+'px; height:'+h+'px; top:-'+h+'px;text-align:center;"></div>');
	document.write('<div style="position:absolute; width:'+w+'px; height:'+h+'px; top:'+h+'px;text-align:center;"></div>');
	document.write('</div>');
	slideshowref=document.getElementById(id);
	canvases[0]=slideshowref.childNodes[0];
	canvases[1]=slideshowref.childNodes[1];
	canvases[2]=slideshowref.childNodes[2];
}
function init() {
	SetCanvas(canvases[activecanvasindex], 0);
	SetupSlide();
}

function SetCanvas(canvas, imageindex){
	var imageHTML='<img src="'+imgArray[imageindex]+'" style="border: 0" />';
	canvas.innerHTML=imageHTML;
}

function SetupSlide(){
	if (curimageindex>imgArray.length-1) {
		curimageindex = 0;
	}
	if (curimageindex<0) {
		curimageindex = imgArray.length - 1;
	}
	
	if (curimageindex > imgArray.length) {
		curimageindex = 0;
	}
	else if (curimageindex < 0) {
		curimageindex = imgArray.length;	
	}
			
	if (activecanvasindex == 0) {
		nextCanvasIndex = 1;
		prevCanvasIndex = 2;
	}
	else if (activecanvasindex == 1) {
		nextCanvasIndex = 2;
		prevCanvasIndex = 0;
	}
	else if (activecanvasindex == 2) {
		nextCanvasIndex = 0;
		prevCanvasIndex = 1;
	}

	activeCanvas=canvases[activecanvasindex];
	nextCanvas=canvases[nextCanvasIndex];//
	prevCanvas=canvases[prevCanvasIndex];//
	
	activeCanvas.style.top="0";
	nextCanvas.style.top=NextPos+"px";
	prevCanvas.style.top=PrevPos+"px";

	curImagepos = 0;
	curNextImagepos = NextPos;
	curPrevImagepos = PrevPos;

	activeCanvas.style.zIndex=(++zindex);
	nextCanvas.style.zIndex=(++zindex);
	prevCanvas.style.zIndex=(++zindex);
	
	ActiveImageIndex = curimageindex;
	NextImageIndex = curimageindex + 1;
	PrevImageIndex = curimageindex - 1;
	
	if (NextImageIndex>imgArray.length-1) {
		NextImageIndex = 0;
	}
	if (NextImageIndex<0) {
		NextImageIndex = imgArray.length - 1;
	}
	if (PrevImageIndex>imgArray.length-1) {
		PrevImageIndex = 0;
	}
	if (PrevImageIndex<0) {
		PrevImageIndex = imgArray.length - 1;
	}
	
	SetCanvas(nextCanvas, NextImageIndex);
	SetCanvas(prevCanvas, PrevImageIndex);
}

function Animate(Direction) {
	if (Direction == "Up") {
		if (curNextImagepos < 0) {
			curImagepos=curImagepos+slidedegree; //
			activeCanvas.style.top=curImagepos+"px"; //
			
			curNextImagepos=curNextImagepos+slidedegree;
			nextCanvas.style.top=curNextImagepos+"px";
			
			curPrevImagepos=curPrevImagepos+slidedegree;
			prevCanvas.style.top=curPrevImagepos+"px";
		}
		else {
			FinishedScroll = true;
			nextCanvas.style.top=0;
			if (activecanvasindex == 2) {
				activecanvasindex = 0;	
			}
			else {
				activecanvasindex = activecanvasindex + 1;
			}
		}
	}
	else {
		if (curPrevImagepos > 0) {
			curImagepos=curImagepos-slidedegree; //
			activeCanvas.style.top=curImagepos+"px"; //
			
			curNextImagepos=curNextImagepos-slidedegree;
			nextCanvas.style.top=curNextImagepos+"px";
			
			curPrevImagepos=curPrevImagepos-slidedegree;
			prevCanvas.style.top=curPrevImagepos+"px";
		}
		else {
			FinishedScroll = true;
			prevCanvas.style.top=0;
			if (activecanvasindex == 0) {
				activecanvasindex = 2;	
			}
			else {
				activecanvasindex = activecanvasindex - 1;
			}
		}
	}
	
	if (FinishedScroll) {
		clearInterval(animatetimer);
		animatetimer = "";
		FinishedScroll = false;
		if (Direction == "Up") {
			curimageindex = curimageindex + 1;
		}
		else {
			curimageindex = curimageindex - 1;
		}
		SetupSlide();
	}
}

function Slider(slide) {
	if (animatetimer) return false;
	direction = slide;
	animatetimer=setInterval("Animate(direction)", slidedelay);
}