/*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.customer_sites = (function() {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var

        configMap = {
            //main_html: '',

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

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

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




    // Begin private DOM methods 



    // End private DOM methods 
    //---------------------- END DOM METHODS ---------------------

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





    //-------------------- 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');
        }

        $container.html(Handlebars.templates['app.customer_sites']({}));
       
        var title = "Sites";

        if (stateMap.id) {


            app.api.getName('customer', stateMap.id, function(res) {
                if (res.error) {
                    $('#app-error-message').text(res.msg);
                } else {
                    //set customer name 
                    title = 'Sites - ' + res.name;
                    $("#app-form-title").text(title);

                    if (stateMap.id) {

                        //fetch sites list

                        //fetch existing record                       
                        app.api.getRelativeList('site', 'name', 'a', 'customer_id', stateMap.id, function(res) {
                            if (res.error) {                              
                                $('#app-error-message').text(res.msg);
                            } else {
                                //get the list ul
                                var $appList = $('#app-list');

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

                            }
                        });
                    }
                }
            });
        }

        //Context menu, back and new
        $(".app-context-nav").append('<li><a href="#!/customer_edit/' + stateMap.id + '">Back</a></li>');
        $(".app-context-nav").append('<li><a href="#!/customer_site_edit/new/' + stateMap.id + '">New +</a></li>');
    };
    // End public method /initModule/



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