﻿/*
Datastructures for the Londonderry Economic Development Site.

Dan Monego
CDM
2008
*/

//Fields to query against are stored here
Datafields = {
    DevelopmentParcel: ["Location", 
                       "Owner1", 
                       "DeededAcreage", 
                       "Zoning", 
                       "erzdistrict", 
                       "ftzdistrict",
                       "parcel_acctnum",
                       "report_link",
                       "GIS_ID"]
}

//Parameters for the main query
function QueryParameters(zoning, 
                         development, 
                         minacreage, 
                         erz,
                         freeTrade,
                         usage)
{
    this.zoning = zoning;
    this.development = development;
    this.minacreage = minacreage;
    this.erz = erz; 
    this.freeTrade = freeTrade;
    this.usage = usage;
    
    //generates the where clause to query with
    this.getWhereClause = function()
    {
        var clause = "";
        //Using % as a wildcard to deal with shape files
        //Will break if a different data source is used for
        //economic development parcels
        if('*' == this.zoning)
        {
            clause += "[Zoning] LIKE '*'"; 
        }
        else
        {
            clause += "([Zoning] LIKE '" + this.zoning + "*'"; 
            clause += (this.zoning == "C" || this.zoning == "I" ? " OR [Zoning] LIKE 'GB')" : ")");
        }
        if('*' == this.development)
        {
            clause += " AND [dev_area] LIKE '*'";
        }
        else
        {
        clause += " AND [dev_area] LIKE '" + this.development + "'";
        }
        if(this.minacreage > 0)
        {
            clause += " AND [DeededAcreage] > " + minacreage;
        }
        
//      Note that both the tif district and the free trade zone queries
//      are inclusive if unchecked (both parcels in and out of the zone are selected)
//      and exclusive if checked (only parcels in the zone are selected)        
        if(this.erz)
        {
            clause += " AND [erzdistrict]";
        }
        if(this.freeTrade)
        {
            clause += " AND [ftzdistrict]";
        }
        
        if(this.usage != "*")
        {
            clause += " AND [" + this.usage +"]";
        }
        debugger;
        return clause;
    }
}

function AddressSet()
{
    //A list of AddressInfo objects
    this.addresses = [];
    
    this.add = function(address)
    {
        if(typeof(address) == "AddressInfo")
        {
            this.addresses.push(address);
        }
        else
        {
            throw "Expected an object of type AddressInfo, got one of type " + typeof(address);
        }    
    }
}

// Information about a particular address
function AddressInfo(feature, map)
{
    this.feature = feature;
    //Private variables
    var parcel_address = feature.attributes["Location"];
    var parcel_owner = feature.attributes["Owner1"];
    var parcel_acreage = feature.attributes["DeededAcreage"];
    var parcel_zoning = feature.attributes["Zoning"];
    var parcel_erz = feature.attributes["erzdistrict"];
    var parcel_ftz = feature.attributes["ftzdistrict"];
    var parcel_report = feature.attributes["report_link"];
    var parcel_account = feature.attributes["parcel_acctnum"];
    var parcel_id = feature.attributes["GIS_ID"];
    
    function show()
    {
        map.infoWindow.hide();
        var extent = feature.geometry.getExtent();
        var symbology = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
                                                         new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, 
                                                                                          new dojo.Color([255,0,0]), 2),
                                                         new dojo.Color([200, 0, 0, 0.5]));
        if(null != extent)
        {
            map.graphics.clear();
            feature.setSymbol(symbology);
            map.graphics.add(feature);

            
            map.infoWindow.resize(300, 115);
            map.infoWindow.setTitle(parcel_address);
            map.infoWindow.setContent("<b>Zoning</b>: " + parcel_zoning + " - <a href=\"" + configuration["zoningurl"] + "\" target=\"_blank\">Explanation</a>" + 
                               "<br /> <b>Owner</b>: " + parcel_owner + 
                               "<br /> <b>Acreage</b>: " + parcel_acreage + 
                               (parcel_erz ? "<br />In an Economic Revitalization Zone" : "") + 
                               (parcel_ftz ? "<br />In a Free Trade Zone" : "") +
                               "<br /><a href='" + parcel_report + "' target='_blank'>Site Report</a>" +
                               " - <a href='" + configuration["patrioturl"] + "/Landing.asp?anum="+ parcel_account +"' target='_blank'>Tax Assessment</a>" + 
                               " - <a href=\"#\" onclick=\"findNearbyBusinessesByGISId('" + parcel_id + "');\">Nearby Businesses</a>" + 
                               " - <a href=\"" + configuration["imsurl"] + "/default.htm?parcelid=" + parcel_id +"\" target=\"_blank\">GIS</a>");
            var screenPoint = {"x":map.width/2,"y":map.height/2};
            
            map.setExtent(extent.expand(3));
            
            //Using a timer is a hack, but it works.
            //The problem is that you can't set the infoWindow while changing the extent,
            //and the zoom change listener pops up on every kind of zoom, not just this one.
            setTimeout(function() 
            {
                map.infoWindow.show(screenPoint,map.getInfoWindowAnchor(screenPoint));
            }, 550);
        }
        else
        {
            throw "Extent not found - is this feature a polygon?";
        }
    }
    
    this.showOnMap = show;
    
    //Returns an HTML node containing the address information
    this.render = function()
    {
        var container = document.createElement("div");
        container.className = "parcelinfo";
        
        var address = document.createElement("div");
        //Note - using setAttribute on the class breaks in IE
        address.className = "addresstext";
        
        var addresslink = document.createElement("a");
        addresslink.onclick = show;
        
        var addresstext = document.createTextNode(parcel_address);
        addresslink.appendChild(addresstext);
        address.appendChild(addresslink);
        container.appendChild(address);
        
        var details = document.createElement("div");
        details.className = "detailstext";
        var detailsstring = "Zoned as " + parcel_zoning;
        detailsstring += " - " + parcel_acreage + " Acres - ";
        detailsstring += "Owned by " + parcel_owner;
        var detailstext = document.createTextNode(detailsstring);
        details.appendChild(detailstext);
        container.appendChild(details);
        
        return container;
    }
}

// Stack for handling previous extents
function ExtentStack()
{
    var prevExtents = [];
    var poppedExtent = null;
    
    this.push = function(extent)
    {
        if(extent !== poppedExtent)
        {
            prevExtents.push(extent);
            poppedExtent = null;
        }
    }
    
    this.pop = function(map)
    {
        if(prevExtents.length == 0)
        {
            throw "Empty extent stack.";
        }
        if(prevExtents.length >= 2)
        {
            prevExtents.pop();
        }
        if(prevExtents.length == 1)
        {
            poppedExtent = prevExtents[0];
        }
        else
        {
            poppedExtent = prevExtents.pop();

        }
        return poppedExtent;
    }
}
