/*$Id: gmaps.js 318 2010-02-20 15:38:14Z stephen $*/		 	
var base_url = "./";
var loc_string='';
var lang_string='';
var pax_string='';

	 function load(location, language) {
	 		loc_string=location;
	 		lang_string=language;
	 		loc = location.split('_');
	 		if (loc.length==3) {
	 			lat = loc[0];
	 			lang = loc[1];
	 		} else {
	 			lat=0;
	 			lang=0;
	 		}
	 		
			g_lat=lat;
			g_lng=lang;
			
			
      if (GBrowserIsCompatible()) {

        var map = new GMap2(document.getElementById("map"),{draggableCursor: 'pointer'});
        if (lat!=0 && lang!=0) {
        	map.setCenter(new GLatLng(lat, lang), 11);
        	
        } else {
        	map.setCenter(new GLatLng(52.065213, 5.369499), 4);	
        }
				
        
			//	map.addControl(new TextualZoomControl());
		map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
						
        
						var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 20);
        baseIcon.shadowSize = new GSize(0, 0);
        baseIcon.iconAnchor = new GPoint(0, 0);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(0, 0);
						

						
						GEvent.addListener(map, "zoomend", function (oldZoom, newZoom) {
						  
						  placeHouses(loc_string,lang_string);
						  
						}); 
						
						GEvent.addListener(map, "dragend", function () {
								var zoom = map.getZoom();
						  
						  placeHouses(loc_string,lang_string);
						  
						  
						}); 
						
						
						GEvent.addListener(map, "click", function drawMapItems(marker, point) {
						  move=true;
						
						 var zoom = map.getZoom();
							
      				if (marker) {return false;}
      				zoom=zoom+2;	
         			if (zoom<16 && move) {map.setCenter(point, zoom);}
         
							
						});
					
				var zoom = map.getZoom();
						  
						  placeHouses(loc_string,lang_string);	
  				
    }
		
		
		function drawMapItems(marker, point, move) {
    		var zoom = map.getZoom();
				if (marker) {return false;}
				zoom=zoom+2;	
   			if (zoom<16 && move) {map.setCenter(point, zoom);}
      	placeHouses();
  			
		}
		
		
		
		
		
		
		function placeHouses(location) {
		  zoom = map.getZoom();
				
			
				
						var url = base_url + "kml.php?output=xml&lang=" + lang_string + "&location=" + location + "&zoom=" + (zoom) + "&points=" + map.getBounds();
					
				  GDownloadUrl(url, function(data, responseCode) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");
								var num = markers.length;
								var zoom = map.getZoom();
								map.clearOverlays();
          for (var i = 0; i < markers.length; i++) {
          	 var infowindow = markers[i].getAttribute("text");
          	 
				var link = markers[i].getAttribute("href");
	      	     var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
	                                  parseFloat(markers[i].getAttribute("lng")));
           
	             if (link) {
	              infowindow = "<div align='left'> <b>" + markers[i].getAttribute("text") + "  " + "<img style=\"border:none;margin:0px;\"  src=\"" + markers[i].getAttribute("stars") + "\" alt=\"\">" + "<br/>" + markers[i].getAttribute("country") + ", " + markers[i].getAttribute("city") + "</b><br/><div style=\"min-height:85px;padding-top:10px;\"><div style=\"float:left\"><a target=\"_blank\" href=\"" + markers[i].getAttribute("href") + "\"><img style=\"border:none;\" height=\"85\" src=\"" + markers[i].getAttribute("img") + "\" alt=\"\"></a></div><div><br/>"+markers[i].getAttribute("pax")+"<br/>"+markers[i].getAttribute("pets")+"<br/><br/><a target=\"_blank\" href=\"" + markers[i].getAttribute("href") + "\" style='text-decoration:underline;'>" + markers[i].getAttribute("more1") + "</a></div></div></div>";
 // document.title=Math.floor(Math.random()*1100) + markers[i].getAttribute("href") + markers[i].getAttribute("open");
	            	var mrkr=	map.addOverlay(createMarker(point,infowindow,markers[i].getAttribute("open")));
	            	if (mrkr) { 
				mrkr.marker.openInfoWindowHtml(infowindow);
			}
	            } else {
	            
	            	map.addOverlay(createGroupMarker(point,infowindow));
	            }
	           
          	 
           }

          });
						
			}
		
					
	
						
function createGroupMarker(point, num) { 
					
var icon = new GIcon();
var sizes = [28, 32, 36, 46, 52];
var size=0;

	icon.image = './images/red-marker.png';
	switch(true) {
	case num<10:
		size=0;		
		
		break;
	case num<25:
				size=1;
		break;
	case num<100:
				size=2;
		break;
	case num<1000:
				size=3;
		break;
	default:
				size=4;
		break;
	}

icon.iconSize = new GSize(sizes[size], sizes[size]);
		icon.iconAnchor = new GPoint(sizes[size]/2, sizes[size]/2);
		icon.infoWindowAnchor = new GPoint(25, 7);

	opts = {
		"icon": icon,
		"clickable": true,
		"labelText": num,
		"labelClass": "markerLabel_" + size,
		"labelOffset": new GSize(-16, -16)
	};
	var marker = new LabeledMarker(point, opts);		
	  return marker; 
                
                }
					
function createGroupMarker2(point, num) { 

 var marker = new GMarker (point);	
GEvent.addListener(marker, 'click', function(){ 
                        var zoom = map.getZoom();
                        if (zoom>5) { 
                        	zoom=zoom+2;
                        } else {
                        	zoom=zoom+1;
                        }	
                        map.setCenter(point, zoom);
                });
                return marker; 
                
                }
		
function createMarker(point, infowindow, open){ 
                
                var icon = new GIcon(baseIcon);
                
                if (map.getZoom()>7) {
				 icon.iconSize = new GSize(20, 20);
			}
			icon.image = "./images/house.png";
			var marker = new GMarker (point, icon); 
                GEvent.addListener(marker, 'click', function(){ 
                        marker.openInfoWindowHtml(infowindow); 
                });
               
                if (open==1) marker.openInfoWindowHtml(infowindow);
                
                
                return marker; 
        } 
}
	
	
/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

/* Constructor */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything if the coordinate system has changed
	if (!force) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}



