$(document).ready(function(){
 
  /*******************************
  *  Image captioning
  *******************************/

  $(".node .show-caption .image-featured").each(function(i) {
      var imgwidth = $(this).width();
      var imgheight = $(this).height();
      var captiontext = $(this).attr('title');
      var alignment = $(this).attr('align');
      $(this).attr({align:""});
      $(this).wrap("<div class=\"image-caption-container\" style=\"float:" + alignment + "\"></div>");
      $(this).parents('.image-attach-body').css({'width':'200px'});
      $(this).parent().width(imgwidth);
      $(this).parent().append("<div class=\"image-caption\">" + captiontext + "</div>");
  });
  
  $("#comment-controls").addClass("hide-comment-controls");
  $("#comment-controls").hide();
  $("#comment-controls").wrap('<div id="comment-controls-wrapper />"');
  $('#comment-controls-wrapper').prepend('<a href="#" class="comment-settings">Show my comment settings</a>');
  $('.comment-settings').click(function(){
    $("#comment-controls").toggle();
    return false;
  });

  /*******************************
  *  Tabbed divs
  ******************************

  Not currently used - included as a reference. Remove if not using

  function makeTabBox(divArray, tabsId) {
  
    headingTag = 'h3';
    
    makeBox = true;
    for (i=0;i<divArray.length;i++) {
      if($(divArray[i]).length <= 0) {
        makeBox = false;
      }
    }
    if(makeBox==true) {
      // should use wrap all when using proper jquery version
//$('#block-views-anyanswers_answered').add('#block-views-anyanswers_best_answers').add('#block-views-anyanswers_unanswered').wrapAll('<div id="anyanswers-tabs"></div>');
      
      $(divArray[0]).before('<div id="' + tabsId + '" class="generic-tabs"></div>');
      
      var tabLinks = new Array();
      
      for(i=0;i<divArray.length;i++) {
        $(divArray[i]).appendTo("#" + tabsId);
        tabLinks[i] = $(divArray[i]).children(headingTag).html();
        $(divArray[i]).children(headingTag).remove();
      }
      
      // create and add tab links
      
      //temporary site specific 
      tabsLinksHtml = '<div class="' + tabsId + '-block generic-tabs-block">';
      
      tabsLinksHtml += '<ul id="' + tabsId + '-links" class="generic-tabs-links">';
      for(j=0;j<tabLinks.length;j++) {
        tabsLinksHtml += '<li><a href="#">' + tabLinks[j] + '</a></li>';
      }
      tabsLinksHtml += '</ul>';
      
      //temporary site specific 
      tabsLinksHtml += '<span class="clear"></span></div>';
      
      $("#" + tabsId).prepend(tabsLinksHtml);
      
      //needs .equalHeight() when jquery is added
      
      $('#' + tabsId + ' div.block-views').hide();
      $(divArray[0]).show();
      $('#' + tabsId + '-links a:eq(0)').parents('li').addClass('active-tab');
      
      
      $('#' + tabsId + '-links a').click(function(){
        tabNo = $('#' + tabsId + '-links a').index(this);
        $('#' + tabsId + ' div.block-views').hide();
        $('#' + tabsId + '-links li').removeClass('active-tab');
        $('#' + tabsId + '-links a:eq('+tabNo+')').parents('li').addClass('active-tab');
        $(divArray[tabNo]).show();
        return false;
      });
    }
  }  
  
 
  var mostTabs = new Array("#block-views-most_read","#block-views-most_commented");
  makeTabBox(mostTabs, 'most-tabs');
  
  */

}); //ready

/*******************************
*  Helper functions and plugins
*******************************/

/*******************************
*  return coordinates of
*  an element
*******************************/

var getCumulativeOffset = function (obj) {
  var left, top;
  left = top = 0;
  if (obj.offsetParent) {
    do {
      left += obj.offsetLeft;
      top  += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return [top, left];
};

/*******************************
*  Tooltip widget
*
*  can't remember excatly where
*  this came from
*
*  amended so as not to clash 
*  with sharethis object
*******************************/

this.tooltip = function(){	
	
  xOffset = 10;
  yOffset = 20;		
    
  $(".tooltip").hover(function(e){

  if(this.title!="") {
    this.t = this.title;
    this.title = "";									  
    $("body").append("<p id='tooltip'>"+ this.t +"</p>");
    $("#tooltip")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast");
  }
      	
  },
  // mouseout callback
  function(){
  
    if(this.t!=""&&this.t!="undefined") {
  
    this.title = this.t;		
    $("#tooltip").remove();
    
    }
    
    });	
  $(".tooltip").mousemove(function(e){
    $("#tooltip")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px");
  });	
};

/*******************************
*  IE specific ad relocation
*******************************/

function relocateAd(iframeObj, divName) { 
  
  var iframeDoc, allScripts; 
  iframeDoc = iframeObj.contentWindow.document; 
  allScripts = iframeDoc.getElementsByTagName('script'); 
  // 'unset' all scripts
  for (s = 0; s < allScripts.length; s++) if (allScripts[s].src) allScripts[s].src = ''; 
    
  document.getElementById(divName).insertAdjacentElement('beforeEnd', iframeDoc.getElementById('adDiv')); 

}
   