thumbnails_reordered = false;

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

function change_animation(number) {
	// skip if mouseup was after reorder drag action.
	if (thumbnails_reordered) {
		thumbnails_reordered = false;
		return;
	}
	animation_number = number;
	id = animations[number];
	selected_animation = id;
	// change animation
	var player = jQuery("#player_container > :first")[0];
	player.setConfig( {  
      autoPlay: true,
      videoFile: '/animations/' + id + '.flv',
      initialScale: 'scale',
	  loop: true,
	  showMenu: false,
	  showFullScreenButton: false,
	  timeDisplayFontColor: 0xFFFFFF,
	  controlBarBackgroundColor: 0x2A1500,
	  controlBarGloss: 'none',
	  showVolumeSlider: false,
	  bufferingAnimationColor: 0xFF99CC,
	  bufferBarColor1: 0x2A1500,
	  bufferBarColor2: 0xFF99CC
    } )
}


function change_artist_portfolio_image(number) {
	// skip if mouseup was after reorder drag action.
	if (thumbnails_reordered) {
		thumbnails_reordered = false;
		return;
	}
	image_number = number;
	id = images[number];
//	alert(id);
	selected_image = id;
	var main_image_behind = document.getElementById('artist_main_image_behind');
	main_image_behind.src = '/i/artist_images_main_images/' + id + '.jpg';
	jQuery('#artist_main_image').fadeOut("slow", function() {setup_front_image();});
	if (lightbox_items[id] == 1) {
		set_image_link(1);
	} else {
		set_image_link(0);
	}
	return false;
}

function setup_front_image() {
	var main_image = document.getElementById('artist_main_image');
	main_image.src = '/i/artist_images_main_images/' + id + '.jpg';
	jQuery('#artist_main_image').fadeIn("fast");
}

function next_image() {
	if (images[image_number+1]) {
		image_number++;
		change_artist_portfolio_image(image_number);
	} else {
		image_number = 1;
		change_artist_portfolio_image(image_number);
	}
}

function next_animation() {
	if (animations[animation_number+1]) {
		animation_number++;
		change_animation(animation_number);
	} else {
		animation_number = 1;
		change_animation(animation_number);
	}
}

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

function previous_animation() {
	if (animations[animation_number-1]) {
		animation_number--;
		change_animation(animation_number);
	} else {
		animation_number = animation_count;
		change_animation(animation_number);
	}
}

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

function add_image_to_lightbox_confirm(msg) {
	if (msg == 'ERROR') {
			alert("Error adding image to lightbox.");
	} else {
//		alert("Image added to lightbox:" + selected_image);
		lightbox_items[selected_image] = 1;
		// update page
		set_image_link(1);
		// are all items in portfolio in lightbox?
		var all_images_in_portfolio = true;
		for(var i = 1; i <= images.length; i++) {
			if (lightbox_items[images[i]] == 1) {
//				alert('Image in lightbox:' + images[i] + '(' + lightbox_items[images[i]] + ')');
			} else {
				if (images[i] > 0) {
//					alert('Image NOT in lightbox:' + images[i] + '(' + lightbox_items[images[i]] + ')');
					all_images_in_portfolio = false;
					break;
				}
			}
		}
		if (all_images_in_portfolio) {
			set_portfolio_link(1);
		}
		update_lightbox_items_count(msg);
	}
}

function update_lightbox_items_count(number) {
	var msg;
//	alert(number + ' items now in lightbox');
	var lightbox_items_count = document.getElementById('lightbox_items_count');
	if (number == 1) {
		msg = number + ' items';
	} else {
		msg = number + ' items';
	}
	lightbox_items_count.innerHTML = msg;
}

function add_portfolio_to_lightbox() {
	jQuery.ajax({
		type: "POST",
		url: "/ajax_add_portfolio_to_lightbox.php",
		data: "portfolio=" + portfolio + "&artist_id=" + artist,
		success: function(msg){
			add_portfolio_to_lightbox_confirm(msg);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ajax_error(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	return false;
}

function add_portfolio_to_lightbox_confirm(msg) {
	if (msg == 'ERROR') {
		alert("Error adding portfolio to lightbox.");
	} else {
		// mark all images as added
		for(var i = 1; i <= images.length; i++) {
			lightbox_items[images[i]] = 1;
		}
		// update page
		set_portfolio_link(1);
		set_image_link(1);
		update_lightbox_items_count(msg);
	}
}

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 {
		lightbox_items[selected_image] = 0;
		set_image_link(0);
		set_portfolio_link(0);
		update_lightbox_items_count(msg);
	}
}

function remove_portfolio_from_lightbox() {
	jQuery.ajax({
		type: "POST",
		url: "/ajax_remove_portfolio_from_lightbox.php",
		data: "portfolio=" + portfolio + "&artist_id=" + artist,
		success: function(msg){
			remove_portfolio_from_lightbox_confirm(msg);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ajax_error(XMLHttpRequest, textStatus, errorThrown);
		}
	});
	return false;
}

function remove_portfolio_from_lightbox_confirm(msg) {
	if (msg == 'ERROR') {
		alert("Error removing portfolio from lightbox: \n" + msg);
	} else {
		// mark all images as removed
		for(var i = 1; i <= images.length; i++) {
			lightbox_items[images[i]] = 0;
		}
		set_image_link(0);
		set_portfolio_link(0);
		update_lightbox_items_count(msg);
	}
}

function ajax_error(XMLHttpRequest, textStatus, errorThrown) {
	alert("ajax error: " + textStatus + "\n" + "errorThrown: " + errorThrown);
}

function set_image_link(added) {
	var link = document.getElementById('image_lightbox_link');
	if (added == 1) {
		link.innerHTML = "Image added to lightbox&nbsp;&nbsp;<a href='#' onclick='return remove_image_from_lightbox();'>[-]</a>";
	} else {
		link.innerHTML = "<a href='#' onclick='return add_image_to_lightbox();'>Add image to Lightbox</a>";
	}
}

function set_portfolio_link(added) {
	var link = document.getElementById('portfolio_lightbox_link');
	if (added == 1) {
		link.innerHTML = "Portfolio in lightbox&nbsp;<a href='#' onclick='return remove_portfolio_from_lightbox();'>[-]</a>";
	} else {
		link.innerHTML = "<a href='#' onclick='return add_portfolio_to_lightbox();'>Add portfolio to Lightbox [+]</a>";
	}
}

function note_thumbnails_reordered() {
		 thumbnails_reordered = true;
}

function save_new_thumbnails_order() {
	// step through child elements of #portfolio_thumbnails
	// to determine new order
	var thumbnails = jQuery('#portfolio_thumbnails').children('.sortable');
	var order_string = new String();
	var bits = new Array();
	var new_order = new Array();
	var thumbnail_id;
	var thumbnail_index;
	
	for(var i = 0; i < thumbnails.length; i++) {
		bits = thumbnails[i].id.split('_');
		thumbnail_id = bits[1];
		thumbnail_index = bits[2];
		new_order[i+1] = thumbnail_id;
	}

	var data = new_order.toString();
//	alert(data);

	if (portfolio == 'animation') {
		animations = new_order;
	} else {
		images = new_order;
	}
	
	// send new order to server
	
	jQuery.ajax({
		type: "POST",
		url: "/ajax_change_portfolio_sort_order.php",
		data: 'artist=' + artist + '&portfolio=' + portfolio + '&new_order=' + new_order,
		success: function(msg){
//			alert(msg);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ajax_error(XMLHttpRequest, textStatus, errorThrown);
		}
	});
}
