jQuery.fn.spoiler = function(userspoiler) {
	var options = {
		setclass : '',
		title : false,
		onlink : false
	}
	jQuery.extend(options, userspoiler);
	var title = "", text = "", css = false, user_class = '';
	getTitle = function(obj) {
		if (!options.title) {
			title = obj.children("span").text();
		} else {
			title = obj.attr("title");
		}
		return title;
	}
	getText = function(obj) {
		if (!options.onlink) {
			text = obj.text();
		} else {
			text = obj.attr("spoiler");
		}
		return text;
	}
	chekTitle = function(title, text) {
		if (title == "") {
			text = jQuery.trim(text);
			var words = text.split(" ");
			title = words[0];
			var i = 1;
			while (title.length < 6) {
				title += " " + words[i];
				i++;
			}
			title += " ...";
		}
		return title;
	}
	return this
			.each(function() {
				if (!jQuery("#spoiler-style").attr("is_set")) {
					jQuery("head")
							.append(
									"<style id='spoiler-style' is_set='1'>.spoiler {display: none;} .sp-link {cursor: pointer;}</style>");
				}
				obj = jQuery(this);
				title = getTitle(obj);
				text = getText(obj);
				title = chekTitle(title, text);

				if (options.setclass) {
					user_class = options.setclass;
				}
				if (options.onlink) {
					var classes = obj.attr("class");
					obj.replaceWith("<span class='sp-link " + user_class + "'>"
							+ title + "</span><div class='" + classes
							+ " spoiler'>" + obj.attr("spoiler") + "</div>");
				} else {
					obj.addClass("spoiler").before(
							"<span class='sp-link'>" + title + "</span>");
				}
			});
};
jQuery(".sp-link").live("click", function() {
	jQuery(this).next(".spoiler").slideToggle();
	return false;
});
