//global variables to track current launch map site
var g_bUseStreetView_PV = false;    //use google street view for picture view
var g_bUseBirdEye_PV = false;       //use bird's eye view for picture view
var g_sLaunchApp = "";              //application to launch from integration button
var gTheme = "";                    //set theme to launch application into

function loadToolSettings() {
    var mapApp = getCookie("MapLaunchApp");
    if (mapApp!=null && mapApp!="") {
        g_sLaunchApp = mapApp;
    } else {
        //default to nothing
        g_sLaunchApp = ""
    }
    
    var useStreetView = getCookie("UseStreetView");
    if (useStreetView!=null && useStreetView!="") {
        if (useStreetView == "true") {
            g_bUseStreetView_PV = true;
        } else {
            g_bUseStreetView_PV = false;
        }
    } else {
        //default to false
        g_bUseStreetView_PV = false;
    }

    var useBirdsEyeView = getCookie("UseBirdsEyeView");
    if (useBirdsEyeView!=null && useBirdsEyeView!="") {
        if (useBirdsEyeView == "true") {
            g_bUseBirdEye_PV = true;
        } else {
            g_bUseBirdEye_PV = false;
        }
    } else {
        //default to false
        g_bUseBirdEye_PV = false;
    }
    
    //set the initial state of the radio buttons on the tools setting dialog box
    dijit.registry.byClass("dijit.form.RadioButton").forEach(function(radiobtn) {
        if (radiobtn.id.indexOf("rbLaunchApp") == 0) {
            if (radiobtn.value.toUpperCase() == g_sLaunchApp.toUpperCase()) {
                radiobtn.attr("checked", true);
            } else {
                radiobtn.attr("checked", false);
            }
        }
    });
    
    //set the checked value for the picture viewers
    dijit.byId("cbGoogleStreet").attr("checked", g_bUseStreetView_PV);
    dijit.byId("cbBirdsEye").attr("checked", g_bUseBirdEye_PV);
}

function setPictureView(cb) {
    switch (cb.name.toUpperCase()) {
        case "STREETVIEW":
            if (cb.checked) {
                g_bUseStreetView_PV = true;
            } else {
                g_bUseStreetView_PV = false;
            }
            break;
        case "BIRDSEYEVIEW":
            if (cb.checked) {
                g_bUseBirdEye_PV = true;
            } else {
                g_bUseBirdEye_PV = false;
            }
            break;
    }

    //save the settings to a cookie
    setCookie("UseStreetView", g_bUseStreetView_PV.toString(), 365);
    setCookie("UseBirdsEyeView", g_bUseBirdEye_PV.toString(), 365);
}

function showPictureView(evt) {
    //be sure we have something set for the picture viewer
    if ((g_bUseStreetView_PV == false) && (g_bUseBirdEye_PV == false)) {
        alert("No default picture viewer set.  Please use the 'Settings' option to set a default viewer.");
    } else {
        //launch the requested viewing application(s)
        if (g_bUseStreetView_PV) launchStreetView(evt);
        if (g_bUseBirdEye_PV) launchBirdEyeView(evt);            
    }
}

function setLaunchApplication(appName) {
    if (appName == "KnoxNetWhere") {
        g_sLaunchApp = "KnoxNetWhere";
    } else if (appName == "MPCCases") {
        g_sLaunchApp = "MPCCases";   
    } else if (appName == "GoogleMap") {
        g_sLaunchApp = "GoogleMap";
    } else if (appName == "GoogleStreetView") {
        g_sLaunchApp = "GoogleStreetView";
    } else if (appName == "MicrosoftLiveSearch") {
        g_sLaunchApp = "MicrosoftLiveSearch";
    } else if (appName == "MicrosoftLiveSearch3d") {
        g_sLaunchApp = "MicrosoftLiveSearch3d";
    } else {
        g_sLaunchApp = "";
    }
    
    //store this value in a cookie
    setCookie("MapLaunchApp", g_sLaunchApp, 365);
}

function setLaunchTheme(selSelectObject) {
    gTheme;
   
    if (selSelectObject.options[selSelectObject.selectedIndex].value != "") { 
        gTheme = (selSelectObject.options[selSelectObject.selectedIndex].value);
    }
}

function launchIntegrationMap() {
    if (g_sLaunchApp == "") {
        alert("No default integration application has been set.  Please use the 'Settings' option to set a default.");
    } else {    
        //have an app so get to launching
        switch (g_sLaunchApp.toUpperCase()) {
            case "MICROSOFTLIVESEARCH":
                launchMSLiveMaps();
                break;
            case "MICROSOFTLIVESEARCH3D":
                launchMSLiveMaps3D();
                break;
            case "GOOGLEMAP":
                launchGoogleMaps();
                break;
            case "GOOGLESTREETVIEW":
                launchGoogleStreetMaps();
                break;
            //added by jeb for MPCCases
            case "MPCCASES":
                launchAdvancedMap();
                //alert("testing at this time.");
                break;
            case "KNOXNETWHERE":
                alert("Not implemented at this time.");
                break;
        }
    }
}

function launchMSLiveMaps() {
    //get the center, upper right, and lower left map points in longitude & latitude
    var mapCenter = getGeoCenterPoint();

    //example URL
    //http://maps.live.com/default.aspx?v=2&FORM=LMLTCC&cp=36.012241~-83.986595&style=r&lvl=16&tilt=-90&dir=0&alt=-1000&phx=0&phy=0&phscl=1&encType=1
    //http://maps.live.com/default.aspx?v=2&FORM=LMLTCC&cp=35.96567~-83.934388&style=r&lvl=15&tilt=-90&dir=0&alt=3375.13818090037&cam=35.96567~-83.934388&phx=0&phy=0&phscl=1&encType=1
    //http://maps.live.com/default.aspx?v=2&FORM=LMLTCC&cp=35.96567~-83.934388&style=h&lvl=15&tilt=-90&dir=0&alt=3375.13818090037&cam=35.96567~-83.934388&phx=0&phy=0&phscl=1&encType=1
    //var sURL = MS_LiveSearch_LaunchPage + '?v=2&FORM=LMLTCC&cp=' + mapCenter.latitude + '~' + mapCenter.longitude + '&style=r&lvl=15&tilt=-90&dir=0&alt=-1000&phx=0&phy=0&phscl=1&encType=1'

    var sURL = MS_LiveSearch_LaunchPage + '?v=2&cp=' + mapCenter.latitude + '~' + mapCenter.longitude + '&style=o&lvl=1';
    window.open(sURL);
}

function launchMSLiveMaps3D() {
    //get the center, upper right, and lower left map points in longitude & latitude
    var mapCenter = getGeoCenterPoint();

    var sURL = MS_LiveSearch_LaunchPage + '?v=2&cp=' + mapCenter.latitude + '~' + mapCenter.longitude + '&style=h&tilt=-25.2249831756733&dir=360&alt=439.139795442112&cam=' + mapCenter.latitude + '~' + mapCenter.longitude + '&phx=0&phy=0&phscl=1&encType=1';
    window.open(sURL);
}

function launchGoogleMaps() {
    //get the center, upper right, and lower left map points in longitude & latitude
    var mapCenter = getGeoCenterPoint();

    //get the upper right and lower left map points in longitude & latitude
    //get the current map extent
    var geoUR = convertSPtoLL(Math.round(map.extent.xmax), Math.round(map.extent.ymax));
    var geoLL = convertSPtoLL(Math.round(map.extent.xmin), Math.round(map.extent.ymin));

    //get the map span
    var yDiff = geoUR.latitude - geoLL.latitude;
    var xDiff = Math.abs(Math.abs(geoUR.longitude) - Math.abs(geoLL.longitude));
    
    //create the URL and open the window
    var sURL = Google_LaunchPage + '?ie=UTF8&ll=' + mapCenter.latitude + ',' + mapCenter.longitude + '&spn=' + yDiff + ',' + xDiff;
    window.open(sURL);
}

function launchGoogleStreetMaps() {
    //get the center, upper right, and lower left map points in longitude & latitude
    var mapCenter = getGeoCenterPoint();

    //get the upper right and lower left map points in longitude & latitude
    var geoUR = convertSPtoLL(Math.round(map.extent.xmax), Math.round(map.extent.ymax));
    var geoLL = convertSPtoLL(Math.round(map.extent.xmin), Math.round(map.extent.ymin));

    //get the map span
    var yDiff = geoUR.latitude - geoLL.latitude;
    var xDiff = Math.abs(geoUR.longitude) - Math.abs(geoLL.longitude);
    
    //create the URL and open the window
    var sURL = Google_LaunchPage + '?ie=UTF8&layer=c&cbll=' + mapCenter.latitude + ',' + mapCenter.longitude + '&panoid=09B6D6we7YPbQyXWiDzGcg&cbp=12,288.2479749636967,,0,5&ll=' + mapCenter.latitude + ',' + mapCenter.longitude + '&spn=' + yDiff + ',' + xDiff;
    window.open(sURL);
}

function launchBirdEyeView(evt) {
    //get the map point clicked by user and convert to longitude & latitude coordinates
    var mapX = Math.round(evt.mapPoint.x);
    var mapY = Math.round(evt.mapPoint.y);
    var geoPoint = convertSPtoLL(mapX, mapY);
    
    var GSurl = ('http://www.kgis.org/KGIS_Lite/LiveMapSearch.htm?yval=' + geoPoint.latitude + '&xval=' + geoPoint.longitude);
    popup2 = window.open(GSurl, 'resultsContent', config='height=425, width=550, left=30, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}

function launchStreetView(evt) {
    //clear any graphics
    map.graphics.clear();
          
    //get the mouse click point
    var mapX = Math.round(evt.mapPoint.x);
    var mapY = Math.round(evt.mapPoint.y);
	        	    
	//create the graphic point for the Google streetview man
    var point = new esri.geometry.Point(mapX, mapY, map.spatialReference);
    var symbol = new esri.symbol.MarkerSymbol().setAngle(360);
    var symbol = new esri.symbol.PictureMarkerSymbol("http://www.kgis.org/kgis_lite/images/gsv_stop.gif", 18, 30);
    var graphic = new esri.Graphic(point, symbol);
    map.graphics.add(graphic)
	    	    
    //get the longitude and latitude for this point
    var geoPoint = convertSPtoLL(mapX, mapY);
    
    //var GSurl = ('http://test.kgis.net/JavaScript_API/gMapDojo/SV.htm?yval=35.976528&xval=-83.921678');
    var gsvURL = GoogleStreetViewPage + "?yval=" + geoPoint.latitude + "&xval=" + geoPoint.longitude;
    var popup = window.open(gsvURL, 'gsvWindow', config='height=350, width=550, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no');
}



//used for launching KGIS KnoxNetWhere - need encrypted coordinate values for querystring
function launchAdvancedMap() {
    var xMin = map.extent.xmin;
    var yMin = map.extent.ymin;
    var xMax = map.extent.xmax;
    var yMax = map.extent.ymax;
    var sCoords = xMin.toString() + ":" + yMin.toString() + ":" + xMax.toString() + ":" + yMax.toString();
     
    //var params = {"value":'"' + sCoords + '"'}
    //var params = {"value":'"' + gTheme + '"'}
    var params = (gTheme + "&area=" + sCoords);
     launchMap(params);
     
//    dojo.xhrGet(
//    {
//        url: "./wsSearches.asmx/GetEncryptedValue",
//        handleAs: "json",
//        contentType: "application/json; charset=utf-8",
//        content: params,
//        load: launchMap,
//        error: function(error,args){
//            console.warn("error!",error);
//        }
//    });            
}

