// drop-down menu
var DDM = function(container, options) {
  options = options || {} ;

  var timeout = options.timeout || 500,
  closetimer = null,
  ddmenu_item = null ;  

  function canceltimer () {
    if (closetimer) {
      window.clearTimeout(closetimer);
      closetimer = null;
    }
  }
  function close () {
    if (ddmenu_item)
      ddmenu_item.css('visibility', 'hidden') ;
  }
  function open () {
    canceltimer();
    close();
    ddmenu_item = $(this).find('ul:first').css('visibility', 'visible');
  }
  function timer () {
    closetimer = setTimeout(close, timeout);
  }

  container.children('li').mouseover( open ).mouseout( timer ) ;
  $(document).click( close ) ;
} ;

var U = {} ;

// setup
$( function() {
     // $.ajaxSetup({cache: false, type: 'POST'}) ;

     var ddm = new DDM($('#menu')) ;
     $.extend(U,
	 { opendiv: function(node, data) {
	     var meta = data || node.metadata(),
	     id = meta.id,
	     path = ( meta.path || '' ).split( '' ) ;
	     
	     if ( meta.element ) {
	       node = meta.element ;
	     }
	     else if (id) {
	       node = $('#' + id) ;
	     }
	     else {
	       $.each( path,
		       function(i, d) {
			 switch (d) {
			 case 'n':
			   node = node.next() ;
			   break;
			 case 'u':
			   node = node.parent() ;
			   break;
			 case 'd':
			   node = node.children().eq(0) ;
			   break;
			 }
		       } ) ;
	     }
	     node.animate({height: 'toggle'}, meta.speed || 'slow') ;
	   }
	 }) ;
     // open and close when clicking on heading
     $('a.collapse').live(
       'click',
       function(e, data) { 
	 e.preventDefault() ;
	 U.opendiv( $(this), data ) ;
       } ) ;

     $('#content form input:text').eq(0).focus() ;
     $('div.flash a').media( { width: 200, height: 200 } ) ;
     $('.linkm a').each( function () {
			   var a = $(this), href = a.attr('href') ;
			   if (href.match(/kno[.]gridrunner/)) {
			     $.ajax( { url: 'migrate_link',
				       data: { link: href },
				       success: function(x) {
					 x ? a.attr('href', x)
					   : alert('Niet-gevonden oude link: ' + href) ; }
				     } ) ;
			   }
			 }) ;
   } ) ;

