function loadAjaxPage(url) {
    
    // AJAX call
    $.ajax({
        type: "POST", url: url, data: { ajax: true },
        beforeSend: function(XMLHttpRequest) { $("#loading").show(); }, // show loading
        complete: function(XMLHttpRequest, textStatus) { $("#loading").hide(); }, // hide loading
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            if (textStatus == 'error') { $(this).die('click'); }
        },
        success: function(html) 
        {
            $("#section").slideUp("fast");
            $("#section").html(html); // zobrazime ziskane HTML v "content" div
            $("#section").slideDown("slow"); // pouzijeme "slide down" efekt

            document.title = $('#ajax_title').text();
            //$('#flag_cs').attr('href', $('#ajax_cs').text());
            //$('#flag_en').attr('href', $('#ajax_en').text());
            
            // po nacteni stranky AJAXem musime obnovit eventy na te nove nactene casti stranky
            reinit();
        }
    });
}

function loadAjaxInline(url) 
{
  // AJAX call
  $.ajax({
      type: "POST", url: url, data: { ajax: true },
      beforeSend: function(XMLHttpRequest) { }, // show loading
      complete: function(XMLHttpRequest, textStatus) { }, // hide loading
      error: function(XMLHttpRequest, textStatus, errorThrown) {
          if (textStatus == 'error') { $(this).die('click'); }
      },
      success: function(html) 
      {
        $("#basket").html(html); // zobrazime obnoveny kosik
      }
  });
}

function bindClickEvents() 
{
  // normal AJAX links - page changing
  $("a:not(.ajax_link, .prevent_click_link)").live('click', function(event) 
  {
    var a = new RegExp('/' + window.location.host + '/');
    if(a.test(this.href)) 
    {
      event.preventDefault();
      var url = $(this).attr('href');
      loadAjaxPage(url);
    }     
  });
  
  // loading only some info - not changing page
  $(".ajax_link").live('click', function(event) 
  {
    var a = new RegExp('/' + window.location.host + '/');
    if(a.test(this.href))
    {
      event.preventDefault();
      var url = $(this).attr('href');
      loadAjaxInline(url);
    }     
  });
  
  // prevent click event
  $(".prevent_click_link").live('click', function(event) { event.preventDefault(); });
}

function bindFormSubmitEvents() 
{
  $("#form").submit(function() {

      var serialized = $("#form").serialize();
      serialized += '&ajax=true';

      $.ajax({
          type: "POST", url: $('#form').attr('action'), data: serialized,
          beforeSend: function(XMLHttpRequest) { },
          complete: function(XMLHttpRequest, textStatus) { },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
              if (textStatus == 'error') { $(this).unbind('submit'); }
          },
          success: function(data) {
              // zobrazit vysledek
              $("#content").html(data);
              // po nacteni stranky AJAXem musime obnovit eventy na te nove nactene casti stranky
              reinit();
          }
      });

      return false;
  });
}

function reinit() 
{
  shows_events();
  init_social();
  shows_info_display();
}

function shows_events()
{
  $(".events_icon").tooltip({ effect: 'toggle', position: 'bottom center'});
  $(".like_icon").tooltip({ effect: 'toggle', position: 'bottom center'});
}

function shows_info_display()
{
  $(".shows_info").hide();
  $(".shows").live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') $(this).children(".shows_info").show();
    else $(this).children(".shows_info").hide();
  });
}

function init_social()
{
  // addthis
  var addthis_config = { "data_track_clickback": true }
  var script = 'http://s7.addthis.com/js/250/addthis_widget.js#pubid=apatheia&domready=1';
  if ( window.addthis ) { window.addthis = null; }
  $.getScript( script );
  // facebook - reinicializace vsech XFBML po nacteni stranky AJAXem
  fbAsyncInit();
  // google+1
  gapi.plusone.go();
}

function init_slider()
{
  // banner scrolling
  $(".scroll").scrollable({ circular: true }).autoscroll({ autoplay: true, interval: 5000 });
  // hide comments on load
  $(".comment").hide();
  // fade in/out comments on hover
  $(".scroll").hover(function() { $(".comment").fadeIn(500); }, function() { $(".comment").fadeOut(100); } );
}                                 

/**
 * Nacte asynchronne Facebook JavaScript SDK a inicializuje vsechny XFBML 
 */ 
function load_fb_sdk()
{
  window.fbAsyncInit = function() {
    if(window.FB) FB.init({appId: '179231168800501', status: true, cookie: true, xfbml: true});
  };
  (function() 
  {
    var e = document.createElement('script'); 
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
}

/**
 * element_button zobrazi/skryje element v pripade events
 */ 
function bind_display_switching(element_button, element, events)
{
  $(element_button).live(events, function() 
  {
    if ($(this).parent().children(element).css('display') == 'none') $(this).parent().children(element).show();
    else $(this).parent().children(element).hide();
  });
}

function init_comments()
{
  bind_display_switching(".comments_button", ".comments", "click");
}

$(document).ready(function() 
{
  // AJAX links
  bindClickEvents();
  // nacte facebook JS SDK
  load_fb_sdk();
  // addthis, facebook
  init_social();
  // shows info
  shows_info_display();
  // show/hide comments
  init_comments();
  // slider
  init_slider();
});
