$(document).ready(function() {
  
  $("#persondd").change(function(){
    var form = $('#personform');
    var value = $(this).val();
    
    if(value.indexOf('#') == 0) {
      location.href = value;
    }
    else {
      form.submit();
    }
  });

  //
  //$('#illustration-main ul').slideDown(1500);

  //
  var total = 0;
  var padding = 0; // 24
  var maxwidth = $('#subnav').width();

  $('#subnav img[@src*="_off"]').hover( 
    function () { $(this).attr("src", $(this).attr("src").replace("_off", "_on") ) },
    function () { $(this).attr("src", $(this).attr("src").replace("_on", "_off") ) }
  );

  $('#subnav>ul>li').each(function(i){  
    if(total>=maxwidth) { $(this).find('ul').css({left : maxwidth}); }
    else { $(this).find('ul').css({left : total}); }
    total += $(this).width() + padding;
  });

  $('#subnav>ul>li').hover(
    function() { $(this).not('.selected').css({ background: "#e7e6e6" }) },
    function() { $(this).not('.selected').css({ background: "transparent" }) }
  );
  
  $('#subnav>ul>li').hover(
    function() { $(this).not('.selected').find('a:first').css({ color: "#000" }) },
    function() { $(this).not('.selected').find('a:first').css({ color: "#000" }) }
  );
  
  $('#subnav>ul li').hover(
    function() { $(this).children('ul').show(); },
    function() { $(this).children('ul').hide(); }
  );

  $('A[href="mailto:etunimi.sukunimi@merilampi.com"]').each(function() {
    var source = $(this).attr('title') ? $(this).attr('title') : $(this).text();
    var user = source.split(' ').join('.').toLowerCase()
      .replace(/ä/g, 'a').replace(/ö/g, 'o').replace(/å/g, 'a').replace(/é/g, 'e');
    $(this).attr('href', $(this).attr('href').replace(/^mailto:[^@]+/, 'mailto:' + user.replace(/\.\./g, '.')));
    if (! $(this).hasClass('keep-text'))
      $(this).text($(this).attr('href').replace('mailto:', ''));
  });

  $('A[href="mailto:etunimi.sukunimi@merilampi.com"]').click(function(e) {
    e.preventDefault();
    document.location.href = $(this).attr('href');
  });

});