function launch_gallery(_xmldoc, url) {
	init_gallery(_xmldoc, url);
	set_photo(0);
}

function launch_gallery_cached(_xmldoc, url) {
	init_gallery(_xmldoc, url);
	window.image_cache = new Array();
	for(var i = 0; i < window.photos.length; i++) {
		var image = new Image();
		set_photo(i, 381, 0, image);
		window.image_cache.push(image);
	}
	window.photo_number = 0;
}

function init_gallery(_xmldoc, url) {
	window.xmldoc = _xmldoc;

	var hed = window.xmldoc.getElementsByTagName("gallery")[0];
	if(hed != undefined) {
		document.title = hed.getAttribute("hed");
	}
	window.photos = window.xmldoc.getElementsByTagName("photo");
	window.back = document.getElementById("galleryBack");
	window.back_icon = document.getElementById("galleryBackIcon");
	window.photoCount = document.getElementById("galleryCount");
	window.photoCurrent = document.getElementById("galleryCurrent");
	window.forward = document.getElementById("galleryForward");
	window.forward_icon = document.getElementById("galleryForwardIcon");
}

function set_photo(photo_number, maxW, border, image) {
	if(photo_number < 0) {
		photo_number = window.photos.length-1;
	}
	if(photo_number >= window.photos.length) {
		photo_number = 0;
	}
	if(image == undefined) {
		image = document.getElementById("galleryImage");
	}
	window.photo_number = photo_number;
	image.src = "/graphics/gallery_loading.gif";
	var photo = window.photos[photo_number];
	set_gallery_item("galleryCutline", "cutline", photo);
	set_gallery_item("galleryCredit", "credit", photo);
	var image_url = photo.getAttribute("src");
	if(maxW == undefined) {
		image_url += "&MaxW=380&MaxH=380"; 
	} else {
		image_url += "&MaxW=" + maxW;
	}
	if(maxW != undefined) {
		image_url += "&Border=" + border;
	}
	image.src = image_url;
	var for_sale = photo.getAttribute("for_sale");
	var order_photo = document.getElementById("galleryOrderPhoto");
	kill_children(order_photo);
	var photo_title = photo.getElementsByTagName("title")[0];
	if(for_sale == "1" && photo_title.lastChild != undefined) {
		var hed = window.xmldoc.getElementsByTagName("gallery")[0].getAttribute("hed");
		var article_url = window.xmldoc.getElementsByTagName("gallery")[0].getAttribute("url");
		var order_photo_url = document.createElement("a");
		order_photo_url.setAttribute("target", "_mycapture");
		order_photo_url.setAttribute("href", "http://bendbulletin.mycapture.com/mycapture/lookup.asp?originalname=" + photo_title.lastChild.data + "&backurl=" + article_url + "&backtext=" + hed);
		order_photo_url.appendChild(document.createTextNode("ORDER PHOTO"));
		order_photo.appendChild(order_photo_url);
	}
	set_navigation(photo_number);
}

function set_gallery_item(gallery_item_name, xml_element_name, photo) {
	var gallery_item = document.getElementById(gallery_item_name);
	kill_children(gallery_item);
	if(photo != null) {
		var photo_item = photo.getElementsByTagName(xml_element_name)[0];
		if(gallery_item != undefined && photo_item.lastChild != null) {
			gallery_item.appendChild(document.createTextNode(photo_item.lastChild.data));
		}
	}
}

function kill_children(element) {
	if(element == null) { return; }
	var kids = element.childNodes;
	var numkids = kids.length;
	for(var i = 0; i < numkids; i++) {
		element.removeChild(kids[i]);
	}
}

function set_navigation(photo_number) {
	if(window.back_icon == undefined || window.forward_icon == undefined) { return; }
	kill_children(window.photoCount);
	kill_children(window.photoCurrent);

	if(photo_number < 0) {
		photo_number = 0;
	}

	if(photo_number > window.photos.length) {
		photo_number = window.photos.length;
	}
	if(photo_number) {
		window.back_icon.setAttribute("src", "/graphics/bulletin-gallery-arrow-left.gif");
                window.back.onclick = function() { return flip(photo_number-1); };
	} else {
		window.back_icon.setAttribute("src", "/graphics/bulletin-gallery-arrow-leftG.gif");
	}

	if(photo_number < window.photos.length-1) {
		window.forward_icon.setAttribute("src", "/graphics/bulletin-gallery-arrow-right.gif");
                window.forward.onclick = function() { return flip(photo_number+1); };
	} else {
		window.forward_icon.setAttribute("src", "/graphics/bulletin-gallery-arrow-rightG.gif");
	}

	var photo_current = document.createTextNode(photo_number+1);
	window.photoCurrent.parentNode.replaceChild(photo_current, window.photoCurrent);
	window.photoCurrent = photo_current;

	var photo_count = document.createTextNode(window.photos.length);
	window.photoCount.parentNode.replaceChild(photo_count, window.photoCount);
	window.photoCount = photo_count;
}

function flip(photo_number) {
	set_photo(photo_number);
	return false;
}

function prev_image(maxW, border) {
	set_photo(window.photo_number-1, maxW, border);
}

function next_image(maxW, border) {
	set_photo(window.photo_number+1, maxW, border);
}


