//  COPYRIGHT © 2006 ESRI
//
//  TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
//  Unpublished material - all rights reserved under the
//  Copyright Laws of the United States and applicable international
//  laws, treaties, and conventions.
// 
//  For additional information, contact:
//  Environmental Systems Research Institute, Inc.
//  Attn: Contracts and Legal Services Department
//  380 New York Street
//  Redlands, California, 92373
//  USA
// 
//  email: contracts@esri.com

ï»¿// JScript File

var esriMagnifiers = new Object;


function Magnifier(controlName, transparency, mapCallbackFunctionString, magnifiyFactor, mapBuddyId, blankImagePath)
{
    this.controlName = controlName;
    this.floatingPanel = document.getElementById(controlName);
    this.floatingPanelBodyCell=document.getElementById(controlName+'_BodyCell');
    this.contentDiv=document.getElementById(controlName+'_ContentDiv');
    this.contentTable=document.getElementById(controlName+'_Content');
    this.contentsContainer=document.getElementById(controlName+'_Contents'); 
    this.factorRow=document.getElementById(controlName+'_FactorRow')
    this.magnifierAOI = document.getElementById(controlName+'_AOIBox');;
    this.magnifierCrosshair = document.getElementById(controlName+'_Crosshair'); 
    this.mapCallbackFunctionString=mapCallbackFunctionString; 
    this.mapImage = document.getElementById(controlName+'_MapImage');
    this.mapArea = document.getElementById(controlName+'_MapArea');
    
    this.mapBuddyId = mapBuddyId;
    this.blankImagePath = blankImagePath; 
    this.extentString = ""; 
    
    this.setAOIBox = function() {
	    var f = document.forms[0];
	    var magSelect = document.getElementById(controlName + "_MagFactor");
	    var index = magSelect.selectedIndex;
	    magnifier=FloatingPanels[this.controlName]; 
	    var val = parseInt(magSelect.options[index].value);
	    var wDiv = this.contentDiv;
	    var aoi = this.magnifierAOI;
	    var magcross = this.magnifierCrosshair;
        var winWidth = wDiv.clientWidth;
        var winHeight = wDiv.clientHeight;
	    var aoiWidth = Math.ceil(winWidth/val);
	    var aoiHeight = Math.ceil(winHeight/val);
	    var magMidX = Math.ceil(winWidth/2);
	    var magMidY = Math.ceil(winHeight/2);
	    var crossWidth = parseInt(magcross.style.width);
	    var crossHeight = parseInt(magcross.style.height);
	    aoi.style.width = aoiWidth + "px";
	    aoi.style.height = aoiHeight + "px";
	    aoi.style.left = Math.floor(magMidX-(aoiWidth/2)) + "px";
	    aoi.style.top = Math.floor(magMidY-(aoiHeight/2)) + "px";
	    magcross.style.left = (Math.floor(magMidX-(crossWidth/2)) + 2) + "px";
	    magcross.style.top = (Math.floor(magMidY-(crossHeight/2)) + 2) + "px";
	    var crossHeight = parseInt( this.magnifierCrosshair.style.height) + 2;
        esriPanel = FloatingPanels[this.controlName];
        esriPanel.minHeight = crossHeight;
	    return false; 
    }

    this.toggleMagnifierMapImage = function(visibility) {
        var imgObj;
        if (visibility==null) visibility = "visible";
        imgObj = this.mapImage;
        if (imgObj!=null) {
            imgObj.style.visibility = visibility;
            if (visibility=="hidden") imgObj.src = this.blankImagePath;
        } 
        return false; 
    }
    
    this.hideMapImage = function() {
        var imgObj = this.mapImage;
        if (imgObj!=null) {
            imgObj.style.visibility = "hidden";
            imgObj.src = this.blankImagePath;
        } 
        if (this.magnifierAOI!=null)
            this.magnifierAOI.style.visibility = "visible";
        if (this.magnifierCrosshair!=null)
            this.magnifierCrosshair.style.visibility = "visible";
        return false; 
    }

    this.showMapImage = function() {
        var imgObj = this.mapImage;
        if (imgObj!=null) 
            imgObj.style.visibility = "visible";
        if (this.magnifierAOI!=null)
            this.magnifierAOI.style.visibility = "hidden";
        if (this.magnifierCrosshair!=null)
            this.magnifierCrosshair.style.visibility = "hidden";
        return false; 
    }
    
    this.requestMagnifierMapImage = function() {
        var m = Maps[this.mapBuddyId];
        var coords = "";
        if (m!=null) {
            var box = calcElementPosition(this.magnifierAOI.id);
            var mbox = calcElementPosition(m.containerDivId);
	        var wDiv = this.contentDiv;
            var winWidth = wDiv.clientWidth;
            //var winHeight = wDiv.clientHeight;
            var winHeight = this.mapArea.clientHeight;
            if (box!=null) {
                var left = box.left - mbox.left;
                var top = box.top - mbox.top;
                var right = left + box.width;
                var bottom = top + box.height;
                coords = left + "," + top + "," + right + "," + bottom;
            }
            var context = this.controlName + ",NewExtent";    
            var argument = "ControlType=Magnifier&PageID=" + m.pageID + "&EventArg=NewExtent&width=" + winWidth + "&height=" + winHeight + "&coords=" + coords;
            eval(this.mapCallbackFunctionString);
        }
        return false; 
        
    }
    
    this.getMapImage = function() {
        this.hideMapImage();
        this.requestMagnifierMapImage();
         return false; 
    }
    
    this.resizeMagnifierArea = function(width, height) {
        height -= (this.factorRow.clientHeight + 2);
        var crossHeight = parseInt( this.magnifierCrosshair.style.height) + 2;
        if (height<crossHeight) height = crossHeight;
        this.contentDiv.style.height = height + "px";
        //this.contentDiv.style.width = width + "px";
        this.mapArea.style.height = height + "px";
        this.setAOIBox();
        esriPanel = FloatingPanels[this.controlName];
        esriPanel.minHeight = crossHeight;
        return false; 
    }
    
    this.setStartSize = function() {
        floatingPanel = document.getElementById(this.controlName);
        // if necessary, need to temporarily expand panel to get interior sizes
        var tDisplay = floatingPanel.style.display;
        floatingPanel.style.display = '';
        var tableheight = this.contentTable.clientHeight;
        var rowheight = this.factorRow.clientHeight;
        var height = tableheight - rowheight - 2;
        this.contentDiv.style.height = height + "px";
        this.mapArea.style.height = height + "px";
        this.setAOIBox();
        floatingPanel.style.display = tDisplay;
        //this.contentsContainer.style.overflow = "hidden";
    }    
    
    this.setEventHandlers = function(panel) {
        // functions to call for content action on drag events
        panel.contentOnDragStartFunction = "esriMagnifiers['" + this.controlName + "'].hideMapImage()";
        panel.contentOnDragMoveFunction = null;
        panel.contentOnDragEndFunction = "esriMagnifiers['" + this.controlName + "'].getMapImage()";
        
        // functions to call for content action on resize events
        panel.contentOnResizeStartFunction = "esriMagnifiers['" + this.controlName + "'].hideMapImage()";
        panel.contentOnResizeMoveFunction = "esriMagnifiers['" + this.controlName + "'].resizeMagnifierArea(tempWidth, tempHeight)";
        panel.contentOnResizeEndFunction = "esriMagnifiers['" + this.controlName + "'].getMapImage()";
        
        // functions to call for content action on hide/show
        panel.contentOnHideFunction = "esriMagnifiers['" + this.controlName + "'].hideMapImage()";
        panel.contentOnShowFunction = "esriMagnifiers['" + this.controlName + "'].getMapImage()";
        
        this.setStartSize();
    }
}

function hideMagnifierPanel(controlID) {
    floatingPanel=document.getElementById(controlID);
    esriPanel = FloatingPanels[controlID];
    floatingPanel.style.display='none';
    var hfVisible = document.getElementById(controlID + '_hfVisible');
    hfVisible.value='false';
    if (esriPanel.contentOnHideFunction!=null) eval(esriPanel.contentOnHideFunction);
}

function onMagFactorChange(magID)
{
    var mag = esriMagnifiers[magID];
    if (mag != null)
    {
        mag.setAOIBox();
        mag.requestMagnifierMapImage();
    }
}

