﻿namespace('nnet');

nnet.PageHistory = $j.klass({
    initialize: function() {
        this.Location = null; // Corresponds to the pagehistory null value

        //        window.dhtmlHistory.create({
        //            toJSON: function(o) {
        //                return NJSON.stringify(o);
        //            },
        //            fromJSON: function(s) {
        //                return NJSON.parse(s);
        //            }
        //        });
    },

    // Contect is not this class.
    startup: function() {
        try {
            Ext.History.init();
            //var hist = window.location.hash.substr(1);
            //Ext.History.add(hist != "" ? hist : " " );
            Ext.History.on('change', NNet.PageHistory.historyChange);
            //        window.dhtmlHistory.initialize();
            //        window.dhtmlHistory.fireOnNewListener = false;
            //        window.dhtmlHistory.addListener(NNet.PageHistory.historyChange);
        } catch (e) { logE("NNet.PageHistory.startup : " + location, e); }
    },

    add: function(bookmark) {
        var location = "";
        try {
            if (bookmark != undefined) {
                this.Location = bookmark;
            } else {
                if (SD.ContentViewState == sVars.CATEGORY_CONTENT_VIEW_STATE & SD.CurrentCategoryType == sVars.SD_PAGE_CATEGORY_TYPE && SD.CurrentCategoryId == 0) {
                    this.Location = " "; //root so leave as basic URL, requires a space other wise wont recognise it
                } else {
                    location = "Services/" + SD.ContentViewState + "/"
                    if (SD.ContentViewState == sVars.DETAIL_CONTENT_VIEW_STATE) location += SD.CurrentServiceId;
                    else {
                        if (SD.SearchSource == sVars.CATEGORY_SEARCH_SOURCE) {
                            if (SD.CurrentCategoryId == 0) location += SD.SearchSource + "/" + SD.CurrentSearchSite;
                            else location += SD.SearchSource + "/" + SD.CurrentSearchSite + "/" + SD.CurrentCategoryId;
                        } else if (SD.SearchSource == sVars.SERVICE_SEARCH_SOURCE) {
                            location += SD.SearchSource + "/" + SD.CurrentServiceId;
                        } else {// text search
                            location += SD.SearchSource + "/" + SD.CurrentSearchSite + "/" + escape(currentTextSearch);
                        }
                        if (currentPage != 0) location += "/" + LIST_PAGE + "/" + currentPage;
                    }
                    this.Location = location;
                }
            }
            Ext.History.add(this.Location);
        } catch (e) { logE("NNet.PageHistory.add : " + location, e); }
    },

    // Returns true is a history token (bookmark) exists
    exists: function() {
        return (Ext.History.getToken() != null && Ext.History.getToken() != "");
    },



    // Returns the current history token (bookmark)
    current: function() {
        return Ext.History.getToken();
    },

    startupBookmarkExists: function() {
        return window.location.hash != "";
    },

    startupBookmark: function() {
        return window.location.hash.substr(1);
    },

    // switch to an application fro
    historyChange: function(newLocation) {
        try {
            if (!sVars.IsAdminSite & NNet.PageHistory.Location != newLocation) SDApplicationHistoryChange(newLocation); // Admin has no history
        } catch (e) { logE("NNet.PageHistory.historyChange : " + newLocation, e); }
    }

});


