/**
 * Address search result builder
 * @author tjunghans, Namics AG, www.namics.com
 * @class address
 * @namespace MEDELA.locationfinder.resultoutput
 * @requires jQuery
 */

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

var MEDELA = MEDELA || {};
MEDELA.locationfinder = MEDELA.locationfinder || {};
MEDELA.locationfinder.resultoutput = MEDELA.locationfinder.resultoutput || {};
(MEDELA.locationfinder.resultoutput.address = function (locationsArray) {

    function AddressItem(locationArray) {
        this.location = locationArray;
    }

    AddressItem.prototype.getDataObj = function () {
        return this.locationArray;
    };

    AddressItem.prototype.toHtml = function (letter) {
        var tmpHtml = '<li class="address-item">';
        tmpHtml += '<div class="item-icon">';
        tmpHtml += letter;
        tmpHtml += '</div>';
        tmpHtml += '<div class="item-details vcard">';
        tmpHtml += '<div class="fn org">' + this.location.name + '</div>';
        tmpHtml += '<div class="adr">';
        tmpHtml += '<div class="street-address">' + this.location.street_address + '</div>';
        tmpHtml += '<div><span class="postal-code">' + this.location.zip + '</span>';
        tmpHtml += '<span class="locality">' + this.location.city + '</span>';
        tmpHtml += '</div>';
        if (this.location.phone != null && this.location.phone.length > 0) {
            tmpHtml += '<div class="tel">';
            tmpHtml += '<span class="type">work</span>';
            tmpHtml += 'Tel. <span class="value">' + this.location.phone + '</span>';
            tmpHtml += '</div>';
        }
        tmpHtml += '</div>';
        tmpHtml += '</div>';
        tmpHtml += '</li>';

        return tmpHtml;
    };

    function Address(locationsArray) {
        this.rawData = locationsArray;
        
    }

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

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

        /**
         * Builds the list in the result Output container #medelaLocationfinderResultoutput .wrapper
         * Fires event <strong>resultOutputListBuilt</strong> when done 
         * @method buildList
         */
        'buildList' : function () {
            var tmpHtml = '',
                i = 0,
                address = null;

            while (i < this.rawData.length) {

                address = new AddressItem(this.rawData[i].location);

                if (address.location.alphaLetter) {
                    tmpHtml += address.toHtml(address.location.alphaLetter);
                } else {
                    tmpHtml += address.toHtml('&nbsp;');
                }
                ++i;
            }

            tmpHtml = '<ul class="resultoutput-address">' + tmpHtml + '</ul>';

            // remove old list
            jQuery('#medelaLocationfinderResultoutput .resultoutput-address').remove();

            // hide anything else
            MEDELA.locationfinder.resultoutput.hideAll();

            // add new list
            jQuery('#medelaLocationfinderResultoutput > .wrapper').append(tmpHtml);

            MEDELA.locationfinder.fireEvent('resultOutputListBuilt');
        }
    };

    return new Address(locationsArray);
});