$(document).ready(function () {
  $('#page_menu li.has-submenu').live('mouseover', function (event) {
    $(this).addClass('mouse-is-over');
    i = 0;
    par = $(event.originalTarget).parent();
    while (par != null && par.length > 0) {
      if($(par).hasClass('submenu'))
        return;
      if($(this).get(0) == $(par).get(0)) {
        break;
      }
      par = $(par).parent();
      i++;
      if(i > 100) {
        return;
      }
    }
    s = $($(this).children()).filter('.submenu-container');
    if(s.data('shown'))
      return;
    pos = $(this).position();
    pos.top += $(this).height();
    $(this).oneTime(10, function () {
      submenu_height = $($(this).children()).filter('.submenu-container').outerWidth();
      max_pos = $('#page_menu').offset().left + $('#page_menu').width()
      if(pos.left + submenu_height > max_pos) {
        pos.left = max_pos - submenu_height;
      }
      if($(this).hasClass('mouse-is-over') && !s.hasClass('to-be-shown'))
        s.addClass('to-be-shown').css({position: 'absolute'}).fadeTo(0, 1).css(pos).slideDown('slow', function() {$(this).data('shown', true).removeClass('to-be-shown');});
    })
  });
  $('#page_menu li.has-submenu').live('mouseout', function (event) {
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(200, function() {
      s = $($(this).children()).filter('.submenu-container');
      if(!$(this).hasClass('mouse-is-over') && !s.hasClass('to-be-hidden')) {
        s.addClass('to-be-hidden').fadeOut('slow', function () {$(this).data('shown', false).removeClass('to-be-hidden')});
      }
    })
  });
  /* menu autoresizing */
  if((container = $('.main_menu').filter(":eq(0)")).length > 0) {
    if(container.width() < $('.page_menu').width()-41) // 40 = margin-left + margin-right of .main_menu + 1px?
      total_w = $('.page_menu').width()-41; // IE6 must rely on menu ul container
    else
      total_w = container.width();
    children_width_new = container.getChildrenWidth();
    addPadding = Math.floor((total_w - children_width_new) / (container.children().length * 2));
    children = container.children();
    children_width_new = 0;
    for(i=0;i<children.length;i++) {
      $(children[i]).find('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          padd = {paddingLeft: addPadding, paddingRight: addPadding};
          $(this).css(padd);
          children_width_new += $(children[i]).outerWidth();
        }
      });
    }
    children_width_new = container.getChildrenWidth();
    to_add = total_w - children_width_new;
    child = children[0];
    paddingPlace = 'paddingLeft';
    while(to_add > 0 && child) {
      $(child).children().filter('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          if(to_add >= 1) {
            $(this).css(paddingPlace, addPadding + 1);
          }
          to_add --;
        }
      });
      child = $(child).next();
      if(child == null || typeof child == 'object' && typeof child.length != 'undefined' && child.length == 0) {
        paddingPlace = 'paddingRight';
        child = children[0];
      }
    }
  }
  $(window).scroll(function () {
    $(window).resize();
  });
  $(window).resize(function () {
    lc = $('.column-left');
    lc.height(lc.parent().height() - 5);
  })
  $('BODY').onImagesLoad(function () {
    $(window).resize();
  });
  $(window).resize();
  $('.replace-emails').replaceEmails();
});
jQuery.extend(jQuery.fn, {
  getChildrenWidth: function () {
    children = $(this).children();
    total = 0;
    for(i=0;i<children.length;i++) {
      total += $(children[i]).width()+parseInt($(children[i]).css('marginLeft'))+parseInt($(children[i]).css('marginRight'))+parseInt($(children[i]).css('paddingLeft'))+parseInt($(children[i]).css('paddingRight'));
    }
    return total;
  },
  replaceEmails: function () {
    $(this).each(function () {
      href = $(this).attr('href');
      if(href.length > 0) {
        $(this).attr('href', href.replace(/ *\( *dot *\) */, '.').replace(/ *\( *at *\) */, '@'));
      }
      $(this).attr('innerHTML', $(this).attr('innerHTML').replace(/ *\( *dot *\) */, '.').replace(/ *\( *at *\) */, '@'));
    })
  }
});

