﻿$(document).ready(function(){
    //split single column into two columns
    //initSplitColumns();
    
    //fix footnote reference links
    $("a.ftnote").each(function(){
        if($(this).attr("href") != "#footnotes") $(this).attr("href","#footnotes");
    });
    
    
    // TOGGLE LIST
    //set display of items and More Link on a toggled list
    $(".toggle.show-5 li:nth-child(-n+5)").addClass("show");
    $(".toggle.show-6 li:nth-child(-n+6)").addClass("show");
    $(".toggle.show-10 li:nth-child(-n+10)").addClass("show");
    $(".toggle.show-15 li:nth-child(-n+15)").addClass("show");
    $(".toggle.show-5").each(function(){
        if($(this).find("li").length < 6) $(this).siblings(".morelink.show").css("display","none");
    });
    $(".show-6").each(function(){
        if($(this).find("li").length < 7) $(this).siblings(".morelink.show").css("display","none");
    });
    $(".show-10").each(function(){
        if($(this).find("li").length < 11) $(this).siblings(".morelink.show").css("display","none");
    });
    $(".show-15").each(function(){
        if($(this).find("li").length < 16) $(this).siblings(".morelink.show").css("display","none");
    });
    
    //open & close a toggled list
    $(".toggle + .morelink.show").live("click",function(){
        $(this).siblings("ul").find("li").css("opacity",1).fadeIn("slow");
        $(this).animate({opacity:0},function(){
            var altText = $(this).html();
            $(this).html($(this).attr("alt")).removeClass("show").addClass("hide").attr("alt",altText);
            $(this).animate({opacity:1});
        });
        scrollPageTop($(this).closest("*[id]"));
        return false;
    });
    
    $(".toggle + .morelink.hide").live("click",function(){
        $(this).siblings("ul").find("li").not(".show").animate({opacity:0},"slow").slideUp("fast");
        $(this).animate({opacity:0},function(){
            var altText = $(this).html();
            $(this).html($(this).attr("alt")).removeClass("hide").addClass("show").attr("alt",altText);
            $(this).animate({opacity:1});
        });
        scrollPageTop($(this).closest("*[id]"));
        return false;
    });
    
    
    //ACCORDION
    $(".accordion .drawer h4:first-child").live("click",function(){
        var currClick = $(this);
        var accordionRel = currClick.closest(".accordion").attr("rel");
        var currDrawer = currClick.closest(".drawer");
        
        if(currDrawer.attr("ajax-url") && currDrawer.find("#ajax,#ajaxData").length == 0) {
            currDrawer.find(".ajaxContent").load(currDrawer.attr("ajax-url"),function(){
                ajaxLoaded();
            });
        }
        
        currClick.closest(".accordion").find(".contents").slideUp().closest(".drawer").removeClass("open").find(".contents").filter(currClick.siblings()).slideDown("slow").closest(".drawer").addClass("open");
        if($(".filterSet[rel=" + accordionRel + "]").length > 0) {
            $(".filterSet[rel=" + accordionRel + "]").find(".listDisplay select").val("single-list");
        }
    });
    
    
    //FEATURE NAV - highlight the current/active page
    $(".featureNav li").each(function(){
        if($(this).attr("rel") == $("meta[name=page-id]").attr("content")) {
            $(this).addClass("current");
        }
    });
    
    
    //MISC CSS styles
    $(".billboard + .section, .tab > .section:first-child, .col > .section:first-child, h1.pgtitle + .section").addClass("top");


    $(".box.navbox").live("click",function(){
            window.location = $(this).attr("data-href");
        }); 


    //make box clickable with URL
    $(".box[data-url]").live("click",function(){
        window.location = $(this).attr("data-url");
    });   
    
}); //document.ready
 
 

$(window).load(function(){

    initMatchHeight();

    $("body").addClass("loaded");

}); //window.load
 

function initMatchHeight() {
    //match height of immediate child elements within a matchHeight container
    $(".matchHeight").not(".matched").each(function(){
        if($(this).closest(".tab").hasClass("on") == true || $(this).closest(".tab").length == 0) {
            var matchItems = $(this).find("> *");
            var maxHeight = 0;
            for(i = 0; i < matchItems.length; i++) {
                var currItemHeight = parseInt($(matchItems[i]).attr("offsetHeight")) - parseInt($(matchItems[i]).css("padding-top")) - parseInt($(matchItems[i]).css("padding-bottom"));
                if(currItemHeight > maxHeight) maxHeight = currItemHeight;
            }
            matchItems.css("height",maxHeight + "px");
            $(this).addClass("matched");
        }
    });
} //initMatchHeight
