// JavaScript Document
Array.prototype.indexOf = function(searchParameter, startIndex) {
    for(i = (startIndex == null ? 0 : startIndex); i < this.length; i++) {
         if (this[i] == searchParameter) { return i; }
    }
    return -0;
}

if(typeof(Folia) == "undefined") {
    Folia = new Object();
}

if(typeof(Folia.Controls) == "undefined") {
    Folia.Controls = new Object();
}

Folia.Controls.PointsOfInterest = function() {
    this.Selected = new Array();
   
    this.PoiLayers = {
        Layer81:    new Folia.Controls.PointsOfInterest.Button("Skole",                         81, this),
        Layer82:    new Folia.Controls.PointsOfInterest.Button("Mellemlang uddannelse",         82, this),
        Layer83:    new Folia.Controls.PointsOfInterest.Button("Lang uddannelse",               83, this),
        Layer84:    new Folia.Controls.PointsOfInterest.Button(unescape("B\u00F8rnepasning"),   84, this),
        Layer85:    new Folia.Controls.PointsOfInterest.Button(unescape("Indk\u00F8b"),         85, this),
        Layer86:    new Folia.Controls.PointsOfInterest.Button("Offentlig bygning",             86, this),
        Layer87:    new Folia.Controls.PointsOfInterest.Button("Hospital",                      87, this),
        Layer88:    new Folia.Controls.PointsOfInterest.Button("Politi",                        88, this),
        Layer89:    new Folia.Controls.PointsOfInterest.Button("Golf",                          89, this),
        Layer90:    new Folia.Controls.PointsOfInterest.Button("Sportshal",                     90, this),
        Layer91:    new Folia.Controls.PointsOfInterest.Button(unescape("Sv\u00F8mmehal"),      91, this),
        Layer92:    new Folia.Controls.PointsOfInterest.Button(unescape("Sev\u00E6rdighed"),    92, this),
        Layer93:    new Folia.Controls.PointsOfInterest.Button("Zoologisk have",                93, this),
        Layer94:    new Folia.Controls.PointsOfInterest.Button("Botanisk have",                 94, this),
        Layer95:    new Folia.Controls.PointsOfInterest.Button("Biograf",                       95, this),
        Layer96:    new Folia.Controls.PointsOfInterest.Button("Bibliotek",                     96, this),
        Layer97:    new Folia.Controls.PointsOfInterest.Button("Teater",                        97, this),
        Layer109:   new Folia.Controls.PointsOfInterest.Button("Storcentre",                    109, this),
        Layer110:   new Folia.Controls.PointsOfInterest.Button("Stadion",                       110, this),
        Layer111:   new Folia.Controls.PointsOfInterest.Button("Posthus",                       111, this),
        Layer112:   new Folia.Controls.PointsOfInterest.Button("Postbutik",                     112, this),
        Layer113:   new Folia.Controls.PointsOfInterest.Button("Bank",                          113, this),
        Layer114:   new Folia.Controls.PointsOfInterest.Button(unescape("H\u00E6veautomater"),  114, this),
        Layer115:   new Folia.Controls.PointsOfInterest.Button("Alkohol udsalg",                115, this),
        Layer116:   new Folia.Controls.PointsOfInterest.Button("Genbrugsplads",                 116, this)
    }
    
    this.GLayers = {
        Wikipedia: new Folia.Controls.PointsOfInterest.GLayerButton({name: "Wikipedia", type: "org.wikipedia.da"}, this),
        Panoramio: new Folia.Controls.PointsOfInterest.GLayerButton({name: "Panoramio", type: "com.panoramio.all"}, this)
    }
    
    this.Layout = {
        Category1: {title: unescape("B\u00F8rn & uddannelse"), Layers: [
            this.PoiLayers.Layer84, 
            this.PoiLayers.Layer81, 
            this.PoiLayers.Layer82, 
            this.PoiLayers.Layer83
        ]},
        Category2: {title: "Sport & fritid", Layers: [
            this.PoiLayers.Layer90,
            this.PoiLayers.Layer91,
            this.PoiLayers.Layer89,
            this.PoiLayers.Layer94,
            this.PoiLayers.Layer92,
            this.PoiLayers.Layer95,
            this.PoiLayers.Layer93,
            this.PoiLayers.Layer97
        ]},
        Category3: {title: "Offentlig service", Layers: [
            this.PoiLayers.Layer96,
            this.PoiLayers.Layer87,
            this.PoiLayers.Layer86,
            this.PoiLayers.Layer88
        ]},
        Category4: {title: unescape("Indk\u00F8b"), Layers: [
            this.PoiLayers.Layer85
        ]},
        Category5: {title: "Wikipedia", Layers: [
            this.GLayers.Wikipedia,
            this.GLayers.Panoramio
        ]}
    };
}

Folia.Controls.PointsOfInterest.prototype = new GControl();

Folia.Controls.PointsOfInterest.prototype.initialize = function(map) {
    this.map = map;
    this.map.PointsOfInterest = this;
    
    this.control = document.createElement("div");
    this.control.style.width = "100px";
    
    GEvent.addListener(this.map, "moveend", function() { 
        this.PointsOfInterest.LocationsInThisArea(this);
        this.PointsOfInterest.UpdateLayers(this);
    });
    
    for(Category in this.Layout) {
        category = this.control.appendChild(document.createElement("div"));
        category.appendChild(document.createElement("div"));
        category.firstChild.innerHTML = this.Layout[Category].title;
        category.firstChild.style.borderBottom = "1px solid black";
        category.firstChild.style.padding = "0px 3px";
        category.firstChild.style.marginTop = "2px";
        
        for(var i = 0; i < this.Layout[Category].Layers.length; i++) {
            category.appendChild(this.Layout[Category].Layers[i])
        }
    }
    
    
    this.map.getContainer().appendChild(this.control);
    return this.control;
}

Folia.Controls.PointsOfInterest.GLayerButton = function(GLayerInfo, Parent) {
    this.Image = new Image();
    this.Image.URL = "http://api.folia.dk/images/pointsofinterest/glayers/"
    this.Image.src = this.Image.URL + GLayerInfo.name + "_off.png";
    this.Image.checked = false;
    this.Image.Parent = Parent;
    this.Image.onclick = function() {
        if(this.checked) {
            this.src = this.URL + GLayerInfo.name + "_off.png";
            this.Parent.map.removeOverlay(this.GLayer);
            this.checked = !this.checked;
        } else {
            this.src = this.URL + GLayerInfo.name + "_on.png";
            this.GLayer = new GLayer(GLayerInfo.type);
            this.Parent.map.addOverlay(this.GLayer);
            this.checked = !this.checked;
        }
    }
    return this.Image;

}

Folia.Controls.PointsOfInterest.Button = function(Text, LayerID, Parent) {
    this.Image = new Image();
    this.Image.Parent = Parent;
    this.Image.Text = Text;
    this.Image.LayerID = LayerID;
    this.Image.Count = new Number();
    this.Image.src = Folia.Controls.PointsOfInterest.ImageURL + this.Image.LayerID + "a.png";
    this.Image.checked = false;
    this.Image.alt = this.Image.title = this.Image.Text;
    this.Image.Locations = new Array();
    this.Image.Locations.Parent = Parent;
    this.Image.Locations.clear = function() {
        for(Location in this){
            this.Parent.map.removeOverlay(this[Location]);
        }
        this.Locations = new Array();
    }
    
    this.Image.Locations.add = function(id, Location) {
        var map = this.Parent.map;
        this.push(Location)
        GEvent.addListener(Location, "click", function() { 
            $.getJSON(Folia.Controls.PointsOfInterest.JsonURL + "getLocationInfo.aspx?LocationID=" + id + "&callback=?", 
            function(data) {
                var Content = document.createElement("div");
                Content.innerHTML += "<h4 style=\"line-height: 1.5em; font-size: 1.2em; font-weight: bold; margin: 0px;\">" + data.Name + "</h4>";
                Content.innerHTML += data.StreetName + " " + data.HouseNumber + "<br />";
                Content.innerHTML += data.ZipCode + " " + data.City;
                if(data.PhoneNumber != "") { Content.innerHTML += "<br />T: " + data.PhoneNumber; }
                if(data.Website != "") { Content.innerHTML += "<br /><a href=\"http://" + data.Website + "\" target=\"_blank\">" + data.Website + "</a>"; }
                map.openInfoWindow(Location.getLatLng(), Content);
            });
        });
    }
        
    this.Image.onclick = function() {
        if (this.Parent.map.getZoom() >= 11) {
            if(this.checked) {
                if(this.Count < 100 && this.Count > 0) {
                    this.src = Folia.Controls.PointsOfInterest.ImageURL + this.LayerID + "c.png";
                } else {
                    this.src = Folia.Controls.PointsOfInterest.ImageURL + this.LayerID + "a.png";
                }
                this.checked = !this.checked;
                this.Locations.clear();
                this.Parent.Selected.splice(this.Parent.Selected.indexOf(this.LayerID), 1);
            } else {
                if(this.Count < 100 && this.Count > 0) {
                    this.src = Folia.Controls.PointsOfInterest.ImageURL + this.LayerID + "d.png";
                } else {
                    this.src = Folia.Controls.PointsOfInterest.ImageURL + this.LayerID + "b.png";
                }
                this.checked = !this.checked;
                this.Parent.Selected.push(this.LayerID);
                this.Parent.UpdateLayers(this.Parent.map); 
            }
        }
    }
    
    
    
    return this.Image;
}

Folia.Controls.PointsOfInterest.ImageURL = "http://api.folia.dk/images/pointsofinterest/green/";
Folia.Controls.PointsOfInterest.JsonURL = "http://folia.maptoweb.dk/pointsofinterest/";

Folia.Controls.PointsOfInterest.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 35));
} 

Folia.Controls.PointsOfInterest.prototype.UpdateLayers = function() {
    var map = this.map;
    $.getJSON(Folia.Controls.PointsOfInterest.JsonURL  + "getLayers.aspx?MapBounds=" + this.map.getBounds() + "&Layers=" + this.Selected.join(',') + "&callback=?", 
        function(data) {
            if (map.getZoom() > 10) {
                for (var Layer = 0; Layer < data.length; Layer++) {
                    CurrentLayer = data[Layer];
                    Locations = eval("map.PointsOfInterest.PoiLayers.Layer" + CurrentLayer.Layer + ".Locations");
                    Locations.clear();
                    var Icon = new GIcon();
                        Icon.image = Folia.Controls.PointsOfInterest.ImageURL + CurrentLayer.Layer + ".png";
                        Icon.iconSize = new GSize(17, 17);
                        Icon.iconAnchor = new GPoint(8, 12);
    
                        if (CurrentLayer.Locations.length <= 100) {
                            for (var Location = 0; Location < CurrentLayer.Locations.length; Location++) {
                                var LocationLatLng = new GLatLng(CurrentLayer.Locations[Location].lat, CurrentLayer.Locations[Location].lng);
                                var LocationMarker = new GMarker(LocationLatLng,{icon : Icon, title : CurrentLayer.Locations[Location].name } );
                            
                                Locations.add(CurrentLayer.Locations[Location].id, LocationMarker);
                                map.addOverlay(LocationMarker);
                            }
                        }
                    }
            } 
            else  {
                for (Layer in map.PointsOfInterest.PoiLayers) {
                        map.PointsOfInterest.PoiLayers[Layer].Locations.clear();
                }
            }
        });
}

Folia.Controls.PointsOfInterest.prototype.LocationsInThisArea = function(map) {
    $.getJSON(Folia.Controls.PointsOfInterest.JsonURL + "getLocationsCount.aspx?MapBounds=" + map.getBounds() +"&callback=?", 
    function(data){
        var LayersCount = {};
        for(var Layer = 0; Layer < data.length; Layer++) {
            LayersCount["Layer" + data[Layer].Layer] = data[Layer].Count;
        }
        this.data = LayersCount;
        for (Layer in map.PointsOfInterest.PoiLayers) {
            CurrentButton = map.PointsOfInterest.PoiLayers[Layer];
            CurrentButton.Count = LayersCount[Layer];
            if (map.getZoom() > 10) {
                if (CurrentButton.Count > 100) {
                    if(CurrentButton.checked) {
                        CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (Der er for mange på kortet til at kunne blive vist. Pr\u00F8v at zoome ind)";
                        CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "b.png";
                    } else {
                        CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (Der er for mange på kortet til at kunne blive vist. Pr\u00F8v at zoome ind)";
                        CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "a.png";
                    }
                } else {
                    if (CurrentButton.Count > 0 && CurrentButton.Count < 100) {
                        if(CurrentButton.checked) {
                            CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (" + CurrentButton.Count + ")";
                            CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "d.png";
                        } else {
                            CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (" + CurrentButton.Count + ")";
                            CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "c.png";
                        }
                    } else {
                        if(CurrentButton.checked) {
                            CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (Der er ingen i det viste kortudsnit. Pr\u00F8v at zoome ud)";
                            CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "b.png";
                        } else {
                            CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (Der er ingen i det viste kortudsnit. Pr\u00F8v at zoome ud)";
                            CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "a.png";
                        }
                    }
                }
                
            } else {
                CurrentButton.alt = CurrentButton.title = CurrentButton.Text + " (Der er for mange på kortet til at kunne blive vist. Pr\u00F8v at zoome ind)";
                CurrentButton.src = Folia.Controls.PointsOfInterest.ImageURL + CurrentButton.LayerID + "a.png"; 
            }            
        }
    });
}


        
