/*
Author:      Nick Hoffman
Date:        2010-03-26
Description:

This script loops through every link on the page and sets its "target" attribute to "_blank" if the link's "href" attribute matches the specified regex.

This is needed so that links to videos open in a new window.
*/

/* function toggle_lightbox_with(url) {{{
 *
 * Description:
 * Toggles whether or not the lightbox is visible, and sets the "src"
 * attribute of the iframe in the lightbox to the specified URL.
 *
 * Arguments:
 *  url   The URL to set the iframe's "src" attribute to.
 *
 * Returns:
 *  Nothing
 *  
 */
function toggle_lightbox_with(url) {
  var lightbox_id = 'lightbox-contents';
  var lightbox    = $(lightbox_id);
  var overlay     = $('lightbox-overlay');
  var iframe      = $$('#' + lightbox_id + ' iframe')[0];

  if (!lightbox || !overlay || !iframe)
    {return false;}

  if (lightbox.visible()) {
    lightbox.hide();
    overlay.hide();
  }
  else {
    lightbox.show();
    overlay.show();
  }

  //iframe.writeAttribute('src', url);
  iframe.src = url;
  return false;
} // }}}

document.observe("dom:loaded", function() {
  var links               = $$('a');
  var lightbox_link_regex = /myhosting\.com\/hosting-tutorials\//;

  links.each(function(link, index) {
    if (link.href.match(lightbox_link_regex)) {
      var link_href = link.href;

      link.onclick = function() {
        toggle_lightbox_with(link_href);
        return false;
      };
    }
  });
});

