$(document).ready(function() {
  $('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;
      }
    }
    pos = $(this).offset();
    pos.top += $(this).height();
    pos.left += parseInt($(this).css('marginLeft'));
    $(this).oneTime(10, function () {
      submenu_height = $($(this).children()).filter('.submenu-container').outerWidth();
      max_pos = $('.main_menu').offset().left + $('.main_menu').width()
      if(pos.left + submenu_height > max_pos) {
        pos.left = max_pos - submenu_height;
      }
      if($(this).hasClass('mouse-is-over'))
        $($(this).children()).filter('.submenu-container').css({position: 'absolute'}).css(pos).slideDown('slow');
    })
  });
  $('li.has-submenu').live('mouseout', function (event) {
    $(this).removeClass('mouse-is-over');
    $(this).oneTime(200, function() {
      if(!$(this).hasClass('mouse-is-over')) {
        $($(this).children()).filter('.submenu-container').fadeOut('slow');
      }
    })
  });
  $('.replace-email').each(function () {
    $(this).attr('href', unescape($(this).attr('href')).replace('+(at)+', '@').replace('+(dot)+', '.'));
    $(this).html($(this).html().replace(/[ \+]*\(at\)[ \+]*/, '@').replace(/[ \+]*\(dot\)[ \+]*/, '.'));
  });
  $('.create-link').each(function () {
    $(this).html($(this).html().replace(/[ \+]*\(at\)[ \+]*/, '@').replace(/[ \+]*\(dot\)[ \+]*/, '.'));
    $(this).attr('href', 'mailto:'+$(this).html());
  });
  
/*  
  Date.firstDayOfWeek = 0;
  //Date.format = 'dd.mm.yyyy';
  Date.format = 'dd/mm/yyyy';
  if(jQuery.isFunction(jQuery.fn.datePicker)) {   
    $('.date-pick').datePicker();
  }
*/    
});




//* stretching the main menu
$(document).ready(function() {
  var browser_system  = $.browser.system();
  var browser_product = $.browser.product(); 
  //var browser_mac     = $.browser.mac();
  //var browser_firefox = $.browser.firefox(); 
  if($('.main_menu').length > 0) {
    container = $('.main_menu');
    total_w = container.width();
    children_width_new = getChildrenWidth(container);
    addPadding = Math.floor((total_w - children_width_new) / (container.children().length * 2));
    //children = $(this).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();
        }
      });
    }
    to_add = total_w - children_width_new;
    //alert('to_add:'+to_add);
    //alert('container:'+container.width()+', children width:'+getChildrenWidth(container)+', to_add:'+to_add);
    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 && browser_system=='MacOSX' && browser_product=='firefox'){
          }else
            $(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];
      }
    }
  }
});

function getChildrenWidth(container) {
  children = $(container).children();
  total = 0;
  for(i=0;i<children.length;i++) {
    total += $(children[i]).outerWidth();
  }
  return total;
}
