/**
 * @author Filip Michalowski / Fi Stockholm
 */
Namespace.create("ng.modules.mapModule");

ng.modules.mapModule.MapModule = function () {

	//private variables:
	var moduleContainer;
	var moduleData;
	
	//private methods:
	
	var draw = function() {	
	
		initializeDirectionsLink();
		initializeMap();	
		
	};
	
	var initializeDirectionsLink = function() {
		var streetaddress = moduleContainer.find('.vcard .street-address').text();
		var city = moduleContainer.find('.vcard .locality').text();
	    var postcode = moduleContainer.find('.vcard .postal-code').text();
		var latitude = moduleContainer.find('.vcard .latitude').text();
	    var longitude = moduleContainer.find('.vcard .longitude').text();  
		
		streetaddress = escape(streetaddress);
		var tmp = streetaddress.split("%0A");
		streetaddress = '';
		for(var i=0; i<tmp.length;i++){
			streetaddress += tmp[i];
		}
		streetaddress = unescape(streetaddress);
		
		var url = "http://maps.live.com/OneClickDirections.aspx?rtp=~pos." + latitude + "_" + longitude + "_" + streetaddress + " " + city + " " + postcode;
			
		moduleContainer.find(".headerLink a").attr("href", url);
		
	};
	
	var initializeMap = function() {
		
		 var mapstraction = new Mapstraction('getDirectionsMap','microsoft');
	     mapstraction.maps['microsoft'].HideDashboard();
	
	     mapstraction.setCenterAndZoom( new LatLonPoint( moduleData.latitude, moduleData.longitude), moduleData.zoom );
	
	     // Add a marker
	     moduleContainer.find('.vcard').each(function() {
	         var hcard = $(this);
	
	        // Add event marker on the map
	         var latitude = hcard.find('.latitude').text();
	         var longitude = hcard.find('.longitude').text();        
	
	         var marker = new Marker( new LatLonPoint(latitude, longitude) );
	         marker.setInfoBubble(
	         '<div class="bubble">' + hcard.html() + '</div>'
	         );
	         mapstraction.addMarker(marker); 
	
	    });

	};
	
	//the returned object here will become ng.modules.mapModule.MapModule:
	return  {
		
		// public
		
		init: function (container, data) {
			moduleContainer = container;
			moduleData = data;
			
			draw();
		}

	};
}(); // the parens here cause the anonymous function to execute and return


/**
 * Markup templates for the MapModule
 */
ng.modules.mapModule.MapModuleTemplates = 
{
		
};