var map, icon, icon2, pharmacies, mode, erasable;

function init() {
	mode="normal";
	erasable=true;
	$('#pharmacies-find .red_button_small').css('display','none');
	$('#pharmacies-find input').get(0).value = 'Introduisez ici le code postal ou la ville';
	
	map.clearOverlays();
	map.setCenter(new GLatLng(50.279419, 4.793670), 8);
	
	jQuery.each(pharmacies, function() {
		var link = $(this).find('h4 a').attr('href');
		var name = $(this).find('h4 a').text();
		var street_address = $(this).find('.street-address').text();
		var postal_code = $(this).find('.postal-code').text();
		var locality = $(this).find('.locality').text();
		var country_name = $(this).find('.country-name').text();
		var latitude = $(this).find('.latitude').text();
		var longitude = $(this).find('.longitude').text();
		
		var image_link = 'epc-familia/_img/pharmacies/'+link.substr(16,link.length-17)+'.jpg';
		
		var point = new GLatLng(latitude, longitude);
		var marker = new GMarker(point, icon);
		
		map.addOverlay(marker);
		GEvent.addListener(marker,"click", function() {
			map.openInfoWindowHtml(marker.getLatLng(),'<div style="width:295px;height:300px;"><p><strong>'+name+'</strong></p><p><img src="'+image_link+'" /></p><p>'+street_address+'<br />'+postal_code+' '+locality+'</p><p><a href="'+link+'" class="red_button red_button_small"><span>Plus d\'info</span></a></p></div>',{pixelOffset: new GSize(0,-28)});
		});
	});
}

function search() {
	mode="search";
	erasable=false;
	$('.find .red_button_small').css('display','block');
	
	var location = $('#pharmacies-find input').get(0).value;

	$.getJSON('/pharmacies/lookupaddress?location='+location+', Belgium', function(result) {
					
		map.clearOverlays();
		map.setCenter(new GLatLng(50.279419, 4.793670), 8);
		
		var c_point1 = new GLatLng(result.data.closest1.latitude, result.data.closest1.longitude);
		var c_marker1 = new GMarker(c_point1, icon);
		
		var image_link1 = 'epc-familia/_img/pharmacies/'+result.data.closest1.link.substr(16,result.data.closest1.link.length-17)+'.jpg';
		
		map.addOverlay(c_marker1);
		GEvent.addListener(c_marker1,"click", function() {
			map.openInfoWindowHtml(c_point1,'<div style="width:295px;height:300px;"><p><strong>'+result.data.closest1.pharmacy.PharmacyName+'</strong></p><p><img src="'+image_link1+'" /></p><p>'+result.data.closest1.pharmacy.Address+', '+result.data.closest1.pharmacy.Number+'<br />'+result.data.closest1.pharmacy.PostCode+' '+result.data.closest1.pharmacy.Town+'</p><p><a href="'+result.data.closest1.link+'" class="red_button red_button_small"><span>Plus d\'info</span></a></p></div>',{pixelOffset: new GSize(0,-28)});
		});
		
		var c_point2 = new GLatLng(result.data.closest2.latitude, result.data.closest2.longitude);
		var c_marker2 = new GMarker(c_point2, icon);
		
		var image_link2 = 'epc-familia/_img/pharmacies/'+result.data.closest2.link.substr(16,result.data.closest2.link.length-17)+'.jpg';
		
		map.addOverlay(c_marker2);
		GEvent.addListener(c_marker2,"click", function() {
			map.openInfoWindowHtml(c_point2,'<div style="width:295px;height:300px;"><p><strong>'+result.data.closest2.pharmacy.PharmacyName+'</strong></p><p><img src="'+image_link2+'" /></p><p>'+result.data.closest2.pharmacy.Address+', '+result.data.closest2.pharmacy.Number+'<br />'+result.data.closest2.pharmacy.PostCode+' '+result.data.closest2.pharmacy.Town+'</p><p><a href="'+result.data.closest2.link+'" class="red_button red_button_small"><span>Plus d\'info</span></a></p></div>',{pixelOffset: new GSize(0,-28)});
		});
		
		var c_point3 = new GLatLng(result.data.closest3.latitude, result.data.closest3.longitude);
		var c_marker3 = new GMarker(c_point3, icon);
		
		var image_link3 = 'epc-familia/_img/pharmacies/'+result.data.closest3.link.substr(16,result.data.closest3.link.length-17)+'.jpg';
		
		map.addOverlay(c_marker3);
		GEvent.addListener(c_marker3,"click", function() {
			map.openInfoWindowHtml(c_point3,'<div style="width:295px;height:300px;"><p><strong>'+result.data.closest3.pharmacy.PharmacyName+'</strong></p><p><img src="'+image_link3+'" /></p><p>'+result.data.closest3.pharmacy.Address+', '+result.data.closest3.pharmacy.Number+'<br />'+result.data.closest3.pharmacy.PostCode+' '+result.data.closest3.pharmacy.Town+'</p><p><a href="'+result.data.closest3.link+'" class="red_button red_button_small"><span>Plus d\'info</span></a></p></div>',{pixelOffset: new GSize(0,-28)});
		});
		
		var r_point = new GLatLng(result.data.requested.latitude, result.data.requested.longitude);
		var r_marker = new GMarker(r_point, icon2);
		
		map.addOverlay(r_marker);
		GEvent.addListener(r_marker,"click", function() {
			map.openInfoWindowHtml(r_marker.getLatLng(),'<div style="width:150px;"><p><strong>Votre recherche :</strong></p><p>'+location+'</p>',{pixelOffset: new GSize(0,-12)});
		});
		
		var bounds = new GLatLngBounds();
		bounds.extend(c_point1);
		bounds.extend(c_point2);
		bounds.extend(c_point3);
		bounds.extend(r_point);
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	});
	
	$.scrollTo('#map', 500);
}

$(document).ready(function(){
	
	pharmacies = $('#map-pharmacies li');
	
	if (GBrowserIsCompatible()) {
		
		map = new GMap2(document.getElementById("map"));
		map.setMapType(G_PHYSICAL_MAP);
		map.addControl(new GLargeMapControl());
		map.enableScrollWheelZoom();
	
		icon = new GIcon();
		icon.image = "/epc-familia/_img/icon.png";
		icon.iconSize = new GSize(38, 38);
		icon.iconAnchor = new GPoint(18, 32);
		
		icon2 = new GIcon();
		icon2.image = "/epc-familia/_img/icon2.png";
		icon2.iconSize = new GSize(13, 12);
		icon2.iconAnchor = new GPoint(5, 5);
		
		// init or search?
		
		if (window.location.search.substr(0,3) === "?q=") {
			$('#pharmacies-find input').get(0).value = decodeURIComponent(window.location.search.substr(3,window.location.search.length-3));
			search();
		}
		else {
			init();
		}
		
		// events
		
		$('#pharmacies-find input').focus(function() {
			if (erasable) {
				this.value = '';
			}
		});
		
		$('.province').mouseover(function() {
			$(this).css('color','#8DEB5B');
		});
		
		$('.province').mouseout(function() {
			$(this).css('color','#5e5e5e');
		});
		
		$('.province').click(function() {
			if (mode=="search") {
				init();
			}
			$.scrollTo('#map', 500);
			var latitude = $(this).find('.latitude').text();
			var longitude = $(this).find('.longitude').text();
			map.setCenter(new GLatLng(latitude, longitude), 10);
		});
		
		$('#pharmacies-find form').submit(function() {
			search();
			return false;
		});
		
		$('#pharmacies-find form .red_button').click(function() {
			$('#pharmacies-find form').submit();
			return false;
		});
		
		$('#pharmacies-find .red_button_small').click(function() {
			init();
			return false;
		})
		
	}
	
});
