function clear_lightbox(lightbox_id, lightbox_name) {
	if (confirm("You are about to remove all images from Folder "+lightbox_id+". Click OK to proceed with deletion. Click Cancel if you do not wish to remove images from Folder "+lightbox_id+".")) {
		image = eval("document.lb_image_"+lightbox_id);
		image.src = "/ximagi/download/lightboxcount.png?act=clear_lightbox&lightbox_id="+lightbox_id+"&lightbox_name="+lightbox_name;
	}
}

function pop_mail(lightbox_id) {
	d = window.open("/ximagi/preparing.php", "mail", "width=450,height=500");
	return true;
}

function send_lightbox(lightbox_id) {
	d = window.open("/ximagi/mail.php?id="+lightbox_id, "mail", "width=450,height=500");
}

function show_lightbox(lightbox_id, lightbox_name) {
	d = window.open("/ximagi/lightbox.php?id="+lightbox_id+"&message="+lightbox_name, "lightbox", "width=970,height=600,scrollbars=yes,resize=yes");
}

function modify_lightbox() {
	if (document.dispatch) {
		if (document.mlightboxform.act[0].checked) {
			return add_to_lightbox();
		} else if (document.mlightboxform.act[1].checked) {
			return true;
		} else if (document.mlightboxform.act[2].checked) {
			return true;
		} else {
			return false;
		}
		document.dispatch.lightbox_id.value = document.mlightboxform.lightbox_id[document.mlightboxform.lightbox_id.selectedIndex].value;
		document.dispatch.submit();
		return false;
	} else {
		return true;
	}
	return false;
}

function new_lightbox() {
	if (document.dispatch &&
			document.lightboxform.with_images &&
			document.lightboxform.with_images[0].checked) {
		for (var ix = 0; ix < imageinfo.length; ix++) {
			if (imageinfo[ix].selected) {
				document.dispatch.elements['image_id['+ix+']'].value = imageinfo[ix].imagenum;
			} else {
				document.dispatch.elements['image_id['+ix+']'].value = 0;
			}
		}
		document.dispatch.act.value = "new_lightbox_with";
		document.dispatch.lightbox_name.value = document.lightboxform.lightbox_name.value;
		document.dispatch.submit();
		return false;
	} else {
		return true;
	}
	return false;
}

function delete_from_current_lightbox() {
	if (document.dispatch) {
		for (var ix = 0; ix < imageinfo.length; ix++) {
			if (imageinfo[ix].selected) {
				document.dispatch.elements['image_id['+ix+']'].value = imageinfo[ix].imagenum;
			} else {
				document.dispatch.elements['image_id['+ix+']'].value = 0;
			}
		}
		document.dispatch.act.value = "delete_images_from_lightbox";
		document.dispatch.lightbox_id.value = document.nlightboxform.lightbox_id.value;
		document.dispatch.submit();
	}
	return false;
}

function add_to_lightbox_bulk(on, off) {
	var query;
	var pick;
	pick = document.lb_add.lb;
	if (pick[pick.selectedIndex].value == "x") {
		return;
	}
	query = "?lightbox_id="+pick[pick.selectedIndex].value+"&lightbox_name="+escape(pick[pick.selectedIndex].text)+"&act=add_image_to_lightbox";
	for (var ix = 0; ix < imageinfo.length; ix++) {
		query += "&"+"image_id[]="+imageinfo[ix].imagenum;
	}
	image = eval("document.lb_image_"+pick[pick.selectedIndex].value);
	image.src = "/ximagi/download/lightboxcount.png"+query;
	show('lightboxesnav', on, off);
}

function add_to_lightbox_bulk_notify(on, off, sender) {
	var query;
	var pick;
	pick = document.lb_add.lb;
	if (pick[pick.selectedIndex].value == "x") {
		return;
	}
	query = "?lightbox_id="+pick[pick.selectedIndex].value+"&lightbox_name="+escape(pick[pick.selectedIndex].text)+"&act=add_image_to_lightbox_notify&sender="+sender;
	for (var ix = 0; ix < imageinfo.length; ix++) {
		query += "&"+"image_id[]="+imageinfo[ix].imagenum;
	}
	image = eval("document.lb_image_"+pick[pick.selectedIndex].value);
	image.src = "/ximagi/download/lightboxcount.png"+query;
	show('lightboxesnav', on, off);
}

function add_to_lightbox() {
	if (document.dispatch) {
		for (var ix = 0; ix < imageinfo.length; ix++) {
			if (imageinfo[ix].selected) {
				document.dispatch.elements['image_id['+ix+']'].value = imageinfo[ix].imagenum;
			} else {
				document.dispatch.elements['image_id['+ix+']'].value = 0;
			}
		}
		document.dispatch.act.value = "add_images_to_lightbox";
		document.dispatch.lightbox_id.value = 
			document.mlightboxform.lightbox_id[document.mlightboxform.lightbox_id.selectedIndex].value;
		document.dispatch.submit();
	}
}

function show(element, navon, navoff) {
	if (shown && document.getElementById(shown)) {
	var link;
	link = shown + "link";
	var elem;
	var img;
	var linkelem;
	linkelem = document.getElementById(link);
	document.getElementById(shown).style.display = "none";
	linkelem.style.backgroundColor = navoff;
	elem = document.getElementById(element);
	elem.style.display = "block";
	eval("document." + shown + "image.src = '/img/" + shown + "_right.png';");
	eval("document." + element + "image.src = '/img/" + element + "_down.png';");
	shown = element;
	link = shown + "link";
	linkelem = document.getElementById(link);
	linkelem.style.backgroundColor = navon;
	}
}

function change_headlines() {
	
	var when;
	var date;
	when = document.headlinesform.headlines.options[document.headlinesform.headlines.selectedIndex].value;
	date = when.split("_");
	window.location = "/ximagi/headlines.php?m="+date[0]+"&y="+date[1];
}

function change_celebrity() {
	
	var alpha;
	alpha = document.peopleform.people.options[document.peopleform.people.selectedIndex].value;
	window.location = "/ximagi/celebrities.php?alpha="+alpha;
}

function celeb_filter() {
	var i;
	var list;
	page = 1;
	if (document.pageform) {
		document.pageform.page.selectedIndex = 0;
	}
	list = document.filter.celeblist;
	if (imageinfo.length == 0) {
		return 0;
	}
	if (list.options[0].selected) {
		for (i = imageinfo.length - 1; i >= 0; i--) {
			imageinfo[i].show = true;
		}
		q = i;
		refresh();
		return 0;
	}
	if (document.filter.any_all[0].checked) {
		for (i = imageinfo.length - 1; i >= 0; i--) {
			imageinfo[i].show = false;
			for (j = 1; j < list.length; j++) {
				if (list.options[j].selected) {
					if (imageinfo[i].people.toLowerCase().indexOf(list.options[j].text.toLowerCase()) != -1) {
						imageinfo[i].show = true;
						q = i;
					}
				}
			}
		}
	} else {
		for (i = 0; i < imageinfo.length; i++) {
			imageinfo[i].show = true;
			for (j = 1; j < list.length; j++) {
				if (list.options[j].selected) {
					if (imageinfo[i].people.toLowerCase().indexOf(list.options[j].text.toLowerCase()) == -1) {
						imageinfo[i].show = false;
					}
				}
			}
		}
	}
	slideshow();
	refresh();
}

function event_filter() {
	var i;
	var list;
	page = 1;
	if (document.pageform) {
		document.pageform.page.selectedIndex = 0;
	}
	list = document.filter.eventlist;
	if (imageinfo.length == 0) {
		return 0;
	}
	if (list.options[0].selected) {
		for (i = 0; i < imageinfo.length; i++) {
			imageinfo[i].show = true;
		}
		refresh();
		return 0;
	}
	for (i = 0; i < imageinfo.length; i++) {
		imageinfo[i].show = false;
		for (j = 1; j < list.length; j++) {
			if (list.options[j].selected) {
				if (imageinfo[i].headline.toLowerCase() == list.options[j].value.toLowerCase()) {
					imageinfo[i].show = true;
				}
			}
		}
	}
	slideshow();
	refresh();
}

function toggle_display() {
// this function is obsolete
	if (display == "t") {
		display = "d";	
		nodisplay = "t";
		document.getElementById('gallerylink').style.display = "none";
		document.getElementById('slidelink').style.display = "none";
		document.getElementById('detaillink').style.display = "block";
		document.getElementById("main").style.display = "none";
		document.getElementById("main2").style.display = "block";
		document.getElementById("slide").style.display = "none";
		show_slideshow = false;
//		refresh();
	} else if (display == "d" && !show_slideshow) {
		document.getElementById('gallerylink').style.display = "none";
		document.getElementById('slidelink').style.display = "block";
		document.getElementById('detaillink').style.display = "none";
		document.getElementById("main").style.display = "none";
		document.getElementById("main2").style.display = "none";
		document.getElementById("slide").style.display = "block";
		show_slideshow = true;
	} else {
		display = "t";
		nodisplay = "d";
		document.getElementById('gallerylink').style.display = "block";
		document.getElementById('slidelink').style.display = "none";
		document.getElementById('detaillink').style.display = "none";
		document.getElementById("main").style.display = "block";
		document.getElementById("main2").style.display = "none";
		document.getElementById("slide").style.display = "none";
		show_slideshow = false;
//		refresh();
	}
}

function show_gallery() {
	document.getElementById("main").style.display = "block";
	document.getElementById("main2").style.display = "none";
	document.getElementById("slide").style.display = "none";
	display = "t";
	nodisplay = "d";
	show_slideshow = false;
}

function show_detail() {
	document.getElementById("main").style.display = "none";
	document.getElementById("main2").style.display = "block";
	document.getElementById("slide").style.display = "none";
	display = "d";	
	nodisplay = "t";
	show_slideshow = false;
}

function show_large() {
	document.getElementById("main").style.display = "none";
	document.getElementById("main2").style.display = "none";
	document.getElementById("slide").style.display = "block";
	show_slideshow = true;
}


function page_up() {
	window.scroll(0,0);
	if (document.pageform.page.selectedIndex < document.pageform.page.length-1) {
		document.pageform.page.selectedIndex++;
		change_page();
	}
}

function page_down() {
	window.scroll(0,0);
	if (document.pageform.page.selectedIndex > 0) {
		document.pageform.page.selectedIndex--;
		change_page();
	}
}

function change_page() {
	page = document.pageform.page.selectedIndex + 1;
	document.getElementById("current_page").innerHTML = page;
	refresh();
}


function old_refresh() {
	if (imageinfo.length == 0) {
		return 0;
	}
	for (i = first_image; i <= last_image; i++) {
		document.getElementById("t"+imageinfo[i].id).style.display = "none";
		document.getElementById("d"+imageinfo[i].id).style.display = "none";
	}
	for (i = first_image; i <= last_image; i++) {
		if (imageinfo[i].show && imageinfo[i].page == page) {
			document.getElementById(display+imageinfo[i].id).style.display = "block";
		}
	}
}

function add_image_to(index, lightbox_ser, on, off) {
	var id;
	var lightbox_id;
	var lightbox_name;
//	id = imageinfo[(page-1)*page_size+index].imagenum;
	id = revlookup[index];
	lightbox_id = lightbox_array[lightbox_ser];
	lightbox_name = lightbox_name_array[lightbox_ser];
	image = eval("image = document.lb_image_"+lightbox_id);
	image.src = "/ximagi/download/lightboxcount.png?act=add_image_to_lightbox&lightbox_name="+escape(lightbox_name)+"&lightbox_id="+lightbox_id+"&image_id[]="+id+"&rand="+Math.random()
	show('lightboxesnav', on, off);
}

function record_checkbox(index, target) {
	var id;
//	id = imageinfo[(page-1)*page_size+index].imagenum;
	id = revlookup[index];
	if (target == "t") {
		document.mainform.elements['dimage_id[]'][index].checked=document.mainform.elements['timage_id[]'][index].checked;
	} else {
		document.mainform.elements['timage_id[]'][index].checked=document.mainform.elements['dimage_id[]'][index].checked;
	}
	if (document.mainform.elements['timage_id[]'][index].checked) {
		document.mainform.elements['id[]'][index].value = "1:"+id;
		imageinfo[(page-1)*page_size+index].selected = true;
		num_selected++;
	} else {
		document.mainform.elements['id[]'][index].value = "0:"+id;
		imageinfo[(page-1)*page_size+index].selected = false;
		num_selected--;
	}
	document.getElementById('selected_total').innerHTML = "";
	document.getElementById('selected_total').innerHTML = num_selected;
}

function toggle_slideshow() {
	window.scroll(0,0);
	if (show_slideshow) {
		if (display == "d") {
			show_detail();
		} else {
			show_gallery();
		}
	} else {
		show_large();
	}
}


function get_info() {
	if (window.opener) {
		return window.opener.imageinfo;
	} else {
		return imageinfo;
	}
}

function toggle_speed() {
	if (speed == 4000) {
		speed = 2000;
		document.getElementById("setting").innerHTML = "fast";
	} else if (speed == 2000) {
		speed = 6000;
		document.getElementById("setting").innerHTML = "slow";
	} else {
		speed = 4000;
		document.getElementById("setting").innerHTML = "med.";
	}
}

function slideshow() {
	var img = new Image();
	var src;
	var info;
//	info = get_info();
	info = imageinfo;
	src = info[q].preview;
	if (img.onload) {
		img.onload = slideshowload(src);
	} else {
//		intv = setTimeout("slideshowload(get_info()[q].preview)", 1000);
		intv = setTimeout("slideshowload(imageinfo[q].preview)", 1000);
	}
	img.src = src;
}

function slideshowload(src) {
	document.getElementById('preview_wrap').style.display = "block";
	var precache1;
	var precache2;
	var precache3;
	var precacheidx1;
	var precacheidx2;
	var precacheidx3;
	var info;
//	info = get_info();
	info = imageinfo;
	if (firsttime == true) {
		firsttime = false;
		return;
	}
	document.preview.src = src;
	document.getElementById("headline").innerHTML = info[q].headline;
	document.getElementById("date").innerHTML = info[q].date;
	document.getElementById("photographer").innerHTML = info[q].photographer;
//	document.getElementById("imagenum").innerHTML = info[q].imagenum;
	document.getElementById("file").innerHTML = info[q].file;
	document.getElementById("caption").innerHTML = info[q].caption;
	document.getElementById("inum").innerHTML = (q+1)+"";
	if (document.getElementById("folder")) {
		document.getElementById("folder").innerHTML = "<a class=\"slide_folder\" href=\"javascript:add_image_to("+q+",1,'','')\">1</a>, <a class=\"slide_folder\" href=\"javascript:add_image_to("+q+",2,'','')\">2</a>, <a class=\"slide_folder\" href=\"javascript:add_image_to("+q+",3,'','')\">3</a>, <a class=\"slide_folder\" href=\"javascript:add_image_to("+q+",4,'','')\">4</a>, <a class=\"slide_folder\" href=\"javascript:add_image_to("+q+",5,'','')\">5</a>";
	}
	if (state == "stop") {
		return;
	}
	last = q;
        q++;
	while (q < info.length && (!info[q].show)) {
		q++;
	}
        if (q >= info.length) {
                q = 0;
		while (q < last && (!info[q].show)) {
			q++;
		}
        }
	precacheidx1 = q + 1;
	if (precacheidx1 >= info.length) {
		precacheidx1 = precacheidx1 - info.length;
	}
	precacheidx2 = q + 2;
	if (precacheidx2 >= info.length) {
		precacheidx2 = precacheidx2 - info.length;
	}
	precacheidx3 = q + 3;
	if (precacheidx3 >= info.length) {
		precacheidx3 = precacheidx3 - info.length;
	}
	precache1 = new Image();
	precache2 = new Image();
	precache3 = new Image();
	precache1.src = info[precacheidx1].preview;
	precache2.src = info[precacheidx2].preview;
	precache3.src = info[precacheidx3].preview;
	clearInterval(intv);
	intv = setTimeout("slideshow()", speed);
}

function next() {
	var info;
//	info = get_info();
	info = imageinfo;
	stop("next");
	last = q;
	q++;
	while (q < info.length && (!info[q].show)) {
		q++;
	}
        if (q >= info.length) {
                q = 0;
		while (q < last && (!info[q].show)) {
			q++;
		}
        }
	slideshow();
}
function prev() {
	var info;
//	info = get_info();
	info = imageinfo;
	last = q;
	if (state == "play") {
		stop("prev");
		q--;
	}
	if (q == 0) {
		q = info.length;
	}
	q--;
	while (q > 0 && (!info[q].show)) {
		q--;
	}
	if (q <= 0) {
		q = info.length-1;
		while (q > last && (!info[q].show)) {
			q--;
		}
	}
	slideshow();
}

function toggle_play() {
	if (state == "stop") {
		play("play_stop");
	} else {
		stop("play_stop");
	}
}

function play(button) {
	state = "play";
	ps = "stop";
	if (button == "play_stop") {
		document.play_stop.src = "/img/ssstop_over.gif";
	} else {
		document.play_stop.src = "/img/ssstop_normal.gif";
	}
	slideshow();
}

function stop(button) {
	clearInterval(intv);
	state = "stop";
	ps = "play";
	if (button == "play_stop") {
		document.play_stop.src = "/img/ssplay_over.gif";
	} else {
		document.play_stop.src = "/img/ssplay_normal.gif";
	}
}
		
function pop_up(r) {

	document.getElementById('preview_wrap').style.display = "none";
	q = r;
	toggle_slideshow();
	slideshow();
//	w = window.open("popup.php", "preview", "width=855,height=658");
//	w.focus();
}


function download_multi(id) {
	var d;
	d = window.open("/ximagi/download/download_multi.php?id="+id, "download_multi", "width=450,height=150");
}


function download(id) {
	var d;
//	d = window.open("/ximagi/download/download.php?id="+id, "download", "width=334,height=217");
	d = window.open("/ximagi/download/download.php?id="+imageinfo[id].imagenum, "download", "width=334,height=217");
//	d = window.open("/hirez/"+imageinfo[id].file, "download", "width=334,height=217");
}


function init() {
	var i;
	var info;
//	info = get_info();
	info = imageinfo;
	intv = setTimeout("slideshow()", 50);
	document.getElementById("itotal").innerHTML = info.length;
	slideshow();
}
