﻿//The varibale to set if the map is new
var newMap = false;


//The map control is the overall control container for all actions that happen within the map
function MapControl(mapControl) 
{
    this.mapItem = mapControl;
    //The internal call for the function
    var internalCall = this;
    this.reloadTimer = null;
    //The properties of the class
    this.accordion = null;
    this.kwicks = null;
    this.fx = null;
    this.szNormal = 25;
    this.szSmall  = 20;
    this.szFull   = 180;
    this.layerManagementWidth = 280;
    this.headerHeight = 130;
    this.resizeIE = null;
    this.quickLinkHeight = 60;

    //Gets the total height of the accordian with the height of the header removed
    this.getAccordionHeight = function()
    {
        return (getWinHeight() - internalCall.headerHeight) - (6 * 18);
    }
    
    this.popUp = function(URL) {
        day = new Date();
        id = day.getTime();
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=500,height=400,left=390,top=312');");
    }
    
    //Set the result height which is called after a postback has been done
    this.setResultHeight = function()
    {
        if ($('resultSet') != null)
        {
            var fullHeight = internalCall.getAccordionHeight();
            var standardHeight = fullHeight - internalCall.quickLinkHeight;
            $('resultSet').setStyle('height', standardHeight - 10);
        }
    }
    
    //Sets up the accoirdion and all of the heights for the layers
    this.setupAccordion = function()
    {
        var fullHeight = internalCall.getAccordionHeight();
        var standardHeight = fullHeight - internalCall.quickLinkHeight;
        //Set the accordian
        internalCall.accordion = new Accordion('h3.atStart', 'div.atStart', {
	        opacity: false,
	        fixedHeight: fullHeight,
	        onActive: function(toggler, element){
		        toggler.setStyle('color', '#ff3300');
	        },
	        onBackground: function(toggler, element){
		        toggler.setStyle('color', '#222');
	        }
        }, $('accordion'));
        //Set the height of items
        $('quickStart').setStyle('height', fullHeight);
        $('mapLegend').setStyle('height', standardHeight);
        $('mapLegendQuickLinks').setStyle('height', internalCall.quickLinkHeight);
        $('layerManagement').setStyle('height', fullHeight);
        $('addressSearch').setStyle('height', standardHeight);
        $('addressSearchQuickLinks').setStyle('height', internalCall.quickLinkHeight);
        $('identity').setStyle('height', standardHeight);
        $('identityQuickLinks').setStyle('height', internalCall.quickLinkHeight);
        $('printContent').setStyle('height', standardHeight - 10);
        $('printContentQuickLinks').setStyle('height', internalCall.quickLinkHeight);
        //Set the visinility of the accordion
        $('accordion').setStyle('visibility', 'visible');
    }
    
    //Setup the links within the accordion so the user can quick scroll to other layers
    this.setupAccordionQuickLinks = function()
    {
        $('zero-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('one-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('one-link-three').addEvent('click', function(event) {
            internalCall.accordion.display(3);
        });
        $('one-link-four').addEvent('click', function(event) {
            internalCall.accordion.display(4);
        });
        $('four-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('four-link-four').addEvent('click', function(event) {
            internalCall.accordion.display(4);
        });
        $('five-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('five-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('five-link-three').addEvent('click', function(event) {
            internalCall.accordion.display(3);
        });
        $('six-link-two').addEvent('click', function(event) {
            internalCall.accordion.display(2);
        });
        $('six-link-three').addEvent('click', function(event) {
            internalCall.accordion.display(3);
        });
        $('six-link-four').addEvent('click', function(event) {
            internalCall.accordion.display(4);
        });
    }
    
    //Setup the kwicks roll over so when the user goes over a hover it displays the extra information
    this.setupKwicks = function()
    {
        this.kwicks = $$("#kwicks .kwick");
        this.fx = new Fx.Elements(this.kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
        this.kwicks.each(function(kwick, i) {
            kwick.addEvent("mouseenter", function(event) {
                var o = {};
                o[i] = {height: [kwick.getStyle("height").toInt(), internalCall.szFull]}
                internalCall.kwicks.each(function(other, j) {
	                if(i != j) {
		                var w = other.getStyle("height").toInt();
		                if(w != internalCall.szSmall) o[j] = {height: [w, internalCall.szSmall]};
	                }
                });
                internalCall.fx.start(o);
            });
        });
         
        $("kwicks").addEvent("mouseleave", function(event) {
            var o = {};
            var z = {};
            internalCall.kwicks.each(function(kwick, i) {
                o[i] = {height: [kwick.getStyle("height").toInt(), internalCall.szNormal]}
            });
            internalCall.fx.start(o);
        });
    }
    
    //Proload the images for the ajax loader
    this.setupImages = function()
    {
        if (document.images)
        {
          preload_image = new Image(16,16); 
          preload_image.src="Components/Images/ajax-loader.gif";
        }
    }
    
    //Setup the location search so only the correct layer is shown to the user
    this.setupLocationSearch = function()
    {
        $('buttonAddress').addEvent('click', function(event) {
            $('SmartAddressControl1_content').setStyle('display', 'block');
            $('TownLandsControl1_content').setStyle('display', 'none');
            $('GridReferenceControl1_content').setStyle('display', 'none');
            $('IrishGridCoordinatesControl1_content').setStyle('display', 'none');
            $('LongitudeAndLatitudeControl1_content').setStyle('display', 'none');            
        });
        $('buttonTownlands').addEvent('click', function(event) {
            $('SmartAddressControl1_content').setStyle('display', 'none');
            $('TownLandsControl1_content').setStyle('display', 'block');
            $('GridReferenceControl1_content').setStyle('display', 'none');
            $('IrishGridCoordinatesControl1_content').setStyle('display', 'none');
            $('LongitudeAndLatitudeControl1_content').setStyle('display', 'none');                        
        });
        $('buttonGridReference').addEvent('click', function(event) {
            $('SmartAddressControl1_content').setStyle('display', 'none');
            $('TownLandsControl1_content').setStyle('display', 'none');
            $('GridReferenceControl1_content').setStyle('display', 'block');
            $('IrishGridCoordinatesControl1_content').setStyle('display', 'none');
            $('LongitudeAndLatitudeControl1_content').setStyle('display', 'none');                        
        });
        $('buttonIrishGridCoordinates').addEvent('click', function(event) {
            $('SmartAddressControl1_content').setStyle('display', 'none');
            $('TownLandsControl1_content').setStyle('display', 'none');
            $('GridReferenceControl1_content').setStyle('display', 'none');
            $('IrishGridCoordinatesControl1_content').setStyle('display', 'block');
            $('LongitudeAndLatitudeControl1_content').setStyle('display', 'none');                        
        });
        $('buttonLongitudeAndLatitude').addEvent('click', function(event) {
            $('SmartAddressControl1_content').setStyle('display', 'none');
            $('TownLandsControl1_content').setStyle('display', 'none');
            $('GridReferenceControl1_content').setStyle('display', 'none');
            $('IrishGridCoordinatesControl1_content').setStyle('display', 'none');
            $('LongitudeAndLatitudeControl1_content').setStyle('display', 'block');                        
        });
        $('SmartAddressControl1_content').setStyle('display', 'block');
        $('TownLandsControl1_content').setStyle('display', 'none');
        $('GridReferenceControl1_content').setStyle('display', 'none');
        $('IrishGridCoordinatesControl1_content').setStyle('display', 'none');
        $('LongitudeAndLatitudeControl1_content').setStyle('display', 'none');
    }
    
    //Show the busy indicator when the map moves
    this.showBusyIndicator = function(sender) 
    {
        showLayer("BusyIndicator");
        if (sender!=null) {
            window.status = "Pending Tiles: " + sender.pendingTiles.length;
        } 
    }

    //Show the pending tiles in the status window
    this.showPendingTiles = function(sender) 
    {
        if (sender!=null) {
            window.status = "Pending Tiles: " + sender.pendingTiles.length;
        } 
    }

    //Hide the busy indicator 
    this.hideBusyIndicator = function(sender) 
    {
        hideLayer("BusyIndicator");
        if (sender!=null) {
            window.status = "";
        }
    }

    //Set the busy indicator so it shows up when the map is loading
    this.setupBusyIndicator = function()
    {
        this.mapItem.pendingTiles.add_onRequestsPending(internalCall.showBusyIndicator);
        this.mapItem.pendingTiles.add_onRequestsRemove(internalCall.showPendingTiles);
        this.mapItem.pendingTiles.add_onRequestsCompleted(internalCall.hideBusyIndicator);
    }

    //Create the refresh timeout for the application so that it does not time out
    this.refreshTimeOut = function()
    {
        var context = null;
        var message = "ControlType=ApplicationRefresh";
        eval(internalCall.mapItem.identifyCallbackFunctionString);
    }

    this.adjustMapSize = function()
    {
        var windowWidth = getWinWidth();
        var windowHeight = getWinHeight();
        var width = windowWidth - internalCall.layerManagementWidth;
        var height = windowHeight - internalCall.headerHeight;
        window.setTimeout("MapControl.resizeTheMap(" + width + ", " + height + ", false);", 500);
        internalCall.setupAccordion();
        internalCall.setResultHeight();
        return false;
    }

    this.resizeTheMap = function(width, height, resizeExtent) {
        var mapPanel = document.getElementById('map');
        var mapAccordion = document.getElementById('accordion');
        mapPanel.style.width = width + 'px';     
        mapPanel.style.height = height + 'px';
        mapAccordion.style.height = height + 'px';
        if (resizeExtent==null) resizeExtent = true;
        map.resize(width, height, resizeExtent);
        return false; 
    }
    
    this.setupMapCanvas = function()
    {
        //Setup the onresize event
        window.onresize = internalCall.adjustMapSizeHandler;
        //Set the map so it is the correct
        var windowWidth = getWinWidth();
        var windowHeight = getWinHeight();
        var width = windowWidth - internalCall.layerManagementWidth;
        var height = windowHeight - internalCall.headerHeight;
        internalCall.resizeTheMap(width, height, false);
    } 
       
    //The windows startup function to make sure everything is working correctly
    this.windowStartUp = function()
    {
        if (newMap) {
            internalCall.setupAccordion();
            internalCall.setupAccordionQuickLinks();
            internalCall.setupKwicks();
            internalCall.setupImages();
            internalCall.setupLocationSearch();
            internalCall.setupBusyIndicator();
            //Send the timeout to the application the first time we load so we know the correct time out (5 mins)
            setTimeout(internalCall.refreshTimeOut, 300000);
        } 
    }

    this.adjustMapSizeHandler = function(e) 
    {
        window.clearTimeout(internalCall.reloadTimer);
	    internalCall.reloadTimer = window.setTimeout(internalCall.adjustMapSize ,500);
    }

    //Toggle the current layers showing on the map
    this.toggleLayer = function(layerId) {
        //Show the processing indicator
        showLayer("BusyIndicator");
        //Set the text to processing
        $('image' + layerId).src = 'Components/Images/ajax-loader.gif';
        //Create the callback function
        var message = "ControlID=Map1&ControlType=LayerManagement&EventArg=ToggleLayer&LayerId=" + layerId;
	    var context = internalCall.mapItem.controlName;
	    //Evaluate the callback function
	    eval(internalCall.mapItem.identifyCallbackFunctionString);
    }

    //Display numbers ober the top of the map
    this.displayNumbering = function()
    {
        var message = "ControlID=Map1&ControlType=Map&EventArg=MapExtentIdentify&DisplayNumbers=True";
	    var context = map.controlName;
	    //Evaluate the callback function
	    eval(internalCall.mapItem.identifyCallbackFunctionString);
    }

    //Display numbers ober the top of the map
    this.displayFullExtent = function()
    {
        var message = "ControlID=Map1&ControlType=Map&EventArg=MapFullExtent";
	    var context = map.controlName;
	    //Evaluate the callback function
	    eval(internalCall.mapItem.identifyCallbackFunctionString);
    }

    //Toggle the menu layer visibility to make it display
    this.toggleLayerText = function(layerId, visibility) {
        if (visibility == true) {
            $('image' + layerId).src = 'Components/Images/Safety_Goggles_Cross.gif';
            $('legend').setStyle('display', 'none');
            $('legend' + layerId).setStyle('display', 'block');
            $('image' + layerId).alt = 'Remove subject from the map';
            $('image' + layerId).title = 'Remove subject from the map';            
            internalCall.accordion.display(1);
        } else {
            $('image' + layerId).src = 'Components/Images/Safety_Goggles.gif';
            $('legend' + layerId).setStyle('display', 'none');
            $('image' + layerId).alt = 'Display subject on map';
            $('image' + layerId).title = 'Display subject on map';
            internalCall.accordion.display(2);
        }
    }

    //Dispay the identity information for the selected item in the results list
    this.displayIdentityInformation = function(itemOne, itemTwo)
    {
        var stringId = "dataOutput" + itemOne + "a" + itemTwo;
        if ($(stringId) != null)
        {
            $(stringId).toggleClass('hide');
        }
        else
        {
            //Create the postback data
            var message = "ControlID=Map1&ControlType=Map&EventArg=MapExtentInfo&ItemOne=" + itemOne + "&ItemTwo=" + itemTwo;
	        var context = internalCall.mapItem.controlName;
	        //Evaluate the callback function
	        eval(internalCall.mapItem.identifyCallbackFunctionString);
        }
    }

    //Setup the map control when it is first loaded
    this.setupMapControl = function()
    {
        //First off lets hide the busy indicator
        hideLayer("BusyIndicator");
        //Then setup the map canvas
        internalCall.setupMapCanvas();
        //Set the onload and resize events
        if (window.addEventListener) 
        {
            window.addEventListener("load", internalCall.windowStartUp, false);
        } 
        else if (window.attachEvent) 
        {
            window.attachEvent("onload", internalCall.windowStartUp);
        }
    }
}