var map;
var map;
var geo;
var _data = new Object();
var _markers = new Array();
var _huisid = '';
var _vpid = '';
var _showAll = false;
var fx;
var _loaded = false;
var type;

function google_load(){
  if (GBrowserIsCompatible() && !_loaded) {
  	_loaded = true;
    map = new GMap2(document.getElementById("map"));
    //map.enableScrollWheelZoom();
	map.removeMapType(G_HYBRID_MAP);
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(_center_lat, _center_lng), _center_zoom);
	
    var mapControl = new GMapTypeControl();
	map.addControl(mapControl);
    
    GEvent.addListener(map, "moveend", mapMove);
    GEvent.addListener(map, "zoomend", mapZoom);
    var bounds = map.getBounds();
    updatePoints(bounds.getSouthWest(), bounds.getNorthEast());
    //fx = new Fx.Tween('googlemapsoverlay', {wait: false});
    //fx.set('opacity', 0);
	geo = new GClientGeocoder();
	
    
    window.addEvent("unload", GUnload);
  }
}


function mapZoom(oldzoom, newzoom)
{
	if(newzoom > 12) {
		map.setZoom(12);
		return;
	}
	if($('houseabout') != null) $('houseabout').dispose();
	mapMove();
}

function mapMove()
{
	//fx.start(.8);
	//var zoom 	= map.getZoom();
	//if(zoom < 15 && _zoom == 15){
		//fx.set(0);
	//	return;
	//}

	var bounds 	= map.getBounds();
	var sw		= bounds.getSouthWest();
	var ne		= bounds.getNorthEast();

	updatePoints(sw, ne)	
}

function updatePoints(sw, ne)
{
	new Ajax(url, {
		method: 'post',
		data: {
			ajax_function: 'get_houses_from_bounds',
			south: sw.lat(),
			west: sw.lng(),
			north: ne.lat(),
			east: ne.lng(),
			showAll: _showAll,
			huisid: _huisid,
			vpid: _vpid
		},
		onComplete: showAllHouses
	}).request();
}

function showAllHouses(response)
{
	if($('debug2')!=null)
	{
		$('debug2').set('html', 'showAllHouses');
	}
	
	_data = Json.evaluate(response);
	for(var i=0; i<_data.length; i++)
	{
		var item = _data[i];
		var show = (_showAll || item.huisid == _huisid || item.vp_locatie_id == _vpid);
		showHouse(item, show);
		
	}
	fx.start('opacity', 0);
}

function closeHouse(a)
{
	fx2.start('opacity', 0);
	$('houseabout').innerHTML = '';
}

function showHouse(item, show)
{
	if($('debug2')!=null)
	{
		$('debug2').set('html', 'showHouse');
	}
	for(var i=_markers.length-1; i<=0; i--)
	{
		if((item.huisid == _markers[i].data.huisid && _markers[i].data.vp_locatieid == null) || (item.vp_locatieid == _markers[i].data.vp_locatieid && _markers[i].data.huisid == null))
		{
			if(show)
			{
				_markers[i].show();
			} else
			{
				_markers[i].hide();
			}
			return;
		}
	}
	var point 			= new GLatLng(item.lat, item.lng);
	var picIcon 		= new GIcon(G_DEFAULT_ICON);
	if(item.huisid == null)
	{
		if($('debug2')!=null)
		{
			$('debug2').set('html', 'type: '+item.type);
		}
		if(item.type == 'hotel')
		{
			picIcon.image 		= (item.vp_locatie_id == _vpid ? "http://www.dfb.nl/images/hotel.png" : "http://www.dfb.nl/images/hotel_blue.png");   
			item.selected 		= (item.vp_locatie_id == _vpid);
		} else
		{
			picIcon.image 		= (item.vp_locatie_id == _vpid ? "http://www.dfb.nl/images/vp.png" : "http://www.dfb.nl/images/vp_blue.png");   
			item.selected 		= (item.vp_locatie_id == _vpid);
		}
	} else
	{
		picIcon.image 		= (item.huisid == _huisid ? "http://www.dfb.nl/images/house.png" : "http://www.dfb.nl/images/house_blue.png");   
		item.selected 		= (item.huisid == _huisid);
	}
	picIcon.shadow		= "http://www.dfb.nl/images/house_shadow.png";    	
	picIcon.iconSize 	= new GSize(24, 30);
	picIcon.shadowSize	= new GSize(27, 27);
	var marker			= new GMarker(point, {icon: picIcon, zIndexProcess: zOrder});
	_markers[_markers.length] = marker;
	marker.data			= item;
	
	GEvent.addListener(marker, "click", houseClick);
	//GEvent.addListener(marker, "mouseover", houseOver);
	//GEvent.addListener(marker, "mouseout", houseOut);
	map.addOverlay(marker);	
	if((item.huisid == _huisid || item.vp_locatie_id == _vpid) && !_showAll)
	{
		GEvent.trigger(marker, "click");
	} else if(!show)
	{
		marker.hide();
	}
}

function zOrder(marker, b)
{
	var selected = (marker.data.selected ? 9 : 0);
	return GOverlay.getZIndex(marker.getPoint().lat()) + selected*1000000;
}

var _current;
var fx2;

function houseOver()
{
	var here = this.getLatLng();
	if($('houseover')==null)
	{
		var houseover = new Element('div');
		houseover.id = 'houseover';
		houseover.injectAfter(map.getPane(G_MAP_MAP_PANE));
		houseover.setStyles({'position': 'absolute', 'background-image': 'url(/images/house_info.png)', 'width': 100, 'height': 24, 'color': '#c1001f', 'font-size': '11px', 'font-weight': 'bold', 'padding': 4, 'z-index': 99999, 'background-repeat': 'no-repeat'});
	} else
	{
		var houseover = $('houseover');
	}
	var pixel = map.fromLatLngToDivPixel(here);
	houseover.innerHTML = this.data.huisid + "(€" + this.data.prijs+")";
	houseover.setStyles({'left': pixel.x+11, 'top':pixel.y-34});
}

function houseOut()
{
}


function houseOpenInfo(response)
{
	var here = _current.getLatLng();
	if($('houseabout')==null)
	{
		var houseabout = new Element('div');
		houseabout.id = 'houseabout';
		houseabout.injectAfter(map.getPane(G_MAP_MAP_PANE));
		houseabout.setStyles({'position': 'absolute', 'background-image': 'url(/images/house_about.png)', 'width': 252, 'height': 236, 'z-index': 999999, 'background-repeat': 'no-repeat', 'opacity': 0});
	} else 
	{
		var houseabout = $('houseabout');
	}
	var pixel = map.fromLatLngToDivPixel(here);
	houseabout.innerHTML = response;
	houseabout.setStyles({'left': pixel.x-9, 'top':pixel.y-225});
	fx2 = new Fx.Tween($('houseabout'), {wait: false});
	//fx.start(0);
	fx2.start('opacity', 1);
	
	var new_center = map.fromLatLngToDivPixel(_current.getLatLng());
	new_center.x += 50;
	new_center.y -= 80;
	new_center = map.fromDivPixelToLatLng(new_center);
	map.panTo(new_center);
}

function toggleShowAll(a)
{
	return;
	//$('fetchingProductsDiv').setStyles({'visibility': 'visible'});
	//fx.start('opacity', .8);
	//_showAll = !_showAll;
	a.innerHTML = (_showAll ? 'Laat alleen huidige huis zien' : 'Laat alle huizen zien');
	_markers = new Array();
	map.clearOverlays();
	mapMove();
}

function  findHouse(point)
{
	if(point == null && _data.trypostcode && _data.tryplaats && _data.trybigpostcode)
	{
		var lat = 0;
		var lng = 0;
		_data.trypostcode = false;
		_data.trybigpostcode = false;
		_data.tryplaats = false;
		type = 'notfound';
	}else if(point == null && _data.trypostcode && _data.tryplaats)
	{
   		type = 'postcode';
		_data.trybigpostcode = true;
   		geo.getLatLng(_data.postcode+', '+_data.land, findHouse);
		return;		
    } else if(point == null && _data.trypostcode)
	{
   		type = 'plaats';
		_data.tryplaats = true;
   		geo.getLatLng(_data.plaats+','+_data.land, findHouse);
   		return;
	} else if(point == null)
	{
   		type = 'straat';
    	_data.trypostcode = true;
   		geo.getLatLng(_data.adres+', '+_data.postcode+', '+_data.land, findHouse);
		return;
	} else
	{
		var lat = point.lat();
		var lng = point.lng();
	}
	//map.setCenter(point, 13); 
	//map.addOverlay(new GMarker(point));

	new Ajax(url, {
		method: 'post',
		data: { 
			ajax_function: 'get_vw',
			lat: lat,
			lng: lng,
			huisid: _data.huisid,
			type: type,
			acco: _data.acco
		},
		onComplete: mapMove
	}).request();
}

function foundHouse(response)
{
		//$('json').innerHTML = response;
		_data = Json.evaluate(response);
		if(_data.adres != '')
		{
	   		type = 'straat';
			geo.getLatLng(_data.adres+', '+_data.plaats+', '+_data.land, showHouse);
		} else
		{
			type = 'plaats';
    	_data.trypostcode = true;
		_data.tryplaats = true;
    	$('debug').innerHTML = _data.plaats+', '+_data.land;
			geo.getLatLng(_data.plaats+', '+_data.land, showHouse);
		}
}