$(document).ready(function()
{   
  // po nacitani webu skontrolujeme hash ci netreba naloadovat nejaky obsah
  if (document.location.hash != "" && document.location.hash.indexOf(".") == -1)
  {  
    // nacitame id sekcie z mr_url
    $.ajax({
    type: "POST",
    url: 'ajax_get_id_section.php?mr_url='+document.location.hash.substring(1),
    success: function(id_section) {
      change_content(id_section, 0); 
    }
    }); 
  }
}
);

function load_content(id_section)
{
  $('#content_top').css('opacity','0');
  change_content(id_section, 0);
}

function change_content(id_section_new, animation_type)
{    
  // najprv nacitame mr_url zobrazovanej sekcie
  $.ajax({
  type: "POST",
  url: 'ajax_get_mr_url.php?id_section='+id_section_new,
  success: function(mr_url) {   
      
  // teraz nacitame obsah
  $.ajax({
  type: "POST",
  url: 'ajax_change_content.php?id_section_new='+id_section_new,
  success: function(msg) {  
    
    // nastavime predoslu a aktualnu sekciu
    // ak sme klikli na baner pricom jeho obsah je uz zobrazeny neulozime si ID_SECTION_PREV
    // lebo by sme sa inak nemohli vratit cez close_button
    if (id_section_new != ID_SECTION_ACTUAL)
    {
      ID_SECTION_PREV = ID_SECTION_ACTUAL;    
    }
    
    ID_SECTION_ACTUAL = id_section_new; 
    document.location.hash = mr_url;
    
    // ziadny, len zobrazenie obsahu
    if (animation_type == 0)
    {
      if ($('#content_top').html() != msg)
        $('#content_top').html(msg).animate({'opacity':'1.0'}, 200);
      else
        $('#content_top').html(msg).css('opacity','1.0');     
    }
    
    // opacity
    if (animation_type == 1)
    {
                               
      $('#content_top #submenu .orange').animate({'color':'rgb(255, 255, 255)'}, 600);
      
      $('#content_top #article').css('opacity','1.0').animate({'opacity':'0.0'}, 600, function() {
        
        $('#content_top').html(msg);
        $('#content_top #submenu .orange').css('color','rgb(255, 255, 255)').animate({'color':'rgb(255, 120, 0)'}, 600);
        $('#content_top #article').css('opacity','0.0').animate({'opacity':'1.0'}, 600);      
      
      });
    
    }
    
    // horizontal shifting
    else if (animation_type == 2)
    {     
    
      $('#content_top #submenu .orange').animate({'color':'rgb(255, 255, 255)'}, 600);
      $('#content_top #article').css('position','relative');
      var article_width = $('#content_top #article').width();
  
      // NOTE: skontroluj ci je v kazdom clanku definovany article_shifter
      $('#content_top #article .article_shifter')
        .css({'position':'relative','opacity':'1.0'})
        .animate({'left':'-' + article_width + 'px','opacity':'0.0'}, 400, function() {
          
          $('#content_top').html(msg);
          $('#content_top #submenu .orange').css('color','rgb(255, 255, 255)').animate({'color':'rgb(255, 120, 0)'}, 600);
          $('#content_top #article .article_shifter')
            .css({'position':'relative', 'left': article_width + 'px', 'opacity':'0.0'})
            .animate({'left':'0', 'opacity':'1.0'}, 400);      
        });      
    
    }
    
    // transformacia
    else if (animation_type == 3)
    {
      
      // pouziva sa v kontakte (je tam cierne pismo)
      $('#content_top #submenu .orange').animate({'color':'rgb(0, 0, 0)'}, 600);
      
      var old_article_width = $('#content_top #article').width();
      var old_article_height = $('#content_top #article').height();
      
      // NOTE: skontroluj ci je v kazdom clanku definovany article_shifter
      $('#content_top #article .article_shifter').css('opacity','1.0').animate({'opacity':'0.0'}, 400, function() {
          
        $('#content_top').html(msg);
        
        var new_article_width = $('#content_top #article').width();
        var new_article_height = $('#content_top #article').height();
        
        // pri obrazkoch nacitavanych cez ajax je problem pri prepocitatani vysky
        // napr. v kontaktom forme sa captcha niekedy nacita az potom co
        // sa vypocita vyska element (samozrejme bez obrazku je vyska nizsia)
                 
        $('#content_top #article').css({'width':old_article_width, 'height':old_article_height});
        $('#content_top .article_shifter').css('opacity','0.0');  
        $('#content_top #submenu .orange').css('color','rgb(0, 0, 0)').animate({'color':'rgb(255, 120, 0)'}, 400);
        
        $('#content_top #article')
          .animate({'width': new_article_width + 'px', 'height': new_article_height + 'px'}, 400, function() {           
        
            $('#content_top #article .article_shifter').animate({'opacity':'1.0'}, 400);
              
          });             
              
      });
    
    }
    
    // vertikalny shifting nahor
    else if (animation_type == 4)
    {     
      var article_height = $('#content_top .article_open').height();
  
      $('#content_top .article_open')
        .css({'position':'relative','opacity':'1.0'})
        .animate({'top':'-' + article_height + 'px','opacity':'0.0'}, 400, function() {
          
          $('#content_top').html(msg);
          $('#content_top .article_open')
            .css({'position':'relative', 'top': article_height + 'px', 'opacity':'0.0'})
            .animate({'top':'0', 'opacity':'1.0'}, 400);      
        });      
    
    }
    
    // vertikalny shifting dole
    else if (animation_type == 5)
    {     
      var article_height = $('#content_top .article_open').height();
  
      $('#content_top .article_open')
        .css({'position':'relative','opacity':'1.0'})
        .animate({'top': article_height + 'px','opacity':'0.0'}, 400, function() {
          
          $('#content_top').html(msg);
          $('#content_top .article_open')
            .css({'position':'relative', 'top':'-' + article_height + 'px', 'opacity':'0.0'})
            .animate({'top':'0', 'opacity':'1.0'}, 400);      
        });      
    
    }
        
    // vyrenderujeme nove sipky na posuvanie obsahu
    $.ajax({
    type: "POST",
    url: 'ajax_change_arrows.php?prev=1&id_section_actual='+id_section_new,
    success: function(msg) {
      $('#go_prev').html(msg);  
    } 
    });
    
    $.ajax({
    type: "POST",
    url: 'ajax_change_arrows.php?next=1&id_section_actual='+id_section_new,
    success: function(msg) {
      $('#go_next').html(msg);  
    }
    });

  }
  });
  
  }
  });
}
