/*
* Script : keepItClear plugin for DOMAssistant
* Auteur:  Samuel desnoes - http://www.ifacta.fr
* Création : 20 10 2009
* Version : 0.1 - 20 10 2009 ( for DOMAssistant v2.7 )
* Licence : X11 - MIT
* Documentation : http://www.ifacta.fr/labo/scripts/domassistant.keepItClear/using.html
* Requires DOMAssistant library - http://www.domassistant.com
*/

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

DOMAssistant.keepItClear = function () {
	return {
		optionsEl: [],
		defaultOptions: {anim : "ltr", closeLink : "Fermer", title : "Voir l'intégralité", keepVisibility : false},
		timerAnime: [],
		
		publicMethods : [
			"do_keepitclear"
		],
		
		do_keepitclear : function (optionsV) {
			if (typeof optionsV.anim === "undefined") {
				optionsV.anim = DOMAssistant.keepItClear.defaultOptions.anim;
			}
			if (typeof optionsV.closeLink === "undefined") {
				optionsV.closeLink = DOMAssistant.keepItClear.defaultOptions.closeLink;
			}
			if (typeof optionsV.keepVisibility === "undefined") {
				optionsV.keepVisibility = DOMAssistant.keepItClear.defaultOptions.keepVisibility;
			}
			var elId, pos, posT, posL, minW, minH, maxW, maxH, titleEl, titleDef, linkEl, exContent, linkContent, closeLinkContainer, contentWidth, contentHeight;
			elId = "clRel_" + $("a.clRelLink").length;
			//$$(this).id = elId;
			//position verticale
			posT = $$(this).offsetTop;
			//alert(posT);//debug
			
			//alert(posL);//debug
			//largeur max
			maxW = $$(this).parentNode.offsetWidth;
			//alert(maxW);//debug
			//position horizontale
			pos = DOMAssistant.keepItClear.getPos($$(this));
			posL = (optionsV.anim === "ltr") ? pos.posX : (pos.posX + maxW);
			//hauteur max
			maxH =  $$(this).offsetHeight;
			if (typeof optionsV.title === "undefined") {
				titleEl = $$(this).cssSelect("span.title");
				titleDef = (titleEl.length > 0) ? titleEl.first().innerHTML : DOMAssistant.keepItClear.defaultOptions.title;
			}
			else {
				titleDef = optionsV.title;
			}
			linkEl = $(document.body).create("a", {
				className : "clRelLink",
				href : "#",
				rel : elId
			}, 0, titleDef);
			exContent = "<div class=\"clRelContent\">" + $$(this).innerHTML + "</div>";
			$$(this).replaceContent(linkEl);
			linkContent = $(document.body).create("div", {
				id : elId,
				className : "clRelContainer"
			}, 1, exContent);
			closeLinkContainer = $$(linkContent).cssSelect("div.clRelContent").first().create("div",null, 1).setStyle({
				"text-align" : (optionsV.anim === "ltr") ? "right" : "left"
			});
			$$(closeLinkContainer).create("a", {
				href : "#",
				rel : elId
			}, 1, optionsV.closeLink).addEvent("click", function(e) {
				DOMAssistant.preventDefault(e);
				var idParent = $$(this).rel;
				DOMAssistant.keepItClear.startClose(idParent);
				return false;
			});
			
			linkEl = $$(this).cssSelect("a.clRelLink").first();
			//largeur min du lien
			minW = $$(linkEl).offsetWidth;
			
			$$(linkEl).setStyle({
				display : "block",
				width : minW + "px"
			});
			
			//hauteur min du lien
			minH = $$(linkEl).offsetHeight;
			
			/*si l'option lien visible est active, diminuer la largeur d'affichage max de la taille du lien et augmenter le positionnement gauche de la même valeur*/
			if (optionsV.keepVisibility === true) {
				maxW -= (minW + 10)
				posL += (optionsV.anim === "ltr") ? (minW + 10) : 0;
			}
			
			contentWidth = $$(linkContent).offsetWidth;
			contentHeight = $$(linkContent).offsetHeight;
			
			DOMAssistant.keepItClear.optionsEl.push(elId);
			DOMAssistant.keepItClear.optionsEl[elId] = {
				minW:  minW,
				minH: minH,
				posT: posT,
				posL: posL,
				maxW: maxW,
				maxH: maxH,
				anim: optionsV.anim
			};
			
			//alert(DOMAssistant.keepItClear.optionsEl[elId]);//debug
			
			$$(linkEl).addEvent("click", function (e) {
				//alert($$(this).parentNode.offsetWidth);//debug
				DOMAssistant.preventDefault(e);
				var idParent, targetEl, targetPos;
				idParent = $$(this).rel;
				targetEl = e.target || e.srcElement;
				while ($$(targetEl).tagName !== "A") {
					targetEl = $$(targetEl).parentNode;
				}
				targetPos = DOMAssistant.keepItClear.getPos(targetEl);
				//alert(targetEl.tagName + "\n" + targetPos.posY);//debug
				DOMAssistant.keepItClear.optionsEl[idParent].posT = targetPos.posY;
				//DOMAssistant.keepItClear.optionsEl[idParent].posL = targetPos.posX;
				$$(idParent).setStyle({
					top : DOMAssistant.keepItClear.optionsEl[idParent].posT + "px",
					left : DOMAssistant.keepItClear.optionsEl[idParent].posL + "px"
				});
				if (DOMAssistant.keepItClear.timerAnime[idParent]) {
					clearTimeout(DOMAssistant.keepItClear.timerAnime[idParent]);
				}
				if ($$(idParent).offsetWidth === 0) {
					DOMAssistant.keepItClear.startOpen(idParent);
				}
				else {
					DOMAssistant.keepItClear.startClose(idParent);
				}
				return false;
			})
			
			$$(this).setStyle({
				width : minW + "px",
				height : minH + "px"
			});
			
			/*$$(this).next().setStyle({
				"padding-top": $$(linkEl).offsetHeight + "px"
			});*/
			
			$$(linkContent).setStyle({
				position: "absolute",
				width: 0,
				height: 0,
				overflow: "hidden"
			});
			
			/*$$(linkContent).cssSelect("div.clRelContent").first().setStyle({
				width : maxW + "px",
				height: maxH + "px"
			});*/
			
			return $$(this);
		},
		
		startOpen: function (el) {
			/*fermeture des blocs éventuellement visibles*/
			$("div.clRelContainer").setStyle({
				width: 0,
				height: 0,
				overflow: "hidden"
			})
			DOMAssistant.keepItClear.anime(el, 1);
		},
		
		startClose: function (el) {
			DOMAssistant.keepItClear.anime(el, -1);
		},
		
		anime : function (el, sens) {
			//alert(el);//debug
			var obj, speedW, speedH, targetW, targetH, curW, curH, curPosL, moveW, moveH, done;
			obj = $$(el);
			//alert(obj);//debug
			if (DOMAssistant.keepItClear.optionsEl[el].maxW >= DOMAssistant.keepItClear.optionsEl[el].maxH) {
				speedW = 50;
				speedH = Math.floor(speedW / (DOMAssistant.keepItClear.optionsEl[el].maxW / DOMAssistant.keepItClear.optionsEl[el].maxH));
			}
			else {
				speedH = 50;
				speedW = Math.floor(speedH / (DOMAssistant.keepItClear.optionsEl[el].maxH / DOMAssistant.keepItClear.optionsEl[el].maxW));
			}
			//alert(speedW + "\n" + speedH);//debug
			curW = $$(obj).offsetWidth;
			curH = $$(obj).offsetHeight;
			//alert(DOMAssistant.keepItClear.optionsEl[el].maxW);//debug
			if (curW === DOMAssistant.keepItClear.optionsEl[el].maxW || curH === DOMAssistant.keepItClear.optionsEl[el].maxH) {
				speedW = speedW * 2;
				speedH = speedH * 2;
			}
			if (sens > 0) {
				moveW = (curW + speedW <= DOMAssistant.keepItClear.optionsEl[el].maxW) ? curW + speedW : DOMAssistant.keepItClear.optionsEl[el].maxW;
				moveH = (curH + speedH <= DOMAssistant.keepItClear.optionsEl[el].maxH) ? curH + speedH : DOMAssistant.keepItClear.optionsEl[el].maxH;
			}
			else {
				moveW = (curW - speedW >= 0) ? curW - speedW : 0;
				moveH = (curH - speedH >= 0) ? curH - speedH : 0;
			}
			
			$$(obj).setStyle({
				width : moveW + "px",
				height : moveH + "px",
				left : ((DOMAssistant.keepItClear.optionsEl[el].anim === "rtl") ? DOMAssistant.keepItClear.optionsEl[el].posL - moveW : DOMAssistant.keepItClear.optionsEl[el].posL) + "px"
			});
			
			if ((sens > 0 && moveW === DOMAssistant.keepItClear.optionsEl[el].maxW && moveH === DOMAssistant.keepItClear.optionsEl[el].maxH) || (sens < 0 && moveW === 0 && moveH === 0)) {
				done = true;
			}
			else {
				done = false;
			}
			//alert("largeur max : " + DOMAssistant.keepItClear.optionsEl[el].maxW + "\nhauteur max : " + DOMAssistant.keepItClear.optionsEl[el].maxH + "\nsens : " + sens + "\nlargueur en cours : " + curW + "\nhauteur en cours : " + curH + "\nlargeur suivante : " + moveW + "\nhauteur suivante : " + moveH + "\nfini ? " + done);//debug
			if (!DOMAssistant.keepItClear.timerAnime[el] || done === false) {
				if (sens < 0) {
					$$(el).setStyle({
						overflow : "hidden"
					});
				}
				DOMAssistant.keepItClear.timerAnime[el] = setTimeout("DOMAssistant.keepItClear.anime('" + el + "', " + sens + ")", 20);
			}
			else {
				clearTimeout(DOMAssistant.keepItClear.timerAnime[el]);
				if (sens > 0) {
					//window.scrollTo(DOMAssistant.keepItClear.optionsEl[el].posL, DOMAssistant.keepItClear.optionsEl[el].posT);
					$$(el).setStyle({
						overflow : "visible"
					});
				}
			}
		},
		
		getPos : function (el) {
			var px, py;
			px = 0;
			py = 0;
			while (typeof el !== "undefined" && el !== null) {
				px += el.offsetLeft;
				py += el.offsetTop;
				el = el.offsetParent;
			}
			return {posX:px, posY:py};
		}
	};
}();
DOMAssistant.attach(DOMAssistant.keepItClear);
