

function show_help() {
	jQuery('#lightbox_help').fadeIn("slow");
}

function hide_help() {
	jQuery('#lightbox_help').fadeOut("slow");
}



function preload_artist_image(id) {
//	alert('preloading image id ' + id);
	MM_preloadImages('/i/artist_images_main_images/'+id+'.jpg');
}

function change_main_image(number) {
	image_number = number;
	id = lightbox_images[number]['id'];
//	alert('changing to image number ' + image_number + ', id ' + id);
	selected_image = id;
//	alert('Current image number: ' + image_number + ', selected_image id: ' + selected_image);
	var main_image_behind = document.getElementById('lightbox_main_image_behind');
	main_image_behind.src = '/i/artist_images_main_images/' + id + '.jpg';
	jQuery('#lightbox_main_image').fadeOut("slow", function() {setup_front_image();});
	if (lightbox_images.length == 2) { // allow for 0 index
		hide_previous_and_next_links();
	}
}

function setup_front_image() {
	var main_image = document.getElementById('lightbox_main_image');
	if (selected_image) {
		main_image.src = '/i/artist_images_main_images/' + selected_image + '.jpg';
	} else {
		main_image.src = '/i/shared/white.jpg';
	}
	jQuery('#lightbox_main_image').fadeIn("fast");
}


function change_main_image_to_null_image(number) {
	selected_image = 0;
	var main_image_behind = document.getElementById('lightbox_main_image_behind');
	main_image_behind.src = '/i/shared/white.jpg';
	jQuery('#lightbox_main_image').fadeOut("slow", function() {setup_front_image();});
	hide_previous_and_next_links();
	hide_remove_image_link();
	hide_pdf_links();
	show_empty_lightbox_message();
}

function show_empty_lightbox_message() {
	var my_div = document.getElementById('lightbox_thumbnails');
	my_div.innerHTML = "<h2><i>Your lightbox is currently empty.</i></h2>";
}

function hide_pdf_links() {
		jQuery('#pdf_text_link').hide("slow");
		jQuery('#pdf_icon_link').hide("slow");
}

function hide_previous_and_next_links() {
		jQuery('#previous_link').hide("slow");
		jQuery('#next_link').hide("slow");
}

function hide_remove_image_link() {
		jQuery('#remove_image_link').hide();
}

function change_main_image_by_id(id) {
//	alert(id);
	selected_image = id;
	// find image number
	for(var i = 1; i <= lightbox_images.length; i++) {
		if (lightbox_images[i]['id'] == id) {
			image_number = i;
			break;
		}
	}
//	alert('Current image number: ' + image_number + ', selected_image id: ' + selected_image);
//	var main_image = document.getElementById('lightbox_main_image');
//	main_image.src = '/i/artist_images_main_images/' + id + '.jpg';

	var main_image_behind = document.getElementById('lightbox_main_image_behind');
	main_image_behind.src = '/i/artist_images_main_images/' + id + '.jpg';
	jQuery('#lightbox_main_image').fadeOut("slow", function() {setup_front_image();});

	if (lightbox_images.length == 2) { // allow for 0 index
		hide_previous_and_next_links();
	}
}

function next_image() {
	if (image_count > 1) {
		if (lightbox_images[image_number+1]) {
			image_number++;
			change_main_image(image_number);
		} else {
			image_number = 1;
			change_main_image(image_number);
		}
	}
}

function previous_image() {
	if (lightbox_images[image_number-1]) {
		image_number--;
		change_main_image(image_number);
	} else {
		image_number = image_count;
		change_main_image(image_number);
	}
}

function remove_portfolio_from_lightbox(artist, portfolio) {
//	alert('removing portfolio ' + portfolio + ' for artist id ' + artist);
		for(var i = 1; i <= image_count; i++) {
			if (lightbox_images[i]['artist'] == artist && 
					lightbox_images[i]['portfolio'] == portfolio) {
					selected_image = lightbox_images[i]['id'];
					image_number = i;
					remove_image_from_lightbox();
			}
		}
}

function remove_image_from_lightbox() {
	jQuery.ajax({
		type: "POST",
		url: "/ajax_remove_from_lightbox.php",
		data: "image_id=" + selected_image,
		success: function(msg){
			remove_image_from_lightbox_confirm(msg);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ajax_error(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	return false;
}

function remove_image_from_lightbox_confirm(msg) {
	if (msg == 'ERROR') {
		alert("Error removing image from lightbox: \n" + msg);
	} else {
		var this_id = lightbox_images[image_number]['id'];
		var this_artist = lightbox_images[image_number]['artist'];
		var this_portfolio = lightbox_images[image_number]['portfolio'];
//		alert("image number: " + image_number + "\nid: " + this_id +"\nartist: " + this_artist + "\nportfolio: " + this_portfolio);

		// remove image from list
		lightbox_images.splice(image_number,1);
		image_count--;
		jQuery('#image_div_'+this_id).hide("slow");
		// if it was the last image in a portfolio, remove that
		var portfolio_count = 0;
		for(var i = 1; i <= image_count; i++) {
			if (lightbox_images[i]['artist'] == this_artist && 
					lightbox_images[i]['portfolio'] == this_portfolio) {
				portfolio_count++;
			}
		}
		if (portfolio_count == 0) {
			jQuery('#portfolio_div_' + this_artist + '_' + this_portfolio).hide("slow");
		}
		// if there are no more images for an artist, remove the artist.
		var artist_count = 0;
		for(var i = 1; i <= image_count; i++) {
			if (lightbox_images[i]['artist'] == this_artist) {
				artist_count++;
			}
		}
		if (artist_count == 0) {
			jQuery('#artist_div_' + this_artist).hide("slow");
		}

		// select the 'previous' image
		// if this was the only image, show the white null image
		if (lightbox_images.length == 1) { // allowing for index 0
			change_main_image_to_null_image();
		} else if (image_number == 1) {
			// if this was the first image, show the second image
			change_main_image(1);
		} else {
			// otherwise, show the previous image
			change_main_image(image_number - 1);
		}
	}
}


