﻿
var currentLL;
var infoBoxOpen = false;
var divInfoBox;
function leftClickOnShape(e){             
    if (e.elementID != null){        
        if (e.leftMouseButton){
            var shape = map.GetShapeByID(e.elementID);   
            if (shape.GetType() == VEShapeType.Polygon || shape.GetType() == VEShapeType.Polyline || shape.GetType() == VEShapeType.Pushpin){
                //map.ShowInfoBox(shape);
                showCustomInfoBox(e, shape);
            }
               
        }
    }
    //displayMsgBox("event...", e.eventName);
    return true; 
    
}
function noOp(e){
    //if (e.eventName == "onmouseup"){
        //displayMsgBox("event...", e.eventName);
    //}
    return true;
}
function myOnMouseOver(e){
    //alert ("mouse over");
    if(e.elementID){
        //  fetch the polgon we are moused over
        var shape = map.GetShapeByID(e.elementID);
        //  increase its width
        if ((shape.GetType() == VEShapeType.Polygon) ||(shape.GetType() == VEShapeType.Polyline)){
            shape.SetLineWidth(3); 
        }
    }
       
    return true;  

}
function myOnMouseOut(e){
    if(e.elementID){
        var shape = map.GetShapeByID(e.elementID);
        //  increase its width
        if ((shape.GetType() == VEShapeType.Polygon) ||(shape.GetType() == VEShapeType.Polyline)){
            shape.SetLineWidth(2);            
        }        
    }    
    return true;
}
function fixImg(){
 var imgs = document.getElementById("MapPanel").getElementsByTagName("img");
    for (var i =0; i < imgs.length; i++){
        
        var img = imgs[i];
        src = img.src;
        //alert (src);
        img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft." +	"AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
    }
}
function attachEvents4IE6Only(){
    if (isIE6OrLower()){
        map.AttachEvent("onendpan", fixTiles);
        map.AttachEvent("onendzoom", fixTiles);    
        map.AttachEvent("onchangeview", fixTiles);
    }
}
function attachDefaultMouseEvents(){
    map.AttachEvent("onclick",leftClickOnShape);
    
    map.DetachEvent("onmouseover", noOp);
    map.DetachEvent("onmouseout", noOp);
    
    map.AttachEvent("onmouseover", myOnMouseOver);
    map.AttachEvent("onmouseout", myOnMouseOut);
   
    map.AttachEvent("onendpan", myOnEndPan);
    map.AttachEvent("onendzoom", myOnEndZoom);
    
    map.AttachEvent("onchangeview", OnChangeView);
    
    
    
    //map.AttachEvent("onmouseup", noOp);
    //map.AttachEvent("onmousedown", noOp);
    
}  
function detachDefaultMouseEvents(){
    map.DetachEvent("onclick",leftClickOnShape);
    //un-comment below lines if you want to show infobox on hover event when drawing
    
    map.DetachEvent("onmouseover", myOnMouseOver);
    map.DetachEvent("onmouseout", myOnMouseOut);
    map.AttachEvent("onmouseover", noOp);
    map.AttachEvent("onmouseout", noOp);
    
    map.DetachEvent("onstartpan", myOnStartPan);
    map.DetachEvent("onstartzoom", myOnStartZoom);
    
}

function showCustomInfoBox(e, shape){
    infoBoxOpen = true;
    divInfoBox = document.getElementById("divInfoBox");
    var theTitle = document.getElementById('infoBoxTitle');
    var theDetails = document.getElementById('infoBoxDetails');

    theTitle.innerHTML = "<B>" + shape.GetTitle() + "</B>";
    theDetails.innerHTML = shape.GetDescription();
    /*
    theInfoBox.style.display = "block";
    theInfoBox.style.top = e.clientY + 'px';
    theInfoBox.style.left = e.clientX + 'px';
    */
    var x = e.mapX;
    var y = e.mapY;

    pixel = new VEPixel(x, y);
    //currentLL = map.PixelToLatLong(pixel);
    currentLL = getShapeCentroid(shape);
    updateInfoBoxPosition();   
    
    //displayDiv('divInfoBox', true, e.mapX, e.mapY, 0);
    /*
    if (getEleWidth("divInfoBox") > 200){
        setElementWidth("divInfoBox", 200);
    }
    */
    
}

function hideInfoBox(){
    displayDiv("divInfoBox", false);
    infoBoxOpen = false;
}
function myOnStartPan(e){    
    return true; 
}
function myOnEndPan(e){    
    updateInfoBoxPosition();   
    
    return true; 
}

function myOnStartZoom(e){

    return false; 
}
function myOnEndZoom(e){    
    
    updateInfoBoxPosition();
    //fixImg();
    
    return true; 
}


function updateInfoBoxPosition(){  
    if (!infoBoxOpen){
        return; 
    }  
    if (currentLL){
        var pixel = map.LatLongToPixel( currentLL);          
        showElementWithinMap("divInfoBox", pixel.x, pixel.y, 0, false);
       
        
        /*
        if (isInsideMapView(pixel.x, pixel.y, map)){
            //if (infoBoxOpen){
                displayDivByObj(divInfoBox, true, pixel.x, pixel.y , 0);
            //}
        }else {
            displayDivByObj(divInfoBox, false, pixel.x, pixel.y , 0);
        }
        */
    }
    
   
}

function getShapeCentroid(shape){
    var latlngArr = shape.GetPoints();
    latlngArr.sort (latLngCompare);
    //take the bottom one
    return latlngArr[0];
}
function latLngCompare(latlng1, latlng2){
    if (latlng1.Latitude == latlng2.Latitude){
        return (latlng1.Longitude - latlng2.Longitude);
    }
    return latlng1.Latitude - latlng2.Latitude;
}

function lngDecrease(latlng1, latlng2){
    if (latlng1.Longitude == latlng2.Longitude){
        return (latlng1.Latitude - latlng2.Latitude);
    }
    return latlng2.Longitude - latlng1.Longitude;
}


function RemoveBlack()
{
    // ALL THIS NEEDS TO BE DONE FOR IE7 ONLY
    
     if(isIE6OrLower()){
    
        var parentMapDiv = document.getElementById('MapPanel');
        var childDivs = parentMapDiv.getElementsByTagName('DIV');
        //var childDivs = childMapDiv.childNodes;
        for(var i = 0; i < childDivs.length; i++)
        {
            var childNode = childDivs[i];
            
            //alert (childNode.src + " height: " + childNode.style.height + " filter is:" +  childNode.style.filter + " bagrond is:" + childNode.style.background);
            if (childNode.style.filter !="none" & childNode.offsetWidth ==256 & childNode.offsetHeight ==256){
            
                var filter = childNode.style.filter;
                var index = filter.indexOf(", alpha");
                //alert (filter);
                if (index > -1){
                    filter = filter.substring(0, index);
                
                    childNode.style.filter = filter ;
                }
                
               
            }
           
          
            
        }
        
        
    }        
    
    
   
}
function updatePageView(){
    window.setTimeout("noOp();");
}
function OnChangeView(e){
    
}
function fixTiles(){
    RemoveBlack();
    window.setTimeout("RemoveBlack();", 3000);       
    
}



