/**
 * @author Tom
 */

$(document).ready(function() {
     $("img[src$=png]").pngfix();

     if($.browser.msie && $.browser.version.substr(0,3) == '6.0' )
     // fix issues that only occur in the worst browser in the world
     {
          $('#copyright').css('background-image', 'none');
          $('#copyright').css('background-color', '#fff');
          $('#copyright').css('width', '950px');
     }

     // Sidebar menu animation
     $('#sidebar ul.sub_menu').hide();
     $('#sidebar div.item_title:contains("Latest News")').next().show( 50, function() {
          $(this).prev().children('span').css({
               paddingRight: '15px',
               backgroundImage: 'url(/img/sidebar_menu_arrow_up.png)',
               backgroundRepeat: 'no-repeat',
               backgroundPosition: 'center right'
          });
     });

     // Latest news in sidebar
     $('#ajax_loading').ajaxStart(function() {
          $(this).show();
     });
     $('#ajax_loading').ajaxStop(function() {
          $(this).hide();
     });

     $.getFeed({
          url: '/articles/rss',
          success: function(feed) {
               var html = '';

               for (var i = 0; i < feed.items.length && i < 5; i++) {
                    var item = feed.items[i];

                    html += '<li>';
                    html += '<a href="' + item.link + '">' + item.title + '</a>';
                    html += '<p>' + item.description.stripHTML().substring(0, 75) + ' &hellip; <br />';
                    html += '<a href="' + item.link + '">Read more</a>';
                    html += '</p>';
                    html += '</li>';
               }

               $('#rotator').hide().append(html).fadeIn(1200);
                                   if ( feed.items.length > 5 ) {
                    $('#rotator').append('<li><a href="/index.php/articles">More news</a></li>');
               }
          }
     });

     // Decorate expandle sidebar menu items with arrows to indicate their expandable nature
     $('#sidebar div.item_title span').css({
          paddingRight: '15px',
          backgroundImage: 'url(/img/sidebar_menu_arrow_down.png)',
          backgroundRepeat: 'no-repeat',
          backgroundPosition: 'center right'
     });
     $('#sidebar div.item_title').click(function() {
          $(this).next().slideToggle( 150, function() {
               $li = $(this).prev().parent();
               if ($(this).is(':visible')) {
                    $(this).prev().children('span').css({
                         paddingRight: '15px',
                         backgroundImage: 'url(/img/sidebar_menu_arrow_up.png)',
                         backgroundRepeat: 'no-repeat',
                         backgroundPosition: 'center right'
                    });
               } else {
                    $(this).prev().children('span').css({
                         paddingRight: '15px',
                         backgroundImage: 'url(/img/sidebar_menu_arrow_down.png)',
                         backgroundRepeat: 'no-repeat',
                         backgroundPosition: 'center right'
                    });
               }
          });
     });


     $('a').focus(function() {
          $(this).blur();
     });

     $('table.data tr:odd').css({ backgroundColor: '#FBF8F3' });

     // Add file-type icons to links (PDF, MS Word, etc.)
     //Apply CSS class to all A links that end in .pdf
     $("a[href$='.pdf']").addClass("PDF").attr( 'target', '_blank' );

     //Apply CSS to all anchors that link to OpenOffice files
     $("a[href$='.odt'], a[href$='.ods'], a[href$='.odf'], a[href$='.odb'], a[href$='.odp']").addClass('OO').attr( 'target', '_blank' );

     //Apply CSS class to all A links that end in .doc
     $("a[href$='.doc'], a[href$='.docx']").addClass("DOC").attr( 'target', '_blank' );

     //Apply CSS class to all A links that end in .xls
     $("a[href$='.xls'], a[href$='.xlsx']").addClass("XLS").attr( 'target', '_blank' );

     //Apply CSS class to all A links that end in .ppt
     $("a[href$='.ppt'], a[href$='.pptx']").addClass("PPT").attr( 'target', '_blank' );

     //Apply CSS class to all A links that end in .jpg
     $("a[href$='.jpg']").addClass("Image");

     //Apply CSS class to all A links that end in .gif
     $("a[href$='.gif']").addClass("Image");

     //Apply CSS class to all A links that end in .png
     $("a[href$='.png']").addClass("Image");

     //Apply CSS class to all A links that start with mailto:
     $("a[href^='mailto:']").addClass("Mail");

     //Apply CSS class to all A links that start with http://
     $("a[href^='http://']").attr( 'target', '_blank' );
     $("a[href^='http://www.americanretina.org']").attr( 'target', '' );
     $("a[href^='http://amretf.dyndns.org']").attr( 'target', '' );

     //Remove CSS class to all A links that start with http://www.YOURDOMAINHERE.co.uk
     $("a[href^='http://www.americanretina.org']").removeClass("ExternalLink");
     $("a[href^='http://www.asrs.org']").removeClass("ExternalLink");
     $("a[href^='http://amretf.dyndns.org']").removeClass("ExternalLink");

     //Remove CSS class from all A links within the carousel
     $("#carousel > li > a").removeClass("ExternalLink");

     // Remove CSS class from all A links with class "img"
     $("a.img").removeClass("ExternalLink");
     // Remove dynamic classes from all A links that contain an img
     $('a:has(img)').removeClass("ExternalLink");
     // Remove PDF class from all links that contain an img
     $('a:has(img)').removeClass("PDF");

     // Remove CSS class from all A links within the item_title class DIVs in the sidebar
     $('.item_title a').removeClass("ExternalLink");

     $('div.form_row:last').css('border-bottom', 0);

     // Show/hide grant descriptions on /grants/submission page
     $('#research_toggle').click(function() {
          $('.grant_description').hide();
          $('#research_grants').fadeIn( 500 );
     });
     $('#cme_toggle').click(function() {
          $('.grant_description').hide();
          $('#cme_grants').fadeIn( 500 );
     });
     $('#outreach_toggle').click(function() {
          $('.grant_description').hide();
          $('#outreach_grants').fadeIn( 500 );
     });
     $('#travel_toggle').click(function() {
          $('.grant_description').hide();
          $('#travel_grants').fadeIn( 500 );
     });

     $('.close_grant_description').click(function() {
          $('.grant_description').fadeOut( 300 );
     });

     // Stripe the grants overview table
     $('table#grants_overview tr:odd').addClass('odd');
});

// Strip HTML tags
String.prototype.stripHTML = function()
{
        // What a tag looks like
        var matchTag = /<(?:.|\s)*?>/g;

        // Replace the tag
        return this.replace(matchTag, "");
};
