/**
 *
 * @author tjunghans, Namics AG, www.namics.com
 * @class form
 * @namespace MEDELA.locationfinder.search
 * @requires jQuery
 */

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

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

(MEDELA.locationfinder.search.form = function () {
    var init = null,
        bindings = null,
        changeDropDown = null,
        dataObj = null,
        $form = null,
        getDataObj = null,
        getForm = null,
        getLocationData = null,
        locationData = null,
        prepareJsonResponse = null,
        refreshForm = null,
		isSearchInProgress = null,

        /**
         * When the ajax request of the search is started <strong>searchInProgress</strong> is set to true.
         * @property {Boolean} searchInProgress 
         */
        searchInProgress = false,
        sendFormData = null,
    
        focusSearchInput = null;

    init = function () {
        refreshForm();
        focusSearchInput();
        bindings();
    };

    refreshForm = function () {
        $form = jQuery('#medelaLocationfinderSearch form');
        return $form;
    };

    getForm = function (refresh) {
        return refresh ? refreshForm() : $form;
    };

    focusSearchInput = function () {
        jQuery('#fldSearchQuery').val('').focus();
    };

    /**
     * Use this method when form is submitted or filter changes
     * @method sendFormData
     */
    sendFormData = function () {
        // Only search if gmap is available and loaded
        if (!MEDELA.locationfinder.gmap.map.getMap().isLoaded()) {
            return false;
        }

        // Let's not overload the server if there's no value in searchfield
        if (searchInProgress || jQuery("#fldSearchQuery").attr("value") === '') {
            return false;
        }

        var success = false;

        jQuery.ajax({
            'type': 'POST',
            'url': getForm().attr('action'),
            'data': getForm().serialize(),
            'dataType' : 'json',
            'beforeSend' : function () {
                searchInProgress = true;
                MEDELA.locationfinder.resultoutput.hide();
				
            },
            'success': function (data) {
            	
                if (data.error) {
                    MEDELA.locationfinder.resultoutput.showMessage({
                        'error' : data.error,
                        'message' : data.message
                    });

                    // reset map on error
                    MEDELA.locationfinder.gmap.map.init();
                    
                    return false;
                }

                prepareJsonResponse(data);

                MEDELA.locationfinder.fireEvent('searchDataReceived');

                success = true;
            },
            'complete' : function () {
                searchInProgress = false;
                if (success === true) {
                    MEDELA.locationfinder.resultoutput.show();
                }
            }
        });
    };

    bindings = function () {
        getForm().submit(function () {
			if (isSearchInProgress()) {
				return false;
			}
			
            sendFormData();
			
          
            return false;
        });

		changeDropDown();
    };

	/* Dropdown Umkreis function */
	changeDropDown = function () {
        jQuery("#fldSearchRadius").change(function () {
            if (isSearchInProgress()) {
                return false;
            }
			  
            sendFormData();
            return false;
        });
    };


    getLocationData = function () {
        return locationData;
    };

    /**
     * Store the json response in MEDELA.locationfinder.data so it is globally accessible.
     * @param data
     */
    prepareJsonResponse = function (data) {
        var tmp = {};

        tmp.locations = data;
   
        dataObj = MEDELA.locationfinder.data(tmp);

        locationData = dataObj.getLocationArray();

    };

    getDataObj = function () {
        return dataObj;
    };

	isSearchInProgress = function () {
	    return searchInProgress;	
	};

    return {
        'init' : init,
        'getLocationData' : getLocationData,
        'getDataObj' : getDataObj,
        'sendFormData' : sendFormData,
		'isSearchInProgress' : isSearchInProgress,
        'prepareJsonResponse' : prepareJsonResponse
    };
 
}());

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