if(typeof console=='undefined'){console={log:function(){},error:function(m){alert(m);}};}

(function($){
    var slideshow = {};

    var initSlideshow = function() {
        try {
            var numSlides = $('#HtmlSlideshow .slide').length;

            var nums = $('<div class="nav">');
            for(var i=1; i<=numSlides; i++) {
                var num = $('<div class="item">');    
                var a = $('<a/>', {href: '#', html: i});

                if(i == 1) {
                    $(num).addClass('selected');
                }

                $(num).append(a);
                $(nums).append(num);
            }

            $('#HtmlSlideshow').append(nums);

            if(!ie6) {
                slideshow.intervalId = setInterval( function() {
                    try {
                        var cur = $('#HtmlSlideshow .item.selected a');
                        var next = $(cur).parent().next().children('a');

                        if($(next).length <= 0) {
                            next = $('#HtmlSlideshow .nav .item:first-child a');
                        }

                        switchSlideshow(next);
                    }catch(e) { alert('Error in slideshow Internval:  ' + e.message); }
                }, 7000);
            }
        }catch(e) { alert('Error in initSlideshow:  ' + e.message); }
    };

    var switchSlideshow = function(elem) {
        try {
            var idx = parseInt($(elem).html(),10);

            $('.nav .item').removeClass('selected');
            $(elem).parent().addClass('selected');

            $('.slide:visible').hide(500, function() {
                $('.slide:nth-child('+idx+')').show(500);
            });
        }catch(e){ alert('Error in switchSlideshow:  ' + e.message); }

    };

    $('.nav .item a').live( 'click', function(e) {
        try {
            if(typeof slideshow.intervalId != 'undefined') {
                clearInterval(slideshow.intervalId);
            }

            switchSlideshow(this);
        }catch(e){ console.error(e.message); }

        e.preventDefault();
    });


    $(document).ready( function() {
        if(typeof ie6 == 'undefined'){ ie6 = false; }
        $('#Nav .item.selected').prev().addClass('nobg');

        //This is now part of the markup in header.php.  Doing it via JS was a bit slow and caused flicker
        //$('#Nav div.item > a').wrap('<span class=cap><span class=cap_end></span></span>');

        $('#Nav .item').hover( 
            function(e) {
                $(this).addClass('hover');
                $('.dropmenu', this).css('display', 'block');

                if(ie6) {
                    this.style.cursor = 'pointer';
                }
            },
            function(e) {
                $(this).removeClass('hover');
                $('.dropmenu', this).css('display', 'none');
            }
        );

        if(ie6) {
            $('#Nav .item').click( function(e) {
                window.location = $('.cap_end a', this).attr('href');
                e.preventDefault();
                return false;
            });
        }

        $('ul.multidrop li').hover(
                function(e) {
                        $(this).children('ul').css('display', 'block');
                },
                function(e) {
                        $(this).children('ul').css('display', 'none');
                }
        );
        
        initSlideshow(); 

        //Debug code to show empty links
        if(window.location.hash == '#missinglinks') {
            $('a').each( function(i, elem) {
                if($(this).parent('#HeritageWrapper').length > 0 || 
                    $(this).parent('.scrollNav').length > 0 ||
                    $(this).parents('.nav').length > 0 ) { 
                    return; 
                }
                var href = $(elem).attr('href');
                if(href === '' || href === '#') {
                    $(elem).css('border', '1px solid red');
                }
            });
        }
    });
})(window.jQuery);

