//************************************************
//*
//*  Slide Banner Functions
//*
//*
//*  These set of functions rearrange the images within the ImagesContainer object
//*  When they are rearranged the array (imgs) is also arranged as well so the indexing
//*  of the array is done by taking the image on the bottom of the array and moving it
//*  to the top (which will make it the next image to be displayed).
//*
//*
//*
//*
//************************************************

window.addEventListener?window.addEventListener("load",InitSlideBanner,false):window.attachEvent("onload",InitSlideBanner);
var d=document, imgs = new Array(), CurrentIndex=0, pause=false;

Delay=5000;
LeftIncrement=50;
var IncrementDelay = 20;

var manual = 0;
var ImageCount;
var LeftAmount = 0;

var Acceleration = 5;
var MaxSpeed = 300;
var Speed = 0;
var DistanceMovedWhileAccelerating = 0;

/*var CurrentImage;
var NextImage;*/

function InitSlideBanner() 
{
    if(!d.getElementById || !d.createElement)return;
	
	css = d.createElement("link");
	
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);
	
	//Get all the images and dots that are rotating
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	
	ImageCount = imgs.length;
	imgs[0].style.display = "block";
	
	//alert(ImageCount);
	
    //Dont start rotating the images if there is only one image
    if(ImageCount == 1)
        return;
    
	//Position all of the images so they rotate in the same order as the database
	for(i = 0; i < ImageCount; i++)
		imgs[0].parentNode.insertBefore(imgs[i], imgs[0]);

	RepositionImages();
	RepositionImages();
	
	pause = setTimeout(SlideNewImage,Delay);
	
}


function move(which) {
	
	nIndex = parseInt(which);
	
	manual = 1;
	clearTimeout(pause);
	
	IndexToScrollTo = nIndex;
	SlideNewImage();
}

function SlideNewImage()
{
    
	if(Speed < MaxSpeed)
	{
		Speed = Speed + Acceleration;
		DistanceMovedWhileAccelerating += Speed;
	}
	
	if(LeftAmount - 1000 <= DistanceMovedWhileAccelerating)
	{
		
	}
	
    //This section does the moving of both images
	imgs[0].style.left = LeftAmount + Speed + "px"; 
	
	LeftAmount += Speed;
	imgs[1].style.left = imgs[0].width + LeftAmount + "px";
	
	
	
	//If the image has moved far enough left, stop moving it, otherwise keep going
	if(LeftAmount  < 0)
	{
		pause = setTimeout(SlideNewImage,IncrementDelay);
		return;
	}
	else
	{
		
		//This is an internal counter to keep track of where we are in the list of images
		if(CurrentIndex == ImageCount - 1)
			CurrentIndex = 0;
		else
			CurrentIndex += 1;
		
		//Move the next image to be displayed to the top of the image array and
		//set the positions
		RepositionImages(ImageCount - 1);
		
		//If the user clicked on the dots, then keep calling this function
		//until we have scrolled to the right image
		if(manual == 1)
		{
			
			if(IndexToScrollTo != CurrentIndex)
			{
				pause = setTimeout(SlideNewImage,1);
				return;
			}
			else
			{
				manual = 0;
				Speed = 0;
			}
		}
		else
		{
			//Reset the acceleration
			Speed = 0;
			pause = setTimeout(SlideNewImage,Delay);
		}
		
	}
		
	
}

function DoIt()
{
	SlideNewImage();
}


function RepositionImages()
{
		imgs[0].parentNode.insertBefore(imgs[ImageCount - 1], imgs[0]);
		
		var Img1Width = imgs[1].width;
		LeftAmount = Img1Width * -1;
		
		imgs[0].style.left = LeftAmount + "px";
		imgs[0].style.top = "0px";
		imgs[1].style.left = imgs[0].width + LeftAmount + "px";
		imgs[1].style.top = "-560px";
		
		
}

function GotoLink(Location) {

	//Open the location in a new window, not the current one.
	window.open(Location);
}


