// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options:  root           - root folder to display; default = /
//           script         - location of the serverside AJAX file to use; default = jqueryFileTree.php
//           folderEvent    - event to trigger expand/collapse; default = click
//           expandSpeed    - default = 500 (ms); use -1 for no animation predkosc chowania
//           collapseSpeed  - default = 500 (ms); use -1 for no animation predkosc rozwijania
//           expandEasing   - easing function to use on expand (optional)
//           collapseEasing - easing function to use on collapse (optional)
//           multiFolder    - whether or not to limit the browser to one subfolder at a time
//           loadMessage    - Message to display while initial tree loads (can be HTML)


if(jQuery) (function($){
	
	$.extend($.fn, {
		fileTree: function(o, h) {
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '';
			if( o.script == undefined ) o.script = 'jqueryFileTree.php';
			if( o.folderEvent == undefined ) o.folderEvent = 'click';
			if( o.expandSpeed == undefined ) o.expandSpeed= 500;
			if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
			if( o.expandEasing == undefined ) o.expandEasing = null;
			if( o.collapseEasing == undefined ) o.collapseEasing = null;
			if( o.multiFolder == undefined ) o.multiFolder = true;
			if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
			
			$(this).each( function() {
				
				function showTree(c, t, i) {
					$(c).addClass('wait');
					$(".jqueryFileTree.start").remove();
					$.post(o.script, { dir: t, id: i, czas: o.argD }, function(data) {
						$(c).find('.start').html('');
						$(c).removeClass('wait').append(data);
						if( o.root == t ) $(c).find('UL:hidden').show(); else $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
						bindTree(c);
					});
				}
				
				function showFiltr(nazwa, war)
				{
				  switch (nazwa)
          {
            case "firma":
            {
              li=1;
              ko=1;
            }
            break;
            case "siec":
            {
              li=1;
              ko=2;
            }
            break;
            case "sklep":
            {
              li=1;
              ko=3;
            }
            break;
            case "dzial":
            {
              li=2;
              ko=1;
            }
            break;
            case "kat":
            {
              li=2;
              ko=2;
            }
            break;
            case "podkat":
            {
              li=2;
              ko=3;
            }
            break;
            case "prod":
            {
              li=3;
              ko=1;
            }
            break;
            case "marka":
            {
              li=3;
              ko=2;
            }
            break;
          }
				  
				  linia='div#filtr table tr#liniaU'+li;
				  
				  if(war==0)
				  {
				    if (ko==1)
            {
              $(linia+' td#komU4 a').html('');
              $(linia+' td#komU4 span').html('&nbsp;');
              $(linia).addClass("ukryty");
            }
				    while (ko<=3)
				    {
				      komorka=linia+' td#komU'+ko;
              $(komorka).html('&nbsp;');
              ko++;
            }
            to1 = $("div#filtr tr#liniaU1").attr("class");
            to2 = $("div#filtr tr#liniaU2").attr("class");
            to3 = $("div#filtr tr#liniaU3").attr("class");
            if (to1=='ukryty' && to2=='ukryty' && to3=='ukryty' && !($("div#filtr tr#linia0").attr("class")=="ukryty"))
            {
              $("div#filtr tr#linia0").addClass("ukryty");
            }
            if (to1=='ukryty' && to2=='ukryty' && to3=='ukryty' && !($("div#filtr").attr("class")=="ukryty"))
            {
              $("div#filtr").addClass("ukryty");
            }
          } else
          {
            
            $("div#filtr table tr#linia0").removeClass("ukryty");
            komorka='div#filtr table tr#liniaU'+li+' td#komU'+ko;
            $(komorka).text(war);
            if (ko==1)
            {
              $('div#filtr table tr#liniaU'+li+' td#komU2').html('&nbsp;');
              $('div#filtr table tr#liniaU'+li+' td#komU3').html('&nbsp;');
            } else if (ko==2)
            {
              $('div#filtr table tr#liniaU'+li+' td#komU3').html('&nbsp;');
              if (li==3) $("div#order td#marka").addClass("ukryty");
            } else if (ko==3)
            {
              if (li==1) $("div#order td#sklep").addClass("ukryty");
              if (li==2) $("div#order td#kateg").addClass("ukryty");
            }
            komorka='div#filtr table tr#liniaU'+li+' td#komU4 a';
            if (li==1) $(komorka).attr("rel", "firma");
            if (li==2) $(komorka).attr("rel", "dzial");
            if (li==3) $(komorka).attr("rel", "prod");
           $(komorka).html("usuń");
           
           
          }
          $("div#filtr").addClass("ukryty");
          $('.active').removeClass("active");
          $('#li1').parent().addClass("active");
          $('#gazetki_top_s').attr("class","gazetki_top_s1");
          $('#typ3').hide();
          $('#typ2').hide();
          $('#typ1').show();
        }
				
				function bindTree(t) {
					$(t).find('LI A').bind(o.folderEvent, function() { // wylapuj klikniecia w link wewnatrz punktu listy
						if( $(this).parent().hasClass('directory') ) {   // jesli katalog
							if( $(this).parent().hasClass('collapsed') ) { // jesli rozwiniety
								// Expand //chowanie
								if( !o.multiFolder ) { // jesli nalezy zwinac rozwiniete przed rozwineiciem kolejnego
									$(this).parent().parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing }); //anim
									$(this).parent().parent().find('LI.directory').removeClass('expanded').addClass('collapsed'); // nadaj klase -
								}                                                                                               //zwiniety alldir
								$(this).parent().find('UL').remove(); // cleanup czysc
								showTree( $(this).parent(), $(this).attr('rel'), $(this).attr('id'));
								$(this).parent().removeClass('collapsed').addClass('expanded'); // nadaj klase zwiniety
								h($(this).attr('rel'), $(this).attr('id'), 'd', $(this).text());
								showFiltr($(this).attr('rel'), $(this).attr('name'));
							} else { // jesli zwiniety
								// Collapse //rozwijanie
								$(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing }); //anim
								$(this).parent().removeClass('expanded').addClass('collapsed'); //nadaj klase rozwiniety
								h($(this).attr('rel'), $(this).attr('id'), 'u', $(this).text());
								showFiltr($(this).attr('rel'), 0);
							}
						} else { // jesli niekatalog - plik
							h($(this).attr('rel'), $(this).attr('id'), 'd', $(this).text());
							showFiltr($(this).attr('rel'), $(this).attr('name'));
						}
						// zmiana reklamy. 
// 						var temp = new String (Math.random()).substring (2, 11);
// 						$.post('js/publiczne/reklamy.php',{ url: temp },function(data){
// 						  alert(data);
//                $('#skyscrapper').html(data);
//             });
            return false
					});
					// Prevent A from triggering the # on non-click events
					if( o.folderEvent.toLowerCase != 'click' ) $(t).find('LI A').bind('click', function() { return false; });
				}
				// Loading message
				$(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
				// Get the initial file list
				showTree( $(this), escape(o.root), escape(o.root));
			});
		}
	});
	
})(jQuery);
