﻿namespace('nnet.sd');
var SD = null;

// Service Directory Events
nnet.sd.onNavigationTreeDataReady = new YAHOO.util.CustomEvent("nnet.sd.onNavigationTreeDataReady");
nnet.sd.onNavigationTreeRendered = new YAHOO.util.CustomEvent("nnet.sd.onNavigationTreeRendered");

nnet.sd.Directory = $j.klass({
    initialize: function(treeData, initialData) {
        try {
            this.CurrentListingId = sVars.SD_CurrentListingId;
            this.CurrentServiceId = sVars.SD_CurrentServiceId;
            this.CurrentCategoryId = sVars.SD_CurrentCategoryId;
            this.CurrentCategoryType = sVars.SD_CurrentCatType;
            this.SearchSource = sVars.SD_SearchSource;
            this.CurrentSearchSite = sVars.SD_CurrentSearchSite;
            this.ContentViewState = sVars.SD_ContentViewState;

            // setup initial panel state
            this.initialView(this.ContentViewState);

            // Adjust layou of content pages    
            SDAdjustContentLayout();

            // Reset setup initial view as sizeing can hide things
            this.initialView(this.ContentViewState);

            // create directory objects
            this.ContentToolBar = new nnet.sd.ContentToolBar();
            this.NavigationTabs = new nnet.sd.NavigationTabs();
            this.NavigationTree = new nnet.sd.NavigationTree(treeData);
            this.CategoryPage = new nnet.sd.CategoryPage(initialData);
            this.ServiceListing = new nnet.sd.ServiceListing();
            this.ListingView = new nnet.sd.ListingView();
            this.DetailView = new nnet.sd.DetailView();
            this.MapView = new nnet.sd.MapView();

            if (sVars.PreloadedData) this.preloadInit(sVars.Bookmark, initialData);
        } catch (e) { logE("sd.Directory.initialize", e); }
    },

    initialView: function(view) {
        // setup initial panel state,
        $j(sVars.SD_DetailContainerPanelID).hide();
        //$j('#MapContainer').hide(); //do not hide map container as the map will not be generated properly
        $j('#CategoryContainer').hide();
        $j(sVars.SD_ListContainerPanelID).hide();
        if (view == sVars.LIST_CONTENT_VIEW_STATE) $j(sVars.SD_ListContainerPanelID).show();
        else if (view == sVars.CATEGORY_CONTENT_VIEW_STATE) $j('#CategoryContainer').show();
        else if (view == sVars.DETAIL_CONTENT_VIEW_STATE) $j(sVars.SD_DetailContainerPanelID).show();
    },

    // Called when dom has been loaded,handler
    startup: function() {
        try {
            SD.NavigationTabs.initNavigationTabs()
            //SD.NavigationTree.startup();
            ServiceContentLoadingImage = new AjaxLoadingImage("rightColumn");
            //SDAdjustContentLayout();
            SD.ContentToolBar.startup();
            SD.ListingView.startup();
            SD.MapView.startup();
            SD.CategoryPage.startup();
            //            if (dhtmlHistory.getCurrentHash() == "") { // No bookmark
            if (NNet.PageHistory.startupBookmarkExists()) { // bookmark, do this first so if there is perloaded with bookmak the preload gets ignored
                SetContentToLocation(NNet.PageHistory.startupBookmark());
            } else if (sVars.PreloadedData) {
                this.preloadStartup(sVars.Bookmark);
                RefreshAd();
            } else {
                if (SD.ContentViewState == sVars.CATEGORY_CONTENT_VIEW_STATE) {
                    this.CategoryPage.display();
                    NNet.PageHistory.add();
                    //SD.show(sVars.CATEGORY_CONTENT_VIEW_STATE)
                    //$j('#MapContainer').hide();
                    //SDCallback.GetCategoryPages(currentCatId, currentCatType, currentPage, SD.CurrentSearchSite, NNet.ProcessID, GetCategoryPages_Callback, OnListCallbackError);
                } else if (SD.ContentViewState == sVars.LIST_CONTENT_VIEW_STATE) {
                    $j('#CategoryContainer').hide();
                    //$j('#ListContainer').show();
                    SD.ListingView.show();
                    if (SD.SearchSource == sVars.CATEGORY_SEARCH_SOURCE) SDCallback.GetServiceListPages(SD.CurrentCategoryId, SD.CurrentCategoryType, currentPage, SD.CurrentSearchSite, NNet.ProcessID, GetServiceList_Callback, OnListCallbackError);
                    else if (SD.SearchSource == sVars.SERVICE_SEARCH_SOURCE) SDCallback.GetOneServiceList(SD.CurrentCategoryId, SD.CurrentCategoryType, 0, SD.CurrentSearchSite, NNet.ProcessID, GetServiceList_Callback, OnListCallbackError);
                } else if (SD.ContentViewState == sVars.MAP_CONTENT_VIEW_STATE) {
                    //xDisplay('ListContainer','none');
                    //$j('#ListContainer').hide();
                    SD.ListingView.hide();
                    SDCallback.GetServiceMap(SD.CurrentCategoryId, SD.CurrentCategoryType, SD.CurrentSearchSite, SD.MapView.callback, OnListCallbackError);
                }
                //AddServicesHistory();
            }

            onLogIn.subscribe(
                function(type, args) {
                    try {
                        if (!args[0].IsStartUp) { SDRefreshContent(); }
                    } catch (e) { logE("Service - onLogIn handler", e); }
                }
            );

            onLogOut.subscribe(
                function(type, args) {
                    try {
                        if (!args[0].IsStartUp) { SDRefreshContent(); } 
                    } catch (e) { logE("Service - onLogOut handler", e); }
                }
            );


            //nnet.sd.onNavigationTreeDataReady.subscribe(fnRenderBookmark); // there is a bookmark, so dependent on tree data
        } catch (e) { logE("sd.Directory.startup", e); }
    },

    // Data has been preloaded into page.
    // Data will ether be embedded in Page, in INITIAL_DATA or both.
    preloadInit: function(bookmark, initialData) {
        try {
            if (this.ContentViewState == sVars.LIST_CONTENT_VIEW_STATE) { // Preloaded as html in List container
                $j('#CategoryContainer').hide();
                this.ListingView.show();
                this.ServiceListing.Data = initialData;
                this.ContentToolBar.showMapButton();
                if (NiftyCheck()) { NFRounded('div.enhanced', 'top', 'transparent', '#5987D6', 'border #8DB1E5 smooth'); NFRounded('div.enhanced', 'bottom', 'transparent', '#F0F0E7', 'small border #8DB1E5'); }
                this.NavigationTree.showTreeNode();
            } else if (this.ContentViewState == sVars.DETAIL_CONTENT_VIEW_STATE) {
                this.show(sVars.DETAIL_CONTENT_VIEW_STATE);
                if (sVars.Function == FEEDBACK) this.DetailView.showFeedback();
                if (sVars.SD_CurrentCategoryId > 0) this.NavigationTree.showTreeNode();
            }
        } catch (e) { logE("sd.Directory.preloadInit", e); }

    },

    preloadStartup: function(bookmark) {
        try {
            if (this.ContentViewState == sVars.LIST_CONTENT_VIEW_STATE) { // Preloaded as html in List container
                //CreateServiceMap(this.MapView, sVars.INITIAL_DATA);
            } else if (this.ContentViewState == sVars.DETAIL_CONTENT_VIEW_STATE) {
            }
            if (sVars.PopupFunction != sVars.NO_POPUP) ShowPopup(sVars.PopupFunction, { id: sVars.PopupId, view: this.ContentViewState, loggedIn: sVars.UserLoggedIn, func: sVars.Function });
            NNet.PageHistory.add(bookmark);

        } catch (e) { logE("sd.Directory.preloadStartup", e); }

    },

    show: function(view) {
        try {
            this.ContentViewState = view;
            if (view == sVars.CATEGORY_CONTENT_VIEW_STATE) {
                this.CategoryPage.show();
                this.ListingView.hide();
                this.DetailView.hide();
                this.MapView.hide();
                try { // May not yet be defined on first page display, will be hidden then anyway
                    this.ContentToolBar.ListButton.hide();
                    this.ContentToolBar.MapButton.hide();
                    this.NavigationTabs.disableMapTab();
                } catch (e) { };
            } else if (view == sVars.DETAIL_CONTENT_VIEW_STATE) {
                this.DetailView.show();
                this.CategoryPage.hide();
                this.ListingView.hide();
                this.MapView.hide();
                if (this.SearchSource != sVars.NO_SEARCH_SOURCE) { // Only display these wen detail is result of a search, not a link
                    try { // May not yet be defined on first page display, will be hidden then anyway
                        this.ContentToolBar.ListButton.show();
                        this.ContentToolBar.showMapButton();
                        this.NavigationTabs.disableMapTab();
                    } catch (e) { };
                }
            } else if (view == sVars.MAP_CONTENT_VIEW_STATE) {
                this.DetailView.hide();
                this.CategoryPage.hide();
                this.ListingView.hide();
                this.MapView.show();
                try { // May not yet be defined on first page display, will be hidden then anyway
                    SD.ContentToolBar.ListButton.show();
                    SD.ContentToolBar.MapButton.hide();
                    SD.NavigationTabs.selectMapTab();
                } catch (e) { };
            } else if (view == sVars.LIST_CONTENT_VIEW_STATE) {
                this.DetailView.hide();
                this.CategoryPage.hide();
                this.ListingView.show();
                this.MapView.hide();
                try { // May not yet be defined on first page display, will be hidden then anyway
                    SD.ContentToolBar.ListButton.hide();
                    SD.ContentToolBar.MapButton.show();
                    SD.NavigationTabs.disableMapTab();
                } catch (e) { };
            }
            AdjustView();
        } catch (e) { logE("sd.Directory.show", e); }
    },

    hide: function() {
    }
});










