/**
 * Locationfinder application root.
 * &lt;div id="medela-locationfinder"&gt;&lt;/div&gt
 * @author tjunghans, Namics AG, www.namics.com
 * @class locationfinder
 * @namespace MEDELA
 * @requires jQuery, MEDELA.locationfinder.search, NX.throttle
 */

/* JSLint Stuff */
/*globals jQuery, NX */

var MEDELA = MEDELA || {};

/**
 * The object jsonData is created before MEDELA.locationfinder but belongs in this namespace.
 * Save any objects to tmp. Further at the bottom, the objects in tmp are transfered back to MEDELA.locationfinder.
 */

if (MEDELA.locationfinder) {
    var tmp = MEDELA.locationfinder;
}

(MEDELA.locationfinder = function () {
    var init = null,

    initialApplicationHeight = 0,
    
    /**
     * The current height of the application container, named #medelaLocationfinder.
     * The css height value is actually applied to the divs in #medelaLocationfinder.
     * @property applicationHeight
     * @type int
     */
    applicationHeight = 0,

    /**
     * Holds collection of jQuery objects, to minimize jQuery requests.<br/>
     * Example:
     * <pre>$dom.application = jQuery('#medelaLocationfinder');</pre>
     * @property $dom
     * @type object
     */
    $dom = {},

    getGoogleMapUrlByCountry = null,

    /**
     * Holds google map url for each country. Default is maps.google.com.
     * @property {Object} googleMapUrlByCountry
     * 
     */
    googleMapUrlByCountry = {
        'Default' : 'maps.google.com',
        'Germany' : 'maps.google.de'
    },

    initialWindowHeight = 0,
    currentWindowHeight = 0,
    percentageChange = 0,

    applyHeightToApplication = null,
    handleEvent = null,
    setBindings = null,
    endLoading = null,
    rebuildApplicationHeight = null,
    setApplicationHeight = null,
    fireEvent = null;

    /**
     * Wrapper for triggering events on the application, '#medelaLocationfinder'.
     * All events should be triggered with this function.
     * To bind an event created with fireEvent you would use the following jQuery syntax:
     * jquery('#medelaLocationfinder').bind(eventName, function () {
     *  // do something
     * };
     * @method fireEvent
     * @param {string} eventName
     */
    fireEvent = function (eventName) {
        $dom.application.trigger(eventName);
    };

    handleEvent = function (eventName, callback) {
        $dom.application.bind(eventName, function () {
            callback();
        });
    };

    /**
     * Removes the class "loading" from #medelaLocationfinder and triggers the event
     * 'endload'. To bind this event with Jquery, use the following code:
     * <pre>
     *  jQuery('#medelaLocationfinder').bind('endload', function () {
     *      //some code
     *  });
     * </pre>
     * 
     * @author tjunghans, Namics AG, www.namics.com
     * @method endLoading
     * @return {boolean} true on success
     */
    endLoading = function () {
        $dom.application.removeClass('loading');
        fireEvent('endload');
        return true;
    };

    /**
     * Retreives the height of the search column and stores this value in
     * <strong>applicationHeight</strong>. Using applicationHeight the function
     * <strong>applyHeightToApplication</strong> is called, to make all columns equal in height.
     * @author tjunghans, Namics AG, www.namics.com
     * @method setApplicationHeight
     * @return {boolean} true on success
     */
    setApplicationHeight = function () {


        applicationHeight = MEDELA.locationfinder.search.getHeight();
  
        // getDocumentHeight = 100%

        applyHeightToApplication(applicationHeight);

        return true;
    };

    /**
     * Applies the calculated height to the three main columns, so that they are equal in height
     * @author tjunghans, Namics AG, www.namics.com
     * @method applyHeightToApplication
     * @return {boolean} true on success
     */
    applyHeightToApplication = function (height) {
        height = height + 'px';
        jQuery('#medelaLocationfinderSearch').height(height);
        jQuery('#medelaLocationfinderResultoutput').height(height);
        jQuery('#medelaLocationfinderGmap').height(height);

        return true;
    };

    rebuildApplicationHeight = function (windowHeight) {

        var offset = parseInt($dom.application.offset().top, 10) + 20;

        windowHeight = parseInt(windowHeight, 10);

        if ((windowHeight - offset) <= initialApplicationHeight) {
            return false;
        }

        applyHeightToApplication(windowHeight - offset);
    };

    setBindings = function () {
        jQuery(window).resize(function () {
            NX.throttle(function () {
                rebuildApplicationHeight(jQuery(window).height());
            }, 500);
        });

        return true;
    };

    /**
     * The parameter <strong>country</strong> is the english country name
     * @method getGoogleMapUrlByCountry
     * @param {String} country eg. "Germany"
     * @return {String} google maps url
     */
    getGoogleMapUrlByCountry = function (country) {
        if (googleMapUrlByCountry[country]) {
            return googleMapUrlByCountry[country];
        } else {
            return googleMapUrlByCountry.Default;
        }
    };

    init = function () {
        jQuery("#medelaLocationfinderSearchFilter ul li ul li input").attr("disabled", true);

        // init $dom cache
        $dom.application = jQuery('#medelaLocationfinder');
        
        // load required packages
        MEDELA.locationfinder.search.init();

        setBindings();
        
        setApplicationHeight();

        initialWindowHeight = parseInt(jQuery(window).height(), 10);
        initialApplicationHeight = applicationHeight;
        rebuildApplicationHeight(jQuery(window).height());

        endLoading();

        return true;
    };

    return {
        'init' : init,
        'fireEvent' : fireEvent,
        'handleEvent' : handleEvent,
        'getGoogleMapUrlByCountry' : getGoogleMapUrlByCountry
    };
}());

for (var i in tmp) {
    if (tmp[i] instanceof Object) {
        MEDELA.locationfinder[i] = tmp[i];    
    }

}

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

jQuery(document).unload(function () {
    GUnload();
});

