google.load("maps", "2.x",{"other_params":"sensor=false"});

function initialize() {
    var json = document.createElement("script");
    json.type = "text/javascript";
    json.src = "/advice.json?callback=advice";
    document.body.appendChild(json);
    
    var message = document.createElement("div");
    message.id = "status";
    message.innerHTML = "<p>LOADING...</p>";
    document.body.appendChild(message);
}
google.setOnLoadCallback(initialize);
/*document.onunload = GUnload;*/
window.onresize = fullSize;

function windowSize() {
    var w = 0, h = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        w = window.innerWidth;
        h = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    }
    return {
        width: w,
        height: h
    };
}

function fullSize() {
    var container = document.getElementById("map");
    container.style.width = windowSize().width + "px";
    container.style.height = windowSize().height + "px";
}

function advice(data) {
    fullSize();
    
    var container = document.getElementById("map");
    var map = new google.maps.Map2(container);
    map.setCenter(new google.maps.LatLng(0, 0), 2);
    map.addControl(new GLargeMapControl());
    
/*    var myLayer = new GLayer("org.wikipedia.en");
    map.addOverlay(myLayer);
*/    
    
    var levels = function() {
        var danger = new GIcon(G_DEFAULT_ICON);
        danger.image = "/ban.png";
        
        var warning = new GIcon(G_DEFAULT_ICON);
        warning.image = "/warning.png";
        
        return {
            "level1": {icon: danger, description: "The FCO advises against ALL TRAVEL to"},
            "level2": {icon: warning, description: "The FCO advises against ALL TRAVEL to PARTS of"},
            "level3": {icon: danger, description: "The FCO advises against ALL BUT ESSENTIAL travel to"},
            "level4": {icon: warning, description: "The FCO advises against ALL BUT ESSENTIAL travel to PARTS of"}
        };
    }();
    
    for (var i = 0, l = data['countries'].length; i < l; i++) {
        var item = data['countries'][i];
        var lat = parseInt(item['lat']);
        var lng = parseInt(item['long']);
        var point = new GLatLng(lat, lng);
        var marker = new GMarker(point, { icon:levels["level" + item.level].icon });
        marker.bindInfoWindowHtml(levels["level" + item.level].description + " " + item.name + "<br /><a href='" + item.href + "' target='_blank'>More from fco.gov.uk</a>");
        map.addOverlay(marker);
    }
    
    document.getElementById("status").style.display = "none";
}