$.fn.SculpteoCategorizer = function(o) {
	return this.each(function() {
		var options = o;
		var defaults = {
			editable:false,
			values:[]
		};
		var options = $.extend(defaults, o);
		var jself = $(this);

		// redesign categories tree
		$("li:not(.redesigned)", jself).each(function(i, el) {
			$(this).addClass("redesigned aleft");
			if (options.editable) {
				var title = $("a:first", this).text();
				var className = $("a:first", this).attr("class");
				if (!className) {
					return;
				}
				var cat = className.substr(className.lastIndexOf("_")+1);
				$("a:first", this).remove();
				$(this).prepend("<span class='branch'><input name='categories' value='"+cat+"' class='small' type='checkbox' /> "+title+"<"+"/span>");
			} else {
				var current_query = $.deparam.querystring();
				// rewrite url, removing unwanted params
				delete(current_query['page']);
				delete(current_query['search']);
				delete(current_query['cat']);
				$("a:first", this).querystring(current_query);
				$("a:first", this).addClass("branch").prepend("» ");
			}
			if ($("ul", this).length) {
				$(this).prepend($("<div></div>").addClass("triangle"));
			} else {
				$(this).addClass("noChild");
			}
		});

		// attach event on each branch if editable
		if (options.editable) {
			$(".branch", jself).unbind("click").bind("click", function(evt) {
				$("input", this).get(0).checked = !$(this).parent("li").is(".selected");
				if ($("input", this).get(0).checked) {
					$(this).parent("li").addClass("selected");
				} else {
					$(this).parent("li").removeClass("selected");
				}
				if (!$(this).prev(".triangle").is(".opened")) {
					$(this).prev(".triangle").addClass("opened");
					$("ul:first", $(this).parent("li")).slideDown('fast');
				}
			}) ;
		} else {
			// add a delete icon, to reset view
			$(".unselect", jself).remove();
			$(".selected", jself).each(function(i,el) {
				$(this).css("text-align","center");
				$(this).css("vertical-align","middle");
				if (!$(this).find(".ui-icon ").length) {
					var current_query = $.deparam.querystring();
					// rewrite url, removing unwanted params
					delete(current_query['page']);
					delete(current_query['search']);
					delete(current_query['cat']);
					var current_url = $.param.querystring(document.location.href, current_query, 2);
					var icon = $("<a></a>").addClass("unselect ui-icon ui-icon-closethick fright").attr("href", current_url);
					if ($("ul", this).length == 0) {
						$(this).find("a:first").addClass("aleft").css("width", "80%");
					}
					icon.insertAfter($(this).find("a:first"));
				}
			});
		}
		
		// set values
		for(v in options.values) {
			if (options.editable) {
				$("input[value="+options.values[v].id+"]").parent().parent("li").addClass("selected");
				$("input[value="+options.values[v].id+"]").get(0).checked=true;
			}
		}

		// attach event on each category triangle
		$(".triangle", jself).unbind("click").bind("click", function(evt) {
			if ($("ul:first", $(this).parent("li")).is(":hidden")) {
				$(this).addClass("opened");
				$("ul:first", $(this).parent("li")).slideDown('fast');
			} else {
				$(this).removeClass("opened");
				$("ul:first", $(this).parent("li")).slideUp('fast');
			}
		});

		// open selected branches and their parents
		
		$("ul:first>li", jself).each(function(i,el) {
			var is_selected = $(this).hasClass("selected") || $(this).find(".selected").length != 0;
			if (is_selected) {
				$("div.triangle", this).addClass("opened");
				$("ul:first", this).show();
			} else {
				$("div.triangle", this).removeClass("opened");
				$("ul:first", this).hide();
			}
		});
		$(jself).show();
	});
}

