﻿/*!
* BIS Core Javascript and JQuery functions and page ready execution.
* 
* !!!PLEASE NOTE!!! 
*
* All Jquery functions should use JQuery. etc instead of $ as
* using $ clashes with Sitecore functionality such as forms wizard
* and page preview.
*/

jQuery(document).ready(function() {
    ApplyDesignToButtons();
    SetWebFormForMarketersFocus();
    SetupSearchArea();
    externalLinks();
    sitefunctions.textresize();
    expandPullquotes();
});

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 (jQuery.cookie(cookie_name)) {
            var selectedScale = parseFloat(jQuery.cookie(cookie_name), 10);

            if (selectedScale > fontScaleOptions.length || selectedScale < 0) {
                selectedScale = defaultFontScale;
            }

            jQuery("#container").css({ fontSize: fontScaleOptions[selectedScale] });
            toggleOptions(selectedScale);

            //Now resize dynamic content areas
            resizeDynamicContentAreasForNewFontSize();
        }
        else {
            jQuery.cookie(cookie_name, defaultFontScale);
            toggleOptions(defaultFontScale);
        }

        // text "A" link
        jQuery("#headerLinks .increase a").bind("click", function() {
            var selectedScale = parseFloat(jQuery.cookie(cookie_name), 10);
            var newFontScale = selectedScale + 1;
            if (newFontScale < fontScaleOptions.length) {
                var newFontSize = fontScaleOptions[newFontScale];
                jQuery("#container").css("font-size", newFontSize);
                jQuery.cookie(cookie_name, newFontScale);

                toggleOptions(newFontScale);
                showFontScaleText(newFontScale);

                //Now resize dynamic content areas
                resizeDynamicContentAreasForNewFontSize();
            }
            return false;
        });

        // text "a" link
        jQuery("#headerLinks .decrease a").bind("click", function() {
            var selectedScale = parseFloat(jQuery.cookie(cookie_name), 10);
            var newFontScale = selectedScale - 1;
            if (newFontScale >= 0) {
                var newFontSize = fontScaleOptions[newFontScale];
                jQuery("#container").css("font-size", newFontSize);
                jQuery.cookie(cookie_name, newFontScale);
                toggleOptions(newFontScale);
                showFontScaleText(newFontScale);

                //Now resize dynamic content areas
                resizeDynamicContentAreasForNewFontSize();
            }
            return false;
        });

        function toggleOptions(selectedIndex) {
            jQuery("#headerLinks .increase a").show();
            jQuery("#headerLinks .increase .disabled").hide();
            jQuery("#headerLinks .decrease a").show();
            jQuery("#headerLinks .decrease .disabled").hide();

            if (selectedIndex == (fontScaleOptions.length - 1)) { //hide option
                jQuery("#headerLinks .increase a").hide();
                jQuery("#headerLinks .increase .disabled").show();
            }
            else if (selectedIndex == 0) {
                jQuery("#headerLinks .decrease a").hide();
                jQuery("#headerLinks .decrease .disabled").show();
            }
        }

        function showFontScaleText(selectedIndex) {
            var text = fontScaleOptionsText[selectedIndex];
            jQuery("#headerLinks .resizeinfo").text(text);
            jQuery("#headerLinks .resizeinfo").hide();
            jQuery("#headerLinks .resizeinfo").fadeIn(1000);
            jQuery("#headerLinks .resizeinfo").fadeOut(1000);
        }

        //Place all functions in this method that need to be 
        //triggered if font resize options are used
        function resizeDynamicContentAreasForNewFontSize() {

            //If feature control exists the resize
            if (jQuery('#rotationControl').length != 0) {
                resizeFeatureRotationArea();
            }

        }
    }
}

function ApplyDesignToButtons() {
    jQuery("#mainColumn :submit").wrap("<span class=\"button\"></span>");
    jQuery("#mainColumn :button").wrap("<span class=\"button\"></span>");
    jQuery("#rightColumn :submit").wrap("<span class=\"button\"></span>");
    jQuery("#rightColumn :button").wrap("<span class=\"button\"></span>");
}

function SetWebFormForMarketersFocus() {
    var formContainers = jQuery(".scfForm");
    if (formContainers.length > 0) {
        formContainers.each(function() {
            var isNotSuccessfulSubmission = jQuery(this).find(".scfTitleBorder").length;

            if (isNotSuccessfulSubmission) {
                var validationContainer = jQuery(this).find(".scfValidationSummary:visible");
                var isFailedSubmit = validationContainer.length;

                if (isFailedSubmit) {
                    validationContainer.prepend("<a id=\"check\" name=\"check\"> </a>");
                    jQuery.scrollTo(jQuery("#check"), 800);
                }
            }
            else {
                jQuery(this).prepend("<a id=\"success\" name=\"success\"> </a>");
                jQuery.scrollTo(jQuery("#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 clearDefaultContent(el, val) {

    if (el.value == val.toString()) {
        el.value = "";
    }

}

function SetupSearchArea() {
    var searchButton = jQuery('#searchSubmit');
    var searchTextbox = jQuery('#keywords');
    var searchResultsTextbox = jQuery('#searhResultKeywords');

    /* Click function to select text */
    searchTextbox.click(function() { selectAllText(jQuery(this)) });
    /* Note that clearDefaultContent above is also called directly from textbox */

    /* Disables search button on load */
    /* Removed as part of UAT BIS-25
    searchButton.attr("disabled", true);
    */

    /* Handles disabling and enabling of search button on text entered */
    /* Removed as part of UAT BIS-25
    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 */
    /* Removed as part of UAT BIS-25

    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 */
    jQuery(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 */
    jQuery(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();
}

function alterShareThisForAccessibility() {
    jQuery(".stbuttontext").append("<span class=\"hiddenAccessible\">, will open in a new window.</span>");
}

//Fixes the size of the feature rotation text and button areas
function resizeFeatureRotationArea() {

    var isSingleFeatureItem = true;
    
    if (jQuery('#rotationControl .ui-tabs-panel').size() > 0 )
    {
        isSingleFeatureItem = false;
    }
    
    if(isSingleFeatureItem == false)
    {    
        resizeFeatureRotationButtons();

        jQuery('#rotationControl .ui-tabs-panel').each(function() {
            var infoHeight = 0;
            var infoImageHeight = 0;

            if (jQuery(this).hasClass("ui-tabs-hide")) {
                jQuery(this).removeClass("ui-tabs-hide");
                infoHeight = jQuery(this).find(".info").height();
                infoImageHeight = jQuery(this).find(".image").height();
                jQuery(this).addClass("ui-tabs-hide");
            }
            else {
                infoHeight = jQuery(this).find(".info").height();
                infoImageHeight = jQuery(this).find(".image").height();
            }

            var infoTopOffset = -504 - (infoHeight - 252);

            if (infoHeight > infoImageHeight) {
                jQuery(this).height(infoHeight);
                jQuery(this).find(".infoBackground").height(infoHeight);
                jQuery(this).find(".info").css('top', infoTopOffset);
            }
            else {
                jQuery(this).height(infoImageHeight);
            }
        });
    }

    //Resizes current visible feature
    var heightInfo = jQuery('#rotationControl .info:visible').height();
    var heightImage = jQuery('#rotationControl .image:visible').height();
    var buttonsHeight = jQuery('#rotationControl .ui-tabs-nav').height();
    
    if (heightInfo > heightImage) {
        jQuery("#rotationControl").height(heightInfo + buttonsHeight);
        //Resize feature 1 if only one item
        if (isSingleFeatureItem) { jQuery("#rotationControl #feature-1").height(heightInfo + buttonsHeight); }
    }
    else {
        jQuery("#rotationControl").height(heightImage + buttonsHeight);
        //Resize feature 1 if only one item
        if (isSingleFeatureItem) { jQuery("#rotationControl #feature-1").height(heightImage + buttonsHeight); }
    }
}

function resizeFeatureRotationButtons() {
    var height = 39;
    jQuery("#rotationControl .ui-tabs-nav li a span").each(function() {
        if (jQuery(this).height() > height) {
            height = jQuery(this).height();
        }
    });

    jQuery("#rotationControl .ui-tabs-nav li a").each(function() {
        jQuery(this).height(height);
    });
}

function resizeFeatureRotationAreaOnRotate() {
    var heightInfo = jQuery('#rotationControl .info:visible').height();
    var heightImage = jQuery('#rotationControl .image:visible').height();
    var infoTopOffset = -504 - (heightInfo - 252);
    var buttonsHeight = jQuery('#rotationControl .ui-tabs-nav').height();

    if (heightInfo > heightImage) {
        jQuery('#rotationControl .ui-tabs-panel:visible').height(heightInfo);
        jQuery('#rotationControl .infoBackground:visible').height(heightInfo);
        jQuery('#rotationControl .info:visible').css('top', infoTopOffset);
        jQuery("#rotationControl").height(heightInfo + buttonsHeight);
    }
    else {
        jQuery('#rotationControl .ui-tabs-panel:visible').height(heightImage);
        jQuery("#rotationControl").height(heightImage + buttonsHeight);
    }
}

function expandPullquotes() {
    // <strong> pullquotes (deprecated)
    jQuery('strong.blockquote').each(function(index) {

        var $parentParagraph = jQuery(this).parent('p');
         if ($parentParagraph.length == 0)
            $parentParagraph = jQuery(this).parent('div');
            
       $parentParagraph.before(
            jQuery('<div />').addClass('pulledquote')
                .append(jQuery('<div />').addClass('start'))
                .append(jQuery('<p />').append(jQuery(this).html()))
                .append(jQuery('<div />').addClass('end'))
        );

        // Remove original <strong> wrapper
        jQuery(this).replaceWith(jQuery(this).html());
    });
    
    // <span> pullquotes
    jQuery('span.pullquote').each(function(index) {

        var $parentParagraph = jQuery(this).parent('p');
        if ($parentParagraph.length == 0)
            $parentParagraph = jQuery(this).parent('div');
            
        $parentParagraph.before(
            jQuery('<div />').addClass('pulledquote')
                .append(jQuery('<div />').addClass('start'))
                .append(jQuery('<p />').append(jQuery(this).html()))
                .append(jQuery('<div />').addClass('end'))
        );
    });
    
    // <p> pullquotes
    jQuery('p.pullquote').each(function(index) {

        jQuery(this).before(
            jQuery('<div />').addClass('pulledquote')
                .append(jQuery('<div />').addClass('start'))
                .append(jQuery('<p />').append(jQuery(this).html()))
                .append(jQuery('<div />').addClass('end'))
        );
    });

}
