ProspectDirector.Types.Core.Navigator = Class.extend({

    //=================================
    // fields
    //=================================

    canvasFormChanged: null,

    //=================================
    // constructor
    //=================================

    init: function (options) {

        var that = this;

        this.options = $.extend({
        }, options);

        $(document).on("click", "A", function (e) {
            if (that.isValidHref(e.target)) {
                that.go(e);
            }
        });
        
    },

    //=================================
    // public methods
    //=================================

    go: function (directive, e) {

        var result = true;

        if (document.activeElement
            && document.activeElement.nodeName !== "BODY") {
            document.activeElement.blur();
        }

        if (directive === undefined
            || directive === null) {
            return false;
        }

        switch ($.type(directive)) {
            case "string":
                result = this.goUrl(directive);
                break;
            case "object":
                result = this.goEvent(directive);
                break;
            case "function":
                result = this.goFunction(directive, e);
                break;
        }

        return result;

    },

    //=================================
    // private methods
    //=================================

    goUrl: function (url) {

        this.canGo(function () {
            window.location = ProspectDirector.RUNTIME.resolveUrl(url);
        });

    },

    goFunction: function (fn, e) {
        return this.canGo(fn);
    },

    goEvent: function (e) {

        if (this.isValidHref(e.target)) {

            e.preventDefault();

            this.canGo(function () {
                window.location = e.target.href;
            });

        }

    },

    canGo: function (goFn) {

        if (this.isDirty()) {
            this.prompt(goFn);
            return false;
        }

        goFn();

        return true;

    },

    isDirty: function () {
        return $(document).find(".isDirty").length > 0 || this.canvasFormChanged != null;
    },

    isValidHref: function (element) {

        return element.tagName === "A"
                && element.href !== null
                && element.href.length !== 0
                && !element.href.match(/\#$/);

    },

    prompt: function (fn) {

        var that = this;
        var observedForms = $(".observedForm");
        
        Jade.UI.MessageDialog.open({
            dialogStyle: Jade.UI.MessageDialogStyle.Warning,
            dialogButtons: Jade.UI.MessageDialogButtonStyle.YesNo,
            title: "Unsaved Changes Detected",
            message: "There are unsaved changes that have been made to this form. Discard changes and continue?",
            select: function (e) {

                if (e.button == Jade.UI.MessageDialogButton.yes) {
                    that.canvasFormChanged = null;
                    fn();
                    observedForms.observedForm("clear");
                }
                e.close();
                
            }
        });
        
    }

});