$(document).ready(function() {
  
  clearOnClick( $('#footer .text') );
  clearOnClick( $('#q') );
  clearOnClick( $('#input #sendername') );
  clearOnClick( $('#input #sendermail') );
  clearOnClick( $('#input #subject') );
  clearOnClick( $('#input #mailbody') );
  clearOnClick( $('#email') );

  $('.body img').each( function( i, img ) {
    if ( $( img ).attr('alt') && !$( img ).attr('title') )
      $( img ).attr('title', $( img ).attr('alt') );
  });

  if ( !accessible )
    setupMenu();
  else {
    $('#header ul#nav, #subheader').toggle( navigationstatus == 'show' );
  }
  
  $('#footer #newsletter .button').hover( function() {
    $(this).attr('src', 'images/imm_button_hover.png');
  },
  function() {
    $(this).attr('src', 'images/imm_button.png');
  });
  
  if ( !accessible && $.fancybox )
    setupFancybox();
  
  if ( $('#infobox').length > 0 )
    setupInfobox();
  
  $('#togglenavigation').click( function( e ) {
    e.preventDefault();
    
    if ( navigationstatus  == 'show') {
      
      $(this).text( navigationshow );
      navigationstatus = 'hide';
      
    } else {
      
      $(this).text( navigationhide );
      navigationstatus = 'show';
      
    }
    
    $.cookie('navigationstatus', navigationstatus, { expires: 60, path: '/' });
    $('#header ul#nav, #subheader').toggle( navigationstatus == 'show' );
    
  });
});

function setupInfobox() {
  
  var maxheight = 0;
  $('#infobox .column .info').each( function() {
    
    var height = $(this).height();
    
    if ( height > maxheight )
      maxheight = height;
    
  });
  
  $('#infobox .column .info').css('height', maxheight + 'px');
  
}

function setupMenu() {
  
  $('#header li').bind('mouseenter focusin', function(e) {
    $(this).addClass('headerhover');
  });
  
  $('#header li').bind('mouseleave focusout', function() {
    $(this).removeClass('headerhover');
  });
  
  $('#header li li').bind('mouseenter focusin', function() {
    $(this).addClass('subheaderhover');
  });
  
  $('#header li li').bind('mouseleave focusout', function() {
    $(this).removeClass('subheaderhover');
  });
  
}

function setupFancybox() {

  if ( obsoleteBrowser )
    $("a#browserAlert").fancybox({
      type: 'iframe',
      titlePosition: 'inside',
      titleFormat: setupTitleBar,
      showCloseButton: false,
      padding: 0,
      margin: 0,
      width:  700,
      height: 400
    });
  
  $(".gallery a").fancybox({
    titlePosition: 'inside',
    padding: 30,
    titleFormat: setupTitleBar,
    showCloseButton: false,
    onComplete: function() {
      
      if ( $('.titlebar').height() > 50 )
        $('#fancybox-inner').css('top', ( $('.titlebar').height() - 20 ) + 'px' );
      
    }
  });
  
  function setupTitleBar( title ) {
    
    var closelement = $('<a/>', {
      'href': '#',
      'class': 'titleclose',
      'text': 'Ablak bezárása',
      'click': function() {
        $.fancybox.close();
        return false;
      }
    });
    
    return $('<div/>', {
      'class': 'titlebar',
      'text': title
    }).append( closelement );
    
  }

}

function clearOnClick( elem ){
  
  elem.focusin( function() {
    
    if ( $(this).attr('origval') && $(this).attr('origval') != $(this).val() )
      return;
    
    $(this).attr('origval', $(this).val() );
    $(this).val('');
    
  }).focusout( function() {
    
    if ( !$(this).val() )
      $(this).val( $(this).attr('origval') );
    
  });
  
}
