$(document).ready(function(){

  var url_prefix = 'http://akacja.wzks.uj.edu.pl/~urbankub';
  var url_prefix = '';

  // "back" button
  $(".back_button").click(function(event){
    //prevent the default action for the click event
    event.preventDefault();
    history.go(-1);
  });

  // scrolling to comments
  $(".scroll_to_comments").click(function(event){
    //prevent the default action for the click event
    event.preventDefault();
    //get the destination
    var id = $(this).attr("rel");
    var target = 'post_comments_' + id;
    // hiding opened comments container
    $('.comments').hide('fast');
    // loading comments
    $.ajax({
      url: url_prefix + '/blogfrontend.php/comment/getForPost',
      data: {
        post_id : id 
      },
      success: function(data) {
        $('#comments_' + id).html(data);
        $('#comments_' + id).show('slow');
        var target_offset = $("#"+target).offset();
        var target_top = target_offset.top - 18;
        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({scrollTop:target_top}, 1000);
      }
    });

  });


  // scrolling to comments
  $(".scroll_to_add_comments").click(function(event){
    //prevent the default action for the click event
    event.preventDefault();
    //get the destination
    var id = $(this).attr("rel");
    var target = 'comment_form_' + id;
    // hiding opened comments container
    $('.comments').hide('fast');
    // loading comments
    $.ajax({
      url: url_prefix + '/blogfrontend.php/comment/getForPost',
      data: {
        post_id : id 
      },
      success: function(data) {
        $('#comments_' + id).html(data);
        $('#comments_' + id).show('slow');
        //get the top offset of the target anchor
        var target_offset = $("#"+target).offset();
        var target_top = target_offset.top - 140;
        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({scrollTop:target_top}, 1000);
      }
    });

  });

  // for managing scrolling posts 
  window.scroll_status = 1; // 1 = scroll, 0 = don`t
  var total_height = 0;

  
  // displaying comments (AJAX)
  $('.display_comments').click(function(){
    id = $(this).attr('id').substring(14);
    $.ajax({
      url: url_prefix + '/blogfrontend.php/comment/getForPost',
      data: {
        post_id : id 
      },
      success: function(data) {
        $('#comments_' + id).html(data);
        $('.comments').hide('fast');
        //$('#loader_indicator').hide();
        $('#comments_' + id).show('slow');
      }
    });
    //$('#loader_indicator').show();
    return false;
  });
  


 
  
  $("#footer").mouseenter(function() {
    $("#footer_content_long").slideDown("fast");
  });
  
  $("#footer").mouseleave(function(){
    $("#footer_content_long").slideUp("fast");
  });
  
  $('#show_site').click(function() {
    var height_window = $(window).height() - 150; 
    $('#ajaxloader').slideToggle('fast');
    $('html, body').animate({scrollTop:0}, 'slow');
    $('html, body').height(height_window);
    $('#footer').animate({
    height: height_window
  }, 'fast', function() {
    $('#footer').css('position', 'static');
  });
    return false;
  });
  
   

  $('#show_blog').click(function() {
    var height_window = $(window).height() - 150; 
    $('#ajaxloader').slideToggle('fast');
    $('html, body').animate({scrollTop:0}, 'slow');
    $('#footer').animate({
    height: 129
  }, 'fast', function() {
    $('#footer').css('position', 'fixed');
  });
    return false;
  });
  
   

    
    
    

    
	 
		
		     (function(){
            /* 
            var message = $('<div id="m" />').css({
                top: 20,
                left: 20,
                height: 30,
                lineHeight: '30px',
                padding: '0 10px',
                background: 'black',
                border: '1px solid yellow',
                color: 'yellow',
                opacity: 0
            }).hide().appendTo('body');
            */
            
            jQuery(window).bind('scrollstart', function(){
                /* $(document.body).css({
                    color: '#F6FF96'
                }); 
                message.html('SCROLLING').stop().show().animate({
                    opacity: 1
                }); */
            });
            
            jQuery(window).bind('scrollstop', function(e){
                
                var scrolling_element = true;
                $('.post_container').each(function(index) {
                  if ($(this).isOnScreen()) { // jesli jest widoczny, oryginalne isOnScreen zmodyfikowano
                   var item_height = $(this).find('.item').height();
//                   console.log('item height: ' + item_height);
                   var toolbar_total_height = $(this).find('.item_toolbar').height() + parseInt($(this).find('.item_toolbar').css('marginTop')); // wysokosc od gory posta do dolu tego dinksa po prawej
//                   console.log('total height: ' + toolbar_total_height);
                   toolbar_absolute_height = $(window).scrollTop() - $(this).offset().top;  // roznica miedzy pozycja scolla w oknie a wysokoscia (odlegloscia od gory strony) calego wpisu
                   //console.log('to oc badam: ' + toolbar_absolute_height);
                   toolbar_height = $(this).find('.item_toolbar').height();
                   var item_toolbar = $(this).find('.item_toolbar');
                   if (item_height >= toolbar_total_height) { // jesli wysokosc calego wpisy jest wieksza niz wysokosc dzinksa + jego polozenie - czyli jedzie obok
                     if (toolbar_absolute_height > 0) { // czyli animujze dopiero gdy gora posta schowa sie za ekranem
                       $(this).find('.item_toolbar').animate({"marginTop": toolbar_absolute_height + "px"}, 1000, function() { 
                         var new_position_total = parseInt(item_toolbar.css('marginTop')) + toolbar_height; 
                         //console.log('new position total: ' + new_position_total);
                         if (new_position_total >= item_height) { // sprawdzam czy nie pojechal aby za nisko
                           var go_to_this_position = item_height - toolbar_height;
                           //console.log('total nowa pozycja: ' + go_to_this_position);
                           $(this).find('.item_toolbar').animate({"marginTop": go_to_this_position + "px"}, 1000 );			
                         }
                          
                       });			
                     } else {
                       $(this).find('.item_toolbar').animate({"marginTop": 0 + "px"}, 1000 );			
                     }
                     
                    } else { // jesli juz zjechal ponizej posta - naprostujmy go!
                      var lowest_place = item_height - $(this).find('.item_toolbar').height();
                      $(this).find('.item_toolbar').animate({"marginTop": lowest_place + "px"}, 1000 );			

                      
                     //console.log('b');
                     if (($(window).scrollTop() > $(this).offset().top)) {
                     //console.log('b1');
                      //$(this).find('.item_toolbar').animate({"marginTop": ($(this).find('.item').height() - $(this).find('.item_toolbar').height() - 41) + "px"}, 1000);			
                     } else {
                       
                     //console.log('b2');
                     }
                    }
      
                  }
                });
  
                
                /*                
                message.stop().show().animate({
                    opacity: 0
                }); */
            });
            
        })();


});





(function(){
 
    var special = jQuery.event.special,
        uid1 = 'D' + (+new Date()),
        uid2 = 'D' + (+new Date() + 1);
 
    special.scrollstart = {
        setup: function() {
 
            var timer,
                handler =  function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = 'scrollstart';
                        jQuery.event.handle.apply(_self, _args);
                    }
 
                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind('scroll', handler).data(uid1, handler);
 
        },
        teardown: function(){
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
        }
    };
 
    special.scrollstop = {
        latency: 300,
        setup: function() {
 
            var timer,
                    handler = function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    }
 
                    timer = setTimeout( function(){
 
                        timer = null;
                        evt.type = 'scrollstop';
                        jQuery.event.handle.apply(_self, _args);
 
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind('scroll', handler).data(uid2, handler);
 
        },
        teardown: function() {
            jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
        }
    };
 
})();


        // hidding email address
var lhs = "ja";
var rhs = "kubaurbanczyk.pl";

function print_mail_to_link()
{
   document.write("<a href=\"mailto");
   document.write(":" + lhs + "@");
   document.write(rhs + "\">" + lhs + "@" + rhs + "<\/a>");
}



// images preloading

(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)
jQuery.preLoadImages("/images/footer_menu_bg.png", "/images/bg_main.jpg", "/images/footer_bg.png", "/images/bg_content_item.png", "/images/bg_content_top.png");
