$(document).ready(
/* start jquery */

	function(){
	
	montage=window.location.href.split("/"); 
	simple=montage.length-1; 
	//alert(montage[simple]);
	for(var i in montage) 
	{
    //alert(montage[i]);
    	if(montage[i]=="publicProfile")
    	{
    		var url = "http://www.topcampings.com/social/index.php/user/single_json_data/"+montage[simple];
    		var mode = "simple";
    		break;
    	}
    	else if(montage[i]=="locationData" || montage[i]=="eventData")
    	{
    		var url = "http://www.topcampings.com/social/index.php/location/single_json_data/"+montage[simple];
    		//alert(url);
    		var mode = "simple";
    		break;
    	}
    	else
    	{
    		var url = "http://www.topcampings.com/social/index.php/user/json_data";
    		var mode = "complete";
    		//break;
    	}
    	
    	
	}
	//alert(url);
	/* prettyphoto */
	$("a.gallery").prettyPhoto({theme:'facebook'});
	//$(".gallery").css("border", "red");
	/* prettyphoto */
	
	$("#eventFrom").datepicker({ dateFormat: 'dd-mm-yy' });
	$("#eventTo").datepicker({ dateFormat: 'dd-mm-yy' });
	//$("#eventTo").datepicker("option", "altFormat", 'dd-mm-yy');
	//$("#datepicker").datepicker();
	
	/* search people */
	$("#searchName").click(function(){
		var profileName = $("#firstSearchName").val();
		var profilePlace = $("#searchLocation").val();
		$.ajax({
 			url: "http://www.topcampings.com/social/index.php/utilities/jsonSearchName",
 			data: "name="+profileName+"&place="+profilePlace,
 			type: "POST",
 			dataType: 'json',
  			success: function(data){
  			//alert(data);
  				var peoples = data.peoples;
  				var pplLenght = (peoples.length)-1;
  				var matrixLat = (data.peoples[0]["latitude"]+data.peoples[pplLenght]["latitude"])/2;
  				var matrixLon = (data.peoples[0]["longitude"]+data.peoples[pplLenght]["longitude"])/2;
  				//alert(matrixLat+" "+matrixLon);
  				var map = new Map(document.getElementById("map_canvas"));
  				map.centreWithZoom(matrixLat,matrixLon,10);
  				map.addControls();
  				for(var ppl in peoples)
  				{ 
					//alert(peoples[ppl]["latitude"]+" "+peoples[ppl]["longitude"]+" "+peoples[ppl]["html"]);
					map.addSimpleMarker(peoples[ppl]["latitude"],peoples[ppl]["longitude"],peoples[ppl]["name"],"http://x:8888/frontend2/images/pin.png");
				}
				
  			}
  		});	
	});
	/* searchpeople */
	
	/* search obj  */
	$("#searchLocationButton").click(function(){
		var locationCategory = $("#searchLocationCategory").val();
		var locationPlace = $("#locationPlace").val();
		$.ajax({
 			url: "http://www.topcampings.com/social/index.php/utilities/jsonSearchLocation",
 			data: "category="+locationCategory+"&place="+locationPlace,
 			type: "POST",
 			dataType: 'json',
  			success: function(data){
  			
  			$(".log").html(data);
  				var locations = data.locations;
  				var pplLenght = (locations.length)-1;
  				var matrixLat = (data.locations[0]["latitude"]+data.locations[pplLenght]["latitude"])/2;
  				var matrixLon = (data.locations[0]["longitude"]+data.locations[pplLenght]["longitude"])/2;
  				//alert(matrixLat+" "+matrixLon);
  				var map = new Map(document.getElementById("map_canvas"));
  				/* map.centreWithZoom(matrixLat,matrixLon,10); */
  				map.centreWithZoom(data.locations[1]["latitude"],data.locations[1]["longitude"],10);
  				map.addControls();
  				for(var loc in locations)
  				{ 
  				//alert(locations[loc]["icon"]);
  				var icon = locations[loc]["icon"];
					//alert(locations[loc]["latitude"]+" "+locations[loc]["longitude"]+" "+locations[loc]["title"]);
					map.addSimpleMarker(locations[loc]["latitude"],locations[loc]["longitude"],locations[loc]["title"],"http://x:8888/frontend2/images/pin.png");
				}
				
  			}
  		});	

	});
	/* search obj */
	
	/* search event  */
	$("#eventSearch").click(function(){
		var eventFrom = $("#eventFrom").val();
		var eventTo = $("#eventTo").val();
		var eventPlace = $("#eventLocation").val();
		//alert(eventFrom+" "+eventTo+" "+eventPlace);
		$.ajax({
 			url: "http://www.topcampings.com/social/index.php/utilities/jsonSearchEvent",
 			data: "from="+eventFrom+"&to="+eventTo+"&place="+eventPlace,
 			type: "POST",
 			dataType: 'json',
  			success: function(data){
  			//alert(data);
  			$(".log").html(data);
  				var locations = data.locations;
  				var pplLenght = (locations.length)-1;
  				var matrixLat = (data.locations[0]["latitude"]+data.locations[pplLenght]["latitude"])/2;
  				var matrixLon = (data.locations[0]["longitude"]+data.locations[pplLenght]["longitude"])/2;
  				//alert(matrixLat+" "+matrixLon);
  				var map = new Map(document.getElementById("map_canvas"));
  				if(locations.length<5)
  				{
  					var zoomLevel = 15;
  				}else{
  					var zoomLevel = 10;
  				}
  				map.centreWithZoom(data.locations[0]["latitude"],data.locations[0]["longitude"],zoomLevel);
  				map.addControls();
  				for(var loc in locations)
  				{ 
  				var icon = locations[loc]["icon"];
					//alert(locations[loc]["latitude"]+" "+locations[loc]["longitude"]+" "+locations[loc]["title"]);
					map.addSimpleMarker(locations[loc]["latitude"],locations[loc]["longitude"],locations[loc]["title"],"http://x:8888/frontend2/"+icon);
				}
				
  			}
  		});	

	});
	/* search event */
	
	/* JSON calling for map */
	$.getJSON(url, function(data) {
    	var latitude = data.coordinates[0]["latitude"];
    	var longitude = data.coordinates[0]["longitude"];
    	var zoomLevel = data.coordinates[0]["zoomLevel"];
    	var htmlMessage = data.coordinates[0]["html"];
    	var icon = data.coordinates[0]["icon"];
    	//alert(icon);
    	
    	var map = new Map(document.getElementById("map_canvas"));
		
		map.addMarker(latitude,longitude, htmlMessage, "http://www.topcampings.com/social/frontend2/"+icon);
		if(mode=="complete"){
		var locations = data.myLocations;
    	//alert(locations.length);
		for(var locate in locations) {  
			//document.write(locate + ' Ð ' + locations[locate] + '');
			//alert(locate + '-' + locations[locate]["title"] + '');
			var icon = locations[locate]["icon"];
			//alert(icon);
			map.addMarker(locations[locate]["latitude"],locations[locate]["longitude"], locations[locate]["title"],"http://x:8888/frontend2/"+icon);
		}
		}
		map.addControls();
		map.centreWithZoom(latitude,longitude,zoomLevel);
	});
	/* JSON calling for map */
	
	/* weather forecast */
	$.ajax({
 		url: "http://www.topcampings.com/social/index.php/utilities/proxy",
 		data: "url=http://xoap.weather.com/search/search?where="+$("#citycountry").val(),
 		type: "POST",
 		dataType: 'xml',
  		success: function(data){
  			$(data).find('search').each(function(){
  				var code = $(this).find('loc').attr('id');
  				$('#test').weatherfeed([code]);
  			});
  		}
	});
	/* weather forecast */
	
	/* geocoder service */
	$("#city, #adress, #zip, #country").bind("change", function(){
		var city = $("#city").val()+",";
		var address = $("#adress").val()+",";
		var zip = $("#zip").val()+",";
		var country = $("#country").val()+",";
		var geourl = city+address+zip+country;
	var geocoder = new Coder(document.getElementById("map_canvas"));
	geocoder.locate(geourl);
	});
	/* geocoder service */
	
	/* geocoder json */
	
	//var geocoder = new Coder(document.getElementById("map_canvas"));
	//geocoder.getLocations("verona,it");
	
	/* geocoder json */
	
	/* geo rss */
	
	//var geo_xml = new geoRSS();
	//geo_xml.geoLocate("verona,it");
	
	/* geo rss */
	
	/* direction */
	
	//var map = new Map(document.getElementById("map_canvas"));
	//map.centreWithZoom(49.496675,-102.65625,13);
	//map.getDirections(G_TRAVEL_MODE_WALKING, "direction");
	
	/* direction */

/* end jquery */		
});
