
	
	
	    function TileToQuadKey ( x, y, zoom){ 
	    	var quad = ""; 
	    	for (var i = zoom; i > 0; i--){
				var mask = 1 << (i - 1); 
				var cell = 0; 
				if ((x & mask) != 0) 
	    			cell++; 
				
	    		if ((y & mask) != 0) 
	    			cell += 2; 
				
	    		quad += cell; 
	    	} 
	    	return quad; 
	    }

		function doDrawCircle(center, radius, color) {
		
		   var circleRadius = radius;
		   var bounds		= new GLatLngBounds();
		   var circleUnits	= "KM";
		   var circlePoints	= Array();
		
		   with (Math) {
		   	if (circleUnits == 'KM') {
		   		var d = circleRadius/6378.8;
		   	} else {
		   		var d = circleRadius/3963.189;
		   	}
		   
		   	var lat1 = (PI/180)* center.lat();
		   	var lng1 = (PI/180)* center.lng();
		
		   	for (var a = 0 ; a < 18 ; a++ ) {
		   		var tc = (PI/180)*(a*20);
		   		var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
		   		var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
		   		var x = ((lng1-dlng+PI) % (2*PI)) - PI;
		   		var point = new GLatLng(parseFloat(y*(180/PI)),parseFloat(x*(180/PI)));
		   		circlePoints.push(point);
		   		bounds.extend(point);
		   	}
		
		   	if (d < 1.5678565720686044) {
		   		circle = new GPolygon(circlePoints, color, 0, 1, color, 0.50);	
		   	} else {
		   		circle = new GPolygon(circlePoints, color, 0, 1, color, 0.50);	
		   	}
		   	map.addOverlay(circle); 
		   }
		}
	
	    function onLoad(movie_id, div) {
	    	map = new GMap2(document.getElementById(div), {backgroundColor: '#000000'});
	    	map.disableInfoWindow();
	    	map.setCenter(new GLatLng(46.53678309776156, 6.585230827331543),15);
	    	
	    	var caveTiles = function (a,b) { 
	    		return  "http://mapdata.walking-the-edit-data.net/" + TileToQuadKey(a.x,a.y,b) + ".png";  
	    	};
	
	    	var caveLayer = new GTileLayer(new GCopyrightCollection(''),13,25); 
	
	    	caveLayer.getTileUrl = caveTiles;
	    	caveLayer.getCopyright = function(a,b) {return "";}; 
	    	caveLayer.isPng = function() {return true;};
	
	    	var caveMap = new GMapType([caveLayer], G_SATELLITE_MAP.getProjection(), "Walking the Edit",{errorMessage:""}); 
	    	caveMap.getTextColor = function() {return "#FFFFFF";}; 
	
	    	map.addMapType(caveMap);
	    	map.setMapType(caveMap);
	
	    	geoXml = new GGeoXml("http://www.walking-the-edit.net/api/kml.php?movie_id="+movie_id);
	    	map.addOverlay(geoXml);
	    	geoXml.gotoDefaultViewport(map);
	    	
	    	return geoXml;
	    }
	    function publish(id) {
	var opt  = {
	    method: 'post',
	    postBody: 'state=0&lang='+lng,
	    onSuccess: function(t) {
			var data = t.responseText;
			data = data.split("##");		
			showPopup(data[0], data[1]+"<br/>&nbsp;<br/><input type='button' value='Annuler' onclick='hidePopup();' /> <input type='button' value='Publier' onclick='doPublish("+id+");' />");
		}
	}
	new Ajax.Request('/ajax/publish.php', opt);
}

function doPublish(id) {
	var opt  = {
	    method: 'post',
	    postBody: 'state=1&id='+id+'&lang='+lng+'&name='+$('movieName').value,
	    onSuccess: function(t) {
			var data = t.responseText;
			data = data.split("##");		
			showPopup(data[0], data[1]+"<br/>&nbsp;<br/><input type='button' value='Ok' onclick='hidePopup();' />");
		}
	}
	new Ajax.Request('/ajax/publish.php', opt);

}

function showPopup(title, content) {
	$('popupBackground').show();
	$('popup').show();

	$('popupTitle').update(title);
	$('popupContent').update(content);

	var viewport = document.viewport.getDimensions();
	var element  = $('popup').getDimensions();
	
	$('popup').style.left = (viewport.width/2-element.width/2)+"px";
	$('popup').style.top  = (viewport.height/2-element.height/2)+"px";
}

function hidePopup() {
	$('popupBackground').hide();
	$('popup').hide();
}

function leaveComment() {
	$('commentForm').toggle();
	
	if ($('commentForm').visible()) {
		$('dec').style.height = '170px';
	} else {
		$('dec').style.height = '0px';	
	}
}