// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Add target=_blank for external targets
// (being done in JS so HTML validates)
if (0) {
  g_media_url = "http://media.retrosoft.com/";
}
else {
  g_media_url = "http://localhost/";
}
//g_external_link_icon_url = g_media_url + 'images/assets2/Icon_External_Link.png'
g_external_link_icon_url = '/images/assets2/Icon_External_Link.png'
function fix_external_anchors()
  {
  //debugger;
  var anchors = $$("a");
  anchors.each(function(anchor)
    {
    if ( (anchor.readAttribute("href")) 
      && (anchor.readAttribute("rel") == "external") )
      // && this.hostname && (this.hostname != location.hostname) )
      {
      anchor.writeAttribute("target", "_blank");
      if (!anchor.hasClassName("has_external_icon"))
        {
        //dojo.place('<img src="/media/retros/img/dojo/Icon_External_Link.png" alt="external link" style="vertical-align: top; "/>',
        //  anchor, "after");
        //debugger;
        var sp = document.createElement('span');
        sp.setStyle({white_space: 'nowrap'});
        anchor.parentNode.replaceChild(sp, anchor);
        sp.appendChild(anchor);
        //Element.extend(sp);
        //anchor.insert(anchor);
          //after: '<img src="' + g_external_link_icon_url + '" alt="external link" style="vertical-align: top; "/>'
        anchor.insert({
          after: new Element('img', {src: g_external_link_icon_url, alt: "external link", style: "vertical-align: top; "})
          });
        //var sp = dojo.create("span", { style: "white-space: nowrap" });
        //dojo.place(sp, anchor, "replace");
        //dojo.place(anchor, sp, "first");
        //dojo.place('<img src="' + g_external_link_icon_url + '" alt="external link" style="vertical-align: top; "/>', anchor, "after");
        anchor.addClassName("has_external_icon");
        }
      }
    });
  }
//dojo.addOnLoad(function() { fix_external_anchors(); });
document.observe("dom:loaded", function() { fix_external_anchors(); });


// From <http://joegornick.com/2007/10/29/extending-prototype-copy-to-clipboard/>
// Usage: $('myinput').copyToClipboard();
Object.extend(Form.Element.Methods, 
{
  // Uses a combination of:
  // http://www.jeffothy.com/weblog/clipboard-copy/
  // http://www.rodsdot.com/ee/cross_browser_clipboard_copy_with_pop_over_message.asp
  copyToClipboard: function(element, swf)
  {
    if (Object.isUndefined(swf))
    {
      swf = '/images/_clipboard.swf';
    }
    if (window.clipboardData)
    {
      window.clipboardData.setData("Text", element.getValue());
    }
    else
    {
      if(!$('flash_clipboard_container'))
        $(document.body).insert(new Element('div', {id: 'flash_clipboard_container'}));
 
      var content = encodeURIComponent(element.getValue());
 
      if (!(typeof SWFObject == "undefined"))
      {
        var so = new SWFObject(swf, 'copy_contents', '0', '0', '4');
        so.addVariable('clipboard', content);
        so.write('flash_clipboard_container');
      }
      else
      {
        $('flash_clipboard_container').update(
          new Element('embed',
            {width: 0,
            height: 0,
            flashvars: 'clipboard=' + content,
            quality: 'high',
            name: 'copy_contents',
            id: 'copy_contents',
            src: swf,
            type: 'application/x-shockwave-flash'})
        );
      }
    }
  }
}
);
Element.addMethods();

