/*
 * Treelist 1.3.0 - jQuery plugin to hide and show branches of a tree list
 * by Samurai Girl
 * http://www.samurai.ge
 *
 *
 * Is not licensed under any licenses :)
 *
 * Revision: 2: jquery.treelist-1.3.0.js 09-08-2009 19:46:55 Elene Latsoshvili
 *
 */

(function($){
$.fn.treelist = function(options) {
	 var options = jQuery.extend( {
		show_speed: 200,
		hide_speed: 200,
		active: 'active'
	  },options);
	 
	$(this).each(function() {
        var tree = this;
		// Hide all sub lists except 
		$('#'+tree.id+' ul').hide();
		$('#'+tree.id+' li.'+options.active).parents().show();
		$('#'+tree.id+' li').each(function(){
			// If list item is a link, don't make it expandible. Just go to the link
			if ($('>a',this).length==0) {				
				$(this).toggle(function(e){ if ($('> ul:hidden',this).length>0) show($('> ul',this),options.show_speed); else hide(e,$('> ul',this),options.hide_speed); 
											},function(e){ 
											if ($('> ul:hidden',this).length>0) show($('> ul',this),options.show_speed); else hide(e,$('> ul',this),options.hide_speed);
				});
			}
		});
	});
}
function show(element,speed) {
	$(element).show(speed);
}
function hide(e,element,speed) {
	// Get target element
	if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
	// If the target is not a sublist item, hide list
	if($('> ul',targ).length!=0) $(element).hide(speed);
	else if ($(targ).attr('href')!=null) window.location.href = $(targ).attr('href');
}

$.fn.expandAll = function() {
	$(this).each(function() {
        var tree = this;
		// Show all sub lists
		$('#'+tree.id+' ul').show();
	});
}

$.fn.collapseAll = function() {
	$(this).each(function() {
        var tree = this;
		// Hide all sub lists
		$('#'+tree.id+' ul').hide();
	});
}




})(jQuery);
