﻿var findTextAddressParcel;
var findTextStreet1;
var findTextStreet2;
var findLabel;
var findCombo;
var findButton;
var helpButton;
var westTabs;
var mapsTree;
var tb;
//var map;
var isFloodplain = false;
var currheight = '';
var currwidth = '';
var initialLoad = true;
var viewMenu;

//  Following variables are used for all of the buffer layer selection windows in address, parcels and fcmaps results controls.
var BufferLayerSelectionPane;
var layerSelectionToolbar;
var layerSelectionStore;
var layerSelectionCombo;
var bufferToolLiquorLicenseTypeCombo;
var bufferRadiusTxt;
var bufferToolLiquorLicenseTypeStore;
var bufferToolLiquorLicenseTypeCombo;
var currentTheme;

var limitAccess;

var themeMaxTries = 30;
var themeCurrentTries = 1;

function processCallbackResultLocal(returnmessage, context) {
    ESRI.ADF.System.processCallbackResult(returnmessage, context);
    m_hideLoading();
}
 
function m_hideLoading() {
	var loadDiv = document.getElementById("LoadingDiv");
	if (loadDiv!=null) loadDiv.style.visibility = "hidden";
}
     
function m_showLoading() {
	var loadDiv = document.getElementById("LoadingDiv");
	NCSToc.ForceRefresh();
	if (loadDiv!=null) loadDiv.style.visibility = "visible";
}

function StartUp(accessLimited)
{
    Ext.Msg.show({
        title: 'Please read this disclaimer before continuing...',
        msg: ConfigurationData.GetDisclaimerText(),
        buttons: {ok:'I Agree'},
        closable: false,
        fn: function(btn){
                if (btn == 'ok')
                {
                }
            }
    });

    limitAccess = accessLimited;
    if (limitAccess)
    {
        Toolbar1_Items["Buffer/Select"].disabled = true;
        document.getElementById("Toolbar1Buffer/Select").style.display = "none";
    }
    
    NCSToc.init();
    NCSToc.initLegend();
    
    westTabs = new Ext.TabPanel({
        renderTo:'TabContainer',
        resizeTabs:false,
        width:250,
        defaults: {autoScroll:false},        
        items:[{
                id: 'mapsTab',
                title: 'Maps',
                contentEl: 'MapsTabContainer'
            },{
                id: 'layersTab',
                title: 'Layers',
                contentEl: 'LayersTabContainer'
            },
            {
                id: 'legendTab',
                title: 'Legend',
                contentEl: 'LegendContainer'
            }
        ],
        activeTab: 'mapsTab'
    });
    
    mapsTree = new Ext.tree.TreePanel({
         //tree
         id: 'MapsTree',
         animate:true,
         enableDD:true,
         containerScroll: true,
         rootVisible:false,
         // layout
         width:248,
         split:true,
         // panel
         autoScroll:true,
         //tbar: tb,
         margins: '5 0 5 5'
    });

    var root = new Ext.tree.TreeNode({
        allowDrag:false,
        allowDrop:false
    });
    
    mapsTree.addListener('click', function(node,e){
                            LoadThemeFromQueryStringParam(node.text, true);
                        },
                        this,
                        {
                            single: false
                        }
                    );
                    
    mapsTree.setRootNode(root);

    root.appendChild(ConfigurationData.GetMapsNodes(limitAccess));
    
    mapsTree.render('MapsTabContainer');
    mapsTree.disable();
    
    tb = new Ext.Toolbar({id: 'appToolbar'});
    tb.render('App-Toolbar');
    
    var toolsMenu = AppMenuObject.ToolsMenu(limitAccess);
    viewMenu = AppMenuObject.ViewMenu(limitAccess);
    var helpMenu = AppMenuObject.HelpMenu();
    
    tb.add
    (
        {
            text:'Tools',
            cls: 'x-btn-text-icon',
            menu: toolsMenu
        }
    ),    
    tb.add
    (
        {
            text:'View',
            cls: 'x-btn-text-icon',
            menu: viewMenu
        }
    ),    
    tb.add
    (
        {
            text:'Help',
            cls: 'x-btn-text-icon',
            menu: helpMenu
        }
    )
    
    
    
    tb.addSeparator();
    
    findLabel = new Ext.Toolbar.TextItem('Search by:');
    tb.add(findLabel);
    
    
    // add the find type combo box
    var findTypes = [    
		['add','Address'],		
		['cem', 'Cemetery Plot (Last Name)'],
		['int','Intersection'],
		['par','Parcel']
	];	
    var findStore = new Ext.data.SimpleStore({
        fields: ['abbr', 'type'],
        data : findTypes
    });
    findCombo = new Ext.form.ComboBox({
        store: findStore,
        displayField:'type',
        valueField: 'abbr', 
        id: 'ddlFindType',
        name: 'ddlFindType',
        mode: 'local',
        triggerAction: 'all',
//        editable: false,
        width:160,
        forceSelection: true,
        typeAhead: true
    });
    findCombo.setValue('add');
    findCombo.on('select', onFindTypeChanged);
    tb.addField(findCombo);
    
    findTextAddressParcel = new Ext.form.Field({
        xtype: 'textfield',
        id: 'findTextAddressParcel',
        name: 'findTextAddressParcel',
        value: 'i.e. 1515 Main St #12',
        width: 150
    });    
    tb.addField(findTextAddressParcel);
    
    findTextStreet1 = new Ext.form.Field({
        xtype: 'textfield',
        id: 'findTextStreet1',
        name: 'findTextStreet1',
        value: 'Street 1',
        width: 75
    });    
    tb.addField(findTextStreet1);    
    findTextStreet1.setVisible(false);
    
    findTextStreet2 = new Ext.form.Field({
        xtype: 'textfield',
        id: 'findTextStreet2',
        name: 'findTextStreet2',
        value: 'Street 2',
        width: 75
    });    
    tb.addField(findTextStreet2);
    findTextStreet2.setVisible(false);
    
    findButton = new Ext.Button({
        id:   'btnSearch',
        name: 'btnSearch',
        text: 'Find'
    });
    
    
    findButton.on('click', function(){
                                var type = findCombo.getValue();
                                switch (type)
                                {
                                    case 'add':
                                        onFindClicked(type, findTextAddressParcel.getValue())
                                        break;
                                    case 'int':
                                        onFindClicked(type, findTextStreet1.getValue(), findTextStreet2.getValue())
                                        break;
                                    case 'par':
                                        onFindClicked(type, findTextAddressParcel.getValue())
                                        break;
                                    case 'cem':
                                        onFindClicked(type, findTextAddressParcel.getValue())
                                        break;
                                }
                            }, this, {single: false});
    tb.add(findButton);
    
    var map = $find('Map1');
    // Set the height and width of the toolbar and toc
    tb.setHeight(34);
    if (Ext.isIE6)
    {
        tb.setWidth(0);
        document.getElementById('WestPanel').style.width = 0;
        document.getElementById('MapPanel').style.display = 'inline';
    }
    
    if (Ext.isIE && !Ext.isIE6)
    {
        document.getElementById('WestPanel').style.height = (document.body.offsetHeight - 89) + "px";
        document.getElementById('TabContainer').style.height = (document.body.offsetHeight - 268) + "px";
        westTabs.setHeight(document.body.offsetHeight - 268);
        document.getElementById('MapPanel').style.height = (document.body.offsetHeight - 89) + "px";
        document.getElementById('Map1').style.height = (document.body.offsetHeight - 89) + "px";
        document.getElementById('ncsTreeContainer').style.height = (document.body.offsetHeight -296) + 'px';
        document.getElementById('treeLoc').style.height = (document.body.offsetHeight -296) + 'px';
        document.getElementById('ncsLegendContainer').style.height = (document.body.offsetHeight -296) + 'px';
        document.getElementById('legendLoc').style.height = (document.body.offsetHeight -296) + 'px';
        mapsTree.setHeight(document.body.offsetHeight - 295);
    }
    else if (Ext.isIE6)
    {    
        document.getElementById('WestPanel').style.height = (document.documentElement.clientHeight - 89) + "px";
        document.getElementById('TabContainer').style.height = (document.documentElement.clientHeight - 268) + "px";
        westTabs.setHeight(document.documentElement.clientHeight - 268);
        document.getElementById('MapPanel').style.height = (document.documentElement.clientHeight - 89) + "px";
        document.getElementById('MapPanel').style.width = (document.documentElement.clientWidth - 259) + "px";
        document.getElementById('Map1').style.height = (document.documentElement.clientHeight - 89) + "px";
        document.getElementById('Map1').style.width = (document.documentElement.clientWidth - 259) + "px";
        document.getElementById('ncsTreeContainer').style.height = (document.documentElement.clientHeight -296) + 'px';
        document.getElementById('treeLoc').style.height = (document.documentElement.clientHeight -296) + 'px';        
        document.getElementById('ncsLegendContainer').style.height = (document.documentElement.clientHeight -296) + 'px';
        document.getElementById('legendLoc').style.height = (document.documentElement.clientHeight -296) + 'px';
        mapsTree.setHeight(document.documentElement.clientHeight -295);        
        try
        {
            document.getElementById('layersTab').style.height = (document.documentElement.clientHeight -240) + "px";
        }catch(e){}
    }
    else
    {
        document.getElementById('WestPanel').style.height = (window.innerHeight - 89) + "px";
        document.getElementById('TabContainer').style.height = (window.innerHeight - 268) + "px";
        westTabs.setHeight(window.innerHeight - 268);
        document.getElementById('MapPanel').style.height = (window.innerHeight - 89) + "px";
        document.getElementById('Map1').style.height = (window.innerHeight - 89) + "px";
        document.getElementById('ncsTreeContainer').style.height = (window.innerHeight - 294) + "px";
        document.getElementById('treeLoc').style.height = (window.innerHeight - 294) + "px";
        document.getElementById('ncsLegendContainer').style.height = (window.innerHeight - 294) + "px";
        document.getElementById('legendLoc').style.height = (window.innerHeight - 294) + "px";     
        mapsTree.setHeight(window.innerHeight - 294);
    }
    window.onresize = ApplyResize;
    
    var qs = new Querystring();
    var themeParam = qs.get('map');
    if (themeParam != null)
    {        
        themeParam = themeParam.replace('_', ' ');
        window.setTimeout("LoadThemeFromQueryStringParam('" + themeParam + "', true);", 1000);
    }
    else
    {
        window.setTimeout("LoadThemeFromQueryStringParam('General', true);", 1000);
    }

    
    //initialize street view
    StreetViewControl.Initialize();
}

function EnableMapsTree()
{
    if (NCSToc.TheTree() != null)
    {
        mapsTree.enable();
    }
    else
    {
        if (themeCurrentTries < themeMaxTries)
        {
            themeCurrentTries ++;
            window.setTimeout("EnableMapsTree();", 500);
        }
        else
        {
            mapsTree.enable();
        }
    }
}

function LoadThemeFromQueryStringParam(theme, forceUpdate)
{
    try
    {        
        var message = 'ControlID=Map1&ControlType=Map&EventArg=';
        var context = 'Map1';
        message = message + 'LoadTheme~' + theme;
        eval("WebForm_DoCallback('__Page',message,processThemeTextCallbackResults,context,null,true)");
        
        var node = mapsTree.getRootNode();
        var children = node.childNodes;
        for (var i = 0; i < children.length; ++i)
        {
            if (children[i].text == theme)
            {                    
                children[i].select();                   
            }
        }
        mapsTree.enable();
        
        var layersTab = Ext.getCmp('layersTab');
            
        if (theme == 'Floodplains')
        {
            layersTab.disable();
            isFloodplain = true;
        }
        else
        {
            layersTab.enable();
            try{document.getElementById('ThemeContainer').removeChild(document.getElementById('ThemeTextContainer'));}catch(e){}
            try{document.getElementById('ThemeContainer').removeChild(document.getElementById('ThemeButtonContainer'));}catch(e){}
        }    
    }
    catch(e)
    {
        themeCurrentTries ++;
        window.setTimeout("LoadThemeFromQueryStringParam('" + theme + "'," + forceUpdate + ");", 1000);
    }
    currentTheme = theme;
}

function processThemeTextCallbackResults(s)
{
    if (isFloodplain)
    {
        document.getElementById('ThemeContainer').innerHTML = '';
           
        var headerContainerDiv = document.createElement('div');
        headerContainerDiv.id = 'ThemeHeaderContainer';
        document.getElementById('ThemeContainer').appendChild(headerContainerDiv);
        headerContainerDiv.style.borderStyle = 'solid';
        headerContainerDiv.style.borderWidth = '1px 1px 1px 1px';
        headerContainerDiv.style.borderColor = 'black';
        if (Ext.isIE6)
        {
            headerContainerDiv.style.height = '20px';
        }
        else
        {        
            headerContainerDiv.style.height = '20px';
        }
        headerContainerDiv.innerHTML = '<center>Currently Viewing FEMA Floodplains</center>';   
        
        var textContainerDiv = document.createElement('div');
        textContainerDiv.id = 'ThemeTextContainer';
        document.getElementById('ThemeContainer').appendChild(textContainerDiv);
        if (Ext.isIE6)
        {
            textContainerDiv.style.height = '123px';
        }
        else
        { 
            textContainerDiv.style.height = '125px';
        }
        textContainerDiv.style.borderStyle = 'solid';
        textContainerDiv.style.borderWidth = '0px 1px 1px 1px';
        textContainerDiv.style.borderColor = 'black'; 
        textContainerDiv.style.overflow = 'auto';
        textContainerDiv.style.position = 'relative';
        
        textContainerDiv.innerHTML = s;
        
        var buttonContainerDiv = document.createElement('div');
        buttonContainerDiv.id = 'ThemeButtonContainer';
        document.getElementById('ThemeContainer').appendChild(buttonContainerDiv);
        buttonContainerDiv.style.padding = '3px 0px 3px 60px';
        
        var floodplainSwitchButton = new Ext.Button({
            text: 'Switch Floodplains',
            renderTo: 'ThemeButtonContainer'
            });
        floodplainSwitchButton.render();
        
        floodplainSwitchButton.addListener('click', function(node,e){
                    NCSToc.SwitchFloodplains();
                },
                this,
                {
                    single: false
                }
            );
    }
    else
    {
        document.getElementById('ThemeContainer').innerHTML = s;
    }
    isFloodplain = false;
        
    window.setTimeout("NCSToc.SelectGroup('" + currentTheme + "', true)", 0); 
}

function MinimizeWindow(extWindow)
{
    extWindow.toggleCollapse();
}