MV = MV || {};
MV.ProviderTypeNavigation = (function() {
    var _ptnav = {};

    var createLinks = function(providerTypeArray, templates) {
        var linkContainer = '';
        var link = '';
        var localLinks = [];
        var nationalLinks = [];
        for (var i = 0; i < providerTypeArray.length; i++) {
            var pt = providerTypeArray[i];
            link = templates.link.clone(true).find('a').attr('id', pt.id).html(pt.Name).end();
            if (pt.National) {
                nationalLinks.push(link);
            } else {
                localLinks.push(link);
            }
        };
        return {
            local: localLinks,
            national: nationalLinks
        }
    };

    var linkContainerLoader = function(links, container) {
        container.find('ul').empty();
        if (links.length > 0) {
            var len = links.length;
            for (var i = 0; i < len; i++) {
                var l = links[i];
                container.find('ul').append(l);
            };
        };
        return container;
    };

    return {
        init: function(navigationTemplates) {
            _ptnav = this; //set the reference

            this.providerTypeContainers = null;
            this.templates = navigationTemplates;
            this.linkTitles = {
                local: MV.WebServices.Strings.GetEntry('localProviderTypes'),
                national: MV.WebServices.Strings.GetEntry('nationalProviderTypes')
            }

            return this;
        },

        create: function(providerTypeArray) {
            this.containers = {
                navigation: $('#providerTypeNavigationContainer'),
                local: this.templates.container.clone().attr('id', 'localLinks'),
                national: this.templates.container.clone().attr('id', 'nationalLinks')
            };

            var links = createLinks(providerTypeArray, this.templates);

            this.containers.local = linkContainerLoader(links.local, this.containers.local);
            this.containers.national = linkContainerLoader(links.national, this.containers.national);

            // load up the local provider type links
            if (!links.local || links.local.length == 0) {
                this.containers.local.addClass('invisible')
            } else {
                this.containers.navigation.append(this.templates.title.clone(true).attr('id', 'localLinksTitle').find('a').html(this.linkTitles.local).end());
            }
            this.containers.navigation.append(this.containers.local);


            // load up the national provider type links
            if (!links.national || links.local.national == 0) {
                this.containers.national.addClass('invisible');
            } else {
                this.containers.navigation.append(this.templates.title.clone(true).attr('id', 'nationalLinksTitle').find('a').html(this.linkTitles.national).end());
            }
            this.containers.navigation.append(this.containers.national);

            // the first class needs to be added
            this.containers.navigation.find('dt:lt(2)').addClass('first');

            // return the entire container
            return this.containers.navigation;
        },

        selectProviderType: function(currentProviderType) {
            $('#providerTypeNavigationContainer').find('dt,dd').addClass('invisible');

            // set the search type header
            $('#selectedProviderTypeLink').html(currentProviderType.name);
            $('#selectedProviderTypeTitle').removeClass('invisible').find('a').html((currentProviderType.config.National) ? this.linkTitles.national : this.linkTitles.local);
            $('#selectedProviderTypeWrapper').removeClass('invisible');
        },

        reset: function() {
            $('#providerTypeNavigationContainer').find('dt,dd').removeClass('invisible');
            $('#selectedProviderTypeWrapper').addClass('invisible');
            $('#selectedProviderTypeTitle').addClass('invisible');
            $('.decoration').removeClass('decorationWithForm');
        }
    }
    // module end
})();