/**
 * Data management
 * @author tjunghans, Namics AG, www.namics.com
 * @module Data
 * @namespace MEDELA.locationfinder
 * @requires jQuery
 */

/* JSLint Stuff */
/*globals jQuery */

var MEDELA = MEDELA || {};
MEDELA.locationfinder = MEDELA.locationfinder || {};

(MEDELA.locationfinder.data = function (jsonData) {

    /**
     * Expects data in form of {}.locations...
     * @class Data
     * @constructor
     * @param jsonData
     */
    function Data(jsonData) {
        this.rawData = jsonData.locations;

        // add the alphabet letters to first 26 items
        var i = 0,
            unicodeIndex = 65,
            maxValue = 26; // letters in alphabet

        maxValue = this.rawData.length < maxValue ? this.rawData.length : maxValue;
        
        while (i < maxValue) {

            this.rawData[i].location.alphaLetter = String.fromCharCode(unicodeIndex + i);
            ++i;
        }
        
    }

    Data.prototype = {
        'getLocationArray' : function () {
            return this.rawData;
        },

        'getLocationByIndex' : function (index) {
            return this.rawData[index].location;
        },

        'addMarkerObject' : function (index, marker) {
            this.rawData[index].location.markerObject = marker;
            return true;
        },

        'getLocationById' : function (id) {
            var i = this.rawData.length - 1,
                location = null;

            while (i > 0) {
                location = this.rawData[i].location;
                if (location.id === id) {
                    return location;
                }
                --i;
            }

            return false;
        }
    };

    return new Data(jsonData);
});