jQuery.fn.betterText = function() {
  var text = "";
  $(this).contents().each(function() {
    if (this.nodeType == 3) {
      text += " " + this.nodeValue + " ";
    } else {
      if (this.tagName == "A" ||
          this.tagName == "ABBR" ||
          this.tagName == "ACRONYM" ||
          this.tagName == "STRONG" ||
          this.tagName == "B" ||
          this.tagName == "U" ||
          this.tagName == "I" ||
          this.tagName == "FONT" ||
          this.tagName == "LABEL" ||
          this.tagName == "SPAN" ||
          this.tagName == "Q" ||
          this.tagName == "S" ||
          this.tagName == "SUP" ||
          this.tagName == "SUB" ||
          this.tagName == "TT" ||
          this.tagName == "CODE" ||
          this.tagName == "EM") {
        text += " " + $(this).betterText() + " ";
      } else if (this.tagName == "BR" || this.tagName == "HR") {
        text += "\r\n";
      } else {  
        text += " \r\n " + $(this).betterText() + " \r\n ";
      }
    }
  });
  return text;
};

$(document).ready(function() {
  if ($.isHandHeld() && ($("div.body div.bodySnippet").size() > 0)) {
    var idealTextLength = 150;
    var quick = "";
    if ($("ul.quicklinks").size() > 0) {
      var quick = $("ul.quicklinks").html();
      $("ul.quicklinks").remove();
    }
    var img = "";
    if ($("img.alwaystop").size() > 0) {
      var img = $("img.alwaystop").html();
    }
    $("div.body div.bodySnippet").each(function(index) {
      var txt = $(this).betterText();
      if (txt.length > idealTextLength) { 
        var short = '<div id="shortbox_' + index + '">' + img + txt.substr(0, txt.indexOf(" ", idealTextLength)) + "...<br /><a id='expand_" + index + "'>More</a></div>";
        var long = '<div style="display: none;" id="longbox_' + index + '">' + $(this).html() + "<a id='collapse_" + index + "'>Collapse</a></div>";
        $(this).html(quick + short + long);
        $("#shortbox_" + index + " img.alwaystop").css("float", "right").css("margin-left", "10px").css("margin-bottom", "10px");
        $("a#expand_" + index).click(function() {
          var id = $(this).attr("id");
          var otherid = id.substr(7);
          $("#shortbox_" + otherid).css("display", "none");
          $("#longbox_" + otherid).css("display", "block");
        });
        $("a#collapse_" + index).click(function() {
          var id = $(this).attr("id");
          var otherid = id.substr(9);
          $("#longbox_" + otherid).css("display", "none");
          $("#shortbox_" + otherid).css("display", "block");
        });
      }
    });
    $("div.rightNavContent h3").each(function() {
      if ($(this).text() != "Press Releases") {
        $(this).parent().children("ul").css("display", "none");
      }
    });
    $("div.rightNavContent h3").click(function() {
     $(this).parent().children("ul").each(function() {
       if ($(this).css("display") == "none") {
         $(this).css("display", "block");
       } else {
         $(this).css("display", "none");
       }
     });
    });
  }
});

