$(document).ready(function() {
	jqexpander_init(".jqexpander");
});

function jqexpander_load(elem) {
	if ($(elem).attr("load")) {
		var expander_block = $(elem).next(".jqexpander-block");
		expander_block.html("<img src='/js/images/liload.gif'>");
		$.ajax({
			type: 'get',
			url: $(elem).attr("load"),
			success: function(result){
				expander_block.html(result);
				jqexpander_init(".jqexpander", expander_block);
				$(elem).removeAttr("load")
					.find(".jqexpander-input")
					.data("children", expander_block.find(".jqexpander-input"));
				jqexpander_input_refresh_bottom($(elem).find(".jqexpander-input").data("children"));
			}
		});
	}
}

function jqexpander_input_update(elem, state, dir) {
	if (state == 0) {
		$(elem)
			.addClass("jqexpander-input-unchecked")
			.removeClass("jqexpander-input-partial")
			.removeClass("jqexpander-input-checked")
			.data("checkbox").removeAttr("checked");
	}
	else if (state == 1) {
		$(elem)
			.addClass("jqexpander-input-partial")
			.removeClass("jqexpander-input-checked")
			.removeClass("jqexpander-input-unchecked")
			.data("checkbox").removeAttr("checked");
	}
	else {
		$(elem)
			.addClass("jqexpander-input-checked")
			.removeClass("jqexpander-input-partial")
			.removeClass("jqexpander-input-unchecked")
			.data("checkbox").attr("checked", "checked");
	}

	if (dir != 1) {
		jqexpander_input_refresh_bottom($(elem).data("children"));
	}
	if (dir != -1) {
		jqexpander_input_refresh_top($(elem).data("parent"));
	}
}

function jqexpander_input_refresh_top(elem) {
	$(elem).each(function(){
		if ($(this).data("children").filter(".jqexpander-input-unchecked").length == $(this).data("children").length) {
			if (!$(this).is(".jqexpander-input-unchecked")) {
				jqexpander_input_update(this, 0, 1);
			}
		}
		else if ($(this).data("children").filter(".jqexpander-input-checked").length == $(this).data("children").length) {
			if (!$(this).is(".jqexpander-input-checked")) {
				jqexpander_input_update(this, 2, 1);
			}
		}
		else if (!$(this).is(".jqexpander-input-partial")) {
			jqexpander_input_update(this, 1, 1);
		}
	});
}

function jqexpander_input_refresh_bottom(elem) {
	$(elem).each(function(){
		if ($(this).data("parent").is(".jqexpander-input-checked")) {
			if (!$(this).is(".jqexpander-input-checked")) {
				jqexpander_input_update(this, 2, -1);
			}
		}
		else if ($(this).data("parent").is(".jqexpander-input-unchecked")) {
			if (!$(this).is(".jqexpander-input-unchecked")) {
				jqexpander_input_update(this, 0, -1);
			}
		}
		else if (!$(this).is(".jqexpander-input-partial")) {
			jqexpander_input_update(this, 1, -1);
		}
	});
}

function jqexpander_init(selector, parent) {
	if (!parent) {
		parent = document;
	}

	$(selector, parent).each(function() {
		if ($(this).attr("input_name")) {
			$(this).html("<span class='jqexpander-input jqexpander-input-unchecked'><input type='checkbox' name='" + $(this).attr("input_name") + "' value='" + $(this).attr("input_value") + "' style='display:none'>" + $(this).html() + "</span>");
			var input = $(this).children(".jqexpander-input");
			/*
			if ($(parent).is(".jqexpander-block")) {
				input.data("parent", parent.prev(".jqexpander").children(".jqexpander-input"));
			}
			else {
				input.data("parent", $());
			}
			*/
			input.data("parent", $(this).closest(".jqexpander-block").prev(".jqexpander").children(".jqexpander-input"));
			input.data("children", $());

			input.data("checkbox", input.children("input"));

			input.bind("click", function (event) {
				event.stopPropagation();
				if (!$(this).hasClass("jqexpander-input-checked")) {
					jqexpander_input_update(this, 2);
				}
				else {
					jqexpander_input_update(this, 0);
				}

			});

			if ($(this).attr("input_checked") == 1) {
				jqexpander_input_update(input, 2);
			}
		}

		if (!$(this).is(".jqexpander-activated, .jqexpander-deactivated")) {
			return;
		}

		if ($(this).attr("cookie")) {
			var activated = jqexpander_cookie($(this).attr("cookie"));
			if (activated == 0) {
				$(this)
					.addClass("jqexpander-deactivated")
					.removeClass("jqexpander-activated")
					.next(".jqexpander-block")
						.hide();
			}
			else {
				jqexpander_load(this);

				$(this)
					.addClass("jqexpander-activated")
					.removeClass("jqexpander-deactivated")
					.next(".jqexpander-block")
						.show();
			}
		}
		else {
			if ($(this).hasClass("jqexpander-activated")) {
				jqexpander_load(this);

				$(this)
					.next(".jqexpander-block")
					.show();
			}
			else {
				$(this)
					.addClass("jqexpander-deactivated")
					.next(".jqexpander-block")
					.hide();
			}
		}
	});

	if (parent == document) {
		$(".jqexpander-input", parent).each(function() {

			if ($(this).data("parent").length > 0) {
				$(this).data("parent").data("children").push(this);
			}

		});

		$(".jqexpander-input", parent).each(function() {
			if ($(this).data("children").length == 0) {
				jqexpander_input_refresh_top($(this).data("parent"));
			}
		});
	}

	$(selector, parent).click(function (event) {
		if ($(event.target).is("input") || $(this).is(".jqexpander-disabled") || !$(this).is(".jqexpander-activated, .jqexpander-deactivated")) {
			return;
		}


		if ($(this).hasClass("jqexpander-activated")) {
			$(this)
				.addClass("jqexpander-deactivated")
				.removeClass("jqexpander-activated")
				.next(".jqexpander-block")
					//.slideUp(400);
					.hide();

			if ($(this).attr("cookie")) {
				document.cookie = $(this).attr("cookie") + "=0";
			}
		}
		else {
			jqexpander_load(this);

			$(this)
				.addClass("jqexpander-activated")
				.removeClass("jqexpander-deactivated")
				.next(".jqexpander-block")
					//.slideDown(400);
					.show();

			if ($(this).attr("cookie")) {
				document.cookie = $(this).attr("cookie") + "=1";
			}
		}
	});
}

function jqexpander_cookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
