/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

var imgBase = 'http://www.bridgearcenciel.org/';

function showMasters(year)
{
	if(!$('.masters-container').is(':visible'))
	{
		$('.masters-container').show('fast', function() {
				$('#masters-' + year).show('normal');
				$('#masters-link-' + year).css('font-weight', 'bold');
				$('#masters-link-' + year).css('text-decoration', 'none');
			});
	}
	var loopCount = 2001;
	while(document.getElementById('masters-' + loopCount))
	{
		if($('#masters-' + loopCount).is(':visible') && loopCount != year)
		{
			$('#masters-link-' + loopCount).css('font-weight', 'normal');
			$('#masters-link-' + loopCount).css('text-decoration', 'underline');
			$('#masters-' + loopCount).hide('fast', function() {
					$('#masters-link-' + year).css('font-weight', 'bold');
					$('#masters-link-' + year).css('text-decoration', 'none');
					$('#masters-' + year).show('normal');
				});
		}
		loopCount++;
	}
}

function preload(arrayOfImages)
{
    $(arrayOfImages).each(function()
	{
        $('<img/>')[0].src = this;
    });
}

function slideGallery(slideDirection)
{
	var i = 0;
	while(!$('#gallery-' + i).is(":visible"))
	{
		i++;
	}
	j = i + 5;
	if($('#gallery-' + j).length && slideDirection == 'right')
	{
		$('#gallery-' + i).hide('blind', { direction: 'horizontal' }, 500);
		$('#gallery-' + j).show('blind', { direction: 'horizontal' }, 500);
		$('#gallery-left').css('color', '#515151');
		var k = 0;
		while(document.getElementById('gallery-' + k))
		{
			k++;
		}
		k--;
		if(j == k)
		{
			$('#gallery-right').css('color', '#cccccc');
		}
	}
	else
	{
		i--;
		j = i + 5;
		if($('#gallery-' + i).length && slideDirection == 'left')
		{
			$('#gallery-' + j).hide('blind', { direction: 'horizontal' }, 500);
			$('#gallery-' + i).show('blind', { direction: 'horizontal' }, 500);
			$('#gallery-right').css('color', '#515151');
			if(i == 0)
			{
				$('#gallery-left').css('color', '#cccccc');
			}
		}
	}
}

/*function loadTitle(folder, id)
{
	var ans = $.ajax({
		url: 'images/title.php',
		async: false,
		type: 'POST',
		data: ({ gallery : folder, picture : id}),
		dataType: 'html'
		}).responseText;
	$('#picture-title').html(ans);
}*/

function showGallery(id, folder)
{
	$('#main-album-picture').attr('src', folder + 'large/0.jpg');
	preload([imgBase + folder + 'large/1.jpg']);
	//loadTitle(folder, 0);
	$('#picture-left').css('color', '#cccccc');
	$('#picture-right').css('color', '#515151');
}

function switchImage(switchDirection)
{
	var max = false;
	var nextImage = '';
	var nextId = 0;
	if(switchDirection == 'left')
	{
		var current = $('#main-album-picture').attr('src');
		var newImage = current;
		current = current.split('.');
		current = current[0].split('/');
		current = current[current.length - 1];
		var folder = newImage.replace('large/' + current + '.jpg', '');
		newImage = newImage.replace('/' + current + '.jpg', '/' + (current - 1) + '.jpg');
		if(current != 0)
		{
			$('#main-album-picture').fadeOut(500, function(){
				if(current == 1)
				{
					$('#picture-left').css('color', '#cccccc');
				}
				else
				{
					nextId = current - 1;
					nextImage = newImage.replace('/' + newId + '.jpg', '/' + nextId + '.jpg');
					preload([imgBase + nextImage]);
				}
				//loadTitle(folder, current - 1);
				$('#picture-right').css('color', '#515151');
				$('#main-album-picture').attr('src', newImage);
				$('#main-album-picture').fadeIn(500);
			});
		}
	}
	else
	{
		var current = $('#main-album-picture').attr('src');
		var newImage = current;
		current = current.split('.');
		current = current[0].split('/');
		var arrayPosition = current.length - 1;
		current = current[arrayPosition];
		var newId = parseInt(current) + 1;
		nextId = parseInt(current) + 2;
		newId = new String(newId);
		var folder = newImage.replace('large/' + current + '.jpg', '');
		newImage = newImage.replace('/' + current + '.jpg', '/' + newId + '.jpg');
		nextImage = newImage.replace('/' + newId + '.jpg', '/' + nextId + '.jpg');
		var http = new XMLHttpRequest();
		http.open('HEAD', imgBase + newImage, false);
		http.send();
		if(http.status === 404)
		{
			max = parseInt(newId) - 1;
		}
		else
		{
			max = parseInt(current) + 2;
			http.open('HEAD', imgBase + nextImage, false);
			http.send();
			if(http.status === 404)
			{
				max = newId;
			}
			else
			{
				preload([imgBase + nextImage]);
			}
		}
		if(current != max)
		{
			$('#main-album-picture').fadeOut(500, function(){
				//loadTitle(folder, parseInt(newId));
				if(newId == max)
				{
					$('#picture-right').css('color', '#cccccc');
				}
				if(newId == '1')
				{
					$('#picture-left').css('color', '#515151');
				}
				$('#main-album-picture').attr('src', newImage);
				$('#main-album-picture').fadeIn(500);
			});
		}
	}
}
