// Map Generation

// for georss url - requires jquery.url.packed.js 
if (jQuery.url.segment(2) !== null)
{
	var geoPathname = jQuery.url.segment(1) + "/" + jQuery.url.segment(2);
}
else
{
	var geoPathname = jQuery.url.segment(1);
}

var geoURL = jQuery.url.attr("protocol")  + "://" + jQuery.url.attr("host") + "/maps/georss/" + geoPathname;
// end georss url

// for markers
var locations = {};

function initialize() {  
	if (GBrowserIsCompatible()) {	     
    var map = new GMap2(document.getElementById('communityMap'));

	if (jQuery.url.segment(1) !== null)
	{
		var town = jQuery.url.segment(1);

		if (town == 'corolla')
		{
			map.setCenter(new GLatLng(36.30848550829367,-75.81888198852539), 12);
		}  
		
		if (town == 'duck')
		{
			map.setCenter(new GLatLng(36.19760311603671,-75.75995922088623), 14);
		} 

		if (town == 'northduck')
		{
			map.setCenter(new GLatLng(36.19760311603671,-75.75995922088623), 14);
		} 

		if (town == 'southduck')
		{
			map.setCenter(new GLatLng(36.16185469422557,-75.74622631072998), 14);
		} 

		if (town == 'southernshores')
		{
			map.setCenter(new GLatLng(36.125050047113184,-75.7293176651001), 14);
		} 

		if (town == 'kittyhawk')
		{
			map.setCenter(new GLatLng(36.07664404297879,-75.69820404052734), 14);
		} 

		if (town == 'killdevilhills')
		{
			map.setCenter(new GLatLng(36.029804722015996, -75.63804745674133), 13);
		} 

		if (town == 'nagshead')
		{
			//map.setCenter(new GLatLng(35.97210204309055,-75.63657760620117), 15);
			map.setCenter(new GLatLng(35.95730502867571,-75.62344551086426), 13);
		} 	
		if (town == 'southnagshead')
		{
			//map.setCenter(new GLatLng(35.97210204309055,-75.63657760620117), 15);
			map.setCenter(new GLatLng(35.88279158765826,-75.58490753173828), 13);
		} 	
    }
	else
		{
		map.setCenter(new GLatLng(36.125050047113184, -75.7293176651001), 14);
		}
	
	// large directional and zoom controls
	map.addControl(new GLargeMapControl());

	// scale
	map.addControl(new GScaleControl());

	// small global map in corner
	map.addControl(new GOverviewMapControl());
	
	// show satellite view
	map.addControl(new GMapTypeControl());
	
	//set default view to hybrid
    map.setMapType(G_HYBRID_MAP);
	
  GDownloadUrl(geoURL, function(data) {
		var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");         
          var latlng = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                  parseFloat(markers[i].getAttribute("lng")));		 
		  var view = markers[i].getAttribute("location");
		  var community = markers[i].getAttribute("community");
		  var details = markers[i].getAttribute("details");
		  var img = markers[i].getAttribute("img");
		  var link = markers[i].getAttribute("url");
		  var type = markers[i].getAttribute("type");
          var property = {latlng: latlng, name: name, view: view, community: community, details: details, img: img, link: link, type: type };
          
		  if (type=="home_selected")
		  {			  
			  map.zoomIn(latlng,true);
			  var imageTag = "<img height='100' width='150' border='0' src='" + img + "'/>";
			  var html = "<div class='info_window'><a href='" + link + "'>" + imageTag + "</a><p><a href='" + link + "'>" + name + "</a></b></p><p>" + view + "</p><p>" + community + "</p><p>" + details + "</p></div>";			  
			  map.openInfoWindowHtml(map.getCenter(),(html),{pixelOffset: new GSize(10,10)});
		  }		 
		  
		  var latlngHash = (latlng.lat().toFixed(6) + "" + latlng.lng().toFixed(6));
          latlngHash = latlngHash.replace(".","").replace(".", "").replace("-","");
          if (locations[latlngHash] == null) {
            locations[latlngHash] = [];
          }
          locations[latlngHash].push(property);
        }
        for (var latlngHash in locations) {
          var properties = locations[latlngHash];         			
            map.addOverlay(createMarker(properties));	        			
       
         }
		 document.getElementById('siteLoader').style.display = 'none';
       });	

    // Reverse Geocoder
		$(document).bind('keydown', 'Ctrl+l', function (evt){
			$('#latBar').fadeIn(500);
			$('#latBar').animate({'bottom':26}, 1000);

			GEvent.addListener($.map, 'click', function(overlay, point) {
				if (overlay) {
					map.removeOverlay(overlay);
				} else if (point) {
					map.recenterOrPanToLatLng(point);
					var marker = new GMarker(point);
					map.addOverlay(marker);
				}
			});

		    // Recenter Map and add Coords by clicking the map
			GEvent.addListener(map, 'click', function(overlay, point) {				
				$('#latBar span').html("Lat: "+ point.y + " , Lon: " + point.x);
			});		
			
			return false;
		});
		

		$('#mapBox').fadeIn(500);		
	}
}


function createMarker(properties) {
      var property = properties[0];	  
	  
	  var baseIcon = new GIcon();
	  baseIcon.image = "/images/georss/side_blue.png";
	  //baseIcon.shadow = "";
	  baseIcon.iconSize = new GSize(19, 19);
	  //baseIcon.shadowSize = new GSize(22, 20);	  
	  baseIcon.iconAnchor = new GPoint(0, 0);
	  baseIcon.infoWindowAnchor = new GPoint(10, 10);

	  var newIcon = new GIcon(baseIcon);	  
	   
	  switch (property.type)
	  {
	  case "home":
		  newIcon.image = "/images/georss/side_blue.png";
	      property.imageTag = "<img height='100' width='150' border='0' src='" + property.img + "'/>";		  
	      break
	  case "home_selected":		  
		  newIcon.image = "/images/georss/side_orange.png";
		  property.imageTag = "<img height='100' width='150' border='0' src='" + property.img + "'/>";		  
		  break  
	   case "office":		  
		  newIcon.image = "/images/georss/pin_red.png";
		  property.imageTag = "<img height='100' width='150' border='0' src='" + property.img + "'/>";
		  break  
	  }
	  
	  var marker = new GMarker(property.latlng, {icon: newIcon});
      var html = "<div class='info_window'><a href='" + property.link + "'>" + property.imageTag + "</a><p><a href='" + property.link + "'>" + property.name + "</a></b></p><p>" + property.view + "</p><p>" + property.community + "</p><p>" + property.details + "</p></div>";
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }	


function showMap(name,desc){

	$('#map_main').slideUp(500,function(){
		$.previous= $('#mapContainer');
		$('#mapContainer h3').html(name);
		$('#mapContainer span.text').html(desc);
		$('#mapContainer').css({'position':'relative'});
		$('#mapContainer').animate({'left':0},500);
	})
}

// End Map Generation
