/*

	jQuery treeview plugin
	Copyright (c) 2010 Gael Jaunin

	For support and tutorials visit
	http://www.xuserver.net/

	License: GNU Lesser General Public License (LGPL) 
	at http://opensource.org/licenses/lgpl-2.1.php

	This plugin is free software;  you can redistribute it  and/or  modify  it 
	under the terms of the GNU Lesser General Public License as  published  by 
	the Free Software Foundation;  either version 2.1 of the License,  or  (at 
	your option) any later version.
	This software is distributed in the hope  that  it  will  be  useful,  but 
	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
	or FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU Lesser  General  Public 
	License for more details.
	You should have received a copy of  the  GNU Lesser General Public License 
	along with this library;  if not,  write to the  Free Software Foundation, 
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

	THIS COMMENT AND COPYRIGHT NOTICE MUST BE RETAINED IN THE CODE AS IS FOR LEGAL USE

*/



(function($){
  $.fn.extend({    
	 jxTreeview: function(options){
      var defaults = {
        unique:true,
        collapse:'level',
        iconOpen:'ui-icon-triangle-1-e',
        iconClose:'ui-icon-triangle-1-s',
        onOpen:function(parent){},
        onClose:function(parent){}
        
		};
				
      var options =  $.extend(defaults, options);
      var selection = $(this);
      
      var styling = function(o){
        o.children('label, span').css('cursor','pointer');
        o.children('li').css('list-style','none');
      };
      
      return selection.each(function() {
        
    	var tree = $(this);
    	
        styling(tree);
        $('ul',tree).css('padding','1px 1px 1px 16px');
        
        tree.children('li').each(function(){          
        	if($(this).children('ul').size() > 0){
        		if($(this).hasClass('open')){
        			var icon = $('<span style="" class="clicker jx-inline ui-icon '+options.iconClose+'" />');
        		}else{
        			var icon = $('<span style="" class="clicker jx-inline ui-icon '+options.iconOpen+'" />');
            	}
        	}else{
        		var icon = $('<span style="background:transparent" class="jx-inline ui-icon ui-icon-stop" />');
        	};
        	if(!$(this).hasClass('jxTreeview-arrow')){ 
        		$(this).prepend(icon);	
				$(this).addClass("jxTreeview-arrow")
			}
        });

        
        $('li ul',tree).hide();
        $('li.open ul',tree).show();
        $('li.open span.clicker',tree).removeClass(options.iconOpen).addClass(options.iconClose);
        
        tree.children().each(function(){
        	if($(this).hasClass('jxTreeview-on')){ 
        		return false;
        	}
	        $(this).children('.clicker').click(function(){
	        	var elt= $(this).parent();
	        	var clicker = $(this);
	            var ul = $('ul:first',clicker.parent());
	            if(!options.unique){
	              if(ul.css('display')=='none' ){
	                clicker.parent().addClass('open');
	                ul.slideDown('fast', function(){if(options.onOpen!=null){options.onOpen(elt);}});
	              }else{
	                clicker.parent().removeClass('open');
	                ul.slideUp('fast', function(){if(options.onClose!=null){options.onClose(elt);}});
	                
	              }
	              
	              clicker.toggleClass(options.iconClose).toggleClass(options.iconOpen);
	            }else{
	              if(options.collapse =='level'){
	                if(!clicker.parent().hasClass('open')){
	                  var upperLi = clicker.parent().parent().parent();
	                  $('>ul>li>ul',upperLi).hide();
	                  $('>ul>li',upperLi).removeClass('open');
	                  $('>ul>li>span.clicker',upperLi).removeClass(options.iconClose).addClass(options.iconOpen);
	                  clicker.removeClass(options.iconOpen).addClass(options.iconClose);
	                  ul.parent().addClass('open');
	                  ul.slideDown('fast', function(){if(options.onOpen!=null){options.onOpen(elt);}});
	                }else{
	                  clicker.parent().removeClass('open');
	                  ul.slideUp('fast', function(){if(options.onClose!=null){options.onClose(elt);}});
	                  clicker.toggleClass(options.iconClose).toggleClass(options.iconOpen);
	                };
	              }else if(options.collapse =='all'){
	                if(!clicker.parent().hasClass('open')){
	                  $('li ul',tree).hide();
	                  $('li',tree).removeClass('open');
	                  $('span.clicker',tree).removeClass(options.iconClose).addClass(options.iconOpen);        
	                  clicker.parent().addClass('open').parents('ul').show();
	                  $('li.open span.clicker',tree).removeClass(options.iconOpen).addClass(options.iconClose);        
	                  $('li:not(.open) span.clicker',tree).removeClass(options.iconClose).addClass(options.iconOpen);        
	                  ul.slideDown('fast', function(){if(options.onOpen!=null){options.onOpen(elt);}});
	                }else{
	                  clicker.parent().removeClass('open');
	                  ul.slideUp('fast', function(){if(options.onClose!=null){options.onClose(elt);}});
	                  clicker.toggleClass(options.iconClose).toggleClass(options.iconOpen);
	                }; 
	              };
	            };
	        });
          
	        $(this).children('span.clicker').next('span,label').click(function(){
	        	$(this).prev().click();
	        });
          
	        if(! $(this).hasClass('jxTreeview-on')){
	        	$(this).jxTreeview(options);
        		$(this).addClass('jxTreeview-on');
	        }
        })
        tree.addClass('jxTreeview-on') 
      }); 
    }

  }); 
})(jQuery);




