$(document).ready(function(){
    // Article page
    if ($('.map').length > 0) {
        innerInitialize();
    }

    // Cover page
    if ($('body.home').length) {
        coverInitialize();
    }
});

function coverInitialize () {
    var thislat = '54.9738361';
    var thislon = '-1.6216139';
    var latlng = new google.maps.LatLng(thislat, thislon);

    $('body').append('<div id="map_canvas" style="height:'+$(document).height()+'px;width:100%;"></div>');

    var mapOptions = {
        zoom: 17,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        disableDefaultUI: true
    };
    var thismap = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}

function innerInitialize() {
        var staticMapURL = $('.map img').attr('src');
        var latlon = staticMapURL.match(/markers=.+?\|(-?[0-9]+\.[0-9]+,-?[0-9]+\.[0-9]+)/)[1].split(',');
        var thislat = latlon[0];
        var thislon = latlon[1];
        var zoomLevel = staticMapURL.match(/zoom=([0-9]+)/)[1];
        
        var mapImg = $('.map img');
        var mapHeight = mapImg.attr('height');
        var mapWidth = mapImg.attr('width');

        $('.map').prepend('<div id="map_canvas" style="height:'+mapHeight+'px;width:'+mapWidth+'px;"></div>');
        $('.map img').remove();

        var latlng = new google.maps.LatLng(thislat, thislon);
        var mapOptions = {
            zoom: parseInt(zoomLevel),
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var thismap = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        var marker = new google.maps.Marker({
            position: latlng,
            map: thismap
        });
}

