function isdefined(variable) {
	return (typeof(window[variable]) == "undefined") ? false : true;
}

$(window).load(function() {
	$(document).pngFix();
	$('#topmenu li').hover(
			function(index) {
				var menu = $(this);
				if ($(this).find('ul').length > 0) {
					var submenu = $(this).find('ul');
					submenu.show();
					if (menu.hasClass('last')) {
						var width = ((menu.width() - submenu.width())) + 'px';
					}
					else {
						var width = ((menu.width() - submenu.width()) /2)-3 + 'px';
					}
					submenu.css({"margin-left": width});
					submenu.hide();
					menu.addClass('hover');
					$(this).find('ul').fadeIn(100);
				}
			},
			function(index) {
				var menu = $(this);
				if ($(this).find('ul')) {
					menu.removeClass('hover');
					$(this).find('ul').fadeOut(100);
				}
			}
	);


	
	if (isdefined("initialize") && GBrowserIsCompatible()) {
		initialize();
	}
	 if (isdefined("xmlFeedId") && GBrowserIsCompatible()) {
	        var map = new GMap2(document.getElementById("map"));
	        map.addControl(new GMapTypeControl());
	        map.addControl(new GLargeMapControl3D());
	        var bounds = new GLatLngBounds();
	        var geocoder = new GClientGeocoder();
	        geocoder.setBaseCountryCode("nl");
	        
	        function getMarkers() {
	        	
	        	var countries = Array();
	    		$('#countries input:checked').each(function() {
	    			countries.push($(this).val());
	    		}); 	        	
	        	
	        	
		        var randomnumber=Math.floor(Math.random()*11111);
		        GDownloadUrl("/xml/" + xmlFeedId + ".xml?countries=" + getQueryString(countries) + "&random="+randomnumber, 
		        	function(data, responseCode) {
				        var xml = GXml.parse(data);
			
				        var markers = xml.documentElement.getElementsByTagName("marker");
			
				        var icon = new GIcon();
				        icon.image = "/img/pedro_maps_shield.png";
				        icon.iconSize = new GSize(31, 44);
				        icon.iconAnchor = new GPoint(16, 44);
				        icon.infoWindowAnchor = new GPoint(28, -10);
				        
				        icon.shadow = "/img/pedro_maps_shadow.png";
				        icon.shadowSize = new GSize(50,44);
				        
				        
				        map.clearOverlays();
				        
				        for (var i = 0; i < markers.length; i++) {
				        	var address = markers[i].getAttribute("address");
				            var html = markers[i].getAttribute("html");
				            var lat = markers[i].getAttribute("lat");
				            var lng = markers[i].getAttribute("lng");
				            showAddress(map, geocoder, address, html, icon, lat, lng);
				        }
				        bounds = new GLatLngBounds();
			        }
		        );
	        }
	        getMarkers();
	        
	    	var counter;
	    	$("#countries input").click(function() {
	    		getMarkers();
	    	});		        
	        
	        function zoomfit() {
	        	newzoom = map.getBoundsZoomLevel(bounds);
	        	if (newzoom > 13) {
	        		newzoom = 13;
	        	}
	        	newcenter = bounds.getCenter();
	        	map.setCenter (newcenter,newzoom);
	        }


	        function createMarker(point,html,icon) {
	        	var marker = new GMarker(point,icon);
	        	GEvent.addListener(marker, "click", function() {
	        		marker.openInfoWindowHtml(html);
	        	});
	        	bounds.extend(point);
	        	zoomfit();
	        	return marker;
	        }

	        	
	        function showAddress(map, geocoder, address, html, icon, lat, lng) {
	        	point = new GLatLng(lat, lng, true);
	        	if (point) {
	                map.setCenter(point, 14);
	                var marker = createMarker(point, html ,icon);
	                map.addOverlay(marker);
	        		map.addControl(new GMapTypeControl());
	        	}
	        }	        
	  }
});

$(window).unload(function() {
	if (isdefined("xmlFeedId") && GBrowserIsCompatible()) {
		GUnload();
	}
});

function getQueryString(qArray) {
    var temp = "";
    $.each(qArray, function(index, item) {
        if (item != undefined) {
            if (temp != "") {
                temp += ':';
            }
            temp += item;
        }
    });      
    return temp;        
}

function switchBlock(id) {
	
	$('#country_' + id).find('h2 img').attr('src', '/img/collapse.png');
	$('#country_' + id).find('.companies').slideDown();
	
	if (blockId != -1) {
		$('#country_' + blockId).find('h2 img').attr('src', '/img/expand.png');
		$('#country_' + blockId).find('.companies').slideUp();
	}
	
	if (id == blockId) {
		blockId = -1;
	}
	else {
		blockId = id;
	}
}






