// <![CDATA[
sfHover = function() {
    var sfEls;

    if (document.getElementById("top-quicklinks") != null && document.getElementById("top-quicklinks").getElementsByTagName("LI") != null)
        sfEls = document.getElementById("top-quicklinks").getElementsByTagName("LI");

    if (sfEls != null && sfEls != '' && sfEls.length != null && sfEls.length > 0) {
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function() {
                this.className += " sfHover";
            }
            sfEls[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" sfHover\\b"), "");
            }
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
// ]]>

function applyBottomShadowAndMarginToLastAccordion() {
    var allAccordions = $(".accordion");
    $(allAccordions).each(function(index) {
        if (index == allAccordions.length - 1) {
            $(this).css("margin", "1px 8px 10px 0");
            $(this).find(".modBody").css("margin-bottom", "2px");
        } else {
            $(this).css("margin", "1px 8px 0 0");
            $(this).find(".modBody").css("margin-bottom", "");
        }
    });
}
$(document).ready(function() {

    //modals with iframe use
    $("a.nyroModalClient").each(function() {
        var w = $(this).attr("nyroW") ? $(this).attr("nyroW") : null;
        var h = $(this).attr("nyroH") ? $(this).attr("nyroH") : null;

        $(this).nyroModal({
            modal:true,
            showBackground: function (elts, settings, callback) {
                elts.bg.css({opacity:0,'background-color':'#c3c3c3'}).fadeTo(500, 0.3, callback);
            },
            forceType:'iframe',
            width:w,
            height:h,
            minHeight: 100
        });

    });

    //modals without iframe use
     attachNyroModalClientNoFrame();

    //accordions
    $(".accordion").accordion({
        header: 'span.button',
        active: '.selected',
        alwaysOpen: false,
        autoheight: false,
        animated: 'easeslide'
    });
    applyBottomShadowAndMarginToLastAccordion();

    //client tabs
    $(".tab-container .tab-content").hide();
    $("ul.tabs li,ul.search-tabs li").click(function() {
        $(this).siblings("li").each(function(index) {
            $(this).removeClass("active");
            var tabContentToHide = $(this).find("a").attr("href");
            $(tabContentToHide).hide();
        });
        $(this).addClass("active");
        var activeTabContent = $(this).find("a").attr("href");
        $(activeTabContent).fadeIn();
        return false;
    });
    $("ul.tabs").each(function(){
        var activeTab = $(this).find("li.active:first");
        if(!activeTab.length){
           activeTab =  $(this).find("li:first");
        }
        activeTab.click();
    });

    //server tabs
    $("ul.server-tabs").each(function(){
        var activeTab = $(this).find("li.active:first");
        if(!activeTab.length){
           activeTab =  $(this).find("li:first");
        }
        activeTab.siblings("li").each(function(index) {
            $(this).removeClass("active");

        });
        activeTab.addClass("active");
    });


    //heros
    $('.images').each(function() {
        var $next = $('<a class="if-not-js-hide" href="#"><img class="next" src="/portal/assets/img/tabs/next.png" alt=""/></a>').insertBefore(this);
        var $prev = $('<a class="if-not-js-hide"href="#"><img class="previous" src="/portal/assets/img/tabs/previous.png" alt=""/></a>').insertBefore(this);
        var $nav = $('<div class="nav"></div>').insertBefore(this);
        $(this).cycle({
            fx: 'scrollHorz',
            speed: 300,
            timeout: 0,
            pager: $nav,
            next: $next,
            prev: $prev
        });
    });

    $('.hero-cust').each(function () {
        var $next = $('<a id="heroForwardId" class="hero12colforward"><img class="next2" src="/portal/assets/img/tabs/next.png" alt=""/></a>').insertBefore(this);
        var $prev = $('<a id="heroBackwardId" class="hero12colbackward"><img class="previous2" src="/portal/assets/img/tabs/previous.png" alt=""/></a>').insertBefore(this);
        var $nav = $('<div id="heroNavId" class="nav_hero"></div>').insertBefore(this);
        $(this).cycle({
            fx: 'fade',
            speed: 1500,
            timeoutFn : function(currSlideElement, nextSlideElement, options, forwardFlag){
                var t;
                if(nextSlideElement==1){
                   t = $($(options.$cont).find(".slide-show-hero").get(0)).attr("timeout");
                }else{
                   t = $(nextSlideElement).attr("timeout");
                }

                if(!t){
                    return 8000;
                }else{
                    return parseInt(t) * 1000;
                }
            },
            pager: $nav,
            next: $next,
            prev: $prev
        });
    });

    $('.hero-cust-7col').each(function () {
        var $next = $('<a id="7colHeroForwardId" class="hero7colforward"><img class="next2" src="/portal/assets/img/tabs/next.png" alt=""/></a>').insertBefore(this);
        var $prev = $('<a id="7colHeroBackwardId" class="hero7colbackward"><img class="previous2" src="/portal/assets/img/tabs/previous.png" alt=""/></a>').insertBefore(this);
        var $nav = $('<div id="7colHeroNavId" class="nav_hero hero7colNav"></div>').insertBefore(this);
        $(this).cycle({
            fx: 'fade',
            speed: 1500,
            timeout: 0,
            pager: $nav,
            next: $next,
            prev: $prev
        });
    });

    $('.hero-cust-7col_noHeader').each(function () {
        var $next = $('<a id="7colHeroNoHeaderForwardId" class="hero7colNoHeadForward"><img class="next2" src="/portal/assets/img/tabs/next.png" alt=""/></a>').insertBefore(this);
        var $prev = $('<a id="7colHeroNoHeaderBackwardId" class="hero7colNoHeadBackward"><img class="previous2" src="/portal/assets/img/tabs/previous.png" alt=""/></a>').insertBefore(this);
        var $nav = $('<div id="7colHeroNoHeaderNavId" class="nav_hero hero7colNoHeadNav"></div>').insertBefore(this);
        $(this).cycle({
            fx: 'fade',
            speed: 1500,
            timeout: 0,
            pager: $nav,
            next: $next,
            prev: $prev
        });
    });

    //carousels
    $('.carousel').each(function() {
        var slidesCount = $(this).find(".carousel-show-image").size();
        if (slidesCount > 1) {
            var $next = $('<a class="next if-not-js-none" href="#"><img class="next2" src="/portal/assets/img/tabs/next.png" alt=""/></a>').insertBefore(this);
            var $prev = $('<a class="previous if-not-js-none" href="#"><img class="previous2" src="/portal/assets/img/tabs/previous.png" alt=""/></a>').insertBefore(this);
            var $nav2 = $('<div class="nav2"></div>').insertBefore(this);
            $(this).cycle({
                fx: 'scrollHorz',
                speed: 300,
                timeout: 0,
                pager: $nav2,
                next: $next,
                prev: $prev
            });
        }

    });


    //bubbles
    $('.bubbleInfo').each(function() {
        var distance = 0;
        var time = 20;
        var hideDelay = 20;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var trigger = $('.trigger', this);
        var popup = $('.popup', this).css('opacity', 0);
        $([trigger.get(0), popup.get(0)]).mouseover(function() {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;
                $(".allow-overflow").css("overflow", "visible");
                popup.css({
                    top: -4,
                    left: -1,
                    display: 'block'
                })
                        .animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }
        }).mouseout(function() {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function() {
                hideDelayTimer = null;
                popup.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function() {
                    shown = false;
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
    });

    //overview
    $(".linkList-content").hide();
    $("ul.linkList li").mouseover(function() {
        $("ul.linkList li").each(function(index) {
            $(this).removeClass("active");
            var img = $(this).find("img:first");
            if (img) {
                img.attr("src", img.attr("imageInactive"));
            }
        });

        $(this).addClass("active");
        var img = $(this).find("img:first");
        if(img){
            img.attr("src", img.attr("imageActive"));
        }
        $(".linkList-content").hide();


        var activeTab = "#" + $(this).find("a").attr("rel");
        $(activeTab).stop(true, true).fadeIn();
    });
    $("ul.linkList").each(function(){
        var leftActiveTab = $(this).find("li.active:first");
        if(!leftActiveTab.length){
           leftActiveTab =  $(this).find("li:first");
        }
        leftActiveTab.mouseover();
    });
    $("ul.linkList li").click(function() {
        if ($(this).find("a").attr("href") == "#") {
            return false;
        }
    });


});

function attachNyroModalClientNoFrame() {
    $("a.nyroModalClientNoFrame").each(function() {
        var w = $(this).attr("nyroW") ? $(this).attr("nyroW") : null;
        var h = $(this).attr("nyroH") ? $(this).attr("nyroH") : null;

        $(this).nyroModal({
            modal:true,
            showBackground: function (elts, settings, callback) {
                elts.bg.css({opacity:0,'background-color':'#c3c3c3'}).fadeTo(500, 0.3, callback);
            },
            width:w,
            height:h,
            minHeight: 100
        });

    });
}


function HideDIV(d) {
    document.getElementById(d).style.display = "none";
}
function DisplayDIV(d) {
    document.getElementById(d).style.display = "block";
}


function HideHeroDIVfunc(d, element) {
    document.getElementById(d).style.display = "none";
    element.parentNode.style.height = '0';
}
function DisplayHeroDIVfunc(d, element) {
    document.getElementById(d).style.display = "block";
    element.parentNode.style.height = 'auto';

}

