// Makes popup footnotes and Transfers text and gizmos to difficult parts of the page
// tooltipsy.min.js is loaded in html.tpl.php
// 
jQuery(document).ready(function() {

  jQuery("div.zMagic").each(function(){    // get magic boxes of stuff (un-wrapped)
    var wBox = jQuery(this);               // wrap the box (with methods)
    var targID = wBox.attr('ref');         // find target id
    var stuff  = wBox.html();              // get the html out of the box
    if (wBox.hasClass('zInsert')) {
      jQuery("#"+targID).html(stuff);
      }
    else if (wBox.hasClass('zReplace')) {
      jQuery("#"+targID).replaceWith(stuff);
      }
    else {
      alert("Need class 'zInsert' or 'zReplace' w/ class 'zMagic'");
    }
    jQuery(this).remove();   // Remove the box
  });
  jQuery('span.zScript').each(function(){        // <span class="mscript">script>...</script</span>
    var okScript = jQuery(this).text().replace(/\[=/g,"<"); // Fix script tags.
//    jQuery(this).after(okScript);       // this method also works.
//    jQuery(this).remove();
    jQuery(this).replaceWith(okScript);   // Replace the span with fixed script
  });
//   POPNOTES
/* 1. Find references and <span></span> them with class=hastip
   1b. Loop through class=hastip and add compute note id's and store as data.
   2. Find notes and span their name-nums with class=zNote.
   3. Loop through class=zNote and add id's to <td>s based on name-nums.
   4. Loop through class=hasTip, pull of data, compute id, find footnote and make tooltip
*/
// Wrap a Ref # in <a>.   e.g.  [#5] => <a class=hastip >5</a>
    var translate_footnote_refs = function (index, oldhtml) {
      var new_html = oldhtml.replace(/\[#([^\]\[]{1,50})\]/g, "<span class='hastip' style='border-bottom: 3px double #66FF66'>$1</span>");
      return new_html;
    }
    jQuery("u:contains(\'[#\')").html(translate_footnote_refs);
 
// Format the Ref # for visitors. 
    jQuery("span.hastip").each(function() {
      var ref_el = jQuery(this);
      var refNam = ref_el.text();
/*      var uline = 0;
      if ( refNam.match(/\.u$/) ) {
        uline = 1;
        refNam = refNam.replace(/\.u$/, "");  // take the .u off the end
      }*/
      var idNam  = refNam.replace(/\W/g,"");  // strip out funny chars
      ref_el.attr("ref",idNam);
      if (refNam.length<3) refNam = "[" +refNam+ "]";
      ref_el.html(refNam);
 //     ref_el.css({"border-bottom": "3px double red", "text-decoration": "underline"});
 //     if (uline==0) {
        var utag = ref_el.closest("u")
        var uInside = utag.html();
        utag.replaceWith(uInside);  // css will not undo a <u> tag, so lose the <u>
 //     }
    });

// Wrap Note # in a <span>,  e.g.    FN5 = <span class=zNote ref=FN5>#5</span>
    var translate_footnotes = function (index, oldhtml) {
       var new_html = oldhtml.replace(/\[=([^\]\[]{1,50})\]/g, "<span class=\'zNote\' style=\'color: #003333\'>$1</span>");
       return new_html;
    }
// Find the footnotes and put spans on their #s
    jQuery("td:contains(\'[=\')").html(translate_footnotes);
    jQuery("span.zNote").each(function() {        // ID the notes-table rows and give them borders
      var zSpan = jQuery(this);
      var refNum = zSpan.text();
      var noteID = refNum.replace(/\W/g, "");   // to ID the table row.
      zSpan.html("<b>" +refNum+ ": </b>");
      zSpan.hide();
      zSpan.closest("td").attr("id",noteID).css("border","1px solid #ccbbbb");
    });

    function findOffset(refPos) {
      var tipOff = 0;
      if (refPos<130) tipOff = -230 - refPos/2;
      else if (refPos>390) tipOff = 390 - refPos;
      else tipOff = -5; 
      return tipOff;
    }

    jQuery("span.hastip").each(function() {
       var noteRef = jQuery(this);
       var refPos = noteRef.offset().left - jQuery("#main").offset().left; 
       var noteID = noteRef.attr("ref");
       var tipContent = jQuery("td#"+noteID).html();
       // Add the ToolTipsy
       noteRef.tooltipsy({
         offset: [findOffset(refPos), 1],
         alignTo: "cursor",
         css: {  "width": "300px", "padding":"5px", "background-color": "#f9f9ee", "border": "2px solid #bbbbbb" },
         content: tipContent
       });  // end of tooltipsy
     });  // end of jQuery( hastip );

         jQuery("span.zNote").each(function() {        // ID the notes-table rows and give them borders
           jQuery(this).show();
         });

// Move block-24 (with G-Ad) to just after first <hr>. (should be first inside content)
//  var b24 = jQuery("#block-block-24").remove().html();
//  jQuery("hr").filter(":first").css("margin","0px").after(b24+'<hr>');

});;

