/**
 * jQuery plugin für ad positioning.
 *
 * options:
 * - ns_spacer (required):
 *   Namespace for spacer where an ad should be placed.
 *   
 * - dummy (required):
 *    The dummy js that replaces the origin js source (ie6 / ie7 feature). It must
 *    be either a absolute url or uri to file.
 *    
 *    For instance:
 *    * http://www.macwelt.de/js/dummy.js
 *    * /js/dummy.js
 *
 *  - callback (optional):
 *    * afterappend:
 *      Callback that is executed after appending the ad to the spacer.
 */
jQuery.fn.extend({
  alignToSpacer: function(options) {
    return this.each(function() {
      var obj = jQuery(this);
      
      var spacer_id = obj.attr('id').split('_')[1];

      // replace all src attributes with the dummy javascript to prevent multiple
      // execution.
      // see: http://www.trygve-lie.com/blog/entry/moving_a_script_ie_might
      obj.find('script[src]').attr('src', options.dummy);
      
      var e = obj.get(0);
      // remove old div in the DOM
      // removing the DOM element may lead to problems in FF
      //e.parentNode.removeChild(jQuery(this).get(0));
      //jQuery(this).remove();
      
      // Append ad to the spacer.
      document.getElementById(options.ns_spacer + spacer_id).appendChild(e);
      obj.show();

      // Execute callback
      if(options.callback)
      {
        if(options.callback.afterappend)
        {
          options.callback.afterappend.call(this);
        }
      }
    });
  }
});
