var path = '/';

/*
 * showOverlay() - Zobrazi informacni zpravu v nove vrstve
 * @param title    Titulek vrstvy
 * @param text     Text zpravy (muze obsahovat HTML tagy)
 * @param width    Sirka okna se zpravou (nepovinne)
 **/
function showOverlay(title, text, width, buttons) {
  if(!$('#overlay').length) {
    // generate HTML code
    var html = '<div id="overlay">'+
               '<div class="lightbox">'+
                  '<div class="top"><b></b></div>'+
                  '<div class="inside">'+
                    '<h2></h2>'+
                    '<div class="msg">'+
                      '<p></p>'+
                    '</div>'+
                    '<a class="close xclose" href=""></a>'+
                    '<div class="clear"></div>'+
                  '</div>'+
                  '<div class="bottom"><b></b></div>'+
                '</div>'+
                '<div class="lightbox_overlay"></div>'+
                '</div>';

    $('body').append(html);

    $('#overlay').click(function() {
      hideOverlay();
    });

    $('#overlay .lightbox').click(function(event) {
      event.stopPropagation();
      return;
    });
  }

  // add actions to close buttons
  $('#overlay a.xclose').click(function(event) {
    hideOverlay();
    event.stopPropagation();
    return false;
  });

  // reset css
  $('#overlay .inside').css('height', 'auto');
  $('#overlay .lightbox').css('margin-top', 0);

  // show title & text
  $('#overlay .inside h2').text(title);
  $('#overlay .inside .msg').html(text);

  // show overlay
  $('#overlay').show();

  // calculate position of the box
  if(!$('#overlay .lightbox').data('defaultWidth')) {
    $('#overlay .lightbox').data('defaultWidth', $('#overlay .lightbox').width());
  }

  if(width) {
    // user defined width
    $('#overlay .lightbox').css('width', width);
    $('#overlay .lightbox').css('margin-left', -Math.round(width/2));

  } else {
    // default width
    $('#overlay .lightbox').css('width', '');
    $('#overlay .lightbox').css('margin-left', -Math.round($('#overlay .lightbox').data('defaultWidth')/2));
  }

  height = getWindowSize().height - 150;
  if($('#overlay .lightbox').height() > height) {
    $('#overlay .inside').css('height', height);
    $('#overlay .inside .msg').css('height', height - 120);
    $('#overlay .lightbox').css('margin-top', -Math.round(height/3));
  }

  return false;
}

function showOverlayContent(title, url, width, height, buttons) {
  if(!buttons)
    buttons = new Array({
                  'text': 'OK',
                  'href': '',
                  'close': true
              })
  // zobrazeni overlay (zatim prazdne)
  showOverlay(
    title,
    loader(),
    width,
    buttons
  );

  // nacteni dat
  $.get(url, null, function(data) {
    $('#overlay .inside .msg').html(data);
  });

  if(height == 'max') {
    height = getWindowSize().height - 150;
  }

  $('#overlay .inside').css('height', height);
  $('#overlay .lightbox').css('margin-top', -Math.round(height/3));
  $('#overlay .inside .msg').css('height', height-120);

  return false;
}

function hideOverlay() {
  $('#overlay .inside .msg').html('');
  $('#overlay').hide();
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return {'width': myWidth, 'height': myHeight};
}


function loader() {
  return '<div class="loader"><span>Načítám</span></div>';
}
