goal = 0;
animation_timer=1;
currentThumb = 0;
showLoop = 0;
scroll_increment = 4;
scrolling = 0;
scrollUp_available = 0;
scrollDown_available = 1;
originalThumbHolderY = 0;
goScroll=0;		

///////////////////////////
//PRELOAD LOADING ANIMATION
///////////////////////////

loadingImage = new Image();
loadingImage.src = "/images/loading_animation.gif";


///////////////////////////
//SCROLL FUNCTIONS
///////////////////////////

bottomEdge = 0;
randomOffset = 0;

window.onload = function() {
	if (document.getElementById('thumbs')) randomOffset = document.getElementById('thumbs').offsetTop;
	if (document.getElementById('thumbs')) shineMe('thumb'+thumbOrder[0]);
}

function setBottom() {
	bottomEdge = 0 - (document.getElementById('thumbs').offsetHeight - document.getElementById('thumb_holder').offsetHeight - randomOffset);
}


function scrollUp() {
	if (scrolling) clearInterval(scrolling);
	setBottom();
	scrolling = setInterval("stepUp()",animation_timer);
	scrollUp_available = 1;
	scrollDown_available = 1;
}

function stepUp() {
	if ((document.getElementById('thumbs').offsetTop - randomOffset) < 0) {
		document.getElementById('thumbs').style.top = document.getElementById('thumbs').offsetTop - randomOffset + scroll_increment + "px";
	}
}

function scrollDown() {
	if (scrolling) clearInterval(scrolling);
	setBottom();
	scrolling = setInterval("stepDown()",animation_timer);
	scrollUp_available = 1;
	scrollDown_available = 1;
}

function stepDown() {
	if (document.getElementById('thumbs').offsetTop > bottomEdge) {
		document.getElementById('thumbs').style.top = document.getElementById('thumbs').offsetTop - randomOffset - scroll_increment + "px";
	}
}

function stopScroll() {
	clearInterval(scrolling);
	if (document.getElementById('thumbs').offsetTop + scroll_increment >= 0) scrollUp_available = 0;
	if (document.getElementById('thumbs').offsetTop - scroll_increment <= bottomEdge) scrollDown_available = 0;
}


///////////////////////////
//SET THUMBNAILS
///////////////////////////


function scrollThumbs() {
	if (scrolling) clearInterval(scrolling);
	setBottom();
	
	if (goal > 0) goal = 0;
	
	finalThumb = thumbOrder[thumbOrder.length-1];
	if (goal < bottomEdge) goal = bottomEdge;
	
	i = document.getElementById('thumbs').offsetTop;

	if ( (i<(goal+10)) && (i>(goal-10)) )  {
		clearInterval(goScroll);
		document.getElementById('thumbs').style.top = (goal - randomOffset)+"px";
	} else {
		i = i + Math.round((goal-i)/10);
		document.getElementById('thumbs').style.top = (i - randomOffset) +"px";
	}
	
	scrollUp_available = 1;
	scrollDown_available = 1;
	if (document.getElementById('thumbs').offsetTop + scroll_increment >= 0) scrollUp_available = 0;
	if (document.getElementById('thumbs').offsetTop - scroll_increment <= bottomEdge) scrollDown_available = 0;
}
  
  
  
  
///////////////////////////
//SHOW DISPLAY IMAGE
///////////////////////////


function showImage(newThumb) {
	
	currentThumb = newThumb;
		
	for(i=0;i<thumbOrder.length;i++) {
		document.getElementById('thumb'+thumbOrder[i]).style.opacity = 0.6;
		document.getElementById('thumb'+thumbOrder[i]).style.filter='alpha(opacity = 60)';
	}
	
	document.getElementById('thumb'+thumbOrder[currentThumb]).style.opacity = 1;
	document.getElementById('thumb'+thumbOrder[currentThumb]).style.filter='alpha(opacity =100)';
	
	document.display_image.src="/images/loading_animation.gif";
	document.display_image.width=25;
	document.display_image.height=17;
	document.getElementById('display_image').style.top = (document.getElementById('display_container').offsetHeight/2) - (document.getElementById('display_image').offsetHeight/2) +"px";
	
	if (showLoop) clearInterval(showLoop);
	preloader = new Image();
	preloader.src = "/uploads/"+thumbInfo[thumbOrder[currentThumb]]['filename']+".jpg";
	showLoop = setInterval("showMe("+currentThumb+")",200);
	
	goal = 0 - thumbInfo[thumbOrder[currentThumb]]['x'] + 150;
	goScroll = setInterval("scrollThumbs()",animation_timer);
	
	document.getElementById("next_button").blur();
	document.getElementById("previous_button").blur();
	
	
	if (currentThumb<thumbOrder.length-1) {
		document.getElementById("next").style.opacity = 1;
		document.getElementById("next").style.filter='alpha(opacity = 100)';
	} else {
		document.getElementById("next").style.opacity = 0;
		document.getElementById("next").style.filter='alpha(opacity = 0)';
	}
	
	
	if (currentThumb>0) {
		document.getElementById("previous").style.opacity = 1;
		document.getElementById("previous").style.filter='alpha(opacity = 100)';
	} else {
		document.getElementById("previous").style.opacity = 0;
		document.getElementById("previous").style.filter='alpha(opacity = 0)';
	}
	
	
}
	
 
function showMe(currentThumb) {
	if (preloader.complete) {
		clearInterval(showLoop);
		document.getElementById('display_image').style.top = "0px";
		document.display_image.src="/uploads/"+thumbInfo[thumbOrder[currentThumb]]['filename']+".jpg";
		document.display_image.width=thumbInfo[thumbOrder[currentThumb]]['width'];
		document.display_image.height=thumbInfo[thumbOrder[currentThumb]]['height'];
		document.getElementById('display_image').style.top = (document.getElementById('display_container').offsetHeight/2) - (document.getElementById('display_image').offsetHeight/2) +"px";
	}
		
}



///////////////////////////
//NEXT & PREVIOUS
///////////////////////////


function getNext() {
	if (goScroll) clearInterval(goScroll);
	if (currentThumb<thumbOrder.length-1) {
		currentThumb++;
		showImage(currentThumb);
	} 
	
}


function getPrev() {
	if (goScroll) clearInterval(goScroll);
	if (currentThumb>0) {
		currentThumb--;
		showImage(currentThumb);
	}
	
}



///////////////////////////
//THUMBNAIL ROLLOVERS
///////////////////////////


function dimMe(which_image,orderNumber) {
	if (currentThumb != orderNumber) {
		document.getElementById(which_image).style.opacity=0.6;
		document.getElementById(which_image).style.filter='alpha(opacity=60)';
	}
}


function shineMe(which_image) {
	document.getElementById(which_image).style.opacity=1;
	document.getElementById(which_image).style.filter='alpha(opacity=100)';
}


///////////////////////////
//WINDOW RELOAD SCROLL
///////////////////////////

function getAnchorPosition (anchorName) {
	var temp = 0;
	for(var i=0;i<window.document.anchors.length;i++){
		if(window.document.anchors[i].name==anchorName) temp = window.document.anchors[i].offsetTop;
	}
	return temp;
}
function ScrollMeTo (theLink) {
	currentLink = String(theLink);
	strpos = currentLink.indexOf('#') + 2;
	anchor = currentLink.substring(strpos);
	var coords = getAnchorPosition(anchor);
	window.scrollTo(0,coords);
}
