/**
  Utilities
**/
(function($){//labelText
  jQuery.fn.shareScroll = function(settings) {
    settings = jQuery.extend({
      top: ''
    }, settings);
    return this.each(function(){
      var post = $(this),
          share = $('.share_box',post),
          win_w = $(window).width();

      var widget = {
            width: share.width(),
            height: share.height(),
            x: share.offset().top,
            y: share.css('left'),
            pTop: 15,
            pBot: 68,
            position: 'absolute'
          }
      
      var widget_height = widget.height + widget.pTop + widget.pBot,
          post_height = post.height(),
          post_offset = post.offset().top,
          max_trigger = post_offset + (post_height - widget_height);
      
      swap_format();
      
      $(window).resize(function(){
        swap_format();
      });
      
      if ($.browser.msie) {
        post.addClass('ie_fix')
      } else {
        doit();
        $(window).scroll(function(){
          doit();
        });
      };
      
      function swap_format() {
        win_w = $(window).width();
        if (win_w > 1110) {
          share.removeClass('share_static');
          if (!$.browser.msie) {doit();}
        } else {  
          share.addClass('share_static');
        };
      };
      function doit() {
        widget_height = widget.height + widget.pTop + widget.pBot;
        post_height = post.height();
        post_offset = post.offset().top;
        max_trigger = post_offset + (post_height - widget_height);
        
        var active_area = post_height - widget_height;
        
        
        if (win_w > 1110) {
          if ($(window).scrollTop() > (widget.x - widget.pTop)) {
            if ($(window).scrollTop() > post_offset + active_area) {
              share.stop().css({
                top: "auto",
                left: widget.y,
                bottom: widget.pBot,
                position: "absolute"
              });
            } else {
              share.stop().css({
                  top: widget.pTop,
                  left: post.offset().left - widget.width - 1,
                  bottom: "auto",
                  position: "fixed"
              }, 200);
            };
          } else {
            share.stop().css({
                top: widget.pTop,
                left: widget.y,
                position: "absolute",
                bottom: "auto"
            }, 200);
          };
        };
      };
    });
  };
})(jQuery);


// On DOM Ready
jQuery(document).ready(function(){
	
  // Check for format	
  if(getParameterByName('media')) {
    window.print();
  }

  // Share box	
  jQuery('.post').shareScroll();
  jQuery('#content hr').wrap('<div class="hr">');
  substringText(jQuery('.post-nav a'),10);
  
  // Replace Search form label
  jQuery('.wp-caption-text').wrapInner('<span></span>');
  searchText('Search this blog');

  //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');

  
});

function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return false;
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

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");
}  


