$.noConflict();
jQuery(document).ready(function($) {

	$('span.region-title').live('click', function(event){
		$('#searchByRegion2 .cities-stores-container').slideUp();
		$(this).next('div.cities-stores-container').slideToggle();

		var region_stores =	$(this).next().next().html();

		var x = $.parseJSON(region_stores);

		$("#map-stores").goMap({
			markers: x,
			zoom: 8,
			scaleControl: true, 
			maptype: 'ROADMAP'
		});

		event.preventDefault();
	});

	$('a.store-marker').click(function(event) {
		var url = $(this).attr("href");

		//alert(url);

		var latitude = getUrlVars(url)["latitude"];
		var longitude = getUrlVars(url)["longitude"];
		var html = getUrlVars(url)["html"];

		$("#map-stores").goMap({
			markers: [{  
				latitude: latitude, 
				longitude: longitude, 
				html: {
					content: html, 
					popup: true 
				}
			}],
			zoom: 16,
			scaleControl: true, 
			maptype: 'ROADMAP'
		});

		event.preventDefault();
	});

	function getUrlVars(url) {
    var vars = {};
    var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
	}

	/** Visualizza sulla mappa tutti i Markers inseriti */
	$("#map-stores").goMap({
		latitude: 42.304503, 
    longitude:12.641895, 
    zoom: 6,
		scaleControl: true, 
    maptype: 'ROADMAP' 
	});

	$('form#searchByCAP').submit(function(){
		var address = $.trim($('#capText').val());
		var distance = $('input[name="distance"]:checked').val();

		//alert(distance);

		if(address == '' || address.match(/[^\d]/)) {
			alert('Inserire un CAP valido');
		} else {
			//---
			geocoder = new google.maps.Geocoder();
			var l;

			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					l = getLatLng(results[0].geometry.location);

					/**
					 *@desc Chiamata Ajax per ricerca trigonometrica
					 *@val1 l.lat
					 *@val2 l.lng
					 */
					$.post('/stores-markers.php', {latitude: l.lat, longitude: l.lng, kilometers: distance}, function(data){
						//alert("Data Loaded: " + data);
						if(data == '')
						{
							alert('Nessun risultato per il CAP inserito.');
						} else {

							$("#map-stores").goMap({
								markers: data,
								zoom: 8,
								scaleControl: true, 
								maptype: 'ROADMAP' 
								});
						}
					},'json');
				} else {
					alert('Nessun risultato per il CAP inserito.');
				}

			});

			//---
		}
		//event.preventDefault();
		return false;
	});

/*
	$.getJSON('/stores-markers.php', function(storesMarkers){
		$("#map-stores").goMap({
			 markers: storesMarkers
		});
	});
*/

});

function geocodeAddress(address) {

}

function getLatLng(arrLocation) {
	var i = 0;
	for(var k in arrLocation) {
		switch (i) {
			case 0:
				var locationLatitude = arrLocation[k];
				break;
			case 1:
				var locationLongitude = arrLocation[k];
				break;
		}
		i++;
	}
	return { 'lat' : locationLatitude, 'lng' : locationLongitude };
}
