﻿ ///<reference path="../jquery-1.3.2-vsdoc.js" />
 ///<reference path="nmls.js" />
 ///<reference path="nmls.turingTest.js" />
 ///<reference path="nlms.branchLocations.js" />

(function($)
{
    nmls.entityDetail = function(baseUrl)
    {
        this.baseUrl = baseUrl;
    }

    nmls.entityDetail.prototype.get = function(entityType, id, success, error)
    {
        // Execute ajax call to get the entity details.
        $.ajax({
            url: this.baseUrl + "EntityDetails.aspx/" + entityType + "/" + id,
            type: "GET",
            dataType: "html",
            cache: false,
            error: error,
            success: success
        });
    }

    // The entityUI is an object that is expected to have "type" and "turingTestRedirect" properties, and a "bind" function.
    // The "type" property specifies the entity detail type, i.e. Individual or Company.
    // The "turingTestRedirect" property specifies a turingTestRedirect object.
    // The "bind" function should bind events and other data to the UI elements.
    nmls.entityDetailUI = function(baseUrl, entityUI)
    {
        var _entityDetail = new nmls.entityDetail(baseUrl);

        function _onGetEntitySuccess(result, textStatus)
        {
            var resObj;
            try
            {
                resObj = eval('(' + result + ')');
            }
            catch (e)
            {
                // Swallow the exception when the result is html and not JSON.
            }
            if (resObj)
                nmls.alertJsonError(resObj);
            else
            {
                $("#entityDetail").html(result);
                entityUI.bind();
            }
        }

        function _onGetEntityError(xmlHttpReq, textStatus, errorThrown)
        {
            nmls.alertAjaxError(xmlHttpReq, textStatus, errorThrown);
        }

        function _onAuthenticated()
        {
            _entityDetail.get(entityUI.type, $("#entityId").val(), _onGetEntitySuccess, _onGetEntityError);
        }

        entityUI.turingTestRedirect.authenticatedCallback = _onAuthenticated;

        // The value "shown" is for when the user refereshes the page, passes the turing test, navigates to another page, and then
        // presses the back button on the browser.  Without this check, the turing test would be shown again due to caching of 
        // the pages by the browser.
        if ($("#displayTuringTest").val() === "Shown")
            _onAuthenticated();
        else if ($("#displayTuringTest").val() === "True")
        {
            $("#displayTuringTest").val("Shown");
            entityUI.turingTestRedirect.show($("#hideTerms").val() === "True");
        }
    }

    nmls.entityCommonTooltips = function()
    {
        $("#ttRegulator").tooltip({ content: $("#ttRegulatorText").html(), delay: 500, offsetPosition: "vt-hc" });
        $("#ttAuthorizedToConductBusiness").tooltip({ content: $("#ttAuthorizedToConductBusinessText").html(), delay: 500, offsetPosition: "vt-hc" });
        $(".ttOriginalIssueDate").tooltip({ content: $("#ttOriginalIssueDateText").html(), delay: 500, offsetPosition: "vt-hc" });
        $(".ttLicenseStatus").tooltip({ content: $("#ttLicenseStatusText").html(), delay: 500, offsetPosition: "vt-hc" });
    }

    nmls.companyBranchCommonTooltips = function()
    {
        $("#ttOtherTradeNames").tooltip({ content: $("#ttOtherTradeNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
        $("#ttPriorOtherTradeNames").tooltip({ content: $("#ttPriorOtherTradeNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
        $(".ttOtherTradeNamesState").tooltip({ content: $("#ttOtherTradeNamesStateText").html(), delay: 500, offsetPosition: "vt-hc" });
    }

    nmls.licenseDetailUI = function(baseUrl)
    {
        var $viewAllDetBtn = $("#viewAllDetailsButton"),
            $viewDetails = $("a.viewDetails");

        function _showViewDetails($viewDetails)
        {
            $viewDetails.removeClass("hideDetails");
            $viewDetails.addClass("viewDetails");
            $viewDetails.text("View Details");
        }

        function _hideViewDetails($viewDetails)
        {
            $viewDetails.removeClass("viewDetails");
            $viewDetails.addClass("hideDetails");
            $viewDetails.text("Hide Details");
        }

        $viewDetails.each(function(i)
        {
            var id = $(this).attr("id"),
                licenseId = id.replace("viewDetails_", "");
            $(this).bind("click", function(e)
            {
                var $licenseDetails = $("#licenseDetails_" + licenseId);
                if ($licenseDetails.css("display") == "none")
                {
                    $licenseDetails.showOrFadeIn();
                    _hideViewDetails($(this));
                }
                else
                {
                    $licenseDetails.hideOrFadeOut();
                    _showViewDetails($(this));
                }

                return false;
            });
        });

        $("#viewAllDetails").bind("click", function(e)
        {
            var $licenseDetails = $(".viewLicenseDetails"),
                imgPlus = baseUrl + "img/but-viewAllDetails-plus.gif";
            if ($viewAllDetBtn.attr("src") === imgPlus)
            {
                $licenseDetails.showOrFadeIn();
                $viewAllDetBtn.attr("src", baseUrl + "img/but-hideAllDetails.gif");
                $viewDetails.each(function(i)
                {
                    _hideViewDetails($(this));
                });
            }
            else
            {
                $licenseDetails.hideOrFadeOut();
                $viewAllDetBtn.attr("src", imgPlus);
                $viewDetails.each(function(i)
                {
                    _showViewDetails($(this));
                });
            }

            return false;
        });
    }

    nmls.companyUI = function(baseUrl, options)
    {
        var win,
            _branchLocationsRedirect = new nmls.branchLocationsRedirect(baseUrl, options);

        function _openResidentAgent(options)
        {
            var left, top, html
            _options =
                {
                    width: 660,
                    height: 375,
                    content: ""
                };

            $.extend(_options, options);

            left = (screen.width - _options.width) / 2;
            top = (screen.height - _options.height) / 2;

            if (win && !win.closed)
                win.close();

            win = window.open("", _options.name,
                'width=' + _options.width + ',height=' + _options.height +
                ',left=' + left + ',top=' + top +
                ',location=0,menubar=0,status=0,toolbar=0,scrollbars=1,resizable=0');

            html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\
<html xmlns="http://www.w3.org/1999/xhtml">\
<head runat="server">\
<title>Consumer Access - Registered Agent</title>\
<link rel="stylesheet" type="text/css" media="all" href="' + baseUrl + 'Content/reset.css" />\
<link rel="stylesheet" type="text/css" media="all" href="' + baseUrl + 'Content/text.css" />\
<link rel="stylesheet" type="text/css" media="all" href="' + baseUrl + 'Content/grid.css" />\
<link rel="stylesheet" type="text/css" media="all" href="' + baseUrl + 'Content/global.css" />\
<link rel="stylesheet" type="text/css" media="all" href="' + baseUrl + 'Content/popup.css" />\
<link rel="stylesheet" type="text/css" media="print" href="' + baseUrl + 'Content/print.css" />\
</head>\
<body>' + _options.content +
'</body>\
</html>'

            win.document.write(html);
            // Just in case width and height are ignored
            win.resizeTo(_options.width, _options.height);
            // Just in case left and top are ignored
            win.moveTo(left, top);
            win.focus();
        }

        this.type = "COMPANY";
        this.turingTestRedirect = _branchLocationsRedirect.turingTestRedirect;
        this.bind = function()
        {
            _branchLocationsRedirect.bind();
            nmls.licenseDetailUI(baseUrl);
            nmls.entityCommonTooltips();
            nmls.companyBranchCommonTooltips();
            $("#ttPriorLegalNames").tooltip({ content: $("#ttPriorLegalNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttOtherTradeNamesBranchLocations").tooltip({ content: $("#ttOtherTradeNamesBranchLocationsText").html(), delay: 500, offsetPosition: "vt-hc" });

            $("a.residentAgentPopup").bind("click", function(e)
            {
                _openResidentAgent(
                {
                    width: 660,
                    height: 440,
                    content: $(this).prev().html()
                });

                return false;
            });
        }

        nmls.entityDetailUI(baseUrl, this);
        this.bind();
    }

    nmls.individualUI = function(baseUrl, options)
    {
        var _turingTestRedirect = new nmls.turingTestRedirect(baseUrl, options.turingTestUIOptions),
            $viewEmpHistBtn = $("#viewEmploymentHistoryButton");

        this.type = "INDIVIDUAL";
        this.turingTestRedirect = _turingTestRedirect;
        this.bind = function()
        {
            $viewEmpHistBtn = $("#viewEmploymentHistoryButton");
            $("#viewEmploymentHistory").bind("click", function(e)
            {
                var imgView = baseUrl + "img/but-EmploymentHist-view.jpg";
                if ($viewEmpHistBtn.attr("src") === imgView)
                    $viewEmpHistBtn.attr("src", baseUrl + "img/but-EmploymentHist-hide.jpg");
                else
                    $viewEmpHistBtn.attr("src", imgView);

                $(".nonCurrentEmployment").each(function(e)
                {
                    if ($(this).css("display") == "none")
                        $(this).showOrFadeIn();
                    else
                        $(this).hideOrFadeOut();
                });

                return false;
            });

            $("a[name='viewPrevAuth']").each(function(i)
            {
                var id = $(this).attr("id"),
                    licenseId = id.replace("viewPrevAuth_", "");
                $(this).bind("click", function(e)
                {
                    var $prevAuthRow = $("#viewPrevAuthRow_" + licenseId);
                    if ($prevAuthRow.css("display") == "none")
                    {
                        $prevAuthRow.showOrFadeIn();

                        $(this).removeClass("plus");
                        $(this).addClass("minus");
                        $(this).text("Hide Previously Authorized to Represent");
                    }
                    else
                    {
                        $prevAuthRow.hideOrFadeOut();

                        $(this).removeClass("minus");
                        $(this).addClass("plus");
                        $(this).text("View Previously Authorized to Represent");
                    }

                    return false;
                });
            });

            $("a.goToBranch").bind("click", function(e)
            {
                var $cols = $(this).parents("td"),
                    officeId = $.trim($cols.siblings(":eq(1)").text()),
                    locationType = $.trim($cols.siblings(":eq(2)").text());
                _turingTestRedirect.redirect(baseUrl + "EntityDetails.aspx/" + (locationType.toUpperCase() === "BRANCH" ? "BRANCH/" : "COMPANY/") + officeId);
                return false;
            });

            $("a.sponsorLink").bind("click", function(e)
            {
                var companyId = $.trim($(this).siblings("span.sponsorshipId").text());
                _turingTestRedirect.redirect(baseUrl + "EntityDetails.aspx/Company/" + companyId);
                return false;
            });

            nmls.licenseDetailUI(baseUrl);
            nmls.entityCommonTooltips();
            $("#ttOtherNames").tooltip({ content: $("#ttOtherNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttPriorOtherNames").tooltip({ content: $("#ttPriorOtherNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttPriorLegalNames").tooltip({ content: $("#ttPriorLegalNamesText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttOfficeLocations").tooltip({ content: $("#ttOfficeLocationsText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttEngagedInOtherBusiness").tooltip({ content: $("#ttEngagedInOtherBusinessText").html(), delay: 500, offsetPosition: "vt-hc" });
            $("#ttFinancialServices").tooltip({ content: $("#ttFinancialServicesText").html(), delay: 500, offsetPosition: "vt-hc" });
            $(".ttCurrentlyAuthorizedToRepresent").tooltip({ content: $("#ttCurrentlyAuthorizedToRepresentText").html(), delay: 500, offsetPosition: "vt-hc" });
            $(".ttPreviouslyAuthorizedToRepresent").tooltip({ content: $("#ttPreviouslyAuthorizedToRepresentText").html(), delay: 500, offsetPosition: "vt-hc" });
        }

        nmls.entityDetailUI(baseUrl, this);
        this.bind();
    }

    nmls.branchUI = function(baseUrl, options)
    {
        var _turingTestRedirect = new nmls.turingTestRedirect(baseUrl, options);

        this.type = "BRANCH";
        this.turingTestRedirect = _turingTestRedirect;
        this.bind = function()
        {
            nmls.licenseDetailUI(baseUrl);
            nmls.entityCommonTooltips();
            nmls.companyBranchCommonTooltips();
            $("a.branchManager").bind("click", function(e)
            {
                var individualId = $.trim($(this).parents("td").siblings(":eq(3)").text());
                _turingTestRedirect.redirect(baseUrl + "EntityDetails.aspx/Individual/" + individualId);
                return false;
            });
        }

        nmls.entityDetailUI(baseUrl, this);
        this.bind();
    }
})(jQuery);