/*jslint         browser : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global $, app */

app.incidents = (function() {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var

        configMap = {
            //main_html: '',

            settable_map: {}
        },
        stateMap = {
            $append_target: null
        },
        onReverse, loadList,
        configModule, initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------
    //-------------------- END UTILITY METHODS -------------------

    //--------------------- BEGIN DOM METHODS --------------------

    loadList = function() {
        //----
        //fetch                       
        app.api.getRelativeList('incident', 'contactedDate', stateMap.sortOrder, 'site_id', stateMap.id, function(res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
            } else {
                //get the list ul
                var $appList = $('#app-list');

                $appList.empty();

                $.each(res, function(i, obj) {
                    $appList.append("<li><a href=\"#!/incident_edit/" + obj._id + "/" + stateMap.id + "\">" +
                        app.util_b.genListColumn(app.util_b.utcStringToShortDate(obj.contactedDate)) +
                        app.util_b.genListColumn(obj.name) +
                        "</a></li>")
                });

            }
        });
        //------
    }


    //---------------------- END DOM METHODS ---------------------

    //------------------- BEGIN EVENT HANDLERS -------------------

    onReverse = function(event) {
        if (stateMap.sortOrder == 'd') {
            stateMap.sortOrder = 'a';
        } else {
            stateMap.sortOrder = 'd';
        }
        loadList();
        return false; //prevent default
    };


    //-------------------- END EVENT HANDLERS --------------------

    //------------------- BEGIN PUBLIC METHODS -------------------
    //CONFIGMODULE
    //
    configModule = function(context) {
        stateMap.context = context.context;
        if (stateMap.context.params.id) {
            stateMap.id = stateMap.context.params.id;
        }
    };

    // Begin public method /initModule/
    // Example    : app.customer.initModule( $('#div_id') );
    // Purpose    : directs the module to being offering features
    // Arguments  : $container - container to use
    // Action     : Provides interface
    // Returns    : none
    // Throws     : none
    //
    initModule = function($container) {
        if (typeof $container === 'undefined') {
            $container = $('#app-shell-main-content');
        }
        stateMap.sortOrder = 'd';

        $container.html(Handlebars.templates['app.incidents']({}));

        var title = "Incidents";

        if (!stateMap.id) {
            throw ('app.incidents.js::initModule - There is no stateMap.id!');
        }

        app.api.get('site', stateMap.id, function(res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
            } else {
                //Context menu, back and new
                //Back button context item, here because we need the customer id for it
                $(".app-context-nav").append('<li><a href="#!/customer_site_edit/' + stateMap.id + '/' + res.customer_id + '">Back</a></li>');
                $(".app-context-nav").append('<li><a href="#!/incident_edit/new/' + stateMap.id + '">New +</a></li>');

                //set customer name 
                title = 'Incidents - ' + res.name;
                $("#app-form-title").text(title);

                if (stateMap.id) {

                    loadList();
                }
            }
        });

        //bind events
        $('#btn-rvs').bind('click', onReverse);

    };
    // End public method /initModule/



    // return public methods
    return {
        configModule: configModule,
        initModule: initModule
    };
    //------------------- END PUBLIC METHODS ---------------------
}());