/****************************
********   Wishlist   ***********
***  1. Dynamically add all divs so that non-JS compatible browsers will see nothing  ***
****************************/

var defaultPhoto = 1;
var numberOfPhotos = 14;
var selected = defaultPhoto;

function galleryInitialize()
{
	loadDefaultPhoto();
	waitForDefaultPhoto();
	
}


function loadDefaultPhoto()
{
	document.getElementById("preloader").innerHTML += '<img id="pre_photo' + defaultPhoto + '" src="http://www.congoshostel.com/photos/loading.gif" />';
    document.getElementById("pre_photo" + defaultPhoto).src = "http://www.congoshostel.com/photos/" + defaultPhoto + ".jpg";
}


function waitForDefaultPhoto()
{
	if (document.getElementById("pre_photo" + defaultPhoto).naturalWidth == 780)
	{
		changeBigPhoto(defaultPhoto);
		
		//pass the batch number of the default photo, which comes from that formula
		generateThumbnails(Math.floor((defaultPhoto-1)/7));
		
	}
	else
	{
		var timer01 = setTimeout("waitForDefaultPhoto()", 400);
	}
}

function waitForThumbnail(thumbNumber, thumbPointer)
{
	var doneWithThisThumb = false;
	
	if(thumbPointer <= numberOfPhotos)
	{
		if (document.getElementById("pre_thumb" + thumbPointer).naturalWidth == 78)
		{
			replaceThumb(thumbNumber, thumbPointer);
			doneWithThisThumb = true;
		}
		else
		{
			var timer01 = setTimeout("waitForThumbnail(" + thumbNumber + ", " + thumbPointer + ")", 250);
		}
	}
	else
	{
		document.getElementById('thumb' + thumbNumber).setAttribute('class', 'emptyThumb');
		document.getElementById('thumb' + thumbNumber).setAttribute('onclick', '#');
		document.getElementById('thumb' + thumbNumber + 'Img').setAttribute('src', 'http://www.congoshostel.com/images/empty.gif');
		
		doneWithThisThumb = true;
	}
	
	if (doneWithThisThumb == true)
	{
		if(thumbNumber == 7)
		{
			
			//Load all big images, starting from the first thumb.
			//Since this is the last thumb,
			//subtract 6 from this one to get the first
			document.getElementById("preloader").innerHTML += '<img id="pre_photo' + (thumbPointer-6) + '" src="http://www.congoshostel.com/photos/loading.gif" />';
			document.getElementById("pre_photo" + (thumbPointer-6)).src = "http://www.congoshostel.com/photos/" + (thumbPointer-6) + ".jpg";
			loadBigPhotos(1, thumbPointer-6);
		}
		else
		{
			document.getElementById("preloader").innerHTML += '<img id="pre_thumb' + (thumbPointer+1) + '" src="http://www.congoshostel.com/photos/loading.gif" />';
			document.getElementById("pre_thumb" + (thumbPointer+1)).src = "http://www.congoshostel.com/photos/thumb_" + (thumbPointer+1) + ".jpg";
			waitForThumbnail(thumbNumber+1, thumbPointer+1);
		}
	}
}

function loadBigPhotos(photoNumber, photoPointer)
{
    var doneWithThisPhoto = false;
    if(photoPointer <= numberOfPhotos)
	{
		if (document.getElementById("pre_photo" + photoPointer).naturalWidth == 780)
		{
			doneWithThisPhoto = true;
		}
		else
		{
			var timer02 = setTimeout("loadBigPhotos(" + photoNumber + ", " + photoPointer + ")", 250);
		}
	}
	else
	{
		doneWithThisPhoto = true;
	}
    
    if (doneWithThisPhoto == true)
	{
		if(photoNumber < 7)
		{
			document.getElementById("preloader").innerHTML += '<img id="pre_photo' + (photoPointer+1) + '" src="http://www.congoshostel.com/photos/loading.gif" />';
			document.getElementById("pre_photo" + (photoPointer+1)).src = "http://www.congoshostel.com/photos/" + (photoPointer+1) + ".jpg";
			loadBigPhotos((photoNumber+1), (photoPointer+1));
		}
	}
	else
	{
		
	}
}

function generateThumbnails (batch)
{
	
	//Disable prev button on first batch
	if(batch > 0)
	{
		document.getElementById('prev').setAttribute('onclick','generateThumbnails(' + (batch-1) + ')');
		document.getElementById('prev').setAttribute('class','galleryNav');
		document.getElementById('prevImg').setAttribute('src','http://www.congoshostel.com/images/00prev.jpg');
		document.getElementById('prevImg').setAttribute('onmouseover','doHoverPrev()');
		document.getElementById('prevImg').setAttribute('onmouseout','doOutPrev()');
	}
	else
	{
		document.getElementById('prev').setAttribute('onclick','#');
		document.getElementById('prev').setAttribute('class','galleryNavDisabled');
		document.getElementById('prevImg').setAttribute('src','http://www.congoshostel.com/images/00prevDisabled.jpg');
		document.getElementById('prevImg').setAttribute('onmouseover','#');
		document.getElementById('prevImg').setAttribute('onmouseout','#');
	}
	
	// ((7 * batch)+1) is here because every batch consists of 7 thumbs and it should start 1 after
	var thumbPointer = ((7 * batch)+1);
	var bigPreLoadingPointer = thumbPointer;
	
	showLoadingThumbs();
	
	document.getElementById("preloader").innerHTML += '<img id="pre_thumb' + thumbPointer + '" src="http://www.congoshostel.com/photos/loading.gif" />';
	document.getElementById("pre_thumb" + thumbPointer).src = "http://www.congoshostel.com/photos/thumb_" + thumbPointer + ".jpg";
	waitForThumbnail(1, thumbPointer);

	//Disable next button on last batch
	if((thumbPointer+6) < numberOfPhotos)
	{
		document.getElementById('next').setAttribute('onclick','generateThumbnails(' + (batch+1) + ')');
		document.getElementById('next').setAttribute('class','galleryNav');
		document.getElementById('nextImg').setAttribute('src','http://www.congoshostel.com/images/00next.jpg');
		document.getElementById('nextImg').setAttribute('onmouseover','doHoverNext()');
		document.getElementById('nextImg').setAttribute('onmouseout','doOutNext()');
	}
	else
	{
		document.getElementById('next').setAttribute('onclick','#');
		document.getElementById('next').setAttribute('class','galleryNavDisabled');
		document.getElementById('nextImg').setAttribute('src','http://www.congoshostel.com/images/00nextDisabled.jpg');
		document.getElementById('nextImg').setAttribute('onmouseover','#');
		document.getElementById('nextImg').setAttribute('onmouseout','#');
	}
}

function showLoadingThumbs()
{
	for (var i = 1; i <= 7; i++)
	{
		document.getElementById('thumb' + i).setAttribute('class', 'thumb');
		document.getElementById('thumb' + i).setAttribute('onclick', '#');
		document.getElementById('thumb' + i + 'Img').setAttribute('src', 'http://www.congoshostel.com/images/loading.gif');
	}
}

function replaceThumb(theThumb, thumbPointer)
{
	document.getElementById('thumb' + theThumb + 'Img').setAttribute('src', 'http://www.congoshostel.com/photos/thumb_' + thumbPointer + '.jpg');
	document.getElementById('thumb' + theThumb).setAttribute('onclick', 'changeBigPhoto(' + thumbPointer + ')');
	
	if(thumbPointer == selected)
	{
		document.getElementById('thumb' + theThumb).setAttribute('class', 'thumbSel');
	}
	else
	{
		document.getElementById('thumb' + theThumb).setAttribute('class', 'thumb');
	}
}

function changeBigPhoto(newPhoto)
{
	document.getElementById('bigPhotoImg').setAttribute('src', 'http://www.congoshostel.com/images/loadingBig.gif');

	var theThumb;
	var lastThumbSel;
	
	// Get the thumb number. But if it's a multiple of 7, the result is 0, so we need that if first.
	if (newPhoto % 7 == 0)
	{
		theThumb = 7;
	}
	else
	{
		theThumb = newPhoto % 7;
	}
	
	// Get the last selected thumb number. But if it's a multiple of 7, the result is 0, so we need that if first.
	if (selected % 7 == 0)
	{
		lastThumbSel = 7;
	}
	else
	{
		lastThumbSel = selected % 7;
	}
	
	// Now the selected pointer can change to the new one
	selected = newPhoto;
	
	document.getElementById('thumb' + lastThumbSel).setAttribute('class', 'thumb');
	document.getElementById('thumb' + theThumb).setAttribute('class', 'thumbSel');

	document.getElementById('bigPhotoImg').setAttribute('src', 'http://www.congoshostel.com/photos/' + newPhoto + '.jpg');
}

function doHoverNext()
{
	document.getElementById('nextImg').setAttribute('src','http://www.congoshostel.com/images/00next_hover.jpg');
}

function doHoverPrev()
{
	document.getElementById('prevImg').setAttribute('src','http://www.congoshostel.com/images/00prev_hover.jpg');
}

function doOutNext()
{
	document.getElementById('nextImg').setAttribute('src','http://www.congoshostel.com/images/00next.jpg');
}

function doOutPrev()
{
	document.getElementById('prevImg').setAttribute('src','http://www.congoshostel.com/images/00prev.jpg');
}
