(function($) {

$.fn.tree = function(options) {

    return this.each(function() {
        if (options && options.constructor == String) {
			alert('c is ' + this.c);
            switch(options) {
            case 'action1':
                alert('action 1');
				return;
            case 'action2':
                alert('action 2');
                return;
            case 'action3':
                alert('action 3');
                return;
            default:
                options = { a: options };
            };
        }
		
		//this simply adds support for the metadata plugin, per this tutorial:
		//http://www.learningjquery.com/2007/10/a-plugin-development-pattern
        var opts = $.extend({}, $.fn.tree.defaults, options || {}, $.meta ? $cont.data() : {});
        
		//heres where the action happens
		
		//easing functions
		if (opts.easing) 
            opts.easeIn = opts.easeOut = opts.easing;
        if (!opts.speedIn) 
            opts.speedIn = opts.speed;
        if (!opts.speedOut) 
            opts.speedOut = opts.speed;
			
		var myUl = $(this);
		$(myUl).find("ul").css('list-style','none');
		$(myUl).css('list-style','none');
	
		
		iterateUl(myUl)
		updateCSS();
		updateActions();
		
		function iterateUl(el){
			el = $(el).children();
			for (var i = 0; i < $(el).length; i++) {
				if ($(el).eq(i).children().length>0) {
					$(el).eq(i).prepend('<span class="treeIcon treeOpen" />&nbsp;</span>');
					$(el).eq(i).children("ul").hide();
					iterateUl($(el).eq(i).children("ul"));
				} else {
					$(el).eq(i).prepend('<span class="treeIcon treeEmpty" />&nbsp;</span>');
				}
			}
		}
		
		function updateCSS(){
		$(".treeIcon").css('margin','0px 5px 0px 0px').css('display','block').css('float','left').css('width','9px').css('height','9px');
		$(".treeOpen").css('background','url(' + opts.openImg + ')');
		$(".treeEmpty").css('background','url(' + opts.emptyImg + ')');
		$(".treeClose").css('background','url(' + opts.closeImg + ')');
		}
		
		function updateActions() {
			$(".treeOpen").unbind().click(function(){
				$(this).removeClass("treeOpen");
				$(this).addClass("treeClose");
				$(this).parent().children("ul").slideDown(opts.speedIn, opts.easeIn);
				updateCSS();
				updateActions();
			});
			
			$(".treeClose").unbind().click(function(){
				$(this).removeClass("treeClose");
				$(this).addClass("treeOpen");
				$(this).parent().children("ul").slideUp(opts.speedOut, opts.easeOut);
				updateCSS();
				updateActions();
			});
		}
		//end of action
    });
};

$.fn.tree.defaults = {
    openImg:	'http://www.acropoliswms.com/master_files/images/bullet_plus.gif', 
				// the image we use for the open action, if child list exists
    closeImg:   'http://www.acropoliswms.com/master_files/images/bullet_minus.gif',  
				// the image we use for the closed action, if child list exists
    emptyImg:  'http://www.acropoliswms.com/master_files/images/bullet_last.gif',
				//the image we use to indicate no child list exists for given item
	easing: null,
	speed: 500
};

})(jQuery);
