/* MAP STUFF */

var mapPoint = new Array; 
var mapLabel = new Array();	
var mapHTML = new Array();
var mapLocation = new Array();
	
var mapMarker = new Array();	
	
function markerCreate(i, icon, point, label, html, single) {
	var marker = new GMarker(point, markerIcon( icon, single ));

	GEvent.addListener(marker, "click", ( function(j){ return function(){ markerClick( j ); } } )(i));
	
	mapMarker[i] = marker;
	
	return marker;
}	

function markerIcon( num, single ){
	var tinyIcon = new GIcon();
		if( single ){
			tinyIcon.image = "images/google_map_logo.png";
			tinyIcon.shadow = "";
			tinyIcon.iconSize = new GSize(32, 24);
			//tinyIcon.shadowSize = new GSize(45, 38);
			tinyIcon.iconAnchor = new GPoint(25, 0);
			tinyIcon.infoWindowAnchor = new GPoint(30, 20);
			tinyIcon.infoShadowAnchor = new GPoint(18, 25);
		}else{
			tinyIcon.image = "/map_icons/"+num+".png";
			//tinyIcon.shadow = "/map_icons/shadow.png";
			//tinyIcon.shadow = "";
			tinyIcon.iconSize = new GSize(35, 30);
			//tinyIcon.shadowSize = new GSize(45, 38);
			tinyIcon.iconAnchor = new GPoint(0, 0);
			tinyIcon.infoWindowAnchor = new GPoint(30, 20);
			tinyIcon.infoShadowAnchor = new GPoint(18, 25);
		}
		
		return tinyIcon;
}	

function markerClick( marker_num ){
	//set the active background color of the current selected
	for( var i = 0; i < mapLabel.length; i++ ){
		var marker_item = document.getElementById('marker_'+i);
		
		if( i == marker_num ){
			marker_item.className = 'location_sel_active';
			marker_item.onmouseover = function(){ return; }
			marker_item.onmouseout = function(){ return; }
		}else{
			marker_item.className = 'location_sel';
			marker_item.onmouseover = function(){ this.className='location_sel_hover'; }
			marker_item.onmouseout = function(){ this.className='location_sel'; }
		}
	}

	mapMarker[ marker_num ].openInfoWindowHtml( mapHTML[ marker_num ] );
}


function executeAJAXMap(){
	var show_map = true;
    
    var myConn = new XHConn();
    if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
    var urlString = '';
    
    var fnWhenDone = function (oXML){ 
        if( oXML.responseXML ){
            var location 	= oXML.responseXML.getElementsByTagName('locations')[0];
   
            if( location.selectNodes("location")[0] ){            
                var locations = location.selectNodes("location");

                for(var i = 0; i < locations.length; i++){
					var icon_id 		= ( locations[i].selectSingleNode("icon_id/text()") 	!= null ? locations[i].selectSingleNode("icon_id/text()").nodeValue : '' );

            		var property_image 	= ( locations[i].selectSingleNode("address_1/text()") 	!= null ? '<div id="prop_image_map"><img src="'+locations[i].selectSingleNode("property_image/text()").nodeValue+'" border="0" /></div>' : '');
                    //var url 			= ( locations[i].selectSingleNode("url/text()") 		!= null ? locations[i].selectSingleNode("url/text()").nodeValue : '' );
					var address_1 		= ( locations[i].selectSingleNode("address_1/text()") 	!= null ? locations[i].selectSingleNode("address_1/text()").nodeValue : '' );
                    var city 			= ( locations[i].selectSingleNode("city/text()") 		!= null ? locations[i].selectSingleNode("city/text()").nodeValue : '' );
                    var state 			= ( locations[i].selectSingleNode("state/text()") 		!= null ? locations[i].selectSingleNode("state/text()").nodeValue : '' );
                    var zip 			= ( locations[i].selectSingleNode("zip/text()") 		!= null ? locations[i].selectSingleNode("zip/text()").nodeValue : '' );
                
                	var daddress = address_1+' '+city+', '+state+' '+zip;
                
                    var html 			= property_image+'<br /><div style="float:left; vertical-align:top; text-align:left; width:190px;">'+address_1+'<br />'+city+', '+state+' '+zip+'</div>';
                    var latitude 		= ( locations[i].selectSingleNode("latitude/text()") 		!= null ? locations[i].selectSingleNode("latitude/text()").nodeValue : '');
                    var longitude 		= ( locations[i].selectSingleNode("longitude/text()") 		!= null ? locations[i].selectSingleNode("longitude/text()").nodeValue : '');

                    if( document.getElementById('map') ){                        
                        mapLocation[i] = icon_id;
                        mapPoint[i] = new GLatLng(latitude, longitude);
                        //mapLabel[i] = label;
                        mapHTML[i] 	= html;
                        
                        show_map = true;
                    }
                }
            }
    
    		if( show_map ){
                var map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                map.addControl(new GMapTypeControl(1));
				
				//map.panTo(mapPoint[0]); 
				//map.setZoom(14); //
				//alert(mapPoint[0]);
				map.setCenter(mapPoint[0], 15); 
				
				var givenmaptypes = map.getMapTypes();
				map.setMapType(givenmaptypes[2]);
				
				//map.checkResize();
                map.enableContinuousZoom();
                map.enableDoubleClickZoom();
				
                //map.enableScrollWheelZoom();
				
                // create the markers
                for(var i = 0; i < mapPoint.length; i++){
                    var marker = markerCreate(i, mapLocation[i], mapPoint[i], mapLabel[i], mapHTML[i], true);
                    map.addOverlay( marker );
    
                    if( i == 0 ) mapMarker[ i ].openInfoWindowHtml( mapHTML[ i ] );
                }
				
				//alert(map.getZoom());
           	}
        }		
    }
    
    myConn.connect("_includes/xml.map_addresses.php", "GET", urlString, fnWhenDone);
}  

function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}





// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ){
	if( typeof XMLDocument == "undefined" ){ XMLDocument = Document; }
  XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
    if( !xNode ) { xNode = this; } 
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++){aResult[i] =  aItems.snapshotItem(i);	}
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode){
		if( !xNode ) { xNode = this; } 
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 ){return xItems[0];	}
		else{return null;	}
	}
	Element.prototype.selectNodes = function(cXPathString){
		if(this.ownerDocument.selectNodes){	return this.ownerDocument.selectNodes(cXPathString, this);}
		else{throw "For XML Elements Only";}
	}
	Element.prototype.selectSingleNode = function(cXPathString){	
		if(this.ownerDocument.selectSingleNode){return this.ownerDocument.selectSingleNode(cXPathString, this);	}
		else{throw "For XML Elements Only";}
	}
}