
$(document).ready(function() {

  // load flashplayer
  $("a.flowplayer").flowembed("/images/flash/FlowPlayerDark.swf",  {width:480, height:412});  

  // load lexikon tooltip
  //$('a.tips').cluetip({ 
  //      dropShadow: false,
  //      positionBy: 'mouse',
  //	ajaxCache: true, 
  //	sticky: true,
  //	closePosition: 'title',
  //	activation: 'click',  
  //	closeText: "&times;"
  //});

  // my tooltip
  $('a.tips').click(function () { tooltip(this); return false; });

  $('a.lexikon').click(function () { lexikon(this); return false; });

  // external links
  $("a[rel='ext']").each( function () { $(this).attr("target", "_blank"); });

  // lexikon pops
  $('a', $('#lettertab')).click(function () { 
      var l = $(this).attr('href');
      $('div.lexikon').hide();
      $(l).show();
  });

  var url = window.location.hash; //document.URL;
  if (url.match(/(#letter_\w)$/)) {
    $(RegExp.$1).show();
  } else if (url.match(/(#entry_\d+)$/)) {
    id = RegExp.$1;
    $(id).parents('div.lexikon').show();
    //alert(id + " a.tips");
    //$(id + " a.tips").mouseover();
    $(id).addClass('active');
  } else {
    $('#letter_A').show();
  }
  
});


// my tooltip
function tooltip(e) {

  // get rel and url from rel-attribute
  var rel = $(e).attr("rel");

  // save parent li element
  var parent = $(e).parents('li');

  // remove all open tooltips
  $('.tooltip').remove();//hide();

  // fetch tooltip
  if (rel) {

    $.ajax({

      url: rel,
      success: function(msg) {
	  // id of current tip
	  var id = $(msg).attr("id");
	  // append to parent
	  parent.append(msg);
	  // add click handler to close button
	  $('#close-' + id).click( function() { $('#' + id).hide(); return false; } );
	  // add click handler to all refs
	  $('a.refs').click(function () { tooltip(this); return false; });
	  // ext links
	  $("a[rel='ext']").each( function () { $(this).attr("target", "_blank"); });

	}
    });

    return false;

  }
}

// within the lexikon just toggle on and off
function lexikon(e) {

  $('div.lexikondesc').hide();
  $(e).next('div').show();
  $('a.refs').click(function () { 
		      var href = $(this).attr('href');
		      if (href.match(/#entry_(\d+)/)) {  pop_lexikon(RegExp.$1); }
		      return false; });
  return false;
}

function pop_lexikon (id) {
     $('div.lexikon').hide();
     $('#entry_' + id).parents('div.lexikon').show();
     $('#entry_' + id + ' div.lexikondesc').show();
 }
