function setCookie(c_name, value, expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
// visszadobjon a főoldalra
var url = window.location+'';
url = url.replace(/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/, '$8');

if (url.substr(0,3) != '/#/' && url != '/') {
  window.location.href = '/#'+url;
}


function sqr(x) {
  return x*x;
}
function sqrt(x) {
  return Math.pow(x, 0.5);
}

var loading = false;
var animate = false;
var speed = 200;
function fillBody(anchor) {
  if (!loading && !animate) {
    activeMenuItem(anchor);
    $('#site_loading').fadeOut(speed, function(){
      $('#main').slideDown(speed);
      initFunctions.execute();
      $.trackPageview(anchor);
    });
  }
}

$(document).ready(function(){

  $.history.init(function callback(hash){
    var $$url = window.location.href.replace(/^.*#/, '');
    $$url = hash;
    
    //$body_container.stop(true, true);
    loading = true;
    animate = true;
    var $$ = $(this);
    //$('#quick-menu>.sub').animate({width: 'hide'}, 400);
    $('#main').slideUp(speed, function(){
      $('#site_loading').fadeIn(speed);

      // ajax kérés indítása
      $.ajax({
        type: 'GET',
        url: $$url + ($$url.indexOf('?') >= 0 ? '&' : '?') + 'ajax&rand=2344',
        dataType: 'text',
        data: '',
        timeout: 20000,
        cache: false, // don't cache the result
        contentType: "application/json", //tell the server we're looking for json

        beforeSend: function(x) {
          if(x && x.overrideMimeType) {
            x.overrideMimeType("application/json;charset=UTF-8");
          }
        },

        success: function(json){
          try {
            eval('json = ' + json);
            try {
              $('title').text(json.title);
            } catch (e) {
              document.title = json.title;
            }
            try {
              $('meta[name="Keywords"]').attr('content', json.keywords);
            } catch (e) {
            }
            $('#main').html(json.content);
          } catch (e) {
            $('#main').html('<pre class="error">Hiba az oldal betöltése közben!</pre>');
          }
          loading = false;
          fillBody($$url);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          try {
            eval('json = ' + XMLHttpRequest.responseText);
            try {
              $('title').text(json.title);
            } catch (e) {
              document.title = json.title;
            }
            try {
              $('meta[name="Keywords"]').attr('content', json.keywords);
            } catch (e) {
            }
            $('#main').html(json.content);
          } catch (e) {
            $('#main').html('<pre class="error">Hiba az oldal betöltése közben (' + textStatus + ')!</pre>');
          }
          loading = false;
          fillBody($$url);
        }
      });

      animate = false;
      fillBody($$);

      $$.blur();
    });
    return false;
  });
    
  // menü animáció
  initFunctions.add(function() {

    // internal links
    $('#container a:not([href^="http"]):not([href^="mailto"]):not(.lightbox):not([href^="/misc"])').unbind('click').click(function(){
      //window.location.href = '/#'+$(this).attr('href');
      $.history.load($(this).attr('href'));
      return false;
    });

  });
  
});

