﻿


//Handles mouse right click, center map
function CenterMapOnClick(e){
    if(e.rightMouseButton){
        var x = e.mapX;
        var y = e.mapY;
        pixel = new VEPixel(x, y);
        var LL = map.PixelToLatLong(pixel);
        map.SetCenter(LL);
    }
};
//handles map mouse over events
function ShapeMouseOverHandler(e){
    //check if mouse was even over a shape (rather than just the map)
    if (e.elementID != null){
        //get shape object by its id
        // e.elementID is the root ID, plus extensions.
     
        // Use VEMap.GetShapeByID, then VEShape.GetID
        // to return the root ID.
        //var id = map.GetShapeByID(e.elementID).GetID();
        
        var shape = map.GetShapeByID(e.elementID);
        //check if shape is part of regions layer
       if(shape.GetType() == 'Point'){
            //return true to supress popup as we dont want mouseover popups for regions
            return true;
        }
    }
    return false;
};

//Debugging function to show current zoom level
//Event fired at "onendzoom"
function EndZoomHandler(){

    var strProjId;
    var frmMain = document.forms[0];
    var z = map.GetZoomLevel();
   
    if (z == prevZoom){ return false; } //If zoom has not changed, exit 
  if(z <= 9){ //Project Zoom level 1-9
    if(prevZoom > maxProjLevel) { //Zoom is coming from FlightLine or Exposure mode
        //Clear FlightLine and Exposure layers
        flLayer.DeleteAllShapes();
        expLayer1.DeleteAllShapes();
        expLayer2.DeleteAllShapes();
        expLayer3.DeleteAllShapes();
        expLayer4.DeleteAllShapes();
        DetachExposureEvents();
    }
  }
  else if(z >= minFLLevel && z <= maxFLLevel){ //FlightLine only zoom level - 11
    if(prevZoom <= maxProjLevel){ //Zoom is coming from Project mode
       //Build Flightlines
       strProjId = GetProjId();
       if(strProjId != ''){
           try{
            KeyView.ImageRetrievalService.GetFlightLines(strProjId, SuccessEval);
           }
           catch(e){
            alert(e.message);
           }
       }
    }
    else if(prevZoom >= minExpLevel){ //Zoom is coming from exposure mode 
        //Clear exposures
        expLayer1.DeleteAllShapes();
        expLayer2.DeleteAllShapes();
        expLayer3.DeleteAllShapes();
        expLayer4.DeleteAllShapes();
        DetachExposureEvents()
        
    }
    
  }
  else if(z >= minExpLevel){ 
    if(prevZoom >= minFLLevel && prevZoom <= maxFLLevel){ //Zoom is coming from FlightLine only mode
        //Build Exposures
        strProjId = GetProjId();
        if(strProjId != ''){
            try{
                AttachExposureEvents();
                //AJAXCallGetExposures(strProjId);
            }
            catch(e){
                alert(e.message);
            }
        }
    }
    else if(prevZoom <= maxProjLevel) { //Zoom is coming from project mode
        //Build FlightLines && Exposures
        strProjId = GetProjId();
        try{
            KeyView.ImageRetrievalService.GetFlightLines(strProjId, SuccessEval);
            AttachExposureEvents();
           // AJAXCallGetExposures(strProjId);
        }
        catch(e){
            alert(e.message);
        }
    }
  }
  prevZoom = z;
  document.getElementById("ctl00_Main_hdnZoomLevel").value = z;
};

function AttachExposureEvents(){
    //map.DetachEvent("onchangeview", FillCenterPoints);
    map.AttachEvent("onchangeview", AJAXCallGetExposures);
    map.AttachEvent("onclick", DrawCoverageArea);
};

function DetachExposureEvents(){
    map.DetachEvent("onchangeview", AJAXCallGetExposures);
};

//TODO: Phase 2
function DrawCoverageArea(e){
    if(e.leftMouseButton && e.elementID != null){
        var currShape = map.GetShapeByID(e.elementID);
        if(currShape.GetType() == "Point"){
            //Load div to show hyperlink options for drawing coverage area
            var dummy = document.getElementById("PointPopup");
            
           
            
            var aryTitle = new Array();
            var temp = currShape.GetTitle();
            aryTitle = temp.split(',');
            
            var spnExpNum = document.getElementById("PPTitle");
            spnExpNum.innerText = "Exposure " + aryTitle[1];
           
            dummy.style.display = 'block'; 
            dummy.style.top = e.clientY + "px";
            dummy.style.left = e.clientX + "px";

            var tdDate = document.getElementById("tdDate");
                temp = currShape.GetDescription();
                var tmpDesc = temp.split(',');
                
                tdDate.innerText = tmpDesc[2];
                
            var strExpId = aryTitle[0];
            var showTR = document.getElementById("trDrawCvg");
            var hideTR = document.getElementById("trHideCvg");
            var HideCvrg = document.getElementById("PPHideCoverage");
                HideCvrg.onclick = function() {HideCoverage(e.elementID)};
            var ShowCvrg = document.getElementById("PPDrawCoverage"); 
                ShowCvrg.onclick = function() {ShowCoverage(currShape.GetDescription(), strExpId, currShape)} ;
            
            if(shownCvgExp == strExpId)
            {              
                showTR.style.display = 'none';
                hideTR.style.display = 'block';  
            }
            else 
            {
                hideTR.style.display = 'none';
                showTR.style.display = 'block';      
            }
            
                  
            var AddExpDiv = document.getElementById("trAddExp");
            var RemoveExpDiv = document.getElementById("trRemoveExp");
            
            //Check to see if this exposure has been selected
            if(ExpIsSelected(currShape.GetTitle())== true){
                AddExpDiv.style.display = 'none';
                RemoveExpDiv.style.display = 'block';
                var RemoveExp = document.getElementById("PPRemoveExp");
                RemoveExp.onclick = function() {RemoveExpFromQuote(currShape, e.elementID)};
            }
            else{
                RemoveExpDiv.style.display = 'none';
                AddExpDiv.style.display = 'block';
                var AddExp = document.getElementById("PPAddExp");
                AddExp.onclick = function() {AddExpToQuote(currShape, e.elementID)};
            }
        }      
    }
};








//Handles onmouseover, used to squelch infobox by returning
function OnMouseOverDisableInfo(e){
    if(e.elementID != null) {  return true;   } 
};
//Handles onmouseout, used to squelch infobox by returning
function OnMouseOutDisableInfo(e){ if(e.elementID != null){  return true;  }
};

//window.onresize = function() { RebuildControl(); }

//Event triggered on window.onresize, rebuilds custom control to appropriate spot
function RebuildControl(){
    
        RemoveMyControl();
        if(map != null){
            if(map.controls != undefined){ //Blocks adding of control on startup, before map is initialized
                AddMyControl();
            }
        }
   
};





function ClearDivs(){
   document.getElementById("PointPopup").style.display = 'none';
};

function ShowCoverage(Description, strExpId, shape){
    cvgLayer.DeleteAllShapes();
    if(Description == null){ 
        var dummy = document.getElementById("PointPopup");
        dummy.style.display = 'none';
        return false;
    } 
    var aryShape = shape.GetPoints();
    var strProjId = GetProjId();
    
    shownCvgExp = strExpId;
   // KeyView.ImageRetrievalService.GetCoverageArea(strProjId, strExpId, aryShape[0].Latitude, aryShape[0].Longitude, SuccessEval);
    KeyView.ImageRetrievalService.GetCoverageArea(strProjId, strExpId, ShowCvgEval);
};

function ShowCvgEval(arg)
{ eval(arg); 
    var showTR = document.getElementById("trDrawCvg");
    var hideTR = document.getElementById("trHideCvg");
    
    showTR.style.display = 'none';
    hideTR.style.display = 'block';
}

function HideCoverage(elementId){
   cvgLayer.DeleteAllShapes();
   shownCvgExp = "";
   var showTR = document.getElementById("trDrawCvg");
    var hideTR = document.getElementById("trHideCvg");
    
    showTR.style.display = 'block';
    hideTR.style.display = 'none';
};

function DisableMapPan(){
    return true;
};







function Redirect(arg){
   Redirect2(arg);
};
function Redirect2(arg){
 var frmMain = document.forms[0];
    frmMain.action = arg;
    frmMain.submit();
};
function ShowExpInfo(){

};
function HideExpInfo(){

};



function UpdatePic(obj, Clicked) {
    if(box_depressed == false && poly_depressed == false) ClearDrawingButtons(obj);
    
    
if(obj.src.indexOf('Disabled') > -1) { return false; }
    if(Clicked){
        obj.src = obj.src.replace('.png','') + 'clicked.png';
        //obj.src = obj.src.replace('.jpg', '') + 'Clicked.jpg';
    }
    else{
        obj.src = obj.src.replace('clicked.png','.png');
        
        //obj.src = obj.src.replace('Clicked.jpg', '.jpg');
    }
}


function ClearDrawingButtons(obj) {
    var poly = document.getElementById("imgPolySelection");
    if (poly != obj) { poly.src = poly.src.replace('clicked.png', '.png'); }

    var box = document.getElementById("imgBoxSelection");
    if (box != obj) { box.src = box.src.replace('clicked.png', '.png'); }
}

//Handles checkbox events to hide/show layers
function ViewToggle(strAction, value){
    switch (strAction){
    case "PROJECT":
        if(value == true) { projLayer.Show(); }
        else { projLayer.Hide(); }
        break;
    case "LINES":
        if(value == true) { flLayer.Show(); }
        else { flLayer.Hide(); }
        break;
    case "EXPOSURE":
        if(map.GetZoomLevel() >= minExpLevel){
            if(value == true) { 
                AJAXCallGetExposures();
            }
            else { 
                expLayer1.DeleteAllShapes();
                expLayer2.DeleteAllShapes();
                expLayer3.DeleteAllShapes();
                expLayer4.DeleteAllShapes();
            }
        }
        break;
    }
}

function SelectionModeChange(){
    if (map.GetZoomLevel() >= minExpLevel) {
        AJAXCallGetExposures();
    }
};

function ClearYear() { } 