function session_keepup() {
  window.setTimeout(function(){
    if (document.cookie.indexOf('ISENTER=true') >= 0) {
      $.get('/?session_keepup', function(data) {
        session_keepup()
      });
    }
  }, 5*60*1000);
}

$(document).ready(function(){

  $('form[action="/null"]').attr('action', '');

  $('#login label>input').focus(function(){
    $(this).parent('label').find('span').hide();
  }).blur(function(){
    if ($(this).val() == '')
      $(this).parent('label').find('span').show();
  }).each(function(){
    if ($(this).val() != '')
      $(this).parent('label').find('span').hide();
  });

  $('.date').each(function(){
    if ($.trim($(this).text()) == '')
      $(this).hide();
  });

  //$("div#leftbar ul:first>li:has(ul)>a").after('<img src="/images/arrow.gif" class="arrow" />');
  var url = window.location + '';
  url = url.replace(/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/, '$9');
  if (url != '/') {
    var cnt = 0;
    $('#menu ul li>a[href="'+url+'"], a#contact[href="'+url+'"]').first().each(function(){
      $(this).addClass('active');
      cnt++;
    });
    if (cnt == 0) {
      url = url.replace(/^\/([^\/]*)(\/.*)?$/g, '$1');
      $('#menu ul li>a[href^="/'+url+'"], a#contact[href^="/'+url+'"]').last().addClass('active');
    }
  } else
    $('#menu ul:first li>a[href="/"], a#contact[href="/"]').first().addClass('active');
  //$("#menu ul:first li:has(ul)").not(':has(.active)').children("ul").not(":has(.active)").hide();
  //$("#menu ul:has(.active)").show(); //:has(.active)
  $('#menu ul li:has(.active)>a').addClass('active');
  //$('#menu ul ul:last').css({right: 0}); // az utolsó menüpontot jobbra zárjuk
  $('#menu ul li:first').addClass('first');
  
  $('img[alt^="http://www.youtube.com/"]').each(function(){
    $(this).replaceWith('<object width="' + $(this).attr('width') + '" height="'+$(this).attr('height')+'"><param name="movie" value="' + $(this).attr('alt') + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + $(this).attr('alt') + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + $(this).attr('width') + '" height="' + $(this).attr('height') + '"></embed></object>');
  }); 

  $('a[href^="http"]:not([href^="http://www.kvartelyhaz.hu"])').attr('target', '_blank').filter(':not([title])').each(function(){
    $(this).attr('title', $(this).attr('href'));
  });

  /* ajax-post */
  function ajax_post() {
    $('form.ajax-post:first').each(function(){
      $form = $(this);
      $text = $form.next('p.resize');
      $text.append('... <span></span>');
      $.ajax({
        url: window.location.pathname + '?ajax',
        type: 'post',
        dataType: 'json',
        data: $form.serialize(),
        cache: false,
        success: function(data, textStatus, XMLHttpRequest) {
          try {
            if (data.content == '1') {
              $text.find('span').text('OK');
              $text.slideUp(function(){
                $form.remove();
                if ($('form.ajax-post:first').length == 0) {
                  window.location.href = window.location.href;
                } else {
                  setTimeout(ajax_post, 1000);
                }
              });
            } else
              throw data.content;
          } catch(e) {
            $text.find('span').text('hiba');
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          $text.find('span').text('hiba');
        }
      });
      
    });
  };
  setTimeout(ajax_post, 1000);
  
  /* datepicker */
  $('.datepicker:not(#month)').datepicker();
  var calendarCache = false;
  $('#month.datepicker').datepicker({
    onSelect: function(dateText, inst) {
      $(this).next('div:first').slideUp('normal', function(){
        var $$ = $(this);
        $$.empty();
        if (calendarCache === false || typeof calendarCache.content[dateText] == 'undefined') {
          $.ajax({
            url: '/?ajax=calendar&date=' + dateText,
            type: 'get',
            dataType: 'json',
            success: function(data) {
              calendarCache = data;
              try {
                $$.append(calendarCache.content[dateText]);
                $$.slideDown('normal');
              } catch(e) {}
            }
          });
        } else {
          try {
            $$.append(calendarCache.content[dateText]);
            $$.slideDown('normal');
          } catch(e) {}
        }
      });
    }
  });
  
  /* autocomplete */
  $('input.autocomplete').each(function(){
    var $$ = $(this);
    $$.autocomplete({
      delay: 300, 
      source: '?autocomplete&field='+$$.attr('name')
    });
  });

  session_keepup();
  
  if ($.browser.msie && parseFloat($.browser.version) < 7) {
    $('<div/>', {
      css: {
        display: 'none',
        margin: 0,
        textAlign: 'center'
      }
    }).addClass('error'
    ).html('A Ön által használt böngésző elavult, verziója nem támogatott! (Microsoft Internet Explorer '+$.browser.version+') Kérjük frissítse: <a href="http://www.browserchoice.eu/BrowserChoice/browserchoice_hu.htm" target="_blank">támogatott böngészők</a>'
    ).prependTo($('body')).slideDown();
  } 
});
