/*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.customerSites = (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.customerSites']({}));

        var title = "Sites";

        if (stateMap.id) {


            app.api.get('customer/' + stateMap.id + '/name', function (res) {
                if (res.error) {
                    $.gevent.publish('app-show-error',res.msg);
                } else {
                    //set customer name 
                    title = 'Sites - ' + res.name;
                    //app.nav.setContextTitle(title);


                    if (stateMap.id) {

                        //fetch sites list

                        //fetch existing record                       
                        app.api.get('customer/' + stateMap.id + '/sites', function (res) {
                            if (res.error) {
                                $.gevent.publish('app-show-error',res.msg);
                            } else {
                                //get the list ul
                                var $appList = $('#rf-list');

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

                            }
                        });
                    }
                }
            });
        }

        //Context menu 
        app.nav.contextClear();
        app.nav.contextAddLink('customerSiteEdit/new/' + stateMap.id, "New", "plus");
        app.nav.contextAddLink("customerEdit/" + stateMap.id, "Customer", "account");
       

    };
    // End public method /initModule/



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