/*
 * jquery.geekSkypeRemover.js - jQuery plugin to remove Skype Call buttons
 *
 * Version 1.0
 *
 * This plugin extends jQuery with a new function:
 *
 *   - $.geekSkypeRemover(10000)
 *       Check for Skype Call buttons for a maximum of 20 seconds.
 *
 * This function checks for 'Skype Call' buttons dynamically created by the
 * Skype Browser Plugin for a defined period of time and replaces any such
 * buttons it finds with the original textual telephone number content.
 *
 * The predefined period of time is required since the Skype Browser Plugin
 * might take a few seconds to add the Skype Call buttons after loading of
 * the page completes.
 *
 * Simply replacing the elements dynamically added by the Skype Browser Plugin
 * with the original textual telephone numbers would cause Skype to recreate
 * the Skype buttons, so this plugin inserts the original textual telephone
 * numbers with hidden underscores ("_") as the second characters.
 *
 * The following metatag is supposed to perform the same function, but it works
 * inconsistently across browsers and DOCTYPES:
 *
 *   <meta name="SKYPE_TOOLBAR" CONTENT="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
 *
 *   <!-- sphoneid telnr="+27210000000" fileas="Willem van Zyl" -->021 000 0000<!-- sphoneid -->
 *
 *
 * This code is in the public domain.
 *
 * Willem van Zyl
 * willem@geekology.co.za
 * http://www.geekology.co.za/blog/
 */
(function($) {

  /**
   * Remove Skype Call buttons that were dynamically added, e.g.:
   *
   *   $(document).ready(function() {
   *     $.geekSkypeRemover(10000);
   *   }
   */
  $.geekSkypeRemover = function(microsecondsLeft) {

    //check if the predefined time limit was reached:
    if (microsecondsLeft == 0) {
      return false;
    }

    microsecondsLeft = microsecondsLeft - 500;


    //seek elements added by the Skype Browser Plugin; remove them if
    //they exist or try again in microsecondsLeft microseconds if they don't:
    if ($('span.skype_pnh_container').length == 0) {
      //no Skype Call buttons exist (yet):
      setTimeout('$.geekSkypeRemover(' + microsecondsLeft + ')', 500);
    } else {
      //Skype Call buttons exist:
      var originalText = '';

      //determine and set the original textual telephone number:
      $('span.skype_pnh_print_container').each(function() {
        originalText = $(this).html();
        originalText = originalText.substring(0,1) + '<span style="display: none;">_</span>' + originalText.substring(1);
        $(this).after(originalText);
      });

      //remove the Skype Call button elements:
      $('span.skype_pnh_container').remove();
      $('span.skype_pnh_print_container').remove();
    }


    return true;

  };

})(jQuery);
