/*
* jQuery SimpleTree Drag&Drop plugin
* Update on 22th May 2008
* Version 0.3
*
* Licensed under BSD <http://en.wikipedia.org/wiki/BSD_License>
* Copyright (c) 2008, Peter Panov <panov@elcat.kg>, IKEEN Group http://www.ikeen.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Peter Panov, IKEEN Group nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Peter Panov, IKEEN Group ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Peter Panov, IKEEN Group BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


$.fn.simpleTree = function(opt){
	return this.each(function(){
		var TREE = this;
		var ROOT = $('.root',this);

		TREE.option = {
			animate: true,
			autoclose: true,
			animationwait: false,
			closechildren: true,
			speed: 'fast'
		};
		TREE.option = $.extend(TREE.option,opt);
		$.extend(this, {getSelected: function(){
			return $('a.active', this).parent();
		}});
		TREE.closeNearby = function(obj){
			$(obj).siblings().filter('.folder-open').each(function(){
				var childUl = $('>ul:visible',this);
				var className = this.className;
				this.className = className.replace('open','close');
				if(TREE.option.animate){
					childUl.animate({height:"toggle"},TREE.option.speed);
				}else{
					childUl.hide();
				}
				if(TREE.option.closechildren){
					TREE.closeChildren(childUl);
				}
			});
		};
		TREE.closeChildren = function(obj){
			var childUl = $('.folder-open ul:visible',obj);
			if(TREE.option.animate){
				childUl.animate({height:"toggle"},TREE.option.speed);
			}else{
				childUl.hide();
			}
			$('.folder-open', obj).each(function(){
				this.className = this.className.replace('open','close');
			});
		};
		TREE.nodeToggle = function(obj){
			var childUl = $('>ul',obj);
			//if(obj.className.indexOf('open')>0){
			if(obj.className=='folder-open'){
				obj.className = obj.className.replace('open','close');
				if(TREE.option.animate){
					childUl.animate({height:"toggle"},TREE.option.speed);
				}else{
					childUl.hide();
				}
				if(TREE.option.closechildren) TREE.closeChildren(childUl);
			}else{
				obj.className = obj.className.replace('close','open');
				if(TREE.option.animate && TREE.option.animationwait){
					childUl.animate({height:"toggle"},TREE.option.speed, function(){
						if(TREE.option.autoclose){
							TREE.closeNearby(obj);
						}
					});
				}else if(TREE.option.animate){
					childUl.animate({height:"toggle"},TREE.option.speed);
				}else{
					childUl.show();
				}
				if(!TREE.option.animationwait){
					if(TREE.option.autoclose)TREE.closeNearby(obj);
				}
			}
		};
		TREE.setTreeNodes = function(obj, useParent){
			obj = useParent? obj.parent():obj;
			$('li>a', obj)
				.addClass('text')
				.bind('selectstart', function() {
					return false;
				});
			$('li', obj).each(function(i){
				var className = this.className;
				var open = false;
				var cloneNode=false;
				var LI = this;
				var childNode = $('>ul',this);
				if(childNode.size()>0){
					var setClassName = 'folder-';
					if(className && className.indexOf('open')>=0){
						setClassName=setClassName+'open';
						open=true;
					}else{
						setClassName=setClassName+'close';
					}
					this.className = setClassName;

					if(!open){
						childNode.hide();
					}

					TREE.setTrigger(this);
				}else{
					var setClassName = 'doc';
					this.className = setClassName;
				}
			});
		};
		TREE.setTrigger = function(node){
			$('>a',node).addClass('trigger');
			var trigger = $('>.trigger', node);
			trigger.click(function(event){
				TREE.nodeToggle(node);
			});
		};
		TREE.init = function(obj){
			TREE.setTreeNodes(obj, false);
		};
		TREE.init(ROOT);
	});
}
