﻿var factSheet = function()
{

    var allDetails,
        showAllDetails,
        accountFeatureHidden,
        showAllBenifits,
        showMarginLendingRates,
        showBrokerFees,
        showBrokerTrades,
        offerTerms,
        showOfferTerms,
        ratesTableTitle,
        notPopularRatesTableRow,
        showAllRatesTable,
        alsoFromProductHidden,
        showAllProducts;

    function SetupHiddenItems()
    {
        OnShowAllDetailsClick();
        OnShowAllBenifitsClick();
        OnShowOfferTermsClick();
        OnShowRatesTableClick();
        OnShowAllProductsClick();
    };

    function IsShowMode(text)
    {
        return text.indexOf("-") == 0;
    };

    function OnShowAllDetailsClick()
    {
        var toggleText = showAllDetails.text();

        if (IsShowMode(toggleText))
        {
            toggleText = toggleText.replace("- Hide", "+ Show");
            allDetails.hide();
        } else
        {
            toggleText = toggleText.replace("+ Show", "- Hide");
            allDetails.show();
        }
        showAllDetails.text(toggleText);
    };

    function OnShowAllBenifitsClick()
    {
        if (showAllBenifits.length > 0)
        {
            var toggleText = showAllBenifits.text();

            if (IsShowMode(toggleText))
            {
                toggleText = toggleText.replace("- Hide", "+ Show");
                accountFeatureHidden.hide();
            } else
            {
                toggleText = toggleText.replace("+ Show", "- Hide");
                accountFeatureHidden.show();
            }
            showAllBenifits.text(toggleText);
        }
    };

    function OnShowOfferTermsClick()
    {
        if (showOfferTerms.length > 0)
        {
            var toggleText = showOfferTerms.text();

            if (IsShowMode(toggleText))
            {
                toggleText = toggleText.replace("- Hide", "+ Show");
                offerTerms.hide();
            } else
            {
                toggleText = toggleText.replace("+ Show", "- Hide");
                offerTerms.show();
            }
            showOfferTerms.text(toggleText);
        }
    };

    function OnShowRatesTableClick()
    {
        if (showAllRatesTable.length > 0)
        {
            var toggleText = showAllRatesTable.text();

            if (IsShowMode(toggleText))
            {
                toggleText = toggleText.replace("- Hide", "+ Show");
                ratesTableTitle.text(ratesTableTitle.text().replace("All ", "Popular "));
                notPopularRatesTableRow.hide();
            } else
            {
                toggleText = toggleText.replace("+ Show", "- Hide");
                ratesTableTitle.text(ratesTableTitle.text().replace("Popular ", "All "));
                notPopularRatesTableRow.show();
            }
            showAllRatesTable.text(toggleText);
        }
    };

    function OnShowAllProductsClick()
    {
        var toggleText = showAllProducts.text();

        if (IsShowMode(toggleText))
        {
            toggleText = toggleText.replace("- Hide", "+ Show");
            alsoFromProductHidden.hide();
        } else
        {
            toggleText = toggleText.replace("+ Show", "- Hide");
            alsoFromProductHidden.show();
        }
        showAllProducts.text(toggleText);
    }

    function OnShowTableClick()
    {
        //        debugger;
        var o = jQuery(this);

        var id = o.attr('targetId');

        if (o.hasClass('on'))
        {
            o.removeClass("on");
            jQuery('#' + id).hide();
        } else
        {
            o.addClass("on");
            jQuery('#' + id).show();
        }
    }

    return {
        Init: function()
        {
            allDetails = jQuery(".allDetails");
            showAllDetails = jQuery(".showAllDetails").click(OnShowAllDetailsClick);
            showMarginLendingRates = jQuery(".productDetails .marginLending .rates h3").click(OnShowTableClick);
            showBrokerFees = jQuery(".productDetails .broker .fees h3").click(OnShowTableClick);
            showBrokerTrades = jQuery(".productDetails .broker .trades h3").click(OnShowTableClick);
            accountFeatureHidden = jQuery(".accountFeatureHidden");
            showAllBenifits = jQuery(".showAllBenifits").click(OnShowAllBenifitsClick);
            offerTerms = jQuery(".offerTermsPanel");
            showOfferTerms = jQuery(".showOfferTerms").click(OnShowOfferTermsClick);
            ratesTableTitle = jQuery(".ratesTableTitle");
            notPopularRatesTableRow = jQuery(".ratesTable .notPopular");
            showAllRatesTable = jQuery(".showAllRatesTable").click(OnShowRatesTableClick);
            alsoFromProductHidden = jQuery(".alsoFromProductHidden");
            showAllProducts = jQuery(".showAllProducts").click(OnShowAllProductsClick);

            SetupHiddenItems();
        }
    }
} ();

jQuery(document).ready(function() {
    factSheet.Init();
});
