/*
* Script : boxIt plugin for DOMAssistant
* Auteur:  Samuel desnoes - http://www.ifacta.fr
* Création : 30 04 2008
* Version : 0.4 - 19 08 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
*/

DOMAssistant.boxIt = function () {
	return {
	
		t : null,
		r : null,
		b : null,
		l : null,
		cornerEnd : null,
	
		publicMethods : [
			"boxIt"
		],
		
		boxIt: function (t, r, b, l, cornerEnd) {
			if (typeof t === "undefined" || t !== 0) {
				DOMAssistant.boxIt.t = true;
			}
			else {
				DOMAssistant.boxIt.t = false;
			}
			if (typeof r === "undefined" || r !== 0) {
				DOMAssistant.boxIt.r = true;
			}
			else {
				DOMAssistant.boxIt.r = false;
			}
			if (typeof b === "undefined" || b !== 0) {
				DOMAssistant.boxIt.b = true;
			}
			else {
				DOMAssistant.boxIt.b = false;
			}
			if (typeof l === "undefined" || l !== 0) {
				DOMAssistant.boxIt.l = true;
			}
			else {
				DOMAssistant.boxIt.l = false;
			}
			if(typeof cornerEnd === "undefined" || cornerEnd === 0) {
				DOMAssistant.boxIt.cornerEnd = false;
			}
			else {
				DOMAssistant.boxIt.cornerEnd = true;
			}
			return DOMAssistant.boxIt.doBox($(this));
		},
		
		doBox: function (obj) {
			var classObj = DOMAssistant.boxIt.classParser(obj.className);
			if (classObj[1] !== "") {
				obj.className = classObj[1];
			}
			else {
				$$(obj).removeClass(obj.className);
			}
			var boxCont = $(document.body).create('div', {className : "round_cadre"}, true);
			boxCont = obj.parentNode.insertBefore($$(boxCont), $$(obj));
			var objTag = obj.tagName;
			var imgType = false;
			var spanType = false;
			if (objTag === "IMG") {
				imgType = true;
				var hImg = obj.height;
				var wImg = obj.width;
				$$(boxCont).setStyle({
					width : wImg + "px",
					height : hImg + "px",
					position: "relative"
				});
				$$(boxCont).addContent(obj);
			}
			else if (objTag === "SPAN") {
				spanType = true;
				var wObj = obj.offsetWidth;
			}
			if (classObj[0] !== "") {
				$$(boxCont).addClass(classObj[0]);
			}
			var boxGCont = $(boxCont).create('div', {className : "gCont" + ((DOMAssistant.boxIt.r === false) ? " noBg" : "")}, true);
			var topContent = (DOMAssistant.boxIt.cornerEnd === true || DOMAssistant.boxIt.l === true) ? "<div class=\"top" + ((DOMAssistant.boxIt.r === false) ? " noBg" : "") + "\"><div>&nbsp;</div></div>" : "<div class=\"top" + ((DOMAssistant.boxIt.r === false) ? " noBg" : "") + "\">&nbsp;</div>";
			if (DOMAssistant.boxIt.t === true || DOMAssistant.boxIt.cornerEnd === true ) {
				var topCont = $(boxGCont).create('div', {className : "topCont" + ((DOMAssistant.boxIt.t === false) ? " noBg" : "")}, true, topContent);
			}
			var midCont = $(boxGCont).create('div', {className : "midCont" + ((DOMAssistant.boxIt.l === false) ? " noBg" : "")}, true, ((imgType === true) ? "&nbsp;" : obj));
			var botContent = (DOMAssistant.boxIt.cornerEnd === true || DOMAssistant.boxIt.l === true) ? "<div class=\"bot" + ((DOMAssistant.boxIt.r === false) ? " noBg" : "") + "\"><div>&nbsp;</div></div>" : "<div class=\"bot" + ((DOMAssistant.boxIt.r === false) ? " noBg" : "") + "\">&nbsp;</div>";
			if (DOMAssistant.boxIt.b === true || DOMAssistant.boxIt.cornerEnd === true) {
				var botCont = $(boxGCont).create('div', {className : "botCont" + ((DOMAssistant.boxIt.b === false) ? " noBg" : "")}, true, botContent);
			}
			if (imgType === true) {
				var hMid = hImg;
				hMid -= (DOMAssistant.boxIt.t === true || DOMAssistant.boxIt.cornerEnd === true) ? parseInt($$(topCont).getStyle("height")) : 0;
				hMid -= (DOMAssistant.boxIt.b === true || DOMAssistant.boxIt.cornerEnd === true) ? parseInt($$(botCont).getStyle("height")) : 0;
				$$(obj).setStyle({
					position : "absolute",
					top : 0,
					left : 0
				});
				$$(boxGCont).setStyle({
					height : "100%",
					position : "absolute",
					top : 0,
					left : 0
				});
				$$(midCont).setStyle({
					height : hMid + "px",
					padding : 0
				});
			}
			else if (spanType === true) {
				var add = parseInt($$(midCont).getStyle("margin-right")) + parseInt($$(midCont).getStyle("padding-left"));
				var wGlob = wObj + add;
				$(boxCont, boxGCont).setStyle({
					width : wGlob + "px"
				});
			}
			return boxCont;
		},
		
		classParser: function (str) {
			if (!str) {
				return [ "", "" ];
			}
			var outerClass = "";
			var innerClass = "";
			var arrayStr = str.split(" ");
			if (arrayStr.length > 0) {
				var add = "";
				for (var i = 0, il = arrayStr.length; i < il; i++) {
					if (arrayStr[i].match(/^cnt_/)) {
						innerClass += add + arrayStr[i].slice(4);
					}
					else {
						outerClass += add + arrayStr[i];
					}
					add = " ";
				}
			}
			return [ outerClass, innerClass ];
		}
		
	};
	
}();

DOMAssistant.attach(DOMAssistant.boxIt);