/*	Prepare the links to load the two different XML files for the maps	*/
	
function prepareMapLinks(){
	var linkList = $('mapLinks');
	var links = linkList.getElements('a');
	for(var i=0; i<links.length; i++){
		
		links[i].onclick = function(){
			for(var i=0; i<links.length; i++){
				links[i].removeClass('active');
			}			
			hideBubbles();
			//this.addClass('active');
			loadMap("/scripts/properties.php?view="+this.id);
			return false;	
		}	
	}
}

/*	Hide the bubbles when one of the links is clicked	*/

function hideBubbles(){
	var activeBubble = $('activeBubble').style.display = 'none';
}

/* GOOGLE MAPS FUNCTIONS
----------------------------------------------------------------------------------------------------*/

var geocoder;
var map;
var xmlFile = "/scripts/properties.php?view=london";
var addresses = new Array();
var bounds;
var point;
//var key = "ABQIAAAA4_GZO0lwxF2b0WXX2re70BRO0ptwfSkZZ0-ItJAct0dgH6YwCRSEEd9PvCQmatkaTQDXBHdGfyu1lQ";



// On page load, call this function
function loadMap(xmlFile)
{	
	// Create new map object
	map = new GMap2($("map"));

	bounds = new GLatLngBounds();
	map.addControl(new GSmallMapControl());
	// Set london as center of the map
	map.setCenter(new GLatLng(51.52, -0.127), 15);	

	map.addControl(new GMapTypeControl());
	
	// Create new geocoding object
	geocoder = new GClientGeocoder();
	xml = xmlLoader(xmlFile); //load xml
	var properties = (parseXml(xml));

	var thisGeoCodeLocation;
	for(var i=0; i<properties.length; i++){
		address = properties[i].address;
		id = properties[i].id;
		latitude = properties[i].lat;
		longitude = properties[i].long;
		name = properties[i].name;
		url = properties[i].url;
		fileref = properties[i].fileref;
		//alert(url)		
		showAddress(id, address, latitude, longitude, name,fileref, url) 
	}
}

function showAddress(id, address, lat, long, name,fileref, url) {
		//marker = new PdMarker(new GLatLng(longitude,latitude));
		point = new GLatLng(longitude,latitude);
		var html = "<a href=\""+url+"\" title=\""+name+"\" id=\"prop_img_"+id+"\"><img src=\"/img/properties/"+fileref+"/thumbs/"+fileref+"_1.jpg\" width=\"133px\" height=\"111px\" /></a>";
		html += "<small>"+name+"</small>";		
	
		var marker = createMarker(point,html, url, "prop_img_"+id);
		var mapMarker = new GIcon(G_DEFAULT_ICON);
		markerOptions = { icon:mapMarker };
		
		bubble = new EBubble(map, "/img/maps/assets/bubble.png",new GSize(174,179), new GSize(120,120), new GPoint(12,11), new GPoint(-70,60), id); 		

		map.addOverlay(marker,markerOptions);

		bounds.extend(point);
		zoomfit();
}

function createMarker(point,html, url, hotspot) {
        
		var markerIcon = new GIcon(G_DEFAULT_ICON);
		markerIcon.image = "/img/maps/assets/marker.png";		
		markerIcon.shadow = "/img/maps/assets/shadow.png";
		markerIcon.iconSize = new GSize(24, 35);
		//markerIcon.iconAnchor = new GPoint(90, 34);		
		markerOptions = { icon:markerIcon };
		var marker = new GMarker(point, markerOptions);
		
        GEvent.addListener(marker, "mouseover", function() {
          bubble.openOnMarker(marker,html, markerOptions);
        });
		GEvent.addListener(marker, "mouseout", function(overlay,point) {
		  (function(){ bubble.hide(); }).delay(4000);
		});
        GEvent.addListener(marker, "click", function() {
          //goToPage(url);
			//bubble.hide();
        });
		
        return marker;
      }

function goToPage(url){
	this.location.href = url;
}
function zoomfit()
{
	newzoom = map.getBoundsZoomLevel(bounds);
	newcenter = bounds.getCenter();
	map.setCenter (newcenter,newzoom);
}

/*	XML parsing
----------------------------------------------------------------------------------------------------*/

function Property(id, address, lat, long, name, fileref, url){
        this.id = id;
        this.address = address;
		this.lat = lat;
		this.long = long;
		this.name = name;
		this.fileref = fileref;
		this.url = url;		
}

function xmlLoader(url){
  //by Micox: micoxjcg@yahoo.com.br.
  //http://elmicoxcodes.blogspot.com
    if(window.XMLHttpRequest){
        var Loader = new XMLHttpRequest();
        //assyncronous mode to load before the 'return' line
        Loader.open("GET", url ,false); 
        Loader.send(null);
        return Loader.responseXML;
    }else if(window.ActiveXObject){
        var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
        //assyncronous mode to load before the 'return' line
        Loader.async = false;
        Loader.load(url);
        return Loader;
    }
}

function parseXml(xml){	
	var properties = xml.getElementsByTagName('property');	
	var allProperties 	= new Array();
	for(var i=0; i<properties.length; i++){
		
		addressNodes 	= properties[i].getElementsByTagName('address');
		thisAddress 	= addressNodes[0].firstChild.nodeValue;		
		
		latNodes 		= properties[i].getElementsByTagName('latitude');
		thisLat 		= latNodes[0].firstChild.nodeValue;	
		
		longNodes 		= properties[i].getElementsByTagName('longitude');
		thisLong 		= longNodes[0].firstChild.nodeValue;					

		nameNodes 		= properties[i].getElementsByTagName('name');
		thisName		= nameNodes[0].firstChild.nodeValue;

		filerefNodes 	= properties[i].getElementsByTagName('fileref');
		thisFileref		= filerefNodes[0].firstChild.nodeValue;		
		
		urlNodes 		= properties[i].getElementsByTagName('url');
		thisUrl			= urlNodes[0].firstChild.nodeValue;
		
		allProperties[i] = new Property(i, thisAddress,thisLat, thisLong, thisName, thisFileref, thisUrl);
	}
	return allProperties;
}
