$(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 ( $.browser.msie && $.browser.version == 6.0 )
    setupMenu();

  if ( $.fancybox )
    setupFancybox();
  
});

function setupMenu() {
  
  $('#header li').hover(
    function() {
      $(this).children('ul').show();
      $(this).addClass('headerhover');
    },
    function() {
      $(this).children('ul').hide();
      $(this).removeClass('headerhover');
    }
  );
  
  $('#header li li').hover(
    function() {
      $(this).addClass('subheaderhover');
    },
    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') );
    
  });
  
}