function equalHeight(group) {
  var tallest = 0;
  
  group.each(function() {
	  var thisHeight = $(this).height();
	  
	  if(thisHeight > tallest)
	  {
	  	tallest = thisHeight;
	  }
	  
  });
  
  group.height(tallest);
}

$(document).ready(function(){
  
  /*
    Category new init
  */
  
  $("#categories_list select").live('click', function()
  {
    var url  = '/categories/' + $(this).attr('value') + '.js';
    var deph = parseInt($(this).closest('.row').attr('data-deph')) + 1;
    
    for(i = deph; i < 7; i++)
    {
      $("#categories_list .row[data-deph="+i+"]").remove();
    }
    
    $.getScript(url);
  });
  
  $('.showComment').live('click', function(){
    $(this).parent().find('.txt').toggle();
    
    return false;
  });
  
  // Init search input validation
  $('#form-search form').live('submit', function(){

    if ($('#form-search #q').attr('value').length < 4)
    {
      alert('Zapytanie musi mieć conajmniej 3 znaki');
      return false;
    }
    
  });

	var panelUser = $('#login-panel-option');
  	var t1;
  	var t1in;

  	$('#login-pane-username').bind('mouseenter', 
  		function(){
  			clearTimeout(t1);
  			t1in = setTimeout(
  					function(){
  						panelUser.stop(true, true).fadeIn();
  					}
  				, 500);
  		}
  	);

  	$('#login-pane-username').bind('mouseleave', 
  		function(){
  			clearTimeout(t1in);
  		}
  	);

  	panelUser.bind('mouseenter', 
  		function(){
  			clearTimeout(t1);
  		}
  	);

  	panelUser.bind('mouseleave', 
  		function(event){
  			if($(event.relatedTarget).attr('id') != 'login-pane-username')
  			{
  				clearTimeout(t1);
  				t1 = setTimeout(
  						function(){
  							panelUser.stop(true, true).fadeOut();
  						}
  					, 700);

  			}
  		}
  	);

  	$('#login-pane-username').bind('mouseleave', 
  		function(event) {
  			if(!($(event.relatedTarget.nodeName).is('span')))
  			{
  				clearTimeout(t1);
  				t1 = setTimeout(
  						function(){
  							panelUser.stop(true, true).fadeOut();
  						}
  					, 700);

  			}
  		}
  	);
  	////////////////////////////////////////////////////////////////menu kategorii	
  	var kategorie = $('#category-option');
  	var t;
  	var tin;

  	$('#category-button').bind('mouseenter', 
  		function(){
  			clearTimeout(t);
  			tin = setTimeout(
  					function(){
  						kategorie.stop(true, true).fadeIn();
  					}
  				, 500);
  		}
  	);

  	$('#category-button').bind('mouseleave', 
  		function(){
  			clearTimeout(tin);
  		}
  	);

  	kategorie.bind('mouseenter', 
  		function(){
  			clearTimeout(t);
  		}
  	);

  	kategorie.bind('mouseleave', 
  		function(event){

  			if($(event.relatedTarget).attr('id') != 'category-button' || event.relatedTarget.nodeName == null)
  			{
  				clearTimeout(t);
  				t = setTimeout(
  						function(){
  							kategorie.stop(true, true).fadeOut();
  						}
  					, 700);
  			}
  		}
  	);


  	$('#category-button').bind('mouseleave', 
  		function(event) {

  			if(!($(event.relatedTarget.nodeName).is('span')))
  			{
  				clearTimeout(t);
  				t = setTimeout(
  						function(){
  							kategorie.stop(true, true).fadeOut();
  						}
  					, 700);
  			}
  		}
  	);

  	$('#share-active').click(function() {
  		var share = $('#share');
  		if($(this).hasClass('active')) {
  			$(this).removeClass('active')
  			share.css('display', 'none');
  		} else {
  			$(this).addClass('active')
  			share.css('display', 'block');
  		}
  		return false;
  	});

  	$('#userinfo-button').click(function() {
  		var share = $('#userinfo-list');
  		if($(this).hasClass('active')) {
  			$(this).removeClass('active')
  			share.css('display', 'none');
  		} else {
  			$(this).addClass('active')
  			share.css('display', 'block');
  		}
  		return false;
  	});

  	var needs = $('#needs');
  	if(needs) {
  		if(needs.height() < 178+19) {
  			$('#needs span.round').css('height', needs.height());
  		}
  	}

  	$('.box .wrapper ul li .image, .main li, .films-list .top, #top li, #popular li .top, #films ul li .top').find('.image').css('cursor', 'pointer').click(function()
  	{
  	  var sLoc = $(this).parent().find('h3 a').attr('href');

  	    location.href = sLoc;
  	  return false;
  	});

    $('.confirm').live('click', function(){
      return confirm('Czy na pewno?');
    });

  	// $("#form-file").filestyle({
  	// 	image: "/www/images/upload/graybox.file.gif",
  	// 	imageheight : 22,
  	// 	imagewidth : 87,
  	// 	width : 238
  	// });
  	// $(".file-select-common input[type=file]").filestyle({
  	// 	image: "/www/images/upload/graybox.file.gif",
  	// 	imageheight : 22,
  	// 	imagewidth : 87,
  	// 	width : 238
  	// });

    // $('#sidebar ul.second a span').css('background', 'url(' + $(this).parent()[icon] + ') no-repeat 0px 8px');

    $('.abuse').live('click', function(){
      var sPath = $(this).attr('href');

      $.get(sPath, null, null, 'script');

      return false;
    });

    $('.answerComment').css('cursor', 'pointer').live('click', function(){
      $('#comment_form_answer').css('display', 'block').html('Odpowiadasz na komentarz uĹźytkownika: ' + $(this).attr('data-user'));
      $('#parent_id').val($(this).attr('data-id'));
    });

    $('.showComment').live('click', function(){
      $(this).parent().find('.txt').toggle();

      return false;
    });
    
});

