var photoLayer;

// Prepares a call to theserver
getImages = function() {
	document.getElementById("wait").style.backgroundColor="#F55";
	document.getElementById("wait").style.color="#000";

	var boundingBox="";
	boundingBox += map.getSouthWest().lon + "," + map.getSouthEast().lat;
	boundingBox += "," + map.getSouthEast().lon + "," + map.getNorthWest().lat;
	
	getPois(boundingBox, document.getElementById("maxpoi").value, document.getElementById("smartresize").checked, document.getElementById("threshold").checked, document.getElementById("gg_check").checked, document.getElementById("mag_check").checked);
	
}


// Calls the server
getPois = function(bbox, maxpoi, smartresize, threshold, gg, mag) {
	if (document.getElementById("json")) {
		document.getElementById("json").innerHTML="";
	} else {
		var d = document.createElement('div');
		d.id="json";
		document.body.appendChild(d);
	}
	var s = document.createElement('script');
	s.src = "getpois.php?bbox="+bbox+"&maxpoi="+maxpoi+"&threshold="+threshold+"&smartresize="+smartresize+"&gg="+gg+"&mag="+mag;
	//s.src="bouchon.js"
	document.getElementById("log").innerHTML = "<a href='"+s.src+"'>JSON server response</a>";
	document.getElementById("json").appendChild(s);	
}

// Callback when the server sends back the POI search result
poiCallBack = function(pois) {
	if (photoLayer) {
		map.removeLayer(photoLayer);
	}
	if (pois.code != "ok"){
		alert("Error: "+pois.msg);
	}
	var myPoilist = new VMPOIList();
	for (var i=pois.list.length-1; i>=0; i--){
		var photo = pois.list[i];
		myPoi = new VMPOI();
		myPoi.coords = new VMLonLat(photo.lon,photo.lat);
		myPoi.name =  photo.name;
		if (photo.type=="TOUGV") {
			color="#009575";
			rating = (photo.rating!=0?" <img src='"+photo.rating+"star.gif'/>":"");
		} else {
			color = "#0084FF";
			rating = "";
		}
		infobubble = "<div id='"+photo.id+"_bubble' style='width:200px'>";
		infobubble += "<div style='color:"+color+";font-weight: bold'>"+photo.name+rating+"</div><div>"+photo.city+"</div>";
		infobubble += "<div id='"+photo.id+"_show'><a href='javascript:getPOI(\""+photo.type+"\",\""+photo.id+"\");document.getElementById(\""+photo.id+"_bubble\").style.width=\"500px\";void(0);'>Details</a></div>";
		infobubble += "<div style='display:none' id='"+photo.id+"_hide'><a href='javascript:document.getElementById(\""+photo.id+"_details\").innerHTML = \"\";document.getElementById(\""+photo.id+"_bubble\").style.width=\"200px\";document.getElementById(\""+photo.id+"_show\").style.display=\"block\";document.getElementById(\""+photo.id+"_hide\").style.display=\"none\";void(0);'>Hide details</a></div>";
		infobubble += "<div id='"+photo.id+"_details'/>";
		infobubble += "</div>";
		myPoi.setIconLayer(new VMIcon(photo.url,-photo.width/2,-photo.height/2), infobubble);
		myPoilist.addPoi(myPoi);
	}
	photoLayer = myPoilist.getLayer();
	map.addLayer(photoLayer);
	document.getElementById("wait").style.backgroundColor="#fff";
	document.getElementById("wait").style.color="#fff";
}

// Delay not to load POIs if the user drags the map again within 1s
function shortTimer() {
	clearTimeout();
	setTimeout("getImages();", 1000);
}

// Delay not to load POIs if the user drags the map again within 2s
function longTimer() {
	clearTimeout();
	setTimeout("getImages();", 2000);
}

function cancelTimer() {
	clearTimeout();
}

// Get POI details
function getPOI(type, poiid)
{
	if (type=="TOUGV")
		myPOIsearchbyid = new VMPOISearch(ggPoiDefinition);
	else if (type="52116")
	 	myPOIsearchbyid = new VMPOISearch(magGastroPoiDefinition);
	else if (type=="53116")
	 	myPOIsearchbyid = new VMPOISearch(magHebergPoiDefinition);
	myPOIsearchbyid.addEventHandler("onCallBack",showPOI);
	myPOIsearchbyid.getPoiById(poiid);
}

// Display POI details
function showPOI() {
	result = myPOIsearchbyid.result.VMPOIs[0];
	document.getElementById(result.id+'_show').style.display = "none";
	document.getElementById(result.id+'_hide').style.display = "block";
	details = "<div class='address'>"+result.displayAddress+"</div>";
	details += "<div class='tel'>"+result.telNumber+"</div>";
	if (result.webSite) {
		details += "<div class='website'><a href='"+result.webSite+"'>Web site</a></div>";
	}
	for (i=0;i< result.descriptions.length;i++) {
		details += "<div class='description'>"+result.descriptions[i]+"</div>";
	}
	photoList=[];
	for (i=0;i<result.photoURLs.length;i++) {
		if (result.photoURLs[i].length!=0) {
			if (result.type=="52116" || result.type=="53116") {
				// build Magazine photo URL
				photoList.push("http://www.viamichelin.fr/viamichelin/fra/tpl/tpl/"+result.photoURLs[i]);
			} else {
				photoList.push(result.photoURLs[i]);
			}
		}
		
	}
	if (photoList.length!=0) {
		setDiapoList(photoList);
		details += "<div class='photo' id='diaporama'><img src='"+photoList[0]+"' onclick='nextDiapo();'/></div>";
		if (photoList.length>1) {
			details += "<div class='photo' id='diaporama_tools'><a href='javascript:previousDiapo();'><img src='left_arrow.png'></a> <span id='diapoIndex'>1</span>/"+photoList.length+" <a href='javascript:nextDiapo();'><img src='right_arrow.png'></a></div>";
		}
	}
	document.getElementById(result.id+"_details").innerHTML = details;
}

// Sets the list of images for the current POI infobubble
function setDiapoList(param) {
	diapoList = param;
	diapoIndex = 0;
}

function nextDiapo() {
	diapoIndex = (diapoIndex + 1)%diapoList.length;
	document.getElementById("diapoIndex").innerHTML=diapoIndex+1;
	document.getElementById("diaporama").innerHTML = "<img src='"+diapoList[diapoIndex]+"' onclick = 'nextDiapo();'/>";
}

function previousDiapo() {
	diapoIndex = (diapoIndex +diapoList.length - 1)%diapoList.length;
	document.getElementById("diapoIndex").innerHTML=diapoIndex+1;
	document.getElementById("diaporama").innerHTML = "<img src='"+diapoList[diapoIndex]+"' onclick = 'nextDiapo();'/>";
}