﻿function OnRequestStart(target, arguments)
{
 if ((arguments.EventTarget.indexOf("Upload") > -1) || (arguments.EventTarget.indexOf("Download") > -1))
    {                    
        arguments.EnableAjax = false;
    }
}
function closeAlert() {
    var url;
    if ($('#alertUrl')) {
        url = $('#alertUrl').val();
        $('#alertUrl').remove();
    }
    if (url != undefined && url !== '' && url.indexOf("#") < 0) {
        window.location = url;
    }
    else {
        $('#alertContainer').dialog("destroy").remove();
    }
}
function ExpandProducts() {
    $('#sidenav-product-items').slideToggle('slow');
    if ($('#products-expander').text() == '-') {
        $('#products-expander').text('+');
    }
    else {
        $('#products-expander').text('-');
    }
    //check for segments
    if ($('#segments-expander').text() == '-') {
        $('#sidenav-segment-items').slideToggle('slow');
        $('#segments-expander').text('+');
    }
}
function ExpandSegments() {
    $('#sidenav-segment-items').slideToggle('slow');
    if ($('#segments-expander').text() == '-') {
        $('#segments-expander').text('+');
    }
    else {
        $('#segments-expander').text('-');
    }
    //check for products
    if ($('#products-expander').text() == '-') {
        $('#sidenav-product-items').slideToggle('slow');
        $('#products-expander').text('+');
    }
}
function ToggleSignIn() {
    $('#sign-in-form').slideToggle('slow');
}
function ToggleSearch() {
    $('#advanced-search').slideToggle('slow', function () {
        if ($('#advanced-search').is(':hidden')) {
            $('#home-link').css('top','16px');
            $('#product-search-button').css('top','0');
            $('html, body').animate({scrollTop:0},'slow');
        }
         else if ($('#advanced-search').is(':visible')) {
            $('#home-link').css('top','471px');
            $('#product-search-button').css('top','455px');
        }
    });
}
var currentSuggestiveItem = 0;
var originalValue = '';
var originalValueSet = false;
function PerformSearch(el, event) {
    if (event.keyCode != 40 && event.keyCode != 38) {
        var found = FindTerms($(el).val());
        currentSuggestiveItem = 0;
        if (found > 0) {
            if ($('#suggestive-search').is(':hidden')) {
                $('#suggestive-search').slideDown('normal');
            }
        }
        else {
            if ($('#suggestive-search').is(':visible')) {
                $('#suggestive-search').slideUp('normal');
                originalValueSet = false;
            }
        }
    }
    else {
        if ($('#suggestive-search').is(':visible')) {
            if (event.keyCode == 40) {
                currentSuggestiveItem += 1;
                $("#suggestive-search-dictionary div").each(function(i) {
                    if ($(this).attr('order') == currentSuggestiveItem) {
                        if ($(this).is(':visible')) {
                            $(this).attr('class','active');
                            if (originalValueSet == false) {
                                //first save the original search term
                                originalValue = $('#advancedSearchControl_txtSearchTerms').val();
                                originalValueSet = true;
                            }
                            $('#advancedSearchControl_txtSearchTerms').val($(this).attr('base'));
                        }
                    }
                    else {
                        $(this).removeAttr('class');
                    }
                });
            }
            if (event.keyCode == 38) {
                currentSuggestiveItem -= 1;
                if (currentSuggestiveItem < 0) {
                    currentSuggestiveItem = 0;
                }
                $("#suggestive-search-dictionary div").each(function(i) {
                    if ($(this).attr('order') == currentSuggestiveItem) {
                        if ($(this).is(':visible')) {
                            $(this).attr('class','active');
                            if (originalValueSet == false) {
                                //first save the original search term
                                originalValue = $('#advancedSearchControl_txtSearchTerms').val();
                                originalValueSet = true;
                            }
                            $('#advancedSearchControl_txtSearchTerms').val($(this).attr('base'));
                        }
                    }
                    else {
                        $(this).removeAttr('class');
                    }
                });
            }
        }
    }
}
function FindTerms(searchString) {
    var currentItem = 0;
    searchString = searchString.toLowerCase();
    var found = 0;
    if (searchString.length > 1) {
        var number = 0;
        $("#suggestive-search-dictionary div").each(function(i) {
            $(this).text($(this).attr('base'));
            var text = $(this).text();
            text = text.toLowerCase();
            var foundIndex = text.indexOf(searchString);
            if (foundIndex == -1) {
                $(this).hide();
            }
            else {
                found = 1;
                currentItem += 1;
                if (currentItem < 10) {
                    $(this).show();
                    number += 1;
                    var actualText = FormatStrongs(searchString, $(this).text());
                    $(this).replaceWith('<div order="' + number + '" onclick="javascript:LoadSuggestiveSearchTerms(this);" base="' + $(this).attr('base') + '">' + actualText + '</div>');
                }
                else {
                    $(this).hide();
                }
            }
        });
    }
    return found;
}
function FormatStrongs(searchString, actualText) {
    var startPos = actualText.toLowerCase().indexOf(searchString);
    var endPos = startPos + searchString.length;
    var additionalHits = '';
    if (actualText.slice(endPos, actualText.length).toLowerCase().indexOf(searchString) > -1) {
        additionalHits = FormatStrongs(searchString, actualText.slice(endPos, actualText.length));
    }
    else {
        additionalHits = actualText.slice(endPos, actualText.length);
    }
    actualText = actualText.slice(0,startPos) + "<strong>" + actualText.slice(startPos,endPos) + "</strong>" + additionalHits;
    return actualText;
}
function LoadSuggestiveSearchTerms(el) {
    $('#advancedSearchControl_txtSearchTerms').val($(el).attr('base'));
    $('#suggestive-search').slideUp('normal');
}

function ShowProductPanel(panelid,tabid) {
    RemoveOnClass();
    HideProductPanels();
    $('#'+panelid).show();
    $('#'+tabid).attr("class","on");
}
function ShowRelatedProductImage(id) {
    //hide all others
    $('#ctl01_relatedProductsControl_lnkRelatedProductImage1').hide();
    $('#ctl01_relatedProductsControl_lnkRelatedProductImage2').hide();
    $('#ctl01_relatedProductsControl_lnkRelatedProductImage3').hide();
    $('#ctl01_relatedProductsControl_lnkRelatedProductImage4').hide();
    $('#ctl01_relatedProductsControl_lnkRelatedProductImage5').hide();
    document.getElementById(id).style.display = "block";
}
function RemoveOnClass() {
    $('#content-sub-nav-inner').find('.on').removeAttr('class');
}
function HideProductPanels() {
    $('#overview').hide();
    $('#food-service-rewards').hide();
    $('#menu-concepts').hide();
    $('#fact-sheet').hide();
    $('#pos').hide();
    $('#video').hide();
    $('#promotions-rebates').hide();
    $('#request-samples').hide();
}
function ShowHTMLSlideshow() {
    document.getElementById("flash-slideshow").style.display = "none";
    $('#flash-slideshow').remove();
    document.getElementById("html-slideshow").style.display = "block";
}