function preloadImages() { //v3.0
  var d=document; 
  if(d.images) {
    if(!d.pI)
      d.pI=new Array();
    var i,j=d.pI.length,a=preloadImages.arguments;
    for(i=0; i<a.length; i++)
      if (a[i].indexOf("#")!=0) {
        d.pI[j]=new Image; d.pI[j++].src=a[i];
      }
  }
}

function Functions() { 
  this.funcs = new Array();
} 
Functions.prototype.add = function(f) {
  if (typeof f!= 'function') {
    f = new Function(f);
  }
  this.funcs[this.funcs.length] = f;
  f();
}
Functions.prototype.execute = function() {
  for( var i=0; i<this.funcs.length; i++ ) {
    this.funcs[i]();
  }
}
var initFunctions = new Functions;

function activeMenuItem(url) {
  //$("div#leftbar ul:first>li:has(ul)>a").after('<img src="/images/arrow.gif" class="arrow" />');
  url = url.replace(/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/, '$10');
  url = url.replace(/^\/#/, '');
  if (url == '') url = '/';
  $('#menu li>a.active').removeClass('active');
  if (url != '/') {
    url = url.replace(/^\/([^\/]*)(\/.*)?$/g, '$1');
    $('#menu li>a[href^="/'+url+'"]:last').addClass('active');
  } else {
    //$('#menu li>a:first').addClass('active');
  }
  $("#menu li:has(a.active)>a:not(.active)").addClass('active');
  
  // menu images
  $('#menu li>a:has(img)').each(function(){
    var img = $(this).find('img:first');
    img.css('visibility', 'hidden');
    if ($(this).is('.active')) {
      $('#forgo').css('background-image', "url('" + img.attr('src').replace(/menu_/, 'forgo_').replace(/_hover/, '').replace(/.gif/, '') + '.jpg?cache=1' + "')");
      img.attr('src', img.attr('src').replace(/_hover/, '').replace(/.gif/, '_hover.gif'));
    } else {
      img.attr('src', img.attr('src').replace(/_hover/, ''));
    }
    $(this).css('background-image', "url('" + img.attr('src') + "')").css('width', img.attr('width')+'px');
    if ($(this).find('img:first').attr('src').indexOf('_hover.gif') == -1)
      preloadImages(img.attr('src').replace(/.gif/, '_hover.gif'));
    img.hide();
  }).hover(function(){
    if ($(this).find('img:first').attr('src').indexOf('_hover.gif') == -1) {
      $(this).css('background-image', "url('" + $(this).find('img:first').attr('src').replace(/.gif/, '_hover.gif') + "')");
      $('#forgo').css('background-image', "url('" + $(this).find('img:first').attr('src').replace(/menu_/, 'forgo_').replace(/_hover/, '').replace(/.gif/, '') + '.jpg?cache=1' + "')");
    }
  }, function(){
    $(this).css('background-image', "url('" + $(this).find('img:first').attr('src') + "')");
    if ($('#menu li>a.active img:first').is('[src]')) {
      $('#forgo').css('background-image', "url('" + $('#menu li>a.active img:first').attr('src').replace(/menu_/, 'forgo_').replace(/_hover/, '').replace(/.gif/, '') + '.jpg?cache=1' + "')");
    }
  });
}

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(){

  activeMenuItem(window.location + '');

  initFunctions.add(function(){
    // lightbox;
    $(".lightbox").lightbox();

    // external links
    $('a[href^="http"]:not([href^="http://www.egerszegfesztival.hu"])').attr('target', '_blank').each(function(){
      if ($(this).attr('title') == undefined || $(this).attr('title') == '')
        $(this).attr('title', $(this).attr('href'));
    });

    $('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><param name="wmode" value="transparent"></param><embed src="' + $(this).attr('alt') + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="' + $(this).attr('width') + '" height="' + $(this).attr('height') + '"></embed></object>');
    });

  });
/*  
  $('.datepicker').datepicker({
    onSelect: function(dateText, inst) {
      $(this).next('div:first').hide('normal', function(){
        $(this).empty().append($('<h1/>').append(dateText));
        $(this).show('normal');
      });
    }
  });
*/

  function resizeContent() {
    var dHeight = $(window).height() - $('#container').height();
    $('#container').css('min-height', $(window).height());
    $('#body').css('min-height', ($('#body').height() + dHeight) + 'px');
  }
  $(window).resize(function(){resizeContent()});
  resizeContent();

/*
  preloadImages('/images/forgo_animation.gif');
  $(window).load(function() {
    $('<img/>', {
      id: 'forgo-animation',
      src: '/images/forgo_animation.gif'
    }).appendTo('#header');
  });
*/

  $('input[type="file"]').each(function() {
    var $this = $(this);
    $this.bind('change', function(e){
      if ($this.next('div.selectedfiles').length == 0)
        $('<div/>').addClass('selectedfiles').insertAfter($this);
      var $selectedfiles = $this.next('div.selectedfiles');
      $selectedfiles.empty();
      if (typeof $this[0].files != 'undefined') {
        $.each($this[0].files, function(index, value){
          $selectedfiles.text($selectedfiles.text() + ($selectedfiles.text() == '' ? '' : ', ') + '"'+value.name+'"');
        });
      }
    });
  });

  /* 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);

  $('#programvaltozas').click(function(){
    $(this).fadeOut();
  });
  
  $('#body').onImagesLoad({ 
    selectorCallback: function($selector) {
      //resizeContent();
    }
  });
  
  $('#ajanlo').slider({
    delay: 5000,
    showSpeed: 600,
    hideSpeed: 400
  });

  // Google Analytics
  if (document.cookie.indexOf('ISENTER=true') < 0)
    $.trackPage('UA-16010232-1', {});
    $('a[href$=".pdf"]').click(function(){
      $.trackPageview($(this).attr('href'));
    });

});
