var selectedSectionHref = null;
var selectedSectionPic = null;

$(document).ready(function()
{
  selectedSectionHref = $(".active a[href!='#0']").attr("href");
  selectedSectionPic = $("#section_pic img").attr("src");

	$("#sections ul.level1").each(function()
	{
		if($(this).attr('rel') == 'empty')
		{
			var next = $(this).next('ul.level2');
			var parent = $(this);
			var link = parent.children().children();
			link.attr('href',link.attr('href')+'#0');
			link.bind('click',function() {
				next.reactivate();
				parent.children().addClass('active');
				parent.children().attr('id','actived');
				return false;
			});
		}
	}
	);
}
);

$.fn.reactivate = function()
{
	var element = $(this);
	var old_element = $("#sections ul.display");
	$("#sections li.active").each(function()
	{
		$(this).removeClass('active');
		$(this).attr('id','');
	});
	if(old_element.html() == null)
	{
		element.slideDown('slow',
		function()
		{
			element.addClass('display');
		});
	}else
	{
	old_element.slideUp('slow',
	function()
	{
		$(this).removeClass('display');
		element.slideDown('slow',
		function()
		{
			element.addClass('display');
		});
	});
	}
}

var active = null;
var activeSection = null;
var ajaxStop = null;
var obrazocekTENTOK = null;

function getSectionPic(param,obj) {
  //ak sme nad sekciou ktora je selectnuta, do nothing
  if($(param).attr("href") == selectedSectionHref)
    return false;
  //ak je request aktivny, do nothing
  if(active==true)
    return false;
  
  var outputElement = $("#section_pic"); 
  activeSection = param;
    
  active = $.ajaxSync({
    type:"GET",
    url:"getSectionPicture.php",
    data: "s="+param,
    queue: "autocomplete",
    cancelExisting: true,
    beforeSend: function(msg) {
      if(ajaxStop == true)
        return false;
    },
    success: function(param){
      if(ajaxStop == true)
        return false;
        
      //odpalime rozlisovaci bordel
      sub = param.substring(param.search("<span class='hiddenHeader' style='display:none;'>"),param.search("</span>"));
	    param = param.replace(sub,'');
      sub = sub.replace("<span class='hiddenHeader' style='display:none;'>",'');
      sub = sub.replace("</span>",'');
      sub = jQuery.trim(sub);
      
      if(sub == $("#section_pic img").attr("src"))
        return false; 
        
      outputElement.fadeOut("fast",
        function(){outputElement.html(param).fadeIn("fast",
        function(){active = null;});  });
    }
    
  });
}

function getActiveSectionPic(obj) { //return false;
  if($("#section_pic img").attr("src") == selectedSectionPic) {
    return false;
  } else if(active!=null) {
     return false;   
  } else {
    
    pic = $("#section_pic img");
    pic.fadeOut("fast",function(){ pic.attr("src",selectedSectionPic); pic.fadeIn("fast", function(){ } ); });
  } 
}


