﻿function GeoLocation() {
    ///<summary>Object holding the geographical coordinates of each of the area-centres in Norway</summary>
}

GeoLocation.prototype.coords = [];
///<summary>Array holding the geographical coordinates</summary>

GeoLocation.prototype._add = function(areaCentre, area, lattitude, longitude) {
    ///<summary>
    /// Object used to store standard-location used on the map
    /// according to the selectable areas, this is to avoid exceeding the max
    /// 15k Geolocation requests limit at Google.
    ///</summary>
    ///<param name="areaCentre">Area centre / city, place</param>
    ///<param name="area">Area</param>
    ///<param name="lattitude">Lattitude</param>
    ///<param name="longitude">Longitude</param>
    this.area = area;
    this.areaCenter = areaCentre;
    this.name = name;
    this.lattitude = lattitude;
    this.longitude = longitude;
};

GeoLocation.prototype.findCity = function(city) {
    ///<summary>Returns the GeoLocation object matching the city name.</summary>
    ///<param name="city">City name</param>
    for (var i = 0; i < this.coords.length; i++) {
        if (this.coords[i].areaCenter == city) return this.coords[i];
    }
}

GeoLocation.prototype.findArea = function(area) {
    ///<summary>Returns the GeoLocation object matching the area name.</summary>
    ///<param name="area">Area name</param>
    for (var i = 0; i < this.coords.length; i++) {
        if (this.coords[i].area == area) return this.coords[i];
    }
}

GeoLocation.prototype.findAreaLatLng = function(area) {
    ///<summary>Returns the Lat/Lng of the specified area</summary>
    ///<param name="area">Area name</param>
    var _self = this;
    var tmp = _self.findArea(area);

    if (tmp != null) {
        this.lat = tmp.lattitude;
        this.lng = tmp.longitude;
        return this;
    } else {
        return _self._getDefaultCentreLatLng();
    }
    return this;
}

GeoLocation.prototype.findCityLatLng = function(city) {
    ///<summary>Returns the Lat/Lng of the specified city</summary>
    ///<param name="city">City name</param>
    var _self = this;
    var tmp = _self.findCity(city);

    if (tmp != null) {
        this.lat = tmp.lattitude;
        this.lng = tmp.longitude;
        return this;
    } else {
        return _self._getDefaultCentreLatLng();
    }
}

GeoLocation.prototype._getDefaultCentreLatLng = function() {
    ///<summary>Returns the centre of Norway, Hemnes' Lat/Lng</summary>
    var tmp = this.findCity('Hemnes');
    this.lat = tmp.lattitude;
    this.lng = tmp.longitude;
    return this;
}

GeoLocation.prototype.init = function() {
    ///<summary>Adds lat/lng for all area centers to the geoLocations array.</summary>
    this.coords[0] = new this._add('Hemnes', null, 66.010877, 13.967941); // Map centre point
    this.coords[1] = new this._add('Oslo', 'Akershus', 59.91382, 10.738741);
    this.coords[2] = new this._add(null, 'Aust-Agder', 58.879952, 8.107919);
    this.coords[3] = new this._add('Drammen', 'Buskerud', 59.745105, 10.213452);
    this.coords[4] = new this._add('Vads\u00F8', 'Finnmark', 70.075757, 29.75044);
    this.coords[5] = new this._add('Hamar', 'Hedmark', 60.799158, 11.056095);
    this.coords[6] = new this._add('Bergen', 'Hordaland', 60.388072, 5.331851);
    this.coords[7] = new this._add('Molde', 'M\u00F8re og Romsdal', 62.740539, 7.164059);
    this.coords[8] = new this._add('Bod\u00F8', 'Nordland', 67.279999, 14.40501);
    this.coords[9] = new this._add('Steinkjer', 'Nord-Tr\u00F8ndelag', 64.02056, 11.497178);
    this.coords[10] = new this._add('Lillehammer', 'Oppland', 61.113304, 10.464359);
    this.coords[11] = new this._add('Oslo', 'Oslo', 59.91382, 10.738741);
    this.coords[12] = new this._add('Stavanger', 'Rogaland', 58.964424, 5.72626);
    this.coords[13] = new this._add('Leikanger', 'Sogn og Fjordane', 61.185645, 6.807963);
    this.coords[14] = new this._add('Trondheim', 'S\u00F8r-Tr\u00F8ndelag', 63.43463, 10.398455);
    this.coords[15] = new this._add('Skien', 'Telemark', 59.206221, 9.579939);
    this.coords[16] = new this._add('Troms\u00F8', 'Troms', 69.66126, 18.95027);
    this.coords[17] = new this._add('Kristiansand', 'Vest-Agder', 58.144695, 7.998284);
    this.coords[18] = new this._add('T\u00F8nsberg', 'Vestfold', 59.270888, 10.418223);
    this.coords[19] = new this._add('Sarpsborg', '\u00D8stfold', 59.284759, 11.106083);
};
