﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Web");

Web.PathBehavior = function(element) {
    Web.PathBehavior.initializeBase(this, [element]);

    this._onKeyDownHandler = null;
    this._nextID = null;
    this._prevID = null;
    this._albumID = null;
}

Web.PathBehavior.prototype = {
    addEventHandlers: function() {
        $addHandler(document, 'keydown', this._onKeyDownHandler);
    },

    removeEventHandlers: function() {
        try { $removeHandler(document, 'keydown', this._onKeyDownHandler); }
        catch (ex) { }
    },

    initialize: function() {
        Web.PathBehavior.callBaseMethod(this, 'initialize');

        this._onKeyDownHandler = Function.createDelegate(this, this.onKeyDown);
        this.addEventHandlers();
    },

    dispose: function() {
        this.removeEventHandlers();
        Web.PathBehavior.callBaseMethod(this, 'dispose');
    },

    onKeyDown: function(e) {
        if (typeof (document.activeElement) == 'undefined' || document.activeElement == null || (document.activeElement.nodeName != "INPUT" && document.activeElement.nodeName != "TEXTAREA")) {
            var link = null;

            switch (e.keyCode) {
                /*
                case 38: // Up
                if (getScrollXY()[1] == 0)
                link = document.getElementById(this._albumID);
                break;*/ 

                case 39: // Right
                    link = document.getElementById(this._nextID);
                    break;

                case 37: // Left
                    link = document.getElementById(this._prevID);
                    break;
            }

            if (link != null) {
                var hash = link.href.substr(link.href.lastIndexOf('#=')).trimChar('#').trimChar('=');
                top.eval("Sys.Application.addHistoryPoint({'': '" + hash + "'},'')");
                /*            
                if (link.href.toLowerCase().indexOf("javascript:") != -1)
                eval(unescape(link.href));
                else
                top.window.location.href = link.href;
                */

                e.cancelBubble = true;
                if (e.stopPropagation)
                    e.stopPropagation();

                return false;
            }
        }
    }
}
Web.PathBehavior.registerClass('Web.PathBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
