function sideReveal()
{
	this.buttons = new Array();
	this.classes = new Array();
	this.displayNumber;
	this.targetPos = 0;
	this.moveWidth;
	this.imageWidth;
	this.imageMargin;
	this.originalWidth = 0;
	this.thumbsDiv;
	this.gallery_loading;
	this.timedReveal;
	this.sideRevealActivate = sideRevealActivate;
}

function sideRevealActivate(myDiv, buttons, imageCount, myMargins, myID)
{
	getButtons(myDiv, myID);
	this.imageMargin = myMargins;
	this.displayNumber = imageCount;
	sliderDiv = document.getElementById(buttons);
	sliderDiv.innerHTML = "<a id='leftButton_"+myID+"' class='leftButton' onclick='moveLeft(this)'><span title='Move Left'><small>Move Left</small></span></a><a id='rightButton_"+myID+"' class='rightButton' onclick='moveRight(this)'><span title='Move Right'><small>Move Right</small></span></a>"+sliderDiv.innerHTML;
	var activeDiv = document.getElementById(myDiv);
	this.moveWidth = activeDiv.offsetWidth;
	activeDiv.innerHTML = "<div id='thumbs_"+myID+"' class='thumbs'><div id='thumbscontainer_"+myID+"' class='thumbscontainer'></div></div>";
	activeDiv.style.visibility = "visible";
	this.gallery_loading = setInterval("finishActivate('"+myID+"')", 10);
	movingImage = setInterval(moveImages, 30);
	document.write('<style type="text/css">#buttons { visibility:hidden; } #sliderButtons { display:block; }</style>');
}

function finishActivate(myID)
{
	if(document.getElementById("leftButton_"+myID))
	{
		clearInterval(slideReveals[myID].gallery_loading);
		slideReveals[myID].imageWidth = slideReveals[myID].moveWidth/slideReveals[myID].displayNumber;
		var myextralength = slideReveals[myID].buttons.length % slideReveals[myID].displayNumber;
		slideReveals[myID].originalWidth = (slideReveals[myID].imageWidth+slideReveals[myID].imageMargin) * (slideReveals[myID].buttons.length + myextralength);
		slideReveals[myID].thumbsDiv = document.getElementById("thumbscontainer_"+myID);
		slideReveals[myID].thumbsDiv.style.marginRight = -slideReveals[myID].originalWidth + "px";
		slideReveals[myID].thumbsDiv.style.marginLeft = "0px";
		thumbHeight = slideReveals[myID].thumbsDiv.offsetHeight;
		var thumbImages = "";
		for(i=0;i<slideReveals[myID].buttons.length;i++)
		{
			thumbImages += "<div class='"+slideReveals[myID].classes[i]+"' style='margin-right:"+slideReveals[myID].imageMargin+"'>"+slideReveals[myID].buttons[i]+"</div>"
		}
		slideReveals[myID].thumbsDiv.innerHTML += thumbImages;
		slideReveals[myID].timedReveal = setInterval("timermove('"+myID+"')", 15000);
	}
}

function getButtons(myDiv,myID)
{
	var galleryDiv = document.getElementById(myDiv);
	for(i=0;i<galleryDiv.childNodes.length;i++)
	{
		if(galleryDiv.childNodes[i].nodeName == "DIV")
		{
			slideReveals[myID].buttons.push(galleryDiv.childNodes[i].innerHTML);
			slideReveals[myID].classes.push(galleryDiv.childNodes[i].className);
		}
	}
}

function moveLeft(clickedButton)
{
	myID = clickedButton.id;
	myID = myID.substring(myID.lastIndexOf('_')+1,myID.length);
	clearInterval(slideReveals[myID].timedReveal);
	currentMargin = slideReveals[myID].thumbsDiv.style.marginLeft;
	currentMargin = parseInt(currentMargin.substring(0,(currentMargin.length-2)));
	var nextPos = slideReveals[myID].targetPos + slideReveals[myID].moveWidth;
	if(nextPos < 0.5)
	{
		slideReveals[myID].targetPos = nextPos;
	}
	else
	{
		slideReveals[myID].targetPos = 0 - slideReveals[myID].originalWidth + slideReveals[myID].moveWidth;
	}
}

function moveRight(clickedButton)
{
	myID = clickedButton.id;
	myID = myID.substring(myID.lastIndexOf('_')+1,myID.length);
	clearInterval(slideReveals[myID].timedReveal);
	currentMargin = slideReveals[myID].thumbsDiv.style.marginLeft;
	currentMargin = parseInt(currentMargin.substring(0,(currentMargin.length-2)));
	var nextPos = slideReveals[myID].targetPos - slideReveals[myID].moveWidth;
	if(nextPos > 0 - slideReveals[myID].originalWidth)
	{
		slideReveals[myID].targetPos = nextPos;
	}
	else
	{
		slideReveals[myID].targetPos = 0;
	}
}

function timermove(myID)
{
	currentMargin = slideReveals[myID].thumbsDiv.style.marginLeft;
	currentMargin = parseInt(currentMargin.substring(0,(currentMargin.length-2)));
	var nextPos = slideReveals[myID].targetPos - slideReveals[myID].moveWidth;
	if(nextPos > 0 - slideReveals[myID].originalWidth)
	{
		slideReveals[myID].targetPos = nextPos;
	}
	else
	{
		slideReveals[myID].targetPos = 0;
	}
}

function moveImages()
{
	for(var a=0;a<slideReveals.length;a++)
	{
		if(document.getElementById("thumbscontainer_"+a) && document.getElementById("rightButton_"+a) && document.getElementById("leftButton_"+a))
		{
			var currentMargin = slideReveals[a].thumbsDiv.style.marginLeft;
			currentMargin = parseInt(currentMargin.substring(0,(currentMargin.length-2)));
			if(isNaN(currentMargin))
				currentMargin = 0;
			var changeMargin = 0;
			if(currentMargin != slideReveals[a].targetPos)
			{
				if((currentMargin - slideReveals[a].targetPos) < -2 || (currentMargin - slideReveals[a].targetPos) > 2)
				{
					changeMargin = (currentMargin - (currentMargin - slideReveals[a].targetPos)/4) + "px";
					slideReveals[a].thumbsDiv.style.marginLeft = changeMargin;
				}
				else
				{
					slideReveals[a].thumbsDiv.style.marginLeft = slideReveals[a].targetPos + "px";
				}
			}
		}
	}
}

var movingImage;
var slideReveals = new Array();

function addSlideReveal(myDiv, buttons, imageCount, myMargins)
{
	var myslider = new sideReveal();
	slideReveals.push(myslider);
	slideReveals[slideReveals.length-1].sideRevealActivate(myDiv, buttons, imageCount, myMargins, slideReveals.length-1);
}