(function($) {
   $(document).ready(function(){
      
      // Setting up the "tooltips" for dropdowns in the nav menu
      
      $('#articles_trigger').tooltip({
         tip: "#articles_dropdown",
         position: 'bottom right',
         offset: [0, (-$("#articles_trigger").parent().width()-5)]
      })
      
      $('#about_trigger').tooltip({
         tip: "#about_dropdown",
         position: 'bottom right',
         offset: [0, (-$("#about_trigger").parent().width()-5)]
      })
      
      $('#search_options_label').tooltip({
         tip: "#search_options",
         position: 'bottom left',
         offset: [1, $("#search_options_label").outerWidth()+10]
      });
      
      // click words to click radio button on search options dropdown
      $("#as_sec_on_span").click(function(e) {
         if (e.target.type !== 'radio')
            $("#as_sec_on").click();
      });
      $("#as_sec_off_span").click(function(e) {
         if (e.target.type !== 'radio')
            $("#as_sec_off").click();
      });
      
      
      
      // sidebar tabs
      $('.tab-sidebar').each(function() {
         var widgets = $(this).children();
         var tabsid = $(this).attr("id") + '_tabs';
         var titleList = '<ul id="' + tabsid + '" class="sidebar-tabs">';
    
         widgets.each(function() {
            var widgetTitle = $(this).children('.widgettitle').text();
            
            $(this).children('.widgettitle').hide();
    
            var listItem = '<li><a href="#'+$(this).attr('id')+'">'+widgetTitle+'</a></li>';
            titleList += listItem;
         });
         
         titleList += '</ul>';
         $(this).before(titleList);
      });
      $('.tab-sidebar').each(function() {
         $("#" + $(this).attr('id') + "_tabs").tabs("#" + $(this).attr('id') + " > li");
      });
      
      
      // hover code samples to expand them
      // =================================
      $('pre').wrapInner('<span class="prewrap"></span>');
      $('pre').each(function() {
         Width = $(this).width();
         var maxWidth;
      
         $(this).hover(function() {
            maxWidth = getMaxWidth(this);
            //console.log('hover in: ' + maxWidth);
            if(Width < maxWidth) {
               $(this).stop(true,false)
                  .css({zIndex: "100", position: "relative"})
                  .animate({width: maxWidth + "px"});
            }
         },
         function() {
            $(this).stop(true,false).animate({width: Width + "px"});
         });
      });
      
      function getMaxWidth(el, m) {
         if (!m) m = 0;
         if ($(el).children().length != 0) {
            $(el).children().each(function() {
               var test = getMaxWidth(this, m);
               m = (test < m) ? m : test;
               //console.log(m);
            });
         } else {
            return $(el).width();
         }
         
         return m;
         
      }
      
      
      
   });
})(jQuery);
