/**
 * Class for resultoutput
 * @author tjunghans, Namics AG, www.namics.com
 * @class resultoutput (singleton)
 * @namespace MEDELA.locationfinder
 * @requires jQuery
 */

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

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

(MEDELA.locationfinder.resultoutput = function () {
    var hideAll = null,
        showAll = null,
        hideMessage = null,
        showMessage = null,
        init = null,
        locationData = null,
        bindings = null,
        getResultoutput = null,
        getResultoutputList = null,
        getResultoutputMessage = null,
        updateMessage = null,
        hide = null,
        show = null,
        $dom = null;

    /**
     * Holds collection of jQuery objects, to minimize jQuery requests
     * @property $dom
     * @type object
     */
    $dom = {};

    hideAll = function () {
        jQuery('#medelaLocationfinderResultoutput > .wrapper > *').hide();
    };

    showAll = function () {
        jQuery('#medelaLocationfinderResultoutput > .wrapper > *').show();    
    };

    /**
     * Hides the address results
     * @method hide
     */
    hide = function () {
        getResultoutputList().hide();
        return true;
    };

    /**
     * Shows the address results
     * @method show
     */
    show = function () {
        getResultoutputList().show();
        return true;
    };

    /**
     * Hides the message box
     * @method hideMessage
     */
    hideMessage = function () {
        getResultoutputMessage().hide();
        return true;
    };

    /**
     * Displays the message box
     * @method showMessage
     * @param {Object} options: message
     */
    showMessage = function () {
        if (arguments.length > 0) {
            if (typeof arguments[0] === 'object') {
                var options = arguments[0];
                if (typeof options.message === 'string') {
                    if (options.error) {
                        jQuery('.resultoutput-message').addClass('resultoutput-message-error');    
                    } else {
                        jQuery('.resultoutput-message').removeClass('resultoutput-message-error');
                    }
                    updateMessage(options.message);    
                }
            }
        }
        getResultoutputList().hide();
        getResultoutputMessage().show();
        return true;
    };

    /**
     * Update message box content
     * @param {String} message (can contain html)
     * @return true on success
     */
    updateMessage = function (message) {
        getResultoutputMessage().find('.content-frame > span').html(message);
        return true;
    };

    /**
     * Module initializer
     * @method init
     */
    init = function () {
        $dom.medelaLocationfinderResultoutput = jQuery('#medelaLocationfinderResultoutput');
        bindings();
        return true;
    };

    /**
     * Returns dom object
     * @method getResultoutput
     * @return {jQuery} dom object
     */
    getResultoutput = function () {
        return $dom.medelaLocationfinderResultoutput;
    };

    /**
     * Returns dom object
     * @method getResultoutputList
     * @return {jQuery} dom object
     */
    getResultoutputList = function () {
        return $dom.medelaLocationfinderResultoutput.find('.resultoutput-address');
    };

    /**
     * Returns dom object
     * @method getResultoutputMessage
     * @return {jQuery} dom object
     */
    getResultoutputMessage = function () {
        return $dom.medelaLocationfinderResultoutput.find('.resultoutput-message');
    };

    /**
     * Events
     * @method bindings
     */
    bindings = function () {
        MEDELA.locationfinder.handleEvent('searchDataReceived', function () {
            locationData = MEDELA.locationfinder.search.form.getLocationData();
            var result = MEDELA.locationfinder.resultoutput.address(locationData);
            result.buildList();
        });

        MEDELA.locationfinder.handleEvent('resultOutputListBuilt', function () {
            var currentItem = null,
                listItems = null,
                listItemIndex = 0,
                markerObject = null;

            listItems = getResultoutputList().children('li');
            listItems.click(function () {
                currentItem = jQuery(this);
                listItemIndex = listItems.index(currentItem);
                listItems.removeClass('selected');
                currentItem.addClass('selected');
                markerObject = MEDELA.locationfinder.search.form.getDataObj().getLocationByIndex(listItemIndex).markerObject;

                MEDELA.locationfinder.gmap.map.getMap().setCenter(markerObject.getLatLng(), MEDELA.locationfinder.gmap.map.MARKER_MAX_ZOOM);

                /* Set second parameter to true to improve performance. The location data will
                   be retreived from the search result. Currently set to false, because of the
                   search result does not deliver the translation labels. (TODO)
                 */
                MEDELA.locationfinder.gmap.map.getLocationData(markerObject, false);
                return false;
            });
        });
    };

    return {
        'init' : init,
        'hide' : hide,
        'show' : show,
        'showAll' : showAll,
        'hideAll' : hideAll,
        'showMessage' : showMessage,
        'hideMessage' : hideMessage
    };

}());

jQuery(document).ready(function () {
    MEDELA.locationfinder.resultoutput.init();
    
});