﻿esri.config.defaults.map.logoLink = "http://www.tierraplan.com";
var helpLoaded = false;

$(document).ready(function() {
    $(".printpdf").colorbox({ inline: true, href: "#printpdf" });
   // $("#measuredialog").dialog({ title: "<span class='measure' style='font-size:12px;font-weight:bold;'>Measurements</span>", autoOpen: false, width: 225, height: 150 });

    $("#notificationform").dialog({
        title: "<span class='measure' style='font-size:12px;font-weight:bold;'>Notification Report</span>",
        autoOpen: false, width: 400, height: 450,
        open: function(type, data) {
            $(this).parent().appendTo("document.aspnetForm");
        }
    });

//    $('#btnShowreports').click(showReports);
//    $('#btnShowmap').click(hideReports);

    (function($) {
        $("#tabmenu ul li").click(function() {
            $(this).siblings().removeClass('current');
            $(this).addClass('current');
        });
    })(jQuery);

    addClientBaseLayers();
});

function addClientBaseLayers() {
    if (clientBaseServiceInfo != "") {
        if (map) {
            var clientArr = clientBaseServiceInfo.split(','); // name1:url1,name2:url2,...
            for (var clientInfo, i = -1; clientInfo = clientArr[++i]; ) {
                var lyrInfo = clientInfo.split('|');
                var lyrName = lyrInfo[0];
                var layer = null;
                
                // Add as WMS or AGS layer
                if (lyrName.indexOf("WMS:") == 0) {
                    // WMS layer: 2nd part is name, 3rd part is visible layers
                    var nameArr = lyrName.split(":");
                    layer = new WMSLayer(lyrInfo[1], { id: nameArr[1], visible: false });
                    layer.visibleLayers = nameArr[2].split();
                }
                else
                    layer = new esri.layers.ArcGISTiledMapServiceLayer(lyrInfo[1], { id: lyrName, visible: false });

                map.addLayer(layer, 0); // add at top so under any dynamic layers
                if (defaultmap && lyrInfo[0] == defaultmap)
                    setBaseMap(lyrInfo[0]);
            }
        }
        else {
            // wait until map is initialized
            setTimeout("addClientBaseLayers()", 100);
        }
    }
}

function setBaseMap(id) {
    var lyr, lyrClass;
    for (var lyrId, i = -1; lyrId = map.layerIds[++i]; ) {
        lyr = map.getLayer(lyrId);
        lyrClass = lyr.declaredClass;
        // Only show/hide base layers
        if (lyrClass == "esri.layers.ArcGISTiledMapServiceLayer" || lyrClass == "gmaps.GoogleMapsLayer" 
            || lyrClass == "esri.virtualearth.VETiledLayer" || lyrClass == "WMSLayer") {
            if (lyrId == id)
                lyr.show();
            else
                lyr.hide();
        }
    }
}

function showReports() {
    showMainTab('reportSection', 'showReportsTab');
}

function showMap() {
    showMainTab('mapSection', 'showMapTab');
}

function showHelp() {
    showMainTab('helpSection', 'showHelpTab')
}

function showMainTab(sectionId, tab) {
    if (sectionId != "mapSection")
        $('#mapSection').fadeOut('slow');
    if (sectionId != "reportSection")
        $('#reportSection').fadeOut('slow');
    if (sectionId == "helpSection") {
        if (!helpLoaded)
            $("#helpFrame").attr("src", "help/help.html");
    }
    else
        $('#helpSection').fadeOut('slow');
    $('#' + sectionId).fadeIn('slow');

    $('#' + tab).addClass('current').siblings().removeClass('current');
}

function openNR(parcel) {
    $("#notificationform").unload();
    $("#notificationform").load('NotificationReport.aspx?parcel=' + parcel.toString())
    $("#notificationform").dialog('open'); return false;
}

function customClientlink(field,fieldvalue) {
    var strHTML = ""
    if (field.toLowerCase() == "apn") {
        strHTML += "<TR><td colspan=2><a href='http://www.sloplanning.org/PermitView/ParcelLookup/Parcel/" + fieldvalue + "' target='_blank'>&nbsp;&nbsp;Parcel and Permit Lookup System</a></td></tr>";
        strHTML += "<TR><td colspan=2><a class=\"nopener\" onclick=\"openNR('" + fieldvalue.toString() + "');return false;\" href=\"#\">&nbsp;&nbsp;Notification Report</a></td></tr>";
    }
    return strHTML;
    }

function GISPsetupreports(graphic) {
    //setup reports page and point for GISP radius reports
    var sourcePt;
    switch (graphic.geometry.type) {
        case "point":
            sourcePt = graphic.geometry;
            break;
        default:
            sourcePt = graphic.geometry.getExtent().getCenter();
            break;
    }

    var reprojectedPoint = esri.geometry.webMercatorToGeographic(sourcePt);
    dojo.byId("ctl00_ContentPlaceHolder1_hdnX").value = reprojectedPoint.x.toFixed(5);
    dojo.byId("ctl00_ContentPlaceHolder1_hdnY").value = reprojectedPoint.y.toFixed(5);
}

function showReportLoading() {
    loading = document.getElementById("reportloading");
    loading.style.display = "";
}
function hideReportLoading() {
    loading = document.getElementById("reportloading");
    loading.style.display = "none";
}

function zoomtobusiness(x, y,name) {
    var newpt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 4326 })));
    map.centerAndZoom(newpt, 15);
}
