﻿var CarouselItemHeight = 50;
var navTOID = 0;
$(document).ready(function(){
	
	$(".person-box p").after('<div class="c-tl"></div><div class="c-tr"></div><div class="c-bl"></div><div class="c-br"></div>');
	
    $('iframe').each(function() {
        if ($(this).attr("src").indexOf("youtube.com") == -1)
            return false;
        var fixed_src = $(this).attr('src') + '?wmode=transparent';
        $(this).attr('src', fixed_src);
    });
	
    //Add 'read more' links
    $(".shrink-para p:first").nextAll().wrapAll('<div class="shrunk"></div>');
    $(".shrunk").hide().after('<div class="read-more"><a href="#">Read more</a></div>');
    $(".read-more a").click(function(e){
        e.preventDefault();
        if ($(this).html().indexOf("more")>0) {
            $(this).parent().prev().slideDown("normal",function(){
                $(this).next().find("a").html($(this).next().find("a").html().replace('more','less'));
            });
        } else {
            $(this).parent().prev().slideUp("normal",function(){
                $(this).next().find("a").html($(this).next().find("a").html().replace('less','more'));
            });
        }
    });
    
    //Add accordion style fuctionality
    $(".shrink-head h2").each(function(){
        $(this).prepend('<span class="closed">+ </span>').wrapInner('<a href="#"></a>').addClass("bordered rounded").nextUntil("h2").wrapAll('<div class="shrunk"></div>');
    });
    $(".shrink-head h2 a").click(function(e){
        e.preventDefault();
        $(this).parent().next().slideToggle("normal");
        if ($(this).children('span').hasClass("closed")) {
            $(this).children('span').removeClass("closed").html("&ndash; ");
        } else {
            $(this).children('span').addClass("closed").html("+ ");
        }
    });
    $(".shrunk").hide();
        
    //Mega nav
    $(".navdd>ul").each(function(){
        $(this).children("li").eq(4).after('<li class="clear-min">&nbsp;</li>');
    });
    $(".navdd").each(function(){
        parentLeft = $(this).parent().position().left;
        ddLeft = 0;
        ddLeft = (parentLeft-300 < 0) ? 0-parentLeft-5 : (ddLeft-300);
        ddLeft = (parentLeft + 424 < 945) ? ddLeft : (945 - 724 - parentLeft) + 3 ;
        $(this).css("left",ddLeft);
    });
    
    $("li:has(.navdd)").hover(function(){
        window.clearTimeout(navTOID);
        navTOID = window.setTimeout("showMegaNav('" + this.id + "')",500);
        $(this).siblings().trigger("mouseleave",true);
    },function(e,ignoreTimeout){
        if (!ignoreTimeout)
            window.clearTimeout(navTOID);
        $(this).removeClass("hover").css("z-index","1").find(".navdd").slideUp(500);
    });
    
    $(".bannerTabs li:last").addClass("last");
    
    //Job carousel
    if ($(".job-feed-spotlight #JobFeed li").size() > 3)
    {
        $(".carousel-outer").css("overflow","visible");
        $(".job-feed-spotlight #JobFeed li").each(function(){
            if ($(this).height() > CarouselItemHeight)
                CarouselItemHeight = $(this).height();
        }).css("height",CarouselItemHeight);
        $(".job-feed-spotlight #JobFeed li a").each(function(){
            if (CarouselItemHeight - $(this).height() > 2) {
                paddingVal = Math.floor((CarouselItemHeight - $(this).height())/2);
                $(this).css("padding-top", paddingVal).css("padding-bottom", paddingVal);
            }
        });
        $(".carousel-outer").addClass("carousel-act").css("border-bottom","none")
            .before('<a href="#" class="prev"><span class="hide">Previous</span></a>')
            .after('<a href="#" class="next"><span class="hide">Next</span></a>')
            .jCarouselLite({
                btnNext: ".next",
                btnPrev: ".prev",
                vertical: true,
                visible: 3
            }).css("height","189px");
    } else {
        $(".carousel-outer").css("height","auto").css("overflow","visible").append('<div class="c-bl"></div><div class="c-br"></div>');
    }
    
    //show carousel nav
    $(".scrollLeft, .scrollRight").css("display","block");
    
    //Setup group home carousel
    $("#hero-carousel").css("overflow","visible");
    if ($('.hero-item').size() > 1)
    {
        $("#heroControls").css("right","0px");
        $('.hero-item').wrapAll('<div id="hero-items"></div>');
        //var pagerHTML = '<div id="heroControls" class="rounded scroll-control"><a href="#" id="play-pause" class="pause"></a><span class="pgr">';
        for (i = 0; i<$(".hero-item").size(); i++)
        {
            //pagerHTML += '<a href="#" title="' + (i+1) + '"><span class="hide">' + (i+1) + '</span></a>';
            $("#heroControls .pgr").append('<a href="#" title="' + (i+1) + '"><span class="hide">' + (i+1) + '</span></a>');
        }
        //pagerHTML += '</span></div>';
        //$('#hero-carousel').append(pagerHTML);
        $('#hero-items').cycle({
            fx: 'scrollHorz', 
            timeout: 7000,
            pager: "#heroControls .pgr",
            pagerAnchorBuilder: function(idx, slide){ return '#heroControls .pgr a:eq(' + (idx) + ')'; }
        });
        $("#play-pause").click(function(e){
            e.preventDefault();
            $('#hero-items').cycle('toggle');
            $("#play-pause").toggleClass("play pause");
        });
    }
    
    $(document).AmazeAutoTag();
});

$(window).load(function(){
    $(".tabHeading>a").each(function(){ 
        if ($(this).height() > 30){
            $(this).closest("li").find(".tabHeading").css("padding-top","12px");
        }
    });
});

function showMegaNav(elId){
    $("#" + elId).stop(true,true).addClass("hover").css("z-index","100").find(".navdd").slideDown("normal");
}

function hideMegaNav(elId){
    $("#" + elId).removeClass("hover").css("z-index","1").find(".navdd").stop(true,true).slideUp("fast");
}
