var epiGoogleAjax = null;
var epiGoogle = null;
var epiGoogleLarge = null;
var largeMapVisible = false;

var mapInfo = {
    "MapWidth": 870,
    "MapHeight": 477,
    "CenterOnLat": 56.1592,
    "CenterOnLong": 13.7701,
    "InitialZoomLevel": 7,
    "Keyboard": false,
    "Controls_LargeMapControl3d": true,
    "Controls_MapTypeControl": true,
    "Controls_MenuMapTypeControl": false,
    "Controls_ScaleControl": true,
    "Controls_SmallZoomControl3d": false,
    "MapTypes_Hybrid": false,
    "MapTypes_Normal": true,
    "MapTypes_Physical": true,
    "MapTypes_Satellite": true,
    "Zoom_DoubleClick": true,
    "Zoom_ScrollWheel": true
};
var mapInfoSmall = {
    "MapWidth": 195,
    "MapHeight": 195,
    "CenterOnLat": 56.1592,
    "CenterOnLong": 13.7701,
    "InitialZoomLevel": 13,
    "Keyboard": false,
    "Controls_LargeMapControl3d": false,
    "Controls_MapTypeControl": false,
    "Controls_MenuMapTypeControl": false,
    "Controls_ScaleControl": false,
    "Controls_SmallZoomControl3d": false,
    "MapTypes_Hybrid": false,
    "MapTypes_Normal": true,
    "MapTypes_Physical": false,
    "MapTypes_Satellite": false,
    "Zoom_DoubleClick": true,
    "Zoom_ScrollWheel": true
};
var mapInfoLarge = {
    "MapWidth": 645,
    "MapHeight": 420,
    "CenterOnLat": 56.1592,
    "CenterOnLong": 13.7701,
    "InitialZoomLevel": 14,
    "Keyboard": false,
    "Controls_LargeMapControl3d": true,
    "Controls_MapTypeControl": true,
    "Controls_MenuMapTypeControl": false,
    "Controls_ScaleControl": true,
    "Controls_SmallZoomControl3d": false,
    "MapTypes_Hybrid": false,
    "MapTypes_Normal": true,
    "MapTypes_Physical": true,
    "MapTypes_Satellite": true,
    "Zoom_DoubleClick": true,
    "Zoom_ScrollWheel": true
};

google.load('maps', '2.X');
google.load('search', '1');
google.setOnLoadCallback(onApisLoaded);

function onApisLoaded() {
    $(document).ready(function() {
        if (document.getElementById("mapDiv") != null) {
            epiGoogle = new EPiGoogleMap(document.getElementById("mapDiv"), mapInfo, false, null);
            if (typeof (localeJsonArray) != 'undefined')
                loadCapitexObjectsFromArray(localeJsonArray);
        }
        if (document.getElementById("smallMapDiv") != null) {
            epiGoogle = new EPiGoogleMap(document.getElementById("smallMapDiv"), mapInfoSmall, false, {"left": null, "top": null, "copyTextSize": "75%"});
            if (typeof (localeJson) != 'undefined')
                epiGoogle.AddCapitexObjectAndCenter(localeJson, false);
        }
        if (document.getElementById("largeLocaleMapDiv") != null) {
            epiGoogleLarge = new EPiGoogleMap(document.getElementById("largeLocaleMapDiv"), mapInfoLarge, true, null);
            epiGoogleAjax = new EPiGoogleAjax(epiGoogleLarge, null);
            if (typeof (localeJson) != 'undefined')
                epiGoogleLarge.AddCapitexObjectAndCenter(localeJson);
        }
    });
}

function toggleLargeMap() {
    if (largeMapVisible == false) {
        $("#largeLocalMapContainer").show("fast", null);
        $("#largeMapCategoriesContainer").show("fast", null);
        $("#smallMap").hide("fast", null);
        largeMapVisible = true;
    }
    else {
        $("#largeLocalMapContainer").hide("fast", null);
        $("#largeMapCategoriesContainer").hide("fast", null);
        $("#smallMap").show("fast", null);
        largeMapVisible = false;
    }
}

function loadCapitexObjectsFromArray(objArray) {
    for (var i = 0; i < objArray.length; i++)
        epiGoogle.AddCapitexObject(objArray[i]);

    epiGoogle.ZoomToFit(null, 14);
}

function centerOnObject(id) {
    var obj = epiGoogle.GetCapitexObject(id);
    if (obj != null)
        obj.CenterOnThis();
}

function setCategory(id, checked, checkBoxElement) {
    if (checked == true) {
        epiGoogleAjax.GetCategoryObjects(id, function(data) {
            epiGoogleAjax.LoadCategory(id, true, data, document.getElementById('loaderImage'), checkBoxElement);
        });
    }
    else
        epiGoogleLarge.RemoveCategory(id);
}
