// jQuery Mister Tree Plugin (MrTree)
//
// Version 1.00
//
// TERMS OF USE
// 
// jQuery File Tree is licensed under a Creative Commons License and is copyrighted (C)2008 by Cory S.N. LaViska.
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//
if(jQuery) (function($){
	
	$.extend($.fn, {
		MrTree: function(o, h) {
			if( !o ) var o = {};
			if( o.redirect == undefined ) o.redirect = 'redirect';
/*
			// Defaults
			if( !o ) var o = {};
			if( o.root == undefined ) o.root = '/';
			if( o.script == undefined ) o.script = 'jqueryFileTree.asp';
			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(i) {
				loadTree($(this));
				//this.filter(":last-child:not(ul)").addClass('last');
			});

			function loadTree(c){
				var li = $('li', c);
				//child.filter('.open').css("border", "1px solid black");

				var ul = li.children('ul');
				if( ul.size() > 0) {
					ul.css({ display:"none" })
				}
				//child.filter('.open').find('ul').css("border", "1px solid black");
				li.filter('.open').children('ul').css({ display:"block" });

				
				// CLICK
				//-----------------------------------------------------------------------
				$(".select a").click(function(){
					$imgClose = "img/plus.png";
					$imgOpen = "img/minus.png";

					var ulNode = $(this).parent().parent().find('>ul');
					if(!$(this).hasClass('selecionado') ) {
						//$(this).parent().parent().parent().find('ul').slideUp({ duration: 500, easing: null });
						// ARRUMAR ESTE ITEM ---> QUANDO OCULTA, FAZER COM QUE FIQUE SUAVE
						$(this).parent().parent().parent().find('ul').hide();
					}
				
					/*
					$(this).parent().parent().parent().find('ul').each(function(){
						if( $(this).is(':visible') ){
							//ulNode.stop().slideDown('slow');
							ulNode.animate({height:"toggle"},'linear');
						}					
					})
					*/

					if( ulNode.is(':hidden') ){
						ulNode.slideDown({ duration: 500, easing: null });
					}
					//
					

					// Remove TODAS as classes
					// -----------------------------------------------
					/*
					$(".select a").each(function(){
						$(this).parent().parent().removeClass("over");
						$(this).removeClass("selecionado");
					});
					*/
					// -----------------------------------------------

					// Remove somente as classes parentes
					// -----------------------------------------------
					$(this).parent().parent().parent().find('li').removeClass("over");
					$(this).parent().parent().parent().find('a').removeClass("selecionado");

					$(this).addClass("selecionado");
					$(this).parent().parent().addClass("over");


					// O QUE FAZER QUANDO CLICAR ???
					// -----------------------------------------------
					if( ulNode.size() == 0) {						
						h( $(this).attr('rel') );	// CallBack
					}else{
						if(o.redirect == "redirect"){
							h( $(this).attr('rel') );	// CallBack
						} else {
							// Carrega a conteúdo da categoria
							$.Cristalino.CategoriaLoad({
								idcategoria: $(this).attr('rel')
							});
						}
					}

					return false;
				});

				// MOUSOVER
				//-----------------------------------------------------------------------
				$(".select a").mouseover(function(){
					$(this).parent().parent().addClass("over");
				})

				// MOUSEOUT
				//-----------------------------------------------------------------------
				$(".select a").mouseout(function(){
					if(!$(this).hasClass("selecionado")){
						$(this).parent().parent().removeClass("over");
					}
					//alert( $(this).parent().html() );				
				})

			return false;
			}
		}
	});
	
})(jQuery);
