/*
* Script : Tabs plugin for DOMAssistant
* Auteur:  Samuel desnoes - http://www.ifacta.fr
* Création : 04 11 2007
* Version : 0.2 - 09 09 2008 ( for DOMAssistant v2.7 )
* Licence : X11 - MIT
* Documentation : http://www.ifacta.fr/labo/scripts/domassistant.boxit/using.html
* Requires DOMAssistant library - http://www.domassistant.com
*/

/*global DOMAssistant, $, $$, window, document*/

DOMAssistant.tabs = function () {
	return {

		tabsCont : null,

		publicMethods : [
			"doTabs"
		],

		doTabs: function (d) {
			var found, first, numZ, idZ, GCont, tabsCont, partsCont, steps, idP, partC, cl, liC, aC, n;
			found = $(this).cssSelect(d);
			if (found.length === 0) {
				return $$(this);
			}
			first = $(found).first();
			/*zone ident.*/
			numZ = $(".DATGpart").length;
			idZ = "DATGpart_" + numZ;
			/*html structure*/
			GCont = $(document.body).create("div", {
				id : idZ,
				className : "DATGpart"
			}, true);
			tabsCont = $$(GCont).create("div", {
				className : "DATtagsBg"
			}, true);
			DOMAssistant.tabs.tabsCont = $$(tabsCont).create("ul", {
				className : "ongletsCont",
				id : "DATtCont_" + numZ
			}, true);
			partsCont = $$(GCont).create("div", {
				className : "partsCont"
			}, true);
			/*steps definition*/
			steps = [];
			$(this).cssSelect(d).each(function () {
				steps.push($$(this));
			});
			steps.push($$(this).next());
			/*adding content and tabs to structure*/
			while (steps.length > 1) {
				/*ident part*/
				idP = numZ + "_" + steps.length;
				/*content display creation*/
				partC = $$(partsCont).create("div", {
					className : "DATpart" + numZ,
					id : "DATpart_" + idP
				}, true);
				/*tabs links creation*/
				cl = $$(steps[0]).innerHTML;
				liC = $$(DOMAssistant.tabs.tabsCont).create("li", {
					className : "DATonglet"
				}, true);
				aC = $$(liC).create("a", {
					href : "#",
					id : "link_" + idP
				}, true, cl);
				/*content and tabs status*/
				if (steps.length - 1 === found.length) {
					$$(partC).setStyle("display", "block");
					$$(liC).addClass("on");
				}
				else {
					$$(partC).setStyle("display", "none");
					$$(liC).removeClass("on");
				}
				$$(aC).addEvent("click", DOMAssistant.tabs.showPart);
				/*Adding content part to display*/
				n = steps.shift();
				while ($$(n) && $$(n) !== $$(steps[0])) {
					$$(partC).addContent($$(n).cloneNode(true));
					$$(n).addClass("toRemove");
					n = $$(n).next();
				}
			}
			$$(this).insertBefore(GCont, first);
			$(".toRemove").remove();
			return $$(this);
		},

		showPart: function () {
			var idP, zT;
			idP = $$(this).id.replace(/link_/, "DATpart_");
			zT = idP.split("_");
			$("div.DATpart" + zT[1]).each(function () {
				if ($$(this) !== $$(idP)) {
					$$(this).setStyle("display", "none");
				}
				else {
					$$(this).setStyle("display", "block");
				}
			});
			$$("DATtCont_" + zT[1]).cssSelect("li.on").removeClass("on");
			$$(this).parentNode.addClass("on");
			return false;
		}

	};

}();

DOMAssistant.attach(DOMAssistant.tabs);
