$(document).ready( function(){
    var oOriginalImage = $("#HeaderImage_top>#topHeaderImage").attr('src');
    $("#header #nav span a").hover(
        function() { // OnMouseOver
            var newImage = $("#HeaderImage_top>#topHeaderImage").attr('src').replace($("#header #nav span.current a").attr("id"), $(this).attr("id"));
            $("#HeaderImage_top>#topHeaderImage").attr('src', newImage);
        },
        function() { // OnMouseOut
            $("#HeaderImage_top>#topHeaderImage").attr('src', oOriginalImage);
        }
    );
    // Tabs on the left of Whitaker Glen pages
    if( $("#tab-container").length > 0 ) {
        $('#tab-container').tabs('div.panel-container > div', { effect: 'fade' });
    }
    // Image Slideshow on the "Photos" page of Whitaker Glen
    $(".slideshow").each(function (i) {
        $(this).before('<ul class="slide_nav"></ul><div class="slide_caption"></div>').cycle({
            height: '525px',
            width: '725px',
            fit: 1,
            timeout: 0,
            pager: $("table.slide_table ul.slide_nav").eq(i),
            slideExpr: 'img',
            pagerAnchorBuilder: function(index, slide) {
                // return sel string for existing anchor
                return '<li><a href="#"><img src="' + slide.src + '" width="50" height="50" /></a></li>';
            },
            after: function() {
                 $("table.slide_table div.slide_caption").eq(i).html(this.alt);
            }
        });
    });
    // Overlay section
    if( $("#overlay").length > 0 ) {
        $("#overlay").overlay({
            top: 'center',
            left: 'center',
            mask: 'black',
            load: true
        });
    }
    if( $("a.video").length > 0 ) {
        $("a.video").overlay({
            mask: 'black',
            target: "#youtube-frame",
            top: 'center',
            //oneInstance: false,
            onBeforeLoad: function() {
                var _this = this.getTrigger();
                var cHTML = '<iframe height="450" frameborder="0" width="100%" src="http://www.youtube.com/embed/'+_this.attr("href")+'?autoplay=1&amp;rel=0"></iframe>';
                if( $("#video_section div#youtube-frame").find("iframe").length == 0 ) {
                    $("#video_section div#youtube-frame").append(cHTML);
                }else {
                    $("#video_section div#youtube-frame iframe").replaceWith(cHTML);
                }
                $("#video_section div#youtube-frame div#youtube-frame-title").text(_this.attr("title"));
            },
            onClose: function() {
                $("#video_section div#youtube-frame iframe").remove();
            }
        });
    }
});
