$(document).ready(function() {
    ApplyDesignToButtons();
    SetWebFormForMarketersFocus();
    SetupSearchArea();
    externalLinks();
});

var sitefunctions = {
    textresize: function() {
        var fontScaleOptions = ["0.55em", "0.65em", "0.75em", "0.9em", "1.05em"];
        var fontScaleOptionsText = ["Smallest", "Smaller", "Normal", "Larger", "Largest"];
        var defaultFontScale = 2;
        var $cookie_name = "BIScookie-fontscale";

        // if exists load saved value, otherwise store it
        if ($.cookie($cookie_name)) {
            var selectedScale = parseFloat($.cookie($cookie_name), 10);

            if (selectedScale > fontScaleOptions.length || selectedScale < 0) {
                selectedScale = defaultFontScale;
            }

            $("#container").css({ fontSize: fontScaleOptions[selectedScale] });
            toggleOptions(selectedScale);
        }
        else {
            $.cookie($cookie_name, defaultFontScale);
            toggleOptions(defaultFontScale);
        }

        // text "A" link
        $("#headerLinks .increase a").bind("click", function() {
            var selectedScale = parseFloat($.cookie($cookie_name), 10);
            var newFontScale = selectedScale + 1;
            if (newFontScale < fontScaleOptions.length) {
                var newFontSize = fontScaleOptions[newFontScale];
                $("#container").css("font-size", newFontSize);
                $.cookie($cookie_name, newFontScale);

                toggleOptions(newFontScale);
                showFontScaleText(newFontScale);
            }
            return false;
        });

        // text "a" link
        $("#headerLinks .decrease a").bind("click", function() {
            var selectedScale = parseFloat($.cookie($cookie_name), 10);
            var newFontScale = selectedScale - 1;
            if (newFontScale >= 0) {
                var newFontSize = fontScaleOptions[newFontScale];
                $("#container").css("font-size", newFontSize);
                $.cookie($cookie_name, newFontScale);
                toggleOptions(newFontScale);
                showFontScaleText(newFontScale);
            }
            return false;
        });

        function toggleOptions(selectedIndex) {
            $("#headerLinks .increase a").show();
            $("#headerLinks .increase .disabled").hide();
            $("#headerLinks .decrease a").show();
            $("#headerLinks .decrease .disabled").hide();

            if (selectedIndex == (fontScaleOptions.length - 1)) { //hide option
                $("#headerLinks .increase a").hide();
                $("#headerLinks .increase .disabled").show();
            }
            else if (selectedIndex == 0) {
                $("#headerLinks .decrease a").hide();
                $("#headerLinks .decrease .disabled").show();
            }
        }

        function showFontScaleText(selectedIndex) {
            var text = fontScaleOptionsText[selectedIndex];
            $("#headerLinks .resizeinfo").text(text);
            $("#headerLinks .resizeinfo").hide();
            $("#headerLinks .resizeinfo").fadeIn(1000);
            $("#headerLinks .resizeinfo").fadeOut(1000);
        }
    }
}

function ApplyDesignToButtons() {
    $("#mainColumn :submit").wrap("<span class=\"button\"></span>");
    $("#mainColumn :button").wrap("<span class=\"button\"></span>");
    $("#rightColumn :submit").wrap("<span class=\"button\"></span>");
    $("#rightColumn :button").wrap("<span class=\"button\"></span>");   
}

function SetWebFormForMarketersFocus() {
    var $formContainers = $(".scfForm");
    if ($formContainers.length > 0) {
        $formContainers.each(function() {
            var isNotSuccessfulSubmission = $(this).find(".scfTitleBorder").length;

            if (isNotSuccessfulSubmission) {
                var $validationContainer = $(this).find(".scfValidationSummary:visible");
                var isFailedSubmit = $validationContainer.length;

                if (isFailedSubmit) {
                    $validationContainer.prepend("<a id=\"check\" name=\"check\"> </a>");
                    $.scrollTo($("#check"), 800);
                }
            }
            else {
                $(this).prepend("<a id=\"success\" name=\"success\"> </a>");
                $.scrollTo($("#success"), 800);
            }
        });
    }
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}

function SetupSearchArea() {
    var searchButton = $('#searchSubmit');
    var searchTextbox = $('#keywords');
    var searchResultsTextbox = $('#searhResultKeywords');

    /* Click function to select text */
    searchTextbox.click(function() { selectAllText(jQuery(this)) });

    /* Disables search button on load */
    searchButton.attr("disabled", true);

    /* Handles disabling and enabling of search button on text entered */
    searchTextbox.keyup(function(e) {
        var text = searchTextbox.val();
        if (text == '') {
            searchButton.attr("disabled", true);
        }
        else {
            searchButton.attr("disabled", false);
        }
    });

    /* Handles disabling and enabling of search button on text entered in search result box */
    searchResultsTextbox.keyup(function(e) {
        var text = searchTextbox.val();
        if (text == '') {
            searchButton.attr("disabled", true);
        }
        else {
            searchButton.attr("disabled", false);
        }
    });

    /* Sync search boxes */
    /** START - Update keyword boxes **/
    searchTextbox.keyup(function(e) {
        searchResultsTextbox.val(searchTextbox.val());
    });
    searchResultsTextbox.keyup(function(e) {
        searchTextbox.val(searchResultsTextbox.val());
    });
    /** END - Update keyword boxes **/

    /* submits form in IE */
    $(function() {
    searchTextbox.keydown(function(e) {
            if (e.keyCode == 13) {
                var btnSubmit = document.getElementById("searchSubmit");
                btnSubmit.click();
                return false;
            }
        });
    });

    /* submits form in IE on search page */
    $(function() {
    searchResultsTextbox.keydown(function(e) {
            if (e.keyCode == 13) {
                var btnSubmit = document.getElementById("searchAgain");
                btnSubmit.click();
                return false;
            }
        });
    });
}

function selectAllText(textbox) {
    textbox.focus();
    textbox.select();
}
