﻿ ///<reference path="../jquery-1.4.1-vsdoc.js" />
 ///<reference path="nmls.js" />
 ///<reference path="nmls.turingTest.js" />
 ///<reference path="nlms.branchLocations.js" />

(function ($)
{
    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" });
        $("#ttFederalRegulator").tooltip({ content: $("#ttFederalRegulatorText").html(), delay: 500, offsetPosition: "vt-hc" });
        $("#ttFederalRegistrationStatus").tooltip({ content: $("#ttFederalRegistrationStatusText").html(), delay: 500, offsetPosition: "vt-hc" });
        $("#ttFederalAuthorizedToConductBusiness").tooltip({ content: $("#ttFederalAuthorizedToConductBusinessText").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"),
            $viewRegistrationDetail = $("#viewRegistrationDetail");

        $viewRegistrationDetail.addClass("viewDetails");

        function _showViewDetails($viewDetails, $cssShow, $cssHide)
        {
            $viewDetails.removeClass($cssHide);
            $viewDetails.addClass($cssShow);
            $viewDetails.text("View Details");
        }

        function _hideViewDetails($viewDetails, $cssShow, $cssHide)
        {
            $viewDetails.removeClass($cssShow);
            $viewDetails.addClass($cssHide);
            $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), "viewDetails", "hideDetails");
                }
                else
                {
                    $licenseDetails.hideOrFadeOut();
                    _showViewDetails($(this), "viewDetails", "hideDetails");
                }

                return false;
            });
        });

        $viewRegistrationDetail.bind("click", function (e)
        {
            var $registrationDetails = $("tr.viewRegistrationDetails");
            if ($registrationDetails.is(":visible"))
            {
                $registrationDetails.hideOrFadeOut();
                _showViewDetails($(this), "viewDetails", "hideDetails");
            }
            else
            {
                $registrationDetails.showOrFadeIn();
                _hideViewDetails($(this), "viewDetails", "hideDetails");
            }

            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), "viewDetails", "hideDetails");
                });
            }
            else
            {
                $licenseDetails.hideOrFadeOut();
                $viewAllDetBtn.attr("src", imgPlus);
                $viewDetails.each(function (i)
                {
                    _showViewDetails($(this), "viewDetails", "hideDetails");
                });
            }

            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();
        }

        _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" });
        $("#ttAuthorizedToConductBusinessBranchLocations").tooltip({ content: $("#ttAuthorizedToConductBusinessBranchLocationsText").html(), delay: 500, offsetPosition: "vt-hc" });

        $("a.residentAgentPopup").bind("click", function (e)
        {
            _openResidentAgent(
            {
                width: 660,
                height: 440,
                content: $(this).prev().html()
            });

            return false;
        });
    }

    nmls.individualUI = function (baseUrl, options)
    {
        var _turingTestRedirect = new nmls.turingTestRedirect(baseUrl, options.turingTestUIOptions),
            $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[name='viewFederalPrevAuth']").each(function (i)
        {
            var id = $(this).attr("id"),
                licenseId = id.replace("viewFederalPrevAuth_", "");
            $(this).bind("click", function (e)
            {
                var $prevAuthRow = $(".viewFederalPrevAuthRow_" + 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" });
        $("#ttAuthorizedToRepresent").tooltip({ content: $("#ttAuthorizedToRepresentText").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" });
        $(".ttFederalRegistrationStatus").tooltip({ content: $("#ttFederalRegistrationStatusText").html(), delay: 500, offsetPosition: "vt-hc" });
        $(".ttFederalCurrentlyAuthorizedToRepresent").tooltip({ content: $("#ttFederalCurrentlyAuthorizedToRepresentText").html(), delay: 500, offsetPosition: "vt-hc" });
        $(".ttFederalPreviouslyAuthorizedToRepresent").tooltip({ content: $("#ttFederalPreviouslyAuthorizedToRepresentText").html(), delay: 500, offsetPosition: "vt-hc" });
    }

    nmls.branchUI = function (baseUrl, options)
    {
        var _turingTestRedirect = new nmls.turingTestRedirect(baseUrl, options);

        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;
        });
    }
})(jQuery);
