﻿//pan to a continent

var viewList = new ViewList();
function panTo(area){
    var points;
	if (area == 'Africa'){
	    points = new Array(
          new VELatLong(34.4,-15.2),          
          new VELatLong(-29,49.7)
        );
	}else if (area == 'LATAM'){
	    points = new Array(
          new VELatLong(32,-126),          
          new VELatLong(-49, -28)
        );
	}else if (area == 'US'){
	    points = new Array(
          new VELatLong(48,-126),          
          new VELatLong(30,-72)
        );
	}else if (area == 'Central Asia'){		
	    points = new Array(
          new VELatLong(39,65),          
          new VELatLong(12,98)
        );
	}else if (area == 'Asia-PAC'){
	    points = new Array(
          new VELatLong(30,101),          
          new VELatLong(0,124)
        );
	}else if (area == 'Middle East'){
	    points = new Array(
          new VELatLong(39,33),          
          new VELatLong(60,27)
        );
	}else if (area == 'Europe'){
	    points = new Array(
          new VELatLong(58,-11),
          new VELatLong(37,19)
        );
	}else if (area == 'World'){
	    points = new Array(
          new VELatLong(60,-120),
          new VELatLong(-60,120)
        );
	}else if (area == 'Asia'){
	    points = new Array(
          new VELatLong(39,65),          
          new VELatLong(12,98)
        );
	}else if (area == 'Australia-Oceania'){
	    points = new Array(
          new VELatLong(-5.4,109),          
          new VELatLong(-44.7,172)
        );
	}else if (area == 'Central America-Caribbean'){
	    points = new Array(
          new VELatLong(27,-108.6),          
          new VELatLong(9.4,-61.5)
        );
	}else if (area == 'Middle East-North Africa'){
	    points = new Array(
          new VELatLong(46.4,14.2),          
          new VELatLong(24.3,72.5)
        );
	}
	else if (area == 'North America'){
	    points = new Array(
          new VELatLong(26.4,-56.3),          
          new VELatLong(71,-164)
        );
	}else if (area == 'US Continental'){
	    points = new Array(	    
          new VELatLong(50.513427, -128.144531),          
          new VELatLong(23.563987, -65.214844)
        );
	}else if (area == 'Mid-West US Continental'){
	    points = new Array(
          new VELatLong(49.066668, -104.106445),          
          new VELatLong(36.350527, -80.419922)
        );
	}else if (area == 'North-West US Continental'){
	    points = new Array(
          new VELatLong(49.468124, -126.518555),          
          new VELatLong(31.391158, -101.733398)
        );
	}else if (area == 'North-East US Continental'){
	    points = new Array(
          new VELatLong(47.724545, -84.155273),          
          new VELatLong(36.597889, -64.6875)
        );
	}else if (area == 'South US Continental'){
	    points = new Array(
          new VELatLong(37.125286, -106.918945),          
          new VELatLong(24.20689, -74.575195)
        );
	}else if (area == 'West US'){
	    points = new Array(
          new VELatLong(49.496675, -126.5625),          
          new VELatLong(31.653381, -101.953125)
        );
	}
	
	map.SetMapView(points);

} //end of pan


function bookMarkView(){
    
    var center = map.GetCenter();
    document.getElementById("txtZoomLevel").value = map.GetZoomLevel();    
    document.getElementById("txtViewWKT").value = veLatLng2WKT( map.GetCenter());
    document.getElementById("txtViewId").value = randomString(10);
    
    displayDiv ('divViewInfo',true, 300, 200);
    
    
    
}
function saveView(){
    
    //may use isLogin function to check if login and then display login
    /*
    var name = document.getElementById("txtViewName").value;
    
    if (name == ""){
        alert ("Name is required to bookmark a view!");
        return;
    }
    var center = map.GetCenter();
    var zoomLevel = map.GetZoomLevel();
    addViewItem(name, center.Latitude, center.Longitude, zoomLevel);
    displayDiv("divViewInfo", false);
    */
}
function cancelView(){
    displayDiv ('divViewInfo',false);
}
function addViewItem(viewName, lat1, lng1, zoomLevel){
    var viewPanel = document.getElementById ("ViewPanelContent");
    var br = document.createElement("br");
    viewPanel.appendChild(br);
    var link = document.createElement("a");
    link.className = "GeographicButton";
    link.href = "javascript:gotoView(" + lat1 + ", " + lng1 + ", " + zoomLevel + ")";
    link.appendChild(document.createTextNode(viewName));    
    viewPanel.appendChild(link);
}
/*
function addViewItem(viewName, lat1, lng1, lat2, lng2){
    var viewPanel = document.getElementById ("ViewPanelContent");
    var br = document.createElement("br");
    viewPanel.appendChild(br);
    var link = document.createElement("a");
    link.className = "GeographicButton";
    link.href = "javascript:gotoView(" + lat1 + ", " + lng1 + ", " + lat2 + ", " + lng2  + ")";
    link.appendChild(document.createTextNode(viewName));    
    viewPanel.appendChild(link);
}
*/
function gotoView (lat, lng, zoomLevel){
    map.SetCenterAndZoom(new VELatLong(lat, lng), zoomLevel);
}
function gotoViewById(theId){
    viewList.gotoView(theId);
}
function deleteViews(){
    var ids = getValuesOfCheckedBox('VSViews','CHECKBOX_VIEW' );
    
    for (var i = 0; i< ids.length; i++){
        viewList.removeView(ids[i]);
        //document.getElementById('txtDeleteViewIds').value = document.getElementById('txtDeleteViewIds').value + id + ",";
    }
    //checkBoxesToWhereClause(divId, checkBoxClass, colName)
    document.getElementById('txtDeleteViewIds').value = checkBoxesToWhereClause('VSViews','CHECKBOX_VIEW', "view_id");
}
/*

function gotoView(lat1, lng1, lat2, lng2){
    var  points = new Array(
          new VELatLong(lat1*1,lng1*1),
          new VELatLong(lat2*1,lng2*1)
        );
     map.SetMapView(points);
}
*/
function View (theId, theName, theWKT, theZoomLevel){
    var viewId = theId;
    var viewName = theName;
    var wkt = theWKT;    
    //alert ("wkt is " + wkt);
    var zoomLevel = theZoomLevel*1;   
    var veLatLng = null;
    
    this.getId = getId;
    this.getName = getName;
    this.gotoView = gotoView;
    
    function getId(){
        return viewId;
    }   
    function getName(){
        return viewName;
    }
    function gotoView(){
        if (veLatLng == null){
            var veLatLngArr = wkt2VELatLngArr(wkt);
            veLatLng = veLatLngArr[0];
        }
        //alert (veLatLng.Latitude);
        map.SetCenterAndZoom(veLatLng, zoomLevel);
    }
}

function ViewList(){
    var list = new Vector(0);
    
    this.addViewObj = addViewObj;
    this.addView = addView;
    this.removeView = removeView;
    this.getIndexOf = getIndexOf;
    this.gotoView = gotoView;
    
    function addViewObj (view){
        if (!list.contains(view)){
            list.addElement(view);
        }
    }
    function addView(theId, theName, theWKT, theZoomLevel){
        var view = new View(theId, theName, theWKT, theZoomLevel);  
        //alert ("addview" + theId+ theWKT+ theZoomLevel);      
        addViewObj(view);
    }
    function removeView(theId){
        var index = getIndexOf(theId);
        if ( index== -1){
            return;
        }
        list.removeElementAt(index);       
    }
    function getIndexOf(viewId){  
        
        for (var i = 0; i < list.getSize(); i++){
            var view = list.getElementAt(i);
            if (view.getId () == viewId){
                //alert ("found" + i);
                return i;
            }
        }
        //alert ("not found, length" + i);
        return -1;
    }
    function gotoView(theId){
        var index = getIndexOf(theId);
        if ( index== -1){
            return;
        }
        list.getElementAt(index).gotoView();
    }
}