//Third party functions
//Google+
(function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
//Flattr
(function() {
    var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto&button=compact';
    t.parentNode.insertBefore(s, t);
})();
// Common plugins
//--------------------------------------------------------------------//

//--------------------------------------------------------------------//
$(document).ready(function() {
    
    //IE fix opacity
    $('.logo').fadeTo("slow", 0.45);
    $('.faded').fadeTo("slow", 0.10);
    
    //store site url as var
    var site_url = $('#masthead h1 a').attr('href');
    
    //remove link titles
    $('#masthead a').removeAttr('title');
    
    //redirect to donations page
    $('#cbRichSponsorBlurb').click(function () {
        window.location = site_url + "donate"
    });
    
    //show main nav menu
    $('.navigation').hover(function() {
        $(this).find('nav:first').show()
    }, function() {
        $(this).find('nav:first').hide();
    });
    
    //show user menu
    $('#usermenu').hover(function() {
        $(this).find('div:first').show()
    }, function() {
        $(this).find('div:first').hide();
    });

    
    //opacity animation for main site logo
    $('.navigation').mouseenter(function() {
        $('.logo').stop().fadeTo("slow", 1.0);
    });
    $('.navigation').mouseleave(function() {
        $('.logo').stop().fadeTo("slow", 0.45);
    });
    $('.logo').click(function(e){
        e.preventDefault();
    });
    
    //opacity animation for faded elements
    $('.faded').mouseenter(function() {
        $(this).stop().fadeTo("slow", 1.0);
    });
    $('.faded').mouseleave(function() {
        $(this).stop().fadeTo("slow", 0.10);
    });
    
    //close button
    $('.close').click(function(){
        $(this).parent('div').fadeOut('slow');
    });
    
    // Responsive Videos
    var $allVideos = $("iframe[src^='http://player.vimeo.com']"),
    $fluidEl = $('.videowrapper');
    $allVideos.each(function() {
        $(this)
            .data('aspectRatio', this.height / this.width)
            .removeAttr('height')
            .removeAttr('width');
        });
    // Resize on window resize
    $(window).resize(function() {
    var newWidth = $fluidEl.width();
    $allVideos.each(function() {
        var $el = $(this);
        $el
          .width(newWidth)
          .height(newWidth * $el.data('aspectRatio'));
    });
    }).resize();
    
});

