﻿/* Static, stand-alone class to handle header and main navigation UI */

Ext.namespace("IMD", "IMD.Website", "IMD.Website.UI");

Ext.define("IMD.Website.Models.Credentials", {
	extend: "Ext.data.Model",
	fields: ["Authenticated", "FullName"]
});

Ext.define("IMD.Website.UI.Header", {

    CREDENTIALS_URL: "/customcf/ws/user.cfm",
    QUICK_MENU_SIZE: 270,

    tplLogin: new Ext.XTemplate(
		'<strong>{userName}</strong> | ',
		'<a class="contentTextlink" href="/logout.cfm">Logout</a>'
	),

    init: function () {
        if (Ext.get("quickAccess")) {
            new Ext.ux.Menu("quickAccess", {
                reqWidth: this.QUICK_MENU_SIZE,
                hideDelay: 0.8
            });
        }
        if (Ext.get("topBarDummyContainer")) {
            Ext.get("topBarDummyContainer").remove();
        };
        if (Ext.get("navigationDummyContainer")) {
            Ext.get("navigationDummyContainer").remove();
        };
        if (Ext.get("toolsContainer")) {
            Ext.get("toolsContainer").setVisible(true);
        };
        if (Ext.get("topBarContainer")) {
            Ext.get("topBarContainer").setVisible(true);
        };
        if (Ext.get("navigationContainer")) {
            Ext.get("navigationContainer").setVisible(true);
            new Ext.ux.Menu("navigationMenu", {
            });
        };
        var loginNode = Ext.get("loginUser");
        if (loginNode) {
            this.fetchCredentials(this.readCredentials);
        };
        this.attachFBYevent();
    },

    attachFBYevent: function () {
        var FBYel = Ext.get("FBYTrigger");
        if (FBYel) {
            FBYel.on("click", function (event) {
                FBY.showForm("930");
                event.stopEvent();
            });
            FBY.hideFlash(false);
        }
    },

    readCredentials: function (response) {
        var reader = new Ext.data.XmlReader({ type: "xml", model: "IMD.Website.Models.Credentials", record: "User" });
        var userData = reader.readRecords(response.responseXML).records[0].data;
        if (userData.Authenticated == "true") {
            this.displayCredentials(userData.FullName);
        };
    },

    displayCredentials: function (userName) {
        Ext.get("loginUser").update(this.tplLogin.applyTemplate({ userName: userName }));
    },

    fetchCredentials: function (callBack) {
        var self = this;
        Ext.Ajax.request({
            url: this.CREDENTIALS_URL,
            success: function (response) {
                callBack.apply(self, [response]);
            },
            failure: function(response, opts) {
                // Warning, gigantic hack ahead!
                // Try one last time, in case we get a 307 redirect from the crappy HTTPS
                self.fetchAgainCredentials(callBack);
            }
        });
    },

    // This is an orrible hack
	// Try again, but if we fail we give up this time around
    fetchAgainCredentials: function (callBack) {
        var self = this;
        Ext.Ajax.request({
            url: this.CREDENTIALS_URL,
            success: function (response) {
                callBack.apply(self, [response]);
            }
        });
    }

});
