// JavaScript Document
var total;	/*Total = number of pictures in the gallery*/
var moveCounter=0;
var imagesArray=Array();
var moveAmount=0;
var movePosition=0;
var moveCount=0;
var maximum;
var slideShow;
var theGallery;
var slideShowCounter;
var theImageNum;
var slideShowImages;
var theActiveFile;
var theActiveThumb;
var theStyleSheet=0;
var theCurrentId=0;

function pleaseWait(){
	if(getURLParam("gallery")!=null){
		document.getElementById("galleryimagecontainer").style.backgroundImage="url(images/framework/pleasewait.gif)";
	}
}

document.onmouseup=function(){
	moveAmount=0;
}

/*This function sets the position of all the images in the gallery, 90px apart*/
function disperseimages(pictures){
	total=pictures;
	for(i=0; i<pictures; i++){
		theImage="image"+i;
		position=(i*90);
		//document.getElementById(theImage).style.left=position+"px";
		imageStuff=Array(theImage, position);
		imagesArray.push(imageStuff);
	}
	if(string=getURLParam("error")){
		alert(stripUnderscores(string));
	}
	mover();
}

function stripUnderscores(string){
	newString='';
	for(i=0; i<=string.length; i++){
		if(string.charAt(i)=="_"){
			newString+=" ";
		} else {
			newString+=string.charAt(i);
		}
	}
	return newString;
}

function move(direction){
	if(direction=="back"){
		moveAmount=-10;
	} else {
		moveAmount=10;
	}
}

function mover(){
	if(movePosition>0){
		movePosition=0;
		moveAmount=0;
	}
	endItem="image"+(total-1);
if((findPosX(document.getElementById(endItem))+90)<(findPosX(document.getElementById("galleryfilmstripforward")))){
	} else {
		/*This if statement says if we are at the end of the gallery, don't move any further. */
		/*If the position of the galleryfilmstripforward div, minus the position of the last item, is 
		greater than zero, we're at the end so move no further*/
		if(findPosX(document.getElementById("galleryfilmstripforward"))-(findPosX(document.getElementById(endItem)))>0){
			/*alert(findPosX(document.getElementById("galleryfilmstripforward")));*/
			/*alert(findPosX(document.getElementById(endItem)));*/
			movePosition-=moveAmount;
			moveAmount=0;
		}
		movePosition+=moveAmount;
		for(i=0; i<=imagesArray.length-1; i++){
			position=movePosition+(i*90);
			document.getElementById(imagesArray[i][0]).style.left=position+"px";
		}
	}
																						 
	if(slideShow==1){
		if(slideShowCounter==0){
			theImageNum+=1;
			if(theImageNum==slideShowImages.length){
				theImageNum=0;
			}
			theCurrentId=theImageNum;
			document.getElementById("galleryimagecontainer").style.backgroundImage="url("+slideShowImages[theImageNum]+")";
			for(i=0; i<total; i++){
				thisDiv="image"+i;
				document.getElementById(thisDiv).style.borderTop="solid 0px white";
			}
			clickedOn="image"+theImageNum;
			document.getElementById(clickedOn).style.borderTop="solid 0px white";
			
		}
	}
	slideShowCounter+=1;
	if(slideShowCounter==225){
		slideShowCounter=0;
	}

	setTimeout("mover();", 15);
}

function stopScroll(){
	moveAmount=0;
}

function findPosX(obj)
{

	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function change(file, thumb, clickedOn){
	for(i=0; i<total; i++){
		thisDiv="image"+i;
		document.getElementById(thisDiv).style.borderTop="solid 0px white";
	}
	document.getElementById(clickedOn).style.borderTop="solid 0px white";
	document.getElementById("galleryimagecontainer").style.backgroundImage="url(galleryimages/userimages/"+file+"?"+(Math.random()*99999999999999)+")";
	document.getElementById("playbutton").src="galleryimages/framework/play.gif";
	document.getElementById("stopbutton").src="galleryimages/framework/nstop.gif";
	slideShow=0;
	theActiveFile=file;
	theActiveThumb=thumb;
	theCurrentId=(clickedOn.substr(5, clickedOn.length-5));
}


function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}


function startSlideShow(images){
	slideShowImages=images;
	slideShow=1;
	slideShowCounter=0;
	theCurrentId-=1;
	theImageNum=theCurrentId;
	document.getElementById("playbutton").src="galleryimages/framework/nplay.gif";
	document.getElementById("stopbutton").src="galleryimages/framework/stop.gif";
}
function stopSlideShow(){
	slideShow=0;
	document.getElementById("playbutton").src="galleryimages/framework/play.gif";
	document.getElementById("stopbutton").src="galleryimages/framework/nstop.gif";
}
function newGallery(){
	if($name=prompt("Type the name of your new gallery:", "My Gallery")){
		document.getElementById("newgalleryname").value=$name;
		document.getElementById("action").value="creategallery";
		document.getElementById("newgalleryform").submit();
	}
}

function deleteGallery(theGallery){
	if(confirm("Are you sure you want to delete this gallery and all the images in it?\n\nYou will NOT be able to undo this funtion")){
		document.getElementById("deletegalleryname").value=theGallery;
		document.getElementById("action").value="deletegallery";
		document.getElementById("newgalleryform").submit();
	}
}

function showUploadImages(){
	document.getElementById("userfilediv").style.display='block';
}

function switchAll(what){
	if(what==1){
		theValue="all";
	} else {
		theValue="one";
	}
	document.getElementById("uploadwhat").value=theValue;
}

function uploadImages(theUploadPath){
	document.getElementById("action").value="uploadimages";
	document.getElementById("uploadpath").value=theUploadPath;
	document.getElementById("newgalleryform").submit();
}

function changeName(theGallery){
	if($name=prompt("Type the new name for your gallery:", theGallery)){
		document.getElementById("action").value="changename";
		document.getElementById("deletegalleryname").value=theGallery;
		document.getElementById("newgalleryname").value=$name;
		document.getElementById("newgalleryform").submit();
	}
}

function deleteImage(){
	if(theActiveFile){
		if(confirm("Are you sure you want to delete this image?\n\nYou will NOT be able to undo this action")){
			document.getElementById("action").value="deleteimage";
			document.getElementById("deletegalleryname").value=theActiveFile;
			document.getElementById("newgalleryname").value=theActiveThumb;
			document.getElementById("newgalleryform").submit();
		}
	} else {
		alert("You MUST select an image to delete");
	}
}

function changestylesheet(){
	theStyleSheet+=1;
	if(theStyleSheet==3){
		theStyleSheet=0;
	}
	
	switch(theStyleSheet){
		case 0: document.styleSheets[0].href="gallery.css";
				document.getElementById("galleryleftbutton").src="galleryimages/framework/left.gif";
				document.getElementById("galleryrightbutton").src="galleryimages/framework/right.gif";
				break;
		case 1: document.styleSheets[0].href="gallery2.css";
				document.getElementById("galleryleftbutton").src="gallery_img/back_1.gif";
				document.getElementById("galleryrightbutton").src="gallery_img/forward_1.gif";
				break;
		case 2: document.styleSheets[0].href="gallery3.css";
				document.getElementById("galleryleftbutton").src="gallery3/back.gif";
				document.getElementById("galleryrightbutton").src="gallery3/forward.gif";
				break;
		default: document.styleSheets[0].href="gallery.css"; break;
	}
}

function dowait($location){
	document.getElementById('body').innerHTML+="<div id=pleasewait style='position:absolute; left:45%; top:60%; background-color:#FFF; border:solid 1px black; width:100px; font-size:10px; text-align:center; padding:10px;'><b>Please Wait...</b><br>Building Gallery</div>";
	window.location=$location;
}
