$(function() {
    // Open all external links in new window
    $.fn.external = function() {
        $(this).unbind('click').bind('click', function() {
            return !window.open(this.href);
        });
    };



    // Set initial external links
    $('a[href^=http]').not('[href*=discoverycovenewadditions.com]').external();



    // Embed YouTube video and set share links
    function embedYouTube(ytURL, ytTitle) {
        ytURL = (ytURL == null) ? $('#video_list li:first a').attr('href') : ytURL;
        ytTitle = (ytTitle == null) ? $('#video_list li:first a strong').text() : ytTitle;
        var ytID = $.url.setUrl(ytURL).param('v');
        if (ytID == undefined) {
            ytID = $.url.setUrl(ytURL).segment(1);
        }
        var fbURL = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(ytURL) + '&amp;t=' + encodeURIComponent(ytTitle);
        var msURL = 'http://www.myspace.com/Modules/PostTo/Pages/?t=' + encodeURIComponent(ytTitle) + '&u=' + encodeURIComponent(ytURL) + '&c=' + encodeURIComponent('<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/' + ytID + '&hl=en&rel=0"></param><embed src="http://www.youtube.com/v/' + ytID + '&hl=en&rel=0" type="application/x-shockwave-flash" width="425" height="355"></embed></object>');
        var tURL = 'http://twitter.com/?status=' + encodeURIComponent(ytTitle) + '%20-%20' + encodeURIComponent(ytURL);

        $('#videos #video').flash({
            swf: 'http://www.youtube.com/v/' + ytID + '&amp;rel=0',
            width: 417,
            height: 269
        });

        $('#videos .share li.facebook a').attr('href', fbURL).external();
        $('#videos .share li.myspace a').attr('href', msURL).external();
        $('#videos .share li.twitter a').attr('href', tURL).external();
    }



    // Embed initial YouTube video
    embedYouTube();

    $('#videos #video_list ul li a:first').addClass('active');



    // Embed YouTube video on click
    $('#videos #video_list ul li a').unbind('click').click(function(e) {
        $('li a.active', $(this).parents('#video_list')).removeClass('active');
        $(this).addClass('active');

        embedYouTube($(this).attr('href'), $('strong', this).text());

        e.preventDefault();
    });

    // Set the active class on the header links
    function setActiveNav() {
        $("#header #nav a").each(function() {
            var current_href = "/Dolphin/" + $(this).attr("href");

            if (document.location.pathname.toLowerCase() == current_href.toLowerCase()) {
                $(this).addClass("active");
            }
        });
    }

    //setActiveNav();

    function setActiveArchive() {
        $("#recent_births ul li a").each(function() {
            if ($(this).text() == $("#birth_certificate table tbody tr td:first").text()) {
                $(this).addClass("active");
            }
        });
    }

    setActiveArchive();


    // Higlight dolphin names in voting section
    function highlightNames() {
        $('#vote_signup #vote label').each(function() {
            var text = $(this).text().split(' ');

            $(this).html('<strong>' + text.shift() + '</strong> ' + text.join(' '));
        });
    }



    // Highlight names on page load
    highlightNames();



    // Replace voting radio buttons with a span
    $('#vote_signup #vote input[type="radio"]').hide().before('<span class="radio"></span>');



    // Replace "vote now" button with placeholder
    $('#vote_signup #vote .button a').hide().before('<a href="#" id="placeholder">Vote Now</a>');
    $('#vote_signup #vote .button a#placeholder').unbind('click').bind('click', function(e) {
        e.preventDefault();
    });



    // Custom radio buttons
    $('#vote_signup #vote span.radio, #vote_signup #vote label').click(function() {
        $('#vote_signup #vote span.radio').removeClass('checked');
        $('#vote_signup #vote input[type="radio"]').attr('checked', '');

        if ($(this).is('span.radio')) {
            $(this).addClass('checked').next('input[type="radio"]').attr('checked', 'checked');
        }
        else {
            $(this).prev('input[type="radio"]').attr('checked', 'checked').prev('span.radio').addClass('checked');
        }

        $('#vote_signup #vote .button a').hide().not('#placeholder').show();
    });



    // Input text-swap
    $('input[type="text"]').example(function() {
        return $(this).attr('title');
    });



    // Truncate posts
    $('.post_content').truncate({ max_length: 350 });
});
