$(document).ready(function(){
    $('#primary-nav li').dropdown();
    rotateSlides(1,false);
});


/* ---------- DROPDOWN PLUGIN ---------- */
// dropdown function to fix IE6's lack of support for child selectors
$.fn.dropdown = function() {
    $(this).hover(function(){
        $(this).addClass('hover');
        $('> .submenu',this).addClass('show');
    },function(){
        $(this).removeClass('hover');
        $('.show',this).removeClass('show');
    });
}


var Timer = null;

/* ---------- SLIDESHOW FUNCTIONS ---------- */
// show slideshow pagination
$('#pagination').show();

// click function for pagination
$('#pagination a').each(function(i) {
    $(this).click(function() {
        rotateSlides(i+1, true);
        return false;
    });

});


function rotateSlides(currentSlide, isClicked) {
    if (isClicked == true) {
        if (Timer) clearTimeout(Timer);
    }

    $('.active').removeClass('active');

    $('#pagination a').each(function(n) {
    $('#pagination a:eq(' + n + ')').removeClass('active');

        if (n == (currentSlide - 1)) {
            $('#pagination a:eq(' + (n) + ')').addClass('active');
        }
    });

    var totalSlides = $('#slideshow .slide').size();
    var currentSlideNext = currentSlide % totalSlides;

    $('#slideshow .slide').eq(currentSlideNext).fadeOut('slow', function() {

        // re-order the z-index
        $('#slideshow .slide').each(function(i) {
            var zIndexValue = ((totalSlides - i) + currentSlideNext) % totalSlides;
            $(this).css('z-index', zIndexValue);
        });

        $(this).show();

        if (isClicked == false) {

            Timer = setTimeout(function() {

                $('#pagination a').each(function(n) {
                    $('#pagination a:eq(' + n + ')').removeClass('active');
                });

                rotateSlides(++currentSlideNext, false);

            }, 7000); // rotate every 7 seconds (in milliseconds)
        }

    });
}


/* ---------- SELECT JUMP MENU FUNCTION ---------- */
// function creates a select jump menu from the 'jump-menu' unordered list
$("ul#jump-menu").each(function() {
    var select=$(document.createElement('select')).insertBefore($(this).hide());
	  $(select).attr("style","width: 200px");
    $('>li a', this).each(function() { 
        option=$(document.createElement('option')).appendTo(select).val(this.href).html($(this).html());
    });
    select.change(function(){
        window.location.href = this.value;
    })
});


/* ---------- DYNAMICALLY UPDATE SUBSECTION ARTICLE CONTENT FUNCTIONS ---------- */
var content_wrapper_id = "#article-content"; 
var ajax_main_content_identifier = "ajax"; 

$("a[rel*='"+ ajax_main_content_identifier +"']").click(function(){
    var ajax_url = $(this).attr("href");
    var new_tab = $(".article-nav li a").index(this);
    load_ajax_main_content(ajax_url);
    highlight_nav_content(new_tab);
    return false;
});


// function to add 'on-state' class to 'article-nav' link
function highlight_nav_content(nav_num){
    $(".article-nav li a.on-state").removeClass("on-state");
    $(".article-nav li a").eq(nav_num).addClass("on-state");
}


// function to load the article content
function load_ajax_main_content(content_url){
    $.ajax({
        type: "GET",
        url: content_url,
        error: function(){
            $(content_wrapper_id).html("Error loading the document."); 
        },
        success: function(ajax_html){
            var content_html = $(ajax_html).find(content_wrapper_id).html();
            $(content_wrapper_id).slideUp("normal", function(){
                $(content_wrapper_id).html(content_html);
                $(content_wrapper_id).css("display","none");
                $(content_wrapper_id).slideDown();
            });
        }
    });
}



