var timeout    = 500;
var closetimer = 0;
var menuitem   = 0;

function mm_open() {
  mm_canceltimer();
  mm_close();
  menuitem = $(this).find('ul').css('visibility','visible');
}

function mm_close() {
  if (menuitem) menuitem.css('visibility','hidden');
}

function mm_timer() {
  closetimer = window.setTimeout(mm_close, timeout);
}

function mm_canceltimer() {
  if(closetimer){
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}

$(document).ready(function(){
  // site wide navigation menu
  //$("li.current > a").contents().unwrap();
  $("li.current > a").contents().wrap("<span></span>");
  $("li.current > a span").unwrap();
  
  // style for forms
  $('input[type="text"], input[type="password"], textarea').css('color','#666');
  $('input[type="text"], input[type="password"]').focus(function() {
    if (this.value == this.defaultValue) {
      $(this).css({'background-color':'#fff', 'color':'#000'});
      this.value = '';
    } else {
      $(this).css({'background-color':'#fff', 'color':'#000'});
      this.select();
    }
  }).blur(function() {
    if ($.trim(this.value) == ''){
      this.value = (this.defaultValue ? this.defaultValue : '');
      $(this).css({'background-color':'#fff', 'color':'#666'});
    } else {
      $(this).css({'background-color':'#e6e6fa', 'color':'#000'});
    }
  });
  
  // remove defaultvalue in quick search field
  $('#quick_search input[type="submit"]').click(function() {
    var $obj = $('#quick_search input[type="text"]');
    if ($obj.val() == $obj.attr('defaultValue')) {
      return false;
    }
  });
  
  // member menu
  $('#actionmenu > li').bind('mouseover', mm_open);
  $('#actionmenu > li').bind('mouseout', mm_timer);
  
  // member edit profile url
  if (location.search != "") {
    var uri = $.url.param("uri");
    if ((uri !== undefined) || (uri)) {
      $('#url').val(uri).focus();
      $('#url').after('<small style="color: #f00;"><strong>Click update to confirm</strong></small>');
    }
  }
  
  // link icons
  // Add pdf icons to pdf links
  $("a[href$='.pdf']").addClass("icon").addClass("pdf");
  // Add txt icons to document links (doc, rtf, txt)
  $("a[href$='.doc'], a[href$='.txt'], a[href$='.rft']").addClass("icon").addClass("txt");
  // Add zip icons to Zip file links (zip, rar)
  $("a[href$='.zip'], a[href$='.rar']").addClass("icon").addClass("zip");
  // Add cal icons to Calendar file links (ics)
  $("a[href$='.ics']").addClass("icon").addClass("cal");
  // Add email icons to email links
  $("a[href^='mailto:']").addClass("icon").addClass("email");
  //Add external link icon to external links - 
  $('a').filter(function() {
      //Compare the anchor tag's host name with location's host name
      return this.hostname && this.hostname !== location.hostname;
    }).addClass("icon").addClass("external").attr("title", "External link");
  //You might also want to set the _target attribute to blank
  /*
  $('a').filter(function() {
      //Compare the anchor tag's host name with location's host name
      return this.hostname && this.hostname !== location.hostname;
    }).addClass("external").attr("target", "_blank");
  */

  $('div > ul').each(function(){
    if ($(this).children().length == 0) {
      $(this).prev().remove();
      $(this).remove();
      }
    });
});

document.onclick = mm_close;
