$(document).ready(function() {
  // Handler for .ready() called.

  // login
  $(function() {
    $("#login_popover").dialog({
      modal: true,
      autoOpen: false,
      width: 400,
      title: 'Login / Register',
      resizable: false
    });
    $('#login a.open_popover').click(function() {
      $('#login_popover').dialog('open');
      return false;
    });

  });


  // disclaimer
  $(function() {
    $("#disclaimer").dialog({
      modal: true,
      autoOpen: false,
      width: 400,
      title: 'Copyright Disclaimer',
      resizable: false
    });
    $('#disclaimer_link').click(function() {
      $('#disclaimer').dialog('open');
      return false;
    });

  });

  $(function() {
    $("#level_info").dialog({
      modal: true,
      autoOpen: false,
      width: 600,
      title: 'Level Description',
      resizable: false
    });
    $('#level_info_link').click(function() {
      $('#level_info').dialog('open');
      return false;
    });

  });

  $(function() {
    // works not in IE
    if (jQuery.support.htmlSerialize) {
      $('div.popover a').not('[href$=.html]').media( { width: 540, height: 304, autoplay: false, caption: false } );
    }

    $('ul#quicklinks li').each(function() {
      var id = $(".quicklink_id").html();
      var popover = ".popover" + id;
      var popover_link = ".popover_link" + id;
      var title = popover_link + ' span.name';
      $(popover).dialog({
        modal: true,
        autoOpen: false,
        width: 600,
        title: $(title).html(),
        resizable: false
      });
      $(popover_link).click(function() {
        $(popover).dialog('open');
        return false;
      });
    });

    // save parent so we can later reinsert the link
    var last_p = null;
    $('a[target="_top"]').each(function(i, el) {
      last_p = $(el).parent();
    });
    // convert all anchors to media, if possible
    $('td.column2 a').not('[href$=.html]').media(
      { width: 540, height: 304, autoplay: false, caption: false },
      null
      ,function(link, obj, o) {
        if ($(link).attr('target') == '_top')
        {
          $(obj).dialog({
            modal: true,
            autoOpen: false,
            width: 555,
            title: $(link).html(),
            resizable: false
          });
          $(link).click(function() {
            $(obj).dialog('open');
            return false;
          });
          $(link).css('visibility', 'visible');
          last_p.append(link);
        }
      }
    );
  });

  $(function() {
    // works not in IE
    if (jQuery.support.htmlSerialize) {
      $('div.popover a').not('[href$=.html]').media( { width: 540, height: 304, autoplay: false, caption: false } );
    }

    $('ul#quicklinks li').each(function() {
      var id = $(".quicklink_id").html();
      var popover = ".popover" + id;
      var popover_link = ".popover_link" + id;
      var title = popover_link + ' span.name';
      $(popover).dialog({
        modal: true,
        autoOpen: false,
        width: 600,
        title: $(title).html(),
        resizable: false
      });
      $(popover_link).click(function() {
        $(popover).dialog('open');
        return false;
      });
    });
  });
});

