/**
  Utilities
**/

// On DOM Ready
jQuery(document).ready(function(){
  jQuery('#content hr').wrap('<div class="hr">');
  
  substringText(jQuery('.post-nav a'),11);
  
  // Replace Search form label
  
  jQuery('.wp-caption-text').wrapInner('<span></span>');
  searchText('Search this blog');
  jQuery('#typesBar').simpleTabs({includeHeader:false,headerTag:'h2'});
  jQuery('#catBar').simpleTabs({includeHeader:false,headerTag:'h2'});
  
  //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');
  
  //Flickr spinner
  var start = true;
  var slides = jQuery('#flickrBar').find('a');
  slides.hide();
  function spin() {
    if (start==true) {
      c = Math.floor(Math.random()*(slides.length-1))
      start=false;
    };  
    slides.fadeOut(1500);
    jQuery(slides[c]).fadeIn(1500,function(){
      if (c!=(slides.length-1)) {
        c++;
      } else {
        c = 0;
      };
      setTimeout(function(){spin()},3000);
    });
  };
  spin();
});

function searchText(labelText) {
  if (!labelText) var labelText = jQuery('#searchForm #searchsubmit').val();
  
  jQuery('#searchsubmit').val('Go');
  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 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");
}  

