/**
  Utilities
**/
// Expandable Blogroll
(function($){
  $.fn.expandableLinks = function() {
    return this.each(function(){
      var links = $(this);
      var category = links.find('.linkcat');
      var toggler = links.find('H3');
      var catLinks = category.find('.blogroll');
      catLinks.hide();
      category.addClass('closed');
      toggler.click(function(){
        var theIndex = $(toggler).index(this);
        var thisCat = $(category[theIndex]);
        var theseLinks = $(catLinks[theIndex]);
        if (thisCat.hasClass('closed')) {
          theseLinks.slideDown();
          thisCat.removeClass('closed');
        } else {
          theseLinks.slideUp();
          thisCat.addClass('closed');
        };
      });
    });
  };
})(jQuery);

// On DOM Ready
jQuery(document).ready(function(){
  jQuery('#content hr').wrap('<div class="hr">');
  
  substringText(jQuery('.post-nav a'),100);
  
  // Replace Search form label
  
  jQuery('.wp-caption-text').wrapInner('<span></span>');
  searchText('Search OolaMoola');
  
  jQuery('#linksBar').expandableLinks();
  
  //TwitterBar styling hooks
  jQuery('.aktt_tweets LI.aktt_more_updates').remove();
  jQuery('.aktt_tweets LI:odd').addClass('even');
  jQuery('.aktt_tweets LI:last').addClass('last');
  
  //Authors tabs
  jQuery('#postsTab').click(function(){
    jQuery(this).addClass('active');
    jQuery('#commentsTab').removeClass('active');
    jQuery('#postsPanel').addClass('active');
    jQuery('#commentsPanel').removeClass('active');
  });
  jQuery('#commentsTab').click(function(){
    jQuery(this).addClass('active');
    jQuery('#postsTab').removeClass('active');
    jQuery('#postsPanel').removeClass('active');
    jQuery('#commentsPanel').addClass('active');
  });
  
});

function searchText(labelText) {
  if (!labelText) var labelText = jQuery('#searchForm #searchsubmit').val();
  
  if (jQuery('#searchForm #s').val() == '') {
    jQuery('#searchForm #s').val(labelText);
  };
  jQuery('#searchForm #s').focus(function(){
    if (jQuery('#searchForm #s').val() == labelText) {
      jQuery(this).val('');
      jQuery(this).blur(function(){
        if (jQuery(this).val()=='') {
          jQuery('#searchForm #s').val(labelText);
        };
      });
    };
  });
}

function insertIt(framediv) { 
  var _y = document.getElementById(framediv); 
  var _x = window.frames[0].document.body.innerHTML; 
  _y.innerHTML = _x 
}

function substringText(obj,count) {
  obj.each(function(){
    var objSize  = jQuery(this).text().length;
    if (count <= objSize) {
      var dots = '...';
    } else {
      var dots = '';
    };
    var textAll = jQuery(this).text();
    var text = textAll.substring(0,count)+dots;
    jQuery(this).text(text).attr('title',textAll);
  });
};

function showPortalMaximised() {
  jQuery('#eyebrow').animate({ height:87 },400);
  jQuery('#eyebrow .wrapper').animate({ opacity: 0 },400,function() {
    loadFlash();
    jQuery('#eyebrow .wrapper').animate({opacity:1})
  });
};

function loadFlash() {
  var po = new SWFObject("http://www.gold.org/common/swf/portal.swf", "portalswf", "100%", "60", "6", "#ffffff");
  po.addVariable('portalZone', 'www.science.gold.org');
  po.addVariable('hostZone', '/');
  po.addParam('allowScriptAccess','always');
  po.addParam("wmode", "opaque");
  po.write("flashbrow");
}  

