var MYMAP = {
	bounds: null,
	map: null
}
$(document).ready(function(){
	var language = $('body').attr('class').split("language")[1].substr(0,1);
	var mapText = ["","map","carte","karte","mapa","mapa","mappa"][language];
	
	if($("#results li.results_unit.client").length ){
		
		$("#results").prepend("<div id='results_map'></div>");


		var infoWindow;
		MYMAP.init = function(selector, latLng, zoom) {
		  var myOptions = {
		    zoom:zoom,
		    center: latLng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel:false
		  }
		  this.map = new google.maps.Map($(selector)[0], myOptions);
		  this.bounds = new google.maps.LatLngBounds();
		  infoWindow = new google.maps.InfoWindow();
		}
		MYMAP.placeMarkers = function() {
			var image = new google.maps.MarkerImage('/images/maps/marker.png',
				new google.maps.Size(26, 40),
				new google.maps.Point(0, 0),
				new google.maps.Point(26, 40));

			var markers = [];
			var unitCount=0;
		    $("li.results_unit.client").each(function(){
			//alert($(this).find('.title').text());
				var name = $(this).find('.title').text();
				var address = $(this).find('.address').text();
				var postal_code = $(this).find('.postal_code').text();
				var url = $(this).find('a').attr('href');

				// create a new LatLng point for the marker
				var lat = $(this).find('.latitute').text();
				var lng = $(this).find('.longitude').text();
				var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

				// extend the bounds to include the new point
				MYMAP.bounds.extend(point);

				// add the marker itself
				var marker = new google.maps.Marker({
					icon:image,
					position: point,
					map: MYMAP.map,
					title: name
				});
				markers.push(marker);
				

				// create the tooltip and its text
				var html='<a href="'+url+'"><b>'+name+'</b></a><br />'+address+"<br />"+postal_code;

				// add a listener to open the tooltip when a user clicks on one of the markers
				google.maps.event.addListener(marker, 'click', function() {
					infoWindow.setContent(html);
					infoWindow.open(MYMAP.map, marker);
				});
				
				unitCount++;
				$(this).find('.address').before(' <a id="unit'+unitCount+'" href="#results_map" class="see_map" >'+mapText+'</a>');
				$(this).find('.see_map').click(function(e){
					_gaq.push(['_trackEvent', 'Query', 'Show unit on map']);
					MYMAP.map.setCenter(point, 15);
					MYMAP.map.setZoom(15);
					//marker.click();
					infoWindow.setContent(html);
					infoWindow.open(MYMAP.map, marker);
				});
		    });

			var zoomListner = google.maps.event.addListener(MYMAP.map, 'zoom_changed', function() {
				google.maps.event.removeListener(zoomListner);
				if (this.getZoom() > 12) // Change max/min zoom here
					this.setZoom(12); 
			});

		    
		    // Fit the map around the markers we added:
			MYMAP.map.fitBounds(MYMAP.bounds);
		    if ( $("li.results_unit.client").length>1) {
			
				var markerCluster = new MarkerClusterer(MYMAP.map, markers, {styles: [
					{
						url: '/images/maps/cluster_m2.png',
						height: 70,
						width: 70,
						opt_anchor: [27, 0],
						opt_textColor: '#990000',
						opt_textSize: 12
					},
					{
						url: '/images/maps/cluster_m1.png',
						height: 90,
						width: 90,
						opt_anchor: [37, 0],
						opt_textColor: '#990000',
						opt_textSize: 12
					}]
					,maxZoom: 15
					,gridSize: 40
				});
			}
		  
		}
		
		var myLatLng = new google.maps.LatLng(17.74033553, 83.25067267);
		MYMAP.init('#results_map', myLatLng, 11);
		MYMAP.placeMarkers();
		/*console.log(MYMAP.map.zoom);
		if (MYMAP.map.zoom>12){
			MYMAP.map.setZoom(12); 
		}*/


	}
	$( ".tab" ).prepend("<a href='#' class='colapser colapse'>-</a>");

	$( ".colapser" ).toggle(function(e){
		e.preventDefault();
		$(this).html("+");
		$(this).addClass("expand").removeClass("colapse");
		$(this).parent().parent().children('ul').hide();
	}, function() {
		$(this).html("-");
		$(this).addClass("colapse").removeClass("expand");
		$(this).parent().parent().children('ul').show();
	});
	
	$(".tab").each(function(){
		var count = $(this).parent().find('.results_unit').length;
		$(this).append(" <span class='count'>("+count+")</span>");
	});
	
	
	
});

