var map;
var mgr;
var offices;

$(document).ready(function() {
   loadMap();
 });


function loadMap() {
      if (GBrowserIsCompatible()) {
       	$.get('contact_info.php', processData);
		
		var x = 51.491928;
		var y = -0.125888;
		var UK_office = new GLatLng(x, y);
		
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(x-90, y+50), 1);
		map.enableDoubleClickZoom();
		
		window.setTimeout(function() {
  			map.panTo(new GLatLng(x-15, y-10));
		}, 1000);
		
		var ini_info = "<p><b>Selecteer een LEWIS kantoor om de contactgegevens te bekijken.</b></p>";
		map.openInfoWindowHtml(new GLatLng(x-120, y-120), ini_info); 
    }
}


function processData(content) {
	var slink;
	slink += '<li><a href="#" onclick="loadMap()">world map</a></li>';
	
	eval("offices = " + content);
	offices.forEach(function(element, index, array) {
		var marker = new GMarker(new GLatLng(element.x, element.y), {title: element.office, icon: getIcon()});
        map.addOverlay(marker);
		
		slink += '<li><a href="#" onclick="moveMapTo('+index+')">'+element.office+'</a></li>';
	
		GEvent.addListener(marker, 'click', function() {
            var txt;
		
			txt = "<b>"+element.office+"</b>";
			txt += "<p>";
			txt += "tel: "+element.tel+"<br />";
			txt += "fax: "+element.fax+"<br />";
			txt += "address: "+element.address+"<hr />";
			txt += element.person+"<br />";
			txt += element.job+"<br />";
			txt += "<a href='mailto:"+element.email+"'>"+element.email+"</a>";
			txt += "</p>";
			marker.openInfoWindowHtml(txt); 
	
        });
	});
	
	slink = slink.replace('undefined', '');
	
	//$('#contact_navi').html('');
	//$('#contact_navi').append(slink);
	
}

function getIcon() {
	var icon = new GIcon();
	icon.image = "images/lewis_logo.png";
	icon.shadow = "images/shadow.png";
	icon.iconSize = new GSize(15, 15);
	icon.shadowSize = new GSize(25, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
    return icon;
}

function moveMapTo(index) {
    var txt;
	var point = new GLatLng(offices[index].x, offices[index].y);
	
	map.panTo(point);
	if (offices[index].office == 'Shanghai') {
		map.setCenter(point, 5);
	}
	else {
		map.setCenter(point, 15);
	}
		
	txt = "<b>"+offices[index].office+"</b>";
	txt += "<p>";
	txt += "tel: "+offices[index].tel+"<br />";
	txt += "fax: "+offices[index].fax+"<br />";
	txt += "address: "+offices[index].address+"<hr />";
	txt += offices[index].person+"<br />";
	txt += offices[index].job+"<br />";
	txt += "<a href='mailto:"+offices[index].email+"'>"+offices[index].email+"</a>";
	txt += "</p>";
	map.openInfoWindowHtml(point, txt); 	
}

/*
function createMarker(point) {
  var marker = new GMarker(point);
  var txt;
  txt = "<b>london office:</b>";
  txt += "<p>Millbank Tower<br />Millbank<br />London, SW1P 4RS<br />UK</p>";
  
  GEvent.addListener(marker, "click", function() {
											   
    marker.openInfoWindowHtml(txt);
  });
  return marker;
}



var officeLayer = [
  {
    "zoom": [0, 10],
    "places": [
	  { "name": "Benelux", "posn": [51.4235, 5.4295] },
	  { "name": "Boston", "posn": [42.3502, -71.0758] },
	  { "name": "Copenhagen", "posn": [55.6557, 12.5641] },
	  { "name": "Düsseldorf", "posn": [51.2403, 6.7375] },
	  { "name": "Hong Kong", "posn": [22.27979, 114.17627] },
	  { "name": "London", "posn": [51.491928, -0.125888] },
	  { "name": "Los Angeles", "posn": [34.059, -118.4149] },
	  { "name": "Madrid", "posn": [45.7136, 8.517] },
	  { "name": "Manchester", "posn": [53.471612, -2.294495] },
	  { "name": "Milan", "posn": [45.4776, 9.2049] },
	  { "name": "Mumbai", "posn": [-23.772741, -45.950169] },
	  { "name": "Munich", "posn": [48.0985, 11.5331] },
	  { "name": "New York", "posn": [40.7067, -74.0101] },
	  { "name": "Paris", "posn": [44.8514, -0.626] },
	  { "name": "San Diego", "posn": [32.7751, -117.139] },
	  { "name": "San Francisco", "posn": [37.7898, -122.3964] },
	  { "name": "Shanghai", "posn": [31.2479, 121.473] },
	  { "name": "Singapore", "posn": [1.28494, 103.84786] },
	  { "name": "Stockholm", "posn": [59.3332, 18.0571] },
	  { "name": "Sydney", "posn": [-33.839302, 151.205786] },
	  { "name": "Stockholm", "posn": [59.3332, 18.0571] },
	  { "name": "Washington DC", "posn": [38.9008, -77.0411] },
    ]
  }
];


function setupOfficeMarkers() {
  mgr = new GMarkerManager(map);
  for (var i in officeLayer) {
    var layer = officeLayer[i];
    var markers = [];
    for (var j in layer["places"]) {
      var place = layer["places"][j];
      var icon = getIcon();
      var posn = new GLatLng(place["posn"][0], place["posn"][1]);
      markers.push(new GMarker(posn, { title: place["name"], icon: icon }));
	  //markers.push(new GMarker(posn, { title: place["name"]}));
    }
    mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
  }
  mgr.refresh();
}




*/

////



/* search box functions */

function ini_search () {
	var oSearch =  document.getElementById("search_box"); 
	if (oSearch.value == "Zoeken in LEWIS PR") {	
		oSearch.value = '';
	}
}


function clear_search () {
	var oSearch =  document.getElementById("search_box");
	if (oSearch.value== '') {
		oSearch.value = "Zoeken in LEWIS PR";
	}
}

document.onkeydown = checkKeycode

function checkKeycode(e) {
	var oSearch =  document.getElementById("search_box");
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;

	if (keycode == 13 && oSearch.value != '') {
		start_search();
	}
}

function start_search () {
	var oSearch =  document.getElementById("search_box");
	window.location = "search.php?key=" + oSearch.value;
}