//Fancy navigation
$(document).ready(function(){
  var num = 0;
  $("#nav li").each(function(){
    num++;
    $(this).addClass('li-'+num);
    $(this).prepend('<div></div>');
  }).hover(function(){
    $(this).find("div").stop().animate({'opacity': '1'},400);
  }, function(){
    $(this).find("div").stop().animate({'opacity': '0'},400);
  });
});

//Ref navigator
$(function() {
  var num = 0;
  $("#ref_categories li").each(function(){
    num++;
    $(this).addClass('li-'+num);
    var top = (Math.sin(3.14+num*1.5)+1)*25;
    $(this).find("img").css({'margin-top':top});
  }).hover(function(){
    var top = Math.min($(this).outerHeight()-$(this).find("img").outerHeight(true)-5,30);
//    var top = ($(this).find("img").outerHeight(true) >= 140) ? 40 : 40 
    $(this).find("img").stop().animate({'top': top},600,"easeOutBack");
  }, function(){
    $(this).find("img").stop().animate({'top': '0'},600,"easeOutBack");
  }).click(function(e){
	e.preventDefault();
    var ref = $(this).find("a").attr("href").substr(1);
	location.hash = ref;
    filterReferences(ref);
  });
  
    var $preferences = {
    duration: 1000,
    easing: 'easeOutBack',
    adjustHeight: 'dynamic',
    attribute: "id"
    };
  
  function checkHash() {
    var ref = window.location.hash.substr(1);
    filterReferences(ref);
    setTimeout(checkHash,500);
  }

  var $list = $('.referencialista .box_content');
  var $data = $list.clone();

  var actualRef = "";
  function filterReferences(ref) {
    if(actualRef == ref) return;
    var marker = $("a[href=#"+ref+"]").parent();
	var arrow = $("#ref_nav_arrow_holder img");
    if(marker) {
	  if(tags && tags[ref]) {
		$(".referenciamegjelenito .box_header_content").html(tags[ref]);
	  }
	
	
      actualRef = ref;
      var $filtered_data = $data.find('.tartalmak_box.'+ref);
      
      if(marker.length) {
        var left = marker.position().left;
        left += marker.outerWidth()/2;
        left -= arrow.outerWidth();
        var speed = 400;
        arrow.stop().animate({'left':left},speed,"easeOutBack");
        arrow.animate({'opacity':1},{duration:2000,queue:false});
      }
//      $(".ref-box:not(."+ref+")").slideUp(speed);  
//      $(".ref-box."+ref).slideDown(speed);

          $list.quicksand($filtered_data, $preferences);

      }
  }

  checkHash();

});

