$( document ) .ready( function() {
  onBodyLoad();
} );

var loadingPage = false;
function changeLocation( element, href ) {

  if( typeof href == 'undefined' )
    href = $( element ).attr( 'href' );

  if( href.length == 0 )
    return;

  setLoader( $( '#body_container' ) );

  disableMenu();

  $.post( href, { ajax: 'true' }, function( data ) {

    $( '#body_container' ).html( data );
    //onBodyLoad();
    disableMenu();

    $('a.lightbox').lightBox({
      fixedNavigation:true,
      imageBtnClose:'img/lightbox-btn-close.gif',
      imageBtnPrev:'img/lightbox-btn-prev.gif',
      imageBtnNext:'img/lightbox-btn-next.gif',
      imageLoading:'img/lightbox-ico-loading.gif'
    })

/*
    if( href == 'fotky.php' ) {
      $( '.fot_row' ).click(function() {
        if( typeof getFotos != 'undefined' )
          getFotos( this )
        return false
      })
    }
*/
  } );

}

// -------------------------------------------
// funkce vlozi do pozadovaneho elementu loader
function setLoader( jElement ) {

  $( jElement ).prepend( '<div id="imgloader"><img src="img/loading.gif" /></div>' );

}

// -------------------------------------------
// funcke do onclick soupne pouze return false;
// nebo tam vrati funkci changeLocation( this );
var hrefs = Array();
function disableMenu() {

  if( hrefs.length > 0 ) {
    // vratim atributy odkazu v menu zpet
    var i = 0;
    $( '.proc_ajax' ).each( function() {
      $( this ).attr( 'href', hrefs[i++] )
    });
    hrefs = Array();
  } else {
    // dojde k naplneni pole novymi hodnotami atributu
    $( '.proc_ajax' ).each( function() {
      hrefs[ hrefs.length ] = $( this ).attr( 'href' );
      $( this ).attr( 'href', '' );
    });
  }

}

// --- funkce nastavi na vsechny odkazy nacitani pomoci ajaxu
function setProcAjax() {

  $( '.proc_ajax' ).click( function() {
    changeLocation( this, $(this).attr('href') )
    return false
  })

}

function onBodyLoad() {

  setProcAjax();
  checkUserStatus();

}

new String()
if( !String.last )
  String.prototype.last = function( cnt ) {
    return this.substr( this.length-1-cnt )
  }

// -------------------------------------------------------------------------------------------------
// pomocna funkce made by Fux
var print_r_obj = [];
function print_r(object) {
  var valstr = '';
  var result = ((object == null) ? '<font color="red"><strong>Object is NULL !!!<strong></font>': '');
  var divobj = document.getElementById("div_print_r");
  print_r_obj = object;
  for (curProperty in object) {
    valstr = object[curProperty]+'';
    if ((typeof(object[curProperty]) == 'object') && (object[curProperty] != null) && (valstr.indexOf('[object ') == 0 ) && ( curProperty != 'ownerDocument')) {
      result += curProperty + ' = ' +  '<a href="#" onclick="print_r(print_r_obj[\''+curProperty+'\'])">' + valstr + '<a><br>\n';
    } else {
      result += curProperty + ' = ' + valstr + '\n';
    }
  }
  if (divobj == null) {
    document.body.innerHTML += '<div id="div_print_r" title="Use doubleclick to close this window." style="background-color:#BBBBFF; left:25px; position:absolute; text-align:left; padding-left:10px; padding-right:10px; top:25px; z-index:100; opacity:0; filter:alpha(opacity=0);" ondblclick="document.getElementById(\'div_print_r\').innerHTML =\'\'; document.getElementById(\'div_print_r\').opacity=\'.9\'; document.getElementById(\'div_print_r\').filter=\'alpha(opacity=90)\';"></div>';
    var divobj = document.getElementById('div_print_r');
  }
  divobj.innerHTML = '<pre>'+result+'</pre>';
  divobj.style.opacity = '.9';
  divobj.style.filter  = 'alpha(opacity=90)';
}