var map;
var centerLatitude = 47.443804;
var centerLongitude = 9.611270;
var startZoom = 7;
var markerHash = {};
var bounds;
var currentFocus = false;



function init() {
	if (markers.length > 0) {
	  handleResize();
	  Element.hide('load_info');
	  Element.show('result-content');

	  if (GBrowserIsCompatible()) {
	    
		map = new GMap2(document.getElementById("map"));
	    map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
	    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	    
		bounds = new GLatLngBounds();
	    for (i = 0; i < markers.length; i++) {
	      var current = markers[i];
	      if (current.lat != null && current.lng != null) {
	        marker = addMarker(current.lat, current.lng, i, current.id);
			map.addOverlay(marker);
			 
	        var address = '';
	        if (current.street != null && current.street != '') {
	          address = current.street + ', ';
	        }
	        address += current.plz + ' ' + current.city + ' ';
	        address += '[' + current.canton + ']';
	        markerHash[current.id] = {marker:marker,title:current.title,address:address,description:current.description,price:current.price,pict:current.pict,visible:true};
	      }  
	    }
	  }
	}
}



function handleResize() {
  var height = windowHeight() - 30;
  $('sidebar').style.height = height + 'px';
  $('map').style.height = height + 'px';		
}



function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight - 20;
	// IE 5
	if (document.body)
		return document.body.clientHeight - 20;
	// Just in case.
	return 0;
}


// ==================================================
// A function to create a tabbed marker and set up the event window
// This version accepts a variable number of tabs, passed in the arrays htmls[] and labels[]
function createTabbedMarker(point,htmls,labels) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
	  // adjust the width so that the info window is large enough for this many tabs
	  if (htmls.length > 2) {
		htmls[0] = '<div style="width:'+htmls.length*88+'px">' + htmls[0] + '</div>';
	  }
	  var tabs = [];
	  for (var i=0; i<htmls.length; i++) {
		tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
	  }
	  marker.openInfoWindowTabsHtml(tabs);
	});
	return marker;
}
// ==================================================






function addMarker(lat, lng, i, id) {

  // use a custom icon with letter A - Z
  var letter = String.fromCharCode("A".charCodeAt(0) + i);
  var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/mapfiles/marker" + letter + ".png");
  myIcon.printImage = "http://maps.google.com/mapfiles/marker"+letter+"ie.gif"
  myIcon.mozPrintImage = "http://maps.google.com/mapfiles/marker"+letter+"ff.gif"

  //add marker
  var latLng = new GLatLng(lat, lng); 
  var marker = new GMarker(latLng, {icon:myIcon}); 
    
  //add click listener to marker with info window
  GEvent.addListener(marker, 'click',
    function() {
      //focusPoint(id);  
	   
	  	 //var link = '<a href="http://www.anzeiger.ch/immo/suchen/www/result/x_result.cfm?objektid=' + id + '">' + markerHash[id].title + '</a>'
	  
	     var tabs = [];
 		 //tabs.push(new GInfoWindowTab('Text', '<div style="width:500px"><b>'  + link + '</b></div>' + markerHash[id].address + '<p style="width:500px;font-size:0.9em">'  + markerHash[id].price   + '</p>' + '<p style="width:500px;font-size:0.9em">'  + markerHash[id].description   + '</p>'));
 		   
		  
		 if (markerHash[id].pict.substr(0,22) != 'http://www.homegate.ch'){  
		 
		 	
		 
		 	if (markerHash[id].pict != '') {
		 		tabs.push(new GInfoWindowTab('Dossier','<div style="width:700px;text-align:left" align="center"><iframe marginheight=0 marginwidth=0 frameborder=0 width=650 height=400 src=http://www.anzeiger.ch/immo/suchen/www/result2/x_result.cfm?dossier=true&objektid=' + id + '></div>')); 
		 	}
		 }  
		   
		 tabs.push(new GInfoWindowTab('Original','<div style="width:700px;text-align:left" align="center"><iframe marginheight=0 marginwidth=0 frameborder=0 width=650 height=400 src=http://www.anzeiger.ch/immo/suchen/www/liste2/originalforward.cfm?objektid=' + id + '></div>'));
		   
		   
		   
		 //tabs.push(new GInfoWindowTab('Notizen','<div style="width:700px"><iframe marginheight=0 marginwidth=0 frameborder=0 width=650 height=300 src=http://www.anzeiger.ch/immo/suchen/www/liste2/community.cfm?objektid=' + id + '></div>'));  
		   
		   //http://www.homegate.ch
		   
		 
		   
		   //alert (markerHash[id].pict.substr(0,22));  
		   
		  //( markerHash[id].pict != ''){tabs.push(new GInfoWindowTab('Bild','<div style="width:700px"><iframe marginheight=0 marginwidth=0 frameborder=0 width=700 height=400 src=http://www.anzeiger.ch/immo/suchen/www/liste2/images.cfm?objektid=' + id + '></div>'));}
		  
		 //if ( markerHash[id].url != ''){tabs.push(new GInfoWindowTab('Original','<div style="width:700px"><iframe marginheight=0 marginwidth=0 frameborder=0 width=650 height=300 src=http://www.anzeiger.ch/immo/suchen/www/liste2/original.cfm?objektid=' + id + '></div>'));}
		  
		marker.openInfoWindowTabsHtml(tabs);  
    }  
  );

  //extend bounds of map and center
  bounds.extend(latLng);
  map.setZoom(map.getBoundsZoomLevel(bounds));
  map.setCenter(bounds.getCenter());

  return marker;
}



function focusPoint(id) {
  
  if (currentFocus) {
    Element.removeClassName("adTable-" + currentFocus, "selectedAd");
  }
  Element.addClassName("adTable-" + id, "selectedAd");
  
  var link = '<a href="details/' + id + '">' + markerHash[id].title + '</a>'
  
  markerHash[id].marker.openInfoWindowHtml('<div style="width:700px" align="center"><b>'  + link + '</b></div>' + markerHash[id].address + '<p style="width:700px;font-size:0.9em">'  + markerHash[id].price   + '</p>' + '<p style="width:700px;font-size:0.9em">'  + markerHash[id].description   + '</p>');
  currentFocus = id;
}


Event.observe(window, 'load', init, false);
Event.observe(window, 'resize', handleResize, false);

















