function displayLoading(elementid){
	var element = document.getElementById(elementid);
	if(element.hasChildNodes()){
		while(element.hasChildNodes()){
			element.removeChild(element.lastChild);
		}
	}
	var image = document.createElement("img");
	image.setAttribute("src","images/" + ajaxLoadGif);
	image.setAttribute("alt","Loading...");
	element.appendChild(image);
}

function hideLoading(elementid){
	var element = document.getElementById(elementid);
	if(element.hasChildNodes()){
		while(element.hasChildNodes()){
			element.removeChild(element.lastChild);
		}
	}
}

function mapTagSearch(tags){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if((!document.getElementById("search_form"))||(!document.getElementById("t"))) return;
	var tagsearchform = document.getElementById("search_form");
	var tagsearchinput = document.getElementById("t");
	displayLoading("search_results_no");
	closePhotoInfo();
	tagsearchinput.value = tags;
	var datasource = "php/data.MapPeople.php?t=" + escape(tags) + "&c=" + escape(city);
	myMap.loadPointsAjax(datasource);
	return false;
}

function prepMapTagSearch(){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if(!document.getElementById("search_form")) return;
	var tagsearchform = document.getElementById("search_form");
	tagsearchform.searchtags = function(){
		var tags = document.getElementById("t").value;
		mapTagSearch(tags);
		return false;
	}
	tagsearchform.onsubmit = tagsearchform.searchtags;

	if(document.getElementById("cityselect")){
		var cityselect = document.getElementById("cityselect");
		cityselect.onchange = function(){
			city = cityselect.options[cityselect.selectedIndex].value;
			document.getElementById("t").value = "";
			document.getElementById("tag_city").innerHTML = city;
			tagsearchform.searchtags();
		}
	}
}

function prepPagination(){
	if(!document.getElementById || !document.getElementsByTagName) return;
	var next = document.createElement("a");
	next.setAttribute("id","next_link");
	next.setAttribute("title","2");
	var nexttext = document.createTextNode("Next");
	next.appendChild(nexttext);
	var thumb_gallery = elid("thumb_gallery_wrapper");
	thumb_gallery.appendChild(next);
}

function tagSearch(tags){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if((!document.getElementById("search_form"))||(!document.getElementById("t"))) return;
	var tagsearchform = document.getElementById("search_form");
	var tagsearchinput = document.getElementById("t");
	displayLoading("search_results_no");
	tagsearchinput.value = tags;
	var datasource = "php/data.MapPeople.php?t=" + escape(tags) + "&c=" + escape(city);
	myMap.loadPointsAjax(datasource);
	return false;
}

function prepTagSearch(){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if(!document.getElementById("search_form")) return;
	var tagsearchform = document.getElementById("search_form");
	tagsearchform.searchtags = function(){
		alert("search");
		var tags = document.getElementById("t").value;
		tagSearch(tags);
		return false;
	}
	tagsearchform.onsubmit = tagsearchform.searchtags;
	
	if(document.getElementById("cityselect")){
		var cityselect = document.getElementById("cityselect");
		cityselect.onchange = function(){
			city = cityselect.options[cityselect.selectedIndex].value;
			document.getElementById("t").value = "";
			document.getElementById("tag_city").innerHTML = city;
			tagsearchform.searchtags;
		}
	}	
			
}

function drawTagCloud(){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if(!document.getElementById("tagcloud_wrapper")) return;
	document.getElementById("tagcloud_wrapper").innerHTML = unescape(tagcloud);
}

function showPhotoInfo(id){
	var wrapper = document.createElement("div");
	wrapper.setAttribute("id","photo_wrapper");
	var photoinfo = myMap.allpoints[id].templates["photoInfo"].supplant(myMap.allpoints[id]);
	wrapper.innerHTML = photoinfo;
	wrapper.style.display = "block";
	myMap.getWrapper().appendChild(wrapper);
}

function closePhotoInfo(){
	if(document.getElementById("photo_wrapper")){
		var wrapper = document.getElementById("photo_wrapper");
		myMap.getWrapper().removeChild(wrapper);
	}
}

function showLargePhoto(id){
	showOverlay();
	var photoinfo = myMap.allpoints[id].templates["largeImageWindow"].supplant(myMap.allpoints[id]);
	var image_wrapper = document.createElement("div");
	image_wrapper.setAttribute("id","large_image_wrapper");
	image_wrapper.innerHTML = photoinfo;
	closebutton = document.createElement("div");
	closebutton.setAttribute("id","large_image_close");
	closebuttontext = document.createTextNode("close X");
	closebutton.appendChild(closebuttontext);
	changeCursor(closebutton,"pointer");
	closebutton.onclick = closeLargePhoto;
	image_wrapper.appendChild(closebutton);
	document.body.appendChild(image_wrapper);
	adjust();
	return false;
}

function closeLargePhoto(){
	hideOverlay();
	document.body.removeChild(elid("large_image_wrapper"));
}

function showOverlay(){
	var overlay = document.createElement("div");
	overlay.setAttribute("id","overlay");
	overlay.style.height = pageHeight() + "px";
	overlay.style.width = pageWidth() + "px";
	overlay.onclick = closeLargePhoto;
	document.body.appendChild(overlay);
	setOpacity(overlay,50);
	show(overlay);
}

function hideOverlay(){
	document.body.removeChild(elid("overlay"));
}

function adjust(){
	var obj = elid("large_image_wrapper");
	if(!obj) return false;
	var w = getWidth(obj);
	var h = getHeight(obj);
	var t = scroll_Y() + (windowHeight()/2) - (h/2);
	if(t<0) t = 0;
	var l = scroll_X() + (windowWidth()/2) - (w/2);
	var l = (windowWidth()/2) - (w/2);
	if(l<0) l=0;
	
	setY(obj, t);
	setX(obj, l);
}

function prepCitySelect(){
	if(!document.getElementById || !document.getElementsByTagName) return;
	if(document.getElementById("cityselect")){
		var cityselect = document.getElementById("cityselect");
		cityselect.onchange = function(){
			city = cityselect.options[cityselect.selectedIndex].value;
			var tagsearchform = document.getElementById("search_form");
			tagsearchform.submit();
			return false;
		}	
	}
}