var viewportwidth;
var viewportheight;
var map_location;

function setUpPage() {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth;
        viewportheight = window.innerHeight;
    }
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
    }
    else {
        viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
        viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    }
}

function init() {

    if (GBrowserIsCompatible()) {
	
        map1 = new GMap2(document.getElementById("map1"));
        map2 = new GMap2(document.getElementById("map2"));
        map3 = new GMap2(document.getElementById("map3"));
        map4 = new GMap2(document.getElementById("map4"));
        map5 = new GMap2(document.getElementById("map5"));
        map6 = new GMap2(document.getElementById("map6"));
        map7 = new GMap2(document.getElementById("map7"));
        map8 = new GMap2(document.getElementById("map8"));
        map9 = new GMap2(document.getElementById("map9"));
	
        var copyright = new GCopyright('Demo', new GLatLngBounds(new GLatLng( - 90, -180), new GLatLng(90, 180)), 0, ' ');
        var copyrightCollection = new GCopyrightCollection("Tales of The High Seas");
        copyrightCollection.addCopyright(copyright);

        CustomGetTileUrl = function(a, b) {
            var myTilesLocation = "http://maptiles.talesofthehighseas.com/day_" + (b) + "_" + a.x + "_" + a.y + ".jpg";
				if (b==3 && a.x>=2 && a.x<=4 && a.y>=2 && a.y<=4 ) {
				return myTilesLocation
				}
				else if (b==4 && a.x>=4 && a.x<=9 && a.y>=4 && a.y<=8 ) {
				return myTilesLocation
				}
				else if (b==5 && a.x>=8 && a.x<=19 && a.y>=8 && a.y<=16 ) {
				return myTilesLocation	
				}	
				else if (b==6 && a.x>=16 && a.x<=39 && a.y>=16 && a.y<=33 ) {
				return myTilesLocation	
				}
				else if (b==7 && a.x>=32 && a.x<=79 && a.y>=32 && a.y<=67 ) {
				return myTilesLocation	
				}
				else if (b==8 && a.x>=64 && a.x<=159 && a.y>=64 && a.y<=135 ) {
				return myTilesLocation
				}	
				else {
				return "http://maptiles.talesofthehighseas.com/day_maptile.jpg";
				}
			};
			
        var tilelayers = [new GTileLayer(copyrightCollection, 5, 7)];
        tilelayers[0].getTileUrl = CustomGetTileUrl;

        var dayMap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), "ONE", {
            errorMessage: ""
        });

        CustomGetTileUrl = function(a, b) {
            var myTilesLocation = "http://maptiles.talesofthehighseas.com/night_" + (b) + "_" + a.x + "_" + a.y + ".jpg";
				if (b==3 && a.x>=2 && a.x<=4 && a.y>=2 && a.y<=4 ) {
				return myTilesLocation
				}
				else if (b==4 && a.x>=4 && a.x<=9 && a.y>=4 && a.y<=8 ) {
				return myTilesLocation
				}
				else if (b==5 && a.x>=8 && a.x<=19 && a.y>=8 && a.y<=16 ) {
				return myTilesLocation	
				}	
				else if (b==6 && a.x>=16 && a.x<=39 && a.y>=16 && a.y<=33 ) {
				return myTilesLocation	
				}
				else if (b==7 && a.x>=32 && a.x<=79 && a.y>=32 && a.y<=67 ) {
				return myTilesLocation	
				}
				else if (b==8 && a.x>=64 && a.x<=159 && a.y>=64 && a.y<=135 ) {
				return myTilesLocation
				}	
				else {
				return "http://maptiles.talesofthehighseas.com/night_maptile.jpg";
				}
			};

        var tilelayers = [new GTileLayer(copyrightCollection, 5, 7)];
        tilelayers[0].getTileUrl = CustomGetTileUrl;

        var nightMap = new GMapType(tilelayers, G_SATELLITE_MAP.getProjection(), "ONE", {
            errorMessage: ""
        });

        var viewWidthDimensions = viewportwidth / 1000
        var viewHeightDimensions = viewportheight / 1000
		
        map1.addMapType(dayMap);
        map2.addMapType(dayMap);
        map3.addMapType(dayMap);
        map4.addMapType(dayMap);
        map5.addMapType(dayMap);
        map6.addMapType(dayMap);
        map7.addMapType(dayMap);
        map8.addMapType(dayMap);
        map9.addMapType(dayMap);

        function featureDay() {
            map1.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
            map2.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude), chart_zoom, nightMap);
            map3.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
            map4.setCenter(new GLatLng(chart_latitude, chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
            map5.setCenter(new GLatLng(chart_latitude, chart_longitude), chart_zoom, dayMap);
            map6.setCenter(new GLatLng(chart_latitude, chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
            map7.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
            map8.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude), chart_zoom, nightMap);
            map9.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
        }

        function featureNight() {
            map1.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
            map2.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude), chart_zoom, dayMap);
            map3.setCenter(new GLatLng(chart_latitude + (offsetLatitude1 * viewHeightDimensions), chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
            map4.setCenter(new GLatLng(chart_latitude, chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
            map5.setCenter(new GLatLng(chart_latitude, chart_longitude), chart_zoom, nightMap);
            map6.setCenter(new GLatLng(chart_latitude, chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, dayMap);
            map7.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude - (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
            map8.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude), chart_zoom, dayMap);
            map9.setCenter(new GLatLng(chart_latitude - (offsetLatitude2 * viewHeightDimensions), chart_longitude + (offsetLongitude1 * viewWidthDimensions)), chart_zoom, nightMap);
        }

        if  (map_location == "atlantic") {
            var chart_latitude = 38.41;
            var chart_longitude = -30;
            var chart_zoom = 5;
            var offsetLatitude1 = 10.55;
            var offsetLatitude2 = 12.5;
            var offsetLongitude1 = 14.7;
            featureDay();
        }
		else if (map_location == 'carib') {
			var chart_latitude = 12.55;
            var chart_longitude = -61;
            var chart_zoom = 6;
            var offsetLatitude1 = 7;
            var offsetLatitude2 = 7.25;
            var offsetLongitude1 = 7.4;
            featureNight();
		}
		else if (map_location == "finisterre") {
            var chart_latitude = 43.16;
            var chart_longitude = -9.36;
            var chart_zoom = 6;
            var offsetLatitude1 = 5.15;
            var offsetLatitude2 = 5.5;
            var offsetLongitude1 = 7.4;
            featureDay();
        }
		else if (map_location == "madeira") {
            var chart_latitude = 32.76;
            var chart_longitude = -17.13;
            var chart_zoom = 6;
            var offsetLatitude1 = 6;
            var offsetLatitude2 = 6.4;
            var offsetLongitude1 = 7.35;
            featureDay();
        }
		else if (map_location == 'newfoundland') {
			var chart_latitude = 48.28;
            var chart_longitude = -55.28;
            var chart_zoom = 6;
            var offsetLatitude1 = 4.7;
            var offsetLatitude2 = 5.05;
            var offsetLongitude1 = 7.37;
            featureDay();
		}
		else if (map_location == "scandinavia") {
            var chart_latitude = 60.3;
            var chart_longitude = 20.74;
            var chart_zoom = 7;
            var offsetLatitude1 = 2;
            var offsetLatitude2 = 2;
            var offsetLongitude1 = 3.6;
            featureNight();
        }
		else if (map_location == "shetland") {
            var chart_latitude = 60.32;
            var chart_longitude = -1.23;
            var chart_zoom = 6;
            var offsetLatitude1 = 3.45;
            var offsetLatitude2 = 3.8;;
            var offsetLongitude1 = 7.345;
            featureNight();
        }
		else if (map_location == 'greece') {
			var chart_latitude = 37.16;
            var chart_longitude = 25.31;
            var chart_zoom = 7;
            var offsetLatitude1 = 2.85;
            var offsetLatitude2 = 3;
            var offsetLongitude1 = 3.69;
            featureNight();
		}
		else if (map_location == 'northwestpassage') {
			var chart_latitude = 60.86;
		    var chart_longitude = -62.84;
		    var chart_zoom = 6;
		    var offsetLatitude1 = 3.4;
		    var offsetLatitude2 = 3.76;
		    var offsetLongitude1 = 7.23;
		    featureNight();
		}
		else if (map_location == 'capeverde') {
			var chart_latitude = 19.78;
		    var chart_longitude = -20.87;
		    var chart_zoom = 5;
		    var offsetLatitude1 = 13;
		    var offsetLatitude2 = 14.3;
		    var offsetLongitude1 = 14.7;
		    featureNight();
		}
		else if (map_location == 'formentera') {
			var chart_latitude = 38.61;
		    var chart_longitude = 1.49;
		    var chart_zoom = 5;
		    var offsetLatitude1 = 10.7;
		    var offsetLatitude2 = 12.3;
		    var offsetLongitude1 = 14.7;
		    featureNight();
		}
        else {
            var chart_latitude = 50.6;
            var chart_longitude = -1.3;
            var chart_zoom = 6;
            var offsetLatitude1 = 4.45;
            var offsetLatitude2 = 4.9;
            var offsetLongitude1 = 7.35;
            featureNight();
        }
    }


	for (i = 1; i < 10; i++){

		document.getElementById("map" + i).childNodes[2].style.display = "none";

		
		if (i != 7 )
		{
			document.getElementById("map" + i).childNodes[1].style.display = "none";
		}
	
	}
}


window.onload = init;
window.onunload = GUnload;

