// ======================
//    Dropdown Menu
// ======================
function cnb_menu_dropdown(){
    $(" #main_navigation ul ").css({display: "none"}); // Opera Fix
    $(" #main_navigation li").hover(function(){
        $(this).addClass('active_hover');
        $(this).find('ul:first:hidden').css({
            visibility: "visible",
            display: "none"
        }).fadeIn("fast");
    }, function(){
        $(this).removeClass('active_hover');
        $(this).find('ul:first').css({
            visibility: "hidden", 
            display: "none"
        });
    });

    // Check how many list items in 1st level nav and align 2nd level nav accordingly
    $("#compareplans_but ul.dropdown_level_1").find("> li:gt(2)").each(function() {
        $(this).find("ul.dropdown_level_2").addClass('align_mid');                       
    });
     
     // Check Carousel
     $('.carousel').each(function() {
        // If there are more than 5 items make dropdown into scrollable carousel
        var i = $(this).find('div.item').length;
        if(i > 5) {
            $(this).addClass('scrollable'); 
        }
        else {
            $(this).parent().addClass('w'+i);
        }
    });
    
    // Create Scrollable Carousel
    $(".scrollable").scrollable({ 
        item: ".carousel div.item",
        size: 4,
        next: ".carousel .scroller_next a",
        prev: ".carousel .scroller_previous a"
    });
}


// ======================
//    Glossary Tooltip
// ======================
function cnb_glossary_tooltip(){
    $(".glossary").tooltip({
        delay: 0,
        showURL: false,
        showBody: " - ",
        fixPNG: true,
        opacity: 0.95,
        fade: 50,
        top: -25, 
        left: 20
    });
    $(".download").each(function() {
        var link_desc = $(this).attr('title');
        $(this).tooltip({
            delay: 0,
            showURL: false,
            fixPNG: true,
            bodyHandler: function() {
                var link_title = $(this).text();
                var link_size = $(this).attr('rel');
                return '<h3>' + link_title + '&nbsp;<span class="filesize">(' + link_size + ')</span></h3><div class="body">' + link_desc + '</div>';
            },
            opacity: 0.95,
            fade: 50,
            top: -25, 
            left: 20
        });
    });
}


// ======================
//    Text Resize
// ======================

function cnb_print_resize(){
    //js is working, so show text resize buttons
    $("li.increase_text_but, li.decrease_text_but, li.print_but").show();
        
    //Decrease Text size
    $('li.decrease_text_but').click(function () { 
        if($('body').hasClass("font-medium")){
            $("body").removeClass("font-medium").addClass("font-small");
        }
        if($('body').hasClass("font-large")){
            $("body").removeClass("font-large").addClass("font-medium");
        }
        else{}
            
    });
    
    // Add Hover Class
    $('li.decrease_text_but').hover(function() {
        $(this).addClass('decreasetext-hover');
      }, function() {
        $(this).removeClass('decreasetext-hover');
    });


    //Increase Text size
    $('li.increase_text_but').click(function () { 
        if($('body').hasClass("font-medium")){
            $("body").removeClass("font-medium").addClass("font-large");
        }
        if($('body').hasClass("font-small")){
            $("body").removeClass("font-small").addClass("font-medium");
        }
        else{
            //do nothing
        }
    });
    
    // Add Hover Class
    $('li.increase_text_but').hover(function() {
        $(this).addClass('increasetext-hover');
      }, function() {
        $(this).removeClass('increasetext-hover');
    });
        
    //print button action
    $('li.print_but').click(function () { 
        window.print();
    });
    
    // Add Hover Class
    $('li.print_but').hover(function() {
        $(this).addClass('print-hover');
      }, function() {
        $(this).removeClass('print-hover');
    });
}


// =========================================
// Make external links open in a new window 
// =========================================

function cnb_links_external(){
    // add external icon and open in a new window for external links
    $('a[href^=http]:not([href*="'+ window.location.hostname +'"]):not(:has(img))').addClass('external-link').attr('target','_blank');
    $('a.external-link', '#content_col2').addClass('external');
    
    $("a[href*=.pdf]").attr({"target":"_blank"});
    
}


// ======================
//    File Icons
// ======================

function cnb_filetype_icons(){
    /* Add .pdf icon to links */
    $("a[href$='.pdf']", "#content_wrapper").addClass('pdf');
    /* Add .xls icon to links */
    $("a[href$='.xls']", "#content_wrapper").addClass('xls');
    /* Add .ppt icon to links */
    $("a[href$='.ppt']", "#content_wrapper").addClass('ppt');
    /* Add .doc icon to links */
    $("a[href$='.doc']", "#content_wrapper").addClass('doc');
}


// ======================
//    onLoad
// ======================

$(document).ready(function(){
    
    /* Initialize Dropdown Menu */
    cnb_menu_dropdown();
    
    /* Initialize Icons */
    cnb_filetype_icons();
    
    /* Initialize Glossary Tooltip*/ 
    cnb_glossary_tooltip();
    
    /* Initialize Print and Resize Functions*/ 
    cnb_print_resize();
    
    /* Open external links in a new window */
    cnb_links_external();
    
});
