
var map;
var processClick = false;

function GetMap() {
    DoFireFox();
    SizeElements();
    var mapArgs = new GeomaticTechnologies.GMU.MapArgs("myMap", new VELatLong(-37, 146), 7, VEMapStyle.Shaded, false, VEMapMode.Mode2D, VEDistanceUnit.Kilometers);
    map = new GeomaticTechnologies.GMU.Map(GroundwaterOnline.BoreService, mapArgs);
	map.GetMap().SetCredentials("ApPTWnC0qqGGryXghEErgmEpLWmPHTJI0PEn1yGkdwNlTmnvxcZZ28UmuPIl884H");     
map.GetMap().SetDashboardSize(VEDashboardSize.Normal);
    map.GetMap().ShowSwitch = 0;
    map.GetMap().AttachEvent('onclick', MapSearch);
    map.GetMap().SetMapStyle(VEMapStyle.Shaded);
    document.getElementById("myMap").style.display = "block";
    SetupMapControl();
    SizeMapToMenu();
}

function DoFireFox() { // Firefox support - see VE wiki.
    Msn.Drawing.Graphic.CreateGraphic = function(f, b) {
        if (document.all) {
            return new Msn.Drawing.VMLGraphic(f, b);
        }
        else {
            var d = new RegExp("Firefox/(.*)").exec(navigator.userAgent);
            if (d[1] && parseFloat(d[1]) >= 1.5) return new Msn.Drawing.SVGGraphic(f, b);
            throw new Msn.Drawing.Exception(L_GraphicsInitError_Text)
        }
    };
}

function SizeElements() {
    var mapHeight = document.body.clientHeight - 245;
    if (mapHeight < 500) mapHeight = 500;
    document.getElementById("myMap").style.height = mapHeight.toString() + "px";
    document.getElementById("Iframe_Report").style.height = (document.body.clientHeight - 245) + "px";
    document.getElementById("Iframe_SearchDetail").style.height = (document.body.clientHeight - 245) + "px";
}

function SizeMapToMenu() {
    var mapDiv = document.getElementById("myMap");
    if (mapDiv.offsetHeight < document.getElementById("mapControl").offsetHeight + 20) {
        var newHeight = document.getElementById("mapControl").offsetHeight + 20
        mapDiv.style.height = newHeight + "px";
    }
}

function SetupMapControl() {
    var controlDiv = document.getElementById('mapControl');
    Drag.init(document.getElementById('mapcontroldrag'), controlDiv, null, null, null, null, true, false);
    controlDiv.style.position = "absolute";
    controlDiv.style.top = "135px";
    controlDiv.style.right = "2px";
    controlDiv.style.display = "block";
}

function OverlayClose(subobj) {
    document.getElementById(subobj).style.display = "none";
}

function ToggleBoreLayer(active) {
    map.ToggleBoreLayer(active);
}

function ResetMap() {
    document.getElementById("MapResult").style.display = "none";
    map.GetMap().DeleteAllPushpins();
    map.GetMap().SetCenterAndZoom(new VELatLong(-37, 146), 7);
}

function ToggleOptionSelected(toggled) {
    if (toggled) {
        processClick = true;
        document.getElementById("t1").style.display = "none";
        document.getElementById("myMap").childNodes[0].style.cursor = "crosshair";
    }
    else {
        processClick = false;
        document.getElementById("t2").style.display = "none";
        document.getElementById("myMap").childNodes[0].style.cursor = "";
    }
}

function GoToReviewEdits() {

    document.getElementById("Iframe_ReviewEdits").src = "EditSummary.aspx";
    //OverlayClose('MapResult');
    TabClick('menuTabReview');
    //SetFrameHeight("Iframe_ReviewEdits");
}

function SearchNavigate(gmu) {
    if (gmu >= 0) {
        document.getElementById("Iframe_SearchDetail").src = "SearchResult.aspx?gmu=" + gmu;
        OverlayClose('MapResult');
        TabClick('menuTabSearch');
        SetFrameHeight("Iframe_SearchDetail");
    }
}

function AquiferNavigate(aquifer) {
    if (aquifer >= 0) {
        document.getElementById("Iframe_IGC").src = "IGCAquiferDetails.aspx?aquifer=" + aquifer;
        TabClick('menuTabIGC');
    }
}

function BoreNavigate(bore) {
    if (bore >= 0) {
        document.getElementById("Iframe_IGC").src = "IGCBoreDetails.aspx?bore=" + bore;
        TabClick('menuTabIGC');
    }
}

function BoreListNavigate(gmu) {
    if (gmu >= 0) {
        document.getElementById("Iframe_IGC").src = "IGCBoreList.aspx?gmu=" + gmu;
        TabClick('menuTabIGC');
    }
}

function BoreReadingReport(readingId) {
    if (readingId >= 0) {
        TabClick('menuTabReport');
        document.getElementById("Iframe_Report").src = "http://reports.test.gmu.geomatic.com.au/pls/apex/f?p=104:1:0::NO::P1_BORE_READING_ID:" + readingId;
        SetFrameHeight("Iframe_Report");
    }
    //return false;
}

function AquiferReadingReport(readingId) {
    if (readingId >= 0) {
        TabClick('menuTabReport');
        document.getElementById("Iframe_Report").src = "http://reports.test.gmu.geomatic.com.au/pls/apex/f?p=104:2:0::NO::P2_MASTER_READING_ID:" + readingId;
        SetFrameHeight("Iframe_Report");
    }
    //return false;
}

function MapSearch(e) {
    if (processClick) {
        try {
            map.GetMap().DeleteAllPushpins();

            var x = e.mapX;
            var y = e.mapY;
            var ll = map.GetMap().PixelToLatLong(new VEPixel(x, y));
            var pin = new VEPushpin(1, ll, null, 'Query Coordinates', ll.toString(), 'pushpinstyle');
            map.GetMap().AddPushpin(pin);

            var coords = new Array();
            coords = ll.toString().split(', ');
            document.getElementById("Iframe_MapSearch").src = "MapSearch.aspx?coords=" + coords[1] + "," + coords[0];
            var theRoot = document.getElementById("MapResult");
            theRoot.style.display = "block";
            theRoot.style.top = "216px";
            theRoot.style.left = "360px";
            Drag.init(document.getElementById("MapHandle"), theRoot);
        }
        catch (err) {
            alert(err.message);
        }
    }
}

function checkedLayer(layerId, activate) {
    if (activate) LoadLayer(layerId);
    else HideLayer(layerId);
}

function OpenEdit(editWhat, editId, extraString) {
    document.getElementById("Iframe_SearchDetail").src = "EditPages/" + editWhat + ".aspx?gmu=" + editId + extraString;
    SetFrameHeight("Iframe_SearchDetail");
    scroll(0, 0);
}

function OpenEditIGC(editWhat, editId, extraString) {
    document.getElementById("Iframe_SearchDetail").src = editWhat + ".aspx?aquifer=" + editId + extraString;
    SetFrameHeight("Iframe_SearchDetail");
    scroll(0, 0);
}

function showHydrograph(boreId) {

    document.getElementById('img_Hydrograph').innerHTML = '<iframe id="iframe_' + boreId
    + ' frameborder="0" style="width:100%;height:500px;" scrolling="no" src="http://reports.test.gmu.geomatic.com.au/pls/apex/f?p=104:3:1::NO::P3_BORE_ID:' +
    + boreId + '" /></iframe>';
    
    var div = document.getElementById('Div_Hydrograph');
    div.style.display = "block";
    div.style.top = "216px";
    div.style.left = "360px";
    Drag.init(document.getElementById('Hydro_title'), document.getElementById('Div_Hydrograph'));
}

//function showHydrograph(url) {
//    document.getElementById('img_Hydrograph').innerHTML = '<img src="' + url + '" style="height:480px;width:640px"/>';
//    var div = document.getElementById('Div_Hydrograph');
//    div.style.display = "block";
//    div.style.top = "216px";
//    div.style.left = "360px";
//}

function FindLocality() {
    try {
        map.GetMap().Find(null, document.getElementById('locality').value + ', Victoria, Australia');
    }
    catch (err) {
        alert(err.message);
    }
    return false;
}


function DisplayLegend(image, active) {

    //BUILD DIV
    var div = document.createElement('div');   
    var iconIdName = image.replace(/ /g, '');
    div.setAttribute("id", "div" + iconIdName);

    //ADD LINE BREAK
    var spacer = document.createElement('div');
    spacer.innerHTML = "<br>";
    div.appendChild(spacer); 
    
    //BUILD IMAGE
    var icon = document.createElement('img');
    // ON ERROR
    icon.onerror = img_onError;   
    icon.setAttribute("id", "img" + iconIdName);
    icon.setAttribute("src", "Images/" +iconIdName + ".png");
    icon.className = "legend";
    icon.style.visibility = "visible";
    
    div.appendChild(icon)
   

    if (active) {
        //ADD LEGEND
        document.getElementById("imageLegend").appendChild(div);
    }
    else {
      //REMOVE LEGEND
        try {
            var item = document.getElementById("div" + iconIdName);
            item.parentNode.removeChild(item);
        } catch (e) { }

    }
}

//REMOVE IMAGES THAT DONT EXIST
function img_onError() {
    try {       
        var item = document.getElementById(this.parentNode.id);
        item.parentNode.removeChild(item);
    } catch (e) { }
}
    
 
