var currentPhoto;
var newPhoto;

var currentPhotoDiv;
var newPhotoDiv;

var currentOpacity = 0;

var fadePointer;

var scrolling = false;
var topMax;
var scrollBlock;

addLoadEvent(initGallery);
addLoadEvent(initScroll);


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function initGallery() {
	currentPhoto = document.getElementById('photoContainer1');
	newPhoto = document.getElementById('photoContainer2');
	currentPhotoDiv = document.getElementById('photo1');
	newPhotoDiv = document.getElementById('photo2');
}


function initScroll() {
	scrollBlock = document.getElementById('inner_block');
	scrollBlock.style.top = 0;
	scrollBlock.topMax = 410 - scrollBlock.scrollHeight;
	scrollBlock = document.getElementById('thumbs_inner');
	scrollBlock.style.top = 0;
	scrollBlock.topMax = 410 - scrollBlock.scrollHeight;
	
//	alert(scrollBlock.scrollHeight);
}

function startScrollUp(scrollDiv) {
	scrollBlock = document.getElementById(scrollDiv);
	scrolling = setTimeout(scrollUp, 100);
}

function stopScrollUp() {
	clearTimeout(scrolling);
}

function scrollUp() {
	currentTop = parseInt(scrollBlock.style.top);
	newTop = (currentTop + 20);
		
	//scrollBlock = document.getElementById('inner_block');
	if (newTop >= 0)
	{
		scrollBlock.style.top = "0";
		stopScrollUp();
	}else{
		scrollBlock.style.top = newTop + "px";
		scrolling = setTimeout(scrollUp, 100);
	}
	
}

function startScrollDown(scrollDiv) {
	scrollBlock = document.getElementById(scrollDiv);
	if (scrollBlock.scrollHeight > 410)
		scrolling = setTimeout(scrollDown, 100);
}

function stopScrollDown() {
	clearTimeout(scrolling);
}

function scrollDown() {
	currentTop = parseInt(scrollBlock.style.top);
	newTop = (currentTop - 20);

	//scrollBlock = document.getElementById('inner_block');
	
	//alert(newTop);
	//alert(newTop);
	if (newTop <= scrollBlock.topMax)
	{
		scrollBlock.style.top = scrollBlock.topMax + "px";
		stopScrollDown();
	}else{
		scrollBlock.style.top = newTop + "px";
		scrolling = setTimeout(scrollDown, 100);
	}

}



function showPhoto(newImage) {
	
//	photoContainer = document.getElementById('photoContainer');
//	photoDiv = document.getElementById('photo');
	
	clearTimeout(fadePointer);
	set_opacity(currentPhoto, 0);
	set_opacity(newPhoto, 100);
	
	//alert(newImage);
	newPhoto.src = "photos/" + newImage;
//	alert(newPhoto.src);
	
	newPhoto.onload = startFade;
	
	
		
//	photoDiv.style.visibility = "hidden";
//	alert(newImage.width);
//	alert(photoDiv.style.left);
	
	//oImg
}

function fadePhotos() {
	
	//alert("fading");
	set_opacity(currentPhoto, currentOpacity);
	set_opacity(newPhoto, 100-currentOpacity);
	
	currentOpacity += 5;
	//alert(currentOpacity);
	if (currentOpacity < 100)
	{
		fadePointer = setTimeout(fadePhotos, 10);
	}else{
		//alert("done");
		currentOpacity = 0;
		currentPhoto.style.visibility = "hidden";
		tempPhoto = currentPhoto;
		currentPhoto = newPhoto;
		newPhoto = tempPhoto;
		
		tempPhotoDiv = currentPhotoDiv;
		currentPhotoDiv = newPhotoDiv;
		newPhotoDiv = tempPhotoDiv;
	}
}


function startFade() {
//	alert("recenter");
	//newPhotoDiv = document.getElementById('photo');
//	newPhotoDiv.style.left = ((560 - newPhoto.width) / 2) + "px";
//	newPhotoDiv.style.top = ((570 - newPhoto.height) / 2) + "px";
	//newPhotoDiv.style.visibility = "visible";
	newPhoto.style.visibility = "visible";
	fadePhotos();
	
}


function loadSection(section) {
	
	mainSection = document.getElementById('inner_block');
	loadDiv = document.getElementById(section);
	
	mainSection.innerHTML = loadDiv.innerHTML;
	initScroll();
}

function set_opacity(element, opacity)
{
	opacityModifier = opacity / 100;
	
	if (document.all)
	{
		element.style.filter = 'alpha(opacity=' + opacity + ')';
		element.style.filter = 'alpha(opacity=' + (100 - opacity) + ')';
	}else{
		element.style.opacity = Math.max(0.01, opacityModifier);
		element.style.opacity = Math.min(0.99, (1 - opacityModifier));
	}
}

function createRequest() {
	
	// create an Ajax Request
	
	var ajaxRequest;
	
	try
	{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}		
		catch (e1)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
				catch (e2)
				{
					ajaxRequest = new XMLHttpRequest();
				}
		}
	
	return ajaxRequest;
}


function fetchThumbs(set) {

	var ajax_connection = createRequest();
	var logo = document.getElementById('eventLogo');
	logo.innerHTML = '<img src="photos/'+set+'/logo.gif" />';
	
	ajax_connection.open('get', "thumbs.php?set="+set);
	
	ajax_connection.onreadystatechange = function(){
		
		if (ajax_connection.readyState == 4) {
			var theHTML = ajax_connection.responseText;
			var thumbs = document.getElementById('thumbs_inner');
			
			thumbs.innerHTML = theHTML;
		}
	}	
	
	
	
	ajax_connection.send(null);
}

function changeImgSrc(theImage, theSrc)
{
	//var logo = document.getElementById('eventLogo');
	
	theImage.src = "images/" + theSrc;
}