﻿
var HelpIntegration = new Class({

    _topicFrame: null,
    //    _currentPath: null,
    _activeHash: "",
    _helpNewWin: null,

    initialize: function() {
        this._checkLoaded();
    },

    _checkLoaded: function() {
        var iframe = $('helpframe');

        var doc = iframe.contentDocument;
        if (doc == undefined || doc == null) doc = iframe.contentWindow.document;

        var iframeBody = $(doc.body);

        if (!iframeBody) {
            this._checkLoaded.delay(50, this);
        }
        else {
            this._topicFrame = iframeBody.getElement('frame[name="topic"]');
            if (this._topicFrame) {
                this._helpNewWin = $('help_new_window');
                //this._currentPath = this._getTopicPath();
                this._reviewSrc.periodical(100, this);
            }
        }
    },

    _getTopicLocation: function() {
        var doc = this._topicFrame.contentDocument;
        if (doc == undefined || doc == null) doc = this._topicFrame.contentWindow.document;
        return new String(doc.location);
    },

    /*
    _getTopicPath: function() {
        return Atl_Url.parse(this._getTopicLocation).path;
    },
    */

    _setNewWinUrl: function() {
        this._helpNewWin.set('href', this._getTopicLocation());
    },

    /*
    _getHash: function() {
    return Atl_Url.parse(new String(window.location)).anchor;
    },

    _navigateToHash: function(hash) {
    var loc = Atl_Url.parse(new String(window.location));
    var dest;
    if (hash.length > 0) {
    dest = loc.path + '/' + hash;
    }
    else {
    dest = loc.path;
    }
    window.location = dest;
    },    
    */

    _reviewSrc: function() {

        this._setNewWinUrl();
        /*
        var currentPath = this._getTopicPath();
        if (this._currentPath.toLowerCase() != currentPath.toLowerCase()) {
        this._currentPath = currentPath;

            var loc = Atl_Url.parse(new String(window.location));

            // find difference between window.location and this._currentPath
        var notMatchedFrom = -1;
        for (var i = 0; i < this._currentPath.length; ++i) {
        if ((i >= loc.path.length) || (loc.path.substr(i, 1) != this._currentPath.substr(i, 1))) {
        notMatchedFrom = i;
        break;
        }
        }

            var differentBit = this._currentPath.substr(notMatchedFrom + 1);
        this._activeHash = differentBit;
        //window.location = loc.path + '#' + differentBit;
        }

        var hash = this._getHash();
        if ((hash.length > 0) && (hash != this._activeHash)) {
        this._activeHash = hash;
        this._navigateToHash(hash);
        }
        */
    }

});

window.addEvent('domready', function() {
    new HelpIntegration();
});

