// Adam Guichard
// 12/22/06
// This file contains the functions used to create and alter our jeweler maps


var map = null
var gmarkers = [];
var markerWindowHTML = [];
var markerXML= null


function LoadMap(xmlString, mapNum, targetLat, targetLong)
//Description:  This function must be called in the body onload event for it to work correctly.
//  xmlString is an xml representation of jeweler's attributes including latitude and longitude
//  targetLat and targetLong are the coordinates for the user's desired address 
//  mapNum is an integer used to determine a few characteristics of the map such as whether to show the homePoint
//      If mapNum = 1 then the map is the consumer findajeweler map so targetLat and targetLong are provided
//      If mapNum = 2 the map is the update a location map and targetLat/targetLong are not provided
{ 
   //Get reference to map div
   mapobj = document.getElementById("map")   
   if(mapobj == null)
   {
      return;
   }   
   
   //Create map object in the div
   map = new GMap2(mapobj);         
   map.addControl(new GLargeMapControl());
   map.addControl(new GMapTypeControl());
   map.addControl(new GScaleControl());
   map.enableContinuousZoom(); 
   map.enableDoubleClickZoom();         
   GEvent.addDomListener(mapobj, "DOMMouseScroll", wheelZoom); 
   GEvent.addDomListener(mapobj, "mousewheel", wheelZoom); 
   map.setCenter(new GLatLng(41.0, -98, 5),3);
  
   //If there's no informtation in xmlString stop here
   if(!xmlString)
   {
      return;
   }
      
   //Remove ampersands from xml - illegal character
   xmlString = xmlString.replace(/&/g, "&amp;")   
   

   if (GBrowserIsCompatible())    
   {  
      
      var homePoint = new GLatLng(parseFloat(targetLat), parseFloat(targetLong))
      var xml = GXml.parse(xmlString);      
      markerXML = xml.documentElement.getElementsByTagName("Jeweler");
     
      //This function is used for two different map pages so set the
      //center according to the map type you need
      if(mapNum == 1)
        map.setCenter(homePoint,5);
      else
      {
        var point = new GLatLng(parseFloat(markerXML[0].childNodes[10].firstChild.data),
                                parseFloat(markerXML[0].childNodes[11].firstChild.data));        
        map.setCenter(point,5)
      }
      
      //Initialize the zoomBounds object to the homePoint
      var zoomBounds = new GLatLngBounds();
      if(mapNum == 1)
      {
         zoomBounds.extend(homePoint);
      }   
      
      for (var i = 0; i < markerXML.length; i++) 
      { 
         //For each point of interest create a glatlongpoint, html text for the infowindow,
         //Use this info to create a gmarker object, add this object to the map, and extend the
         //map bounds by the point
         var point = new GLatLng(parseFloat(markerXML[i].childNodes[10].firstChild.data),
                                 parseFloat(markerXML[i].childNodes[11].firstChild.data));                      
         var htmlText = CreateMarkerHtml(mapNum, i);
         var newMarker = createMarker(point,htmlText,i, mapNum);

         
         map.addOverlay(newMarker);  
         zoomBounds.extend(point);        
      }  

      //Getting the zoomlevel that contains the points and making it slightly bigger
      var zoomLevel = map.getBoundsZoomLevel(zoomBounds) - 1;
      zoomLevel=(zoomLevel < 1)?1:zoomLevel      
      map.setZoom(zoomLevel);
    
      //If the map is type 1 add a marker for the homePoint
      if(mapNum == 1)
      {
         var customIcon = new GIcon(G_DEFAULT_ICON);   
         customIcon.image = "IconImages/marker.png";
         var marker = new GMarker(homePoint,customIcon);  
         map.addOverlay(marker);
      }              
   }
 }
  
  
function Zindex(markerNumber)  
{
    var num = markerNumber;
    return function(){ return markerXML.length-num;};
}    
    
  
  
function createMarker(point,html, markerNumber, mapNum)
//Description:  This function creates a gmarker object for each point of interest
//  point is the gLatLong point of interest
//  html is a string with the popup html
//  markerNumber is the marker's Number 
//  mapNum is the number designating some of the maps features
{ 
   var customIcon = new GIcon(G_DEFAULT_ICON);
   if(mapNum == 1)
      customIcon.image = "http://redboxfilms.com/redbox_wp/wp-content/themes/redbox/IconImages/marker" + (markerNumber+1) + ".png";
   else
      customIcon.image = "..\\..\\findajeweler\\IconImages\\marker" + (markerNumber+1) + ".png";      
   
   
   //var markerOptions = {zIndexProcess:Zindex(markerNumber)};
   var markerOptions = {icon:customIcon,zIndexProcess:Zindex(markerNumber)};
   var marker = new GMarker(point,markerOptions);
   
   GEvent.addListener(marker, "click", 
                      function()
                      {
                        marker.openInfoWindowHtml(html);
                      });
   
   // We must save the info so that a click on the gridview
   // will be able to open a info window on the map
   gmarkers[markerNumber] = marker;
   markerWindowHTML[markerNumber] = html;    
   
   return marker
}
 
function DisplayMarkerInfo(markerNumber)
{
  gmarkers[markerNumber].openInfoWindowHtml(markerWindowHTML[markerNumber]); 
} 

function wheelZoom(event) 
{ 
   if(event.cancelable)    
      event.preventDefault(); 
    
   if ((event.detail || -event.wheelDelta) < 0) 
      map.zoomIn(); 
   else 
      map.zoomOut();
   
   return false;
} 


//Extract the information from each jeweler and form html to display
//on the popup associated with said jeweler
function CreateMarkerHtml(mapNumber, xmlElementNumber)
{   
   var locationName = markerXML[xmlElementNumber].childNodes[0].firstChild.data;
   var address1 = markerXML[xmlElementNumber].childNodes[1].firstChild.data;
   var address2 = markerXML[xmlElementNumber].childNodes[2].firstChild.data;
   var city = markerXML[xmlElementNumber].childNodes[3].firstChild.data;
   var state = markerXML[xmlElementNumber].childNodes[4].firstChild.data;
   var zip = markerXML[xmlElementNumber].childNodes[5].firstChild.data;
   var country = markerXML[xmlElementNumber].childNodes[6].firstChild.data;
   var phoneNumber = markerXML[xmlElementNumber].childNodes[7].firstChild.data;
   var websiteurl = markerXML[xmlElementNumber].childNodes[8].firstChild.data;
   var emailaddress = markerXML[xmlElementNumber].childNodes[9].firstChild.data;
   var distance = markerXML[xmlElementNumber].childNodes[12].firstChild.data; 
   var locationText = markerXML[xmlElementNumber].childNodes[13].firstChild.data; 
   
   var html = "";
   var destAddress;
  
   html = "<h1 id='popupHeader'>" + locationName + "</h1>";
  
   
   if (address2 == "Empty")
   {
      html += "<p>" + address1 + "<br />";
      destAddress = address1 + ", "  + city + " " + state + "</p>"     
   }
   else
   {
      html += address1 + " " + address2 + "<br />";
      destAddress = address1 + " " + address2 + ", "  + city + " " + state
   }
   
   
   html += city + ", " + state + " "  + zip + "<br />";

	if (country != "United States")
	{
		html += country + "<br />";;
	}

   
   if ( phoneNumber != "Empty")
   {
   	var areaCode;
   	var first3Digits;
   	var last4Digits;
   	var phoneString;
   	if(phoneNumber.length == 10)
   	{
   		areaCode = phoneNumber.substring(0,3);
   		first3Digits = phoneNumber.substring(3,6);
   		last4Digits = phoneNumber.substring(6);
   		phoneString = '(' + areaCode + ') ' + first3Digits + '-' + last4Digits;
   	}
   	else
   	{
   		first3Digits = phoneNumber.substring(0,3);
   		last4Digits = phoneNumber.substring(3);   	
   		phoneString = first3Digits + '-' + last4Digits;   		
   	}
   
   	html += phoneString + "<br />";
   }   	
     
   if (emailaddress != "Empty")
   {
      html+= "Email: " + emailaddress + "<br />";
   } 
    if (locationText != "Empty")
   {
       html += "<p style='width:300px'>" + locationText + "</p>";
   }
   if (websiteurl != "Empty" )
   {
      html+= "<a target='_blank' href='" + websiteurl + "'>Website</a>";
   }   


   return html
}
  
  
