function createPath(srcPath, fileName, start){
	if(srcPath){
		tmpFileName = srcPath.split('/');
		if(start > 0 && tmpFileName[0] == '.'){
			fileName = '';
			start = 0;
		}

		for (var i=start, c=tmpFileName.length; i<c; i++){
			if (tmpFileName[i] != '_thumbs' && tmpFileName[i] != '_black_thumb')
				fileName += tmpFileName[i] + (i < (c - 1) ? '/' : '');
		}
		return fileName;
	}
}

function getNextPhoto(smallArray, bigPhoto, index){
	$(smallArray).each(function(intIndex){
		if(index == intIndex && index < smallArray.length - 1){
			$('#galleryNav .navRight').show();
			
			fileNewName = createPath($(smallArray[intIndex + 1]).attr('src'), '', 0);
			$(bigPhoto).attr('src', fileNewName);
			$(bigPhoto).attr('alt', $(smallArray[intIndex + 1]).attr('alt'));
			$(bigPhoto).attr('height', ($(smallArray[intIndex + 1]).height() * 9)).attr('width', ($(smallArray[intIndex + 1]).width() * 9));

			$('.thumbnails li a').each(function(){
				if($(this).attr('href') == fileNewName){
					$(this).parent().attr('class', 'active');
				}
			});

			$(bigPhoto).parent().attr('href', fileNewName);
			$(bigPhoto).parent().attr('title', $(smallArray[intIndex + 1]).attr('title'));

			$(bigPhoto).parent().next().next().children('h2').html($(smallArray[intIndex + 1]).attr('alt'));
			
			var title = $.ajax({
				url: "validate.php?desc="+$(smallArray[intIndex + 1]).attr('title'),
				async: false
			}).responseText;
			
			$(bigPhoto).parent().next().next().children('p').html(title);
			
			$('#galleryNav .navRight a').click(function(){
				getNextPhoto(smallArray, bigPhoto, ++index);
			});
		}
		else{
			if(index >= smallArray.length - 1){
				$('#galleryNav .navRight').hide();
			}
		}
	});
}

function getPreviousPhoto(smallArray, bigPhoto, index){
	$(smallArray).each(function(intIndex){
		if(index == intIndex && index < smallArray.length - 1){
			$('#galleryNav .navRight').show();
			
			fileNewName = createPath($(smallArray[intIndex - 1]).attr('src'), '', 0);
			$(bigPhoto).attr('src', fileNewName);
			$(bigPhoto).attr('alt', $(smallArray[intIndex - 1]).attr('alt'));
			//$(bigPhoto).attr('height', ($(smallArray[intIndex - 1]).height() * 9)).attr('width', ($(smallArray[intIndex - 1]).width() * 9));

			$(bigPhoto).parent().attr('href', fileNewName);
			$(bigPhoto).parent().attr('title', $(smallArray[intIndex - 1]).attr('title'));

			$(bigPhoto).parent().next().next().children('h2').html($(smallArray[intIndex - 1]).attr('alt'));
			
			var title = $.ajax({
				url: "validate.php?desc="+$(smallArray[intIndex - 1]).attr('title'),
				async: false
			}).responseText;
			
			$(bigPhoto).parent().next().next().children('p').html(title);
			
			$('#galleryNav .navRight a').click(function(){
				getPreviousPhoto(smallArray, bigPhoto, --index);
			});
		}
		else{
			if(index >= smallArray.length - 1){
				$('#galleryNav .navRight').hide();
			}
		}
	});
}


function setPhoto(img, active, isObj){
	if(isObj == 0)
		var pathURL = img.src.split('/');
	else
		var pathURL = img[0].src.split('/');
		
	var newPath = blackWhite = thumbs = '';
	if(active == 1){
		blackWhite = '_black_thumb';
		thumbs = '_thumbs';
	}
	else{
		thumbs = '_black_thumb';
		blackWhite = '_thumbs';
	}

	for(var i=0, c=pathURL.length; i<c; i++){
		if(pathURL[i] == blackWhite)
			newPath += thumbs;
		else
			newPath += pathURL[i];
		
		if(i<c-1)
			newPath += '/';
	}
	
	img.attr('src', newPath);
}

$(document).ready(function() {
	//BlacknWhite.init();
	//BlacknWhite.init({minor : 8});
	
	// fade in default image
	$('#main_image img').css('display','none').fadeIn('normal');
	//$('#main_image img').hide('slow');
	
	// fade out inactive thumbnails
	$('#gallery ul li.active').siblings().css({display:'none',opacity:'1.0'}).fadeIn(500); // 0.4
	setPhoto($('#gallery ul li.active a img'), 1, 1);

	// thumbnails hover effects
	$('#gallery ul li').hover(function() {
			$(this).not('.active').fadeTo('fast',1);
			$(this).not('.active').css('position', 'relative').css('left', 0).css('top', 0); // Fixes Safari 2 rendering issue
		}, function() {
			$(this).not('.active').fadeTo('fast',1.0); // 0.4
		}
	);
	
	// thumbnails click function
	$('#gallery ul li').click(function() {
												 
		// set class for clicked thumbnail to active
		$(this).addClass('active');
		// fade out and remove active classes on all other thumbnails								 
		$(this).siblings().removeClass('active').fadeTo('fast',1.0); // 0.4
		setPhoto($(this).children('a').children('img'), 1, 1);
		
		var imagesArray = $(this).siblings().children('a').children('img').attr('rel','lightbox-journey');
		
		for(var i=0, c=imagesArray.length; i<c; i++){
			var pathURL = imagesArray[i].src.split('/');
			var newPath = '';
			
			for(var j=0, cnt=pathURL.length; j<cnt; j++){
				if(pathURL[j] == '_black_thumb' || pathURL[j] == '_thumbs')
					newPath += '_black_thumb';
				else
					newPath += pathURL[j];
				
				if(j<cnt-1)
					newPath += '/';
			}
			
			imagesArray[i].setAttribute('src', newPath);
		}
		
		
		
		// get image source reference from the thumbnail link and store in a variable
		var img = $(this).children('a').attr('href');
		// get image caption from the title attribute of the image tag
		var caption = $(this).children('a').children('img').attr('title');
		var description = $(this).children('a').children('img').attr('alt');
		
		var description_2 = $.ajax({
			url: "validate.php?desc="+caption,
			async: false
		}).responseText;
		
		// fade out and hide the previous image
		$('#main_image img').fadeOut().hide();	
		// and the caption
		$('#main_image .caption').fadeOut().hide();
		$('#main_image h2').fadeOut().hide();
		
		// set the new image source file and fade in the new image
		
		var imgWidth = parseInt($(this).width());
		var imgHeight = parseInt($(this).height());
		
		
		
		$('#main_image a').attr('href', img).attr('rel', 'lightbox-journey');
		
		if(imgWidth > imgHeight){
			$('#main_image a img').attr('width', '545');
			$('#main_image a img').removeAttr('height');
		}
		else{
			$('#main_image a img').attr('height', '415');
			$('#main_image a img').removeAttr('width');
		}

		$('#main_image a img').attr('src',img);
		$('#main_image a img').fadeIn();
		$('#main_image h2').text(description).fadeIn();
		$('#main_image .caption').html(description_2).fadeIn();
		
	});

	var thumbnails = $('#gallery ul li a img');
	
	
	$('#galleryNav .navLeft a').click(function(){
		getPreviousPhoto(thumbnails, $('#main_image a img'), 0);
		return false;
	});
	
	$('#galleryNav .navRight a').click(function(){
		getNextPhoto(thumbnails, $('#main_image a img'), 0);
		return false;
	});
});

$(window).load(function(){
	for(var i=1; i<=$('input#fCountImages').val(); i++)
		$("<img>").attr("src", $('input#fPathImages').val() + (i < 10 ? ('00'+i) : (i >= 10 && i < 100 ? ('0'+i) : i)) + '.jpg');
});
