﻿$(document).ready(function() {
    //close menus on body click
    $("body").click(function(e) {
        if ($(e.target).closest("ul.top-nav").length === 0) {
            $("ul.top-nav").find("div").hide().end().find(".active").removeClass("active");
        }
    });
    //TOP NAV
    $(".top-nav>li>a").click(function(e) {
        if ($(this).attr("id") != "homeLink") {
            e.preventDefault();

            var clicked = $(this);

            if (clicked.next().is(":visible")) {
                clicked.removeClass("active").next().hide();
            } else {
                clicked.closest("ul.top-nav").find("div").hide().end().find(".active").removeClass("active");
                clicked.addClass("active").next().show();
            }
        }
        else {
            //Siva-- Hack to fix homepage link on single column master page. To be fixed after Wayne is back from holiday
            location.href = "/";
        }
    });

    //RIAS & YOU BANNER
    initBanner();
    $('.articles-banner h2').hover(function() {
        onHover($(this));
    }, function() {
        offHover($(this));
    });
    $('.articles-banner h2').click(function() {
        onClick($(this));
    });
    //IE SPECIFICITY & CUFON SETUP
    var b = $.browser.msie,
        w = $.browser.webkit,
        m = $.browser.mozilla,
        v = parseInt($.browser.version, 10);
        
    if (b) {
        Cufon.replace('.body-left h3, .panel.w220 h4, .panel.w376 h1, .panel.w246 h2, .panel.w142 h3, .panel.w774.blue h2, .page-title, .panel.w445.blue h1, .full-width .acc-title, .acc-middle-panel-form .acc-title, .side-panel-form-title h4, .admin h2');
        $('body').addClass('ie' + v);
        $('.nav .active-link').parent('li').next('li').addClass('active-link-li-next');
    } else if (w) {
        $('body').addClass('webkit');
    } else if (m) {
        $('body').addClass('mozilla');
    }    
    
    //hide Google+ text in IE7
    if ($("#plusText").length && $.browser.msie && $.browser.version === "7.0") {
        $("#plusText").remove();
    }
});

//Panel height setter
function setPanelHeights(parent, panel) {
    var tHeight = 0;
    $(panel).each(function () {
        var iheight = $(this).height();
        if (iheight > tHeight)
            tHeight = iheight;
    });
    $(panel).css('height', tHeight);
}
//Remove margin from listed panels etc
function removeMargin(panel, n) {
    $('div'+panel+':nth-child('+n+'n+'+n+')').css('margin-right','0');
}

//Article banner functionality
var c = 0;
var interval;
var restart;
var backToSelected;
function initBanner() {
    $('.articles-banner h2:eq(0)').addClass('highlighted');
    $('.articles-banner>div:eq(0)').addClass('highlighted').css('display','block');
    $('.articles-banner h2:eq(1)').addClass('after');
    startRotation();
}
function rotate() {
    if(c > 3)
        c = 0;
    else
        c++;
    onRotate($('.articles-banner h2:eq('+c+')'));
}
function startRotation() {
    c = -1;
    interval = window.setInterval("rotate()", 7000);
}
function onHover(e) {
    if($(e).is('.selected'))
        $('.selected').removeClass('off');
    else {
        $(e).siblings('.selected').addClass('off');
        $(e).addClass('highlighted').siblings('h2').removeClass('highlighted');
        $('.articles-banner h2').removeClass('after');
        $('div.off').stop(true, true).fadeOut(30);
        $(e).next('div').addClass('highlighted').stop(true, true).fadeIn(30).next('h2').addClass('after');
    }
    c = 0;
    window.clearTimeout(restart);
    window.clearTimeout(backToSelected);
    window.clearInterval(interval);
}
function offHover(e) {
    $(e).removeClass('highlighted');
    $('.articles-banner>div:not(".selected")').stop(true, true).fadeOut(30)
    $('.articles-banner>div:not(".selected")').removeClass('highlighted');
    $(e).siblings('h2').removeClass('after');
    $('div.off').stop(true, true).fadeIn(30);
    $('.off').removeClass('off');
    $('.selected').next('div').next('h2').addClass('after');
    restart = window.setTimeout("startRotation()", 15000);
}
function onClick(e) {
    $(e).siblings('.selected').removeClass('selected').removeClass('off').siblings().removeClass('after');
    $(e).addClass('selected')
    $(e).next('div').removeClass('highlighted').addClass('selected').fadeIn(300).next('h2').addClass('after');
}
function onRotate(e) {
    $(e).siblings().removeClass('selected').removeClass('off').removeClass('after').removeClass('highlighted');
    $(e).siblings('div').fadeOut(300);
    $(e).addClass('selected')
    $(e).next('div').addClass('selected').fadeIn(300).next('h2').addClass('after');
}
