/* CBB - Custom border and css effects on divs */
/* Adapted from cbb function by Roger Johansson, http://www.456bereastreet.com/, Many thanks !*/

var cbb = {
	init : function() {
		if (cbb.is_disabled()) return;
		cbb.disabled = true;
		cbb.init_ie6();
	// Check that the browser supports the DOM methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oOuter, oI1, oI2, tempId;
	// Find all elements with a class name begining with cbb
		var arrElements = document.getElementsByTagName('*');
		var oRegExp = new RegExp("(^|\\s)cbb_", "g");
		for (var i=0; i<arrElements.length; i++) {
	// Save the original outer element for later
			oElement = arrElements[i];
			if (oRegExp.test(oElement.className)) {
	// 	Create a new element and give it the original element's class name(s) while replacing 'cbb' with 'cb'.
				oOuter = document.createElement('div');
				oOuter.className = oElement.className.replace(oRegExp, '$1');
	// Give the new div the original element's id if it has one
				if (oElement.getAttribute("id")) {
					tempId = oElement.id;
					oElement.removeAttribute('id');
					oOuter.setAttribute('id', '');
					oOuter.id = tempId;
				}
	// Change the original element's class name and replace it with the new div
				oElement.className = '';
				oElement.parentNode.replaceChild(oOuter, oElement);
				// List : 6 divs
				if (oOuter.className.match("(^|\\s)list(\\s|$)")) {
					cbb.insertDivs(oOuter, oElement, Array('lmr', 'ltr', 'lbr', 'lml', 'ltl', 'lbl'));
				// Box
				} else if (oOuter.className.match("(^|\\s)box(\\s|$)")) {
					cbb.insertDivs(oOuter, oElement, Array('r', 'l'));
					cbb.insertBottom(oOuter);
				// Dialog (title)
				} else if (oOuter.className.match("(^|\\s)dlg_title(\\s|$)")) {
					cbb.insertDivs(oOuter, oElement, Array('dtr', 'dtl'));
				// Dialog (content)
				} else if (oOuter.className.match("(^|\\s)dlg_content(\\s|$)")) {
					cbb.insertDivs(oOuter, oElement, Array('dmr', 'dbr', 'dml', 'dbl', 'dc'));
				// Button (link, input or button)
				} else if (oOuter.className.match("(^|\\s)button(\\s|$)")) {
					cbb.insertButton(oOuter, oElement);
				}
			}
		}
	},
	insertDivs : function(oOuter, oElement, divs) {
		// Create new div elements and insert them into the outermost div
				oi = Array();
				for (i=0; i<divs.length; i++) {
					oi[i]= document.createElement('div');
					oi[i].className = divs[i];
					if (i>0)
						oi[i-1].appendChild(oi[i]);
					else
						oOuter.appendChild(oi[i]);
				}
	// Insert the original element
				oi[divs.length-1].appendChild(oElement);
	},
	insertTop : function(obj) {
		var oOuter, oInner;
	// Create the two div elements needed for the top of the box
		oOuter=document.createElement("div");
		oOuter.className="top"; // The outer div needs a class name
	    oInner=document.createElement("div");
	    oOuter.appendChild(oInner);
		obj.insertBefore(oOuter,obj.firstChild);
	},
	insertBottom : function(obj) {
		var oOuter, oInner;
	// Create the two div elements needed for the bottom of the box
		oOuter=document.createElement("div");
		oOuter.className="bot"; // The outer div needs a class name
	    oInner=document.createElement("div");
	    oOuter.appendChild(oInner);
		obj.appendChild(oOuter);
	},
	insertButton : function(oOuter, oElement) {
		br=document.createElement("div");
		br.className="br";
		bl=document.createElement("div");
		bl.className="bl";
		oOuter.appendChild(br);
		oOuter.appendChild(bl);
		oOuter.appendChild(oElement);

		// Add Hover effect for Ie6
		if (cbb.is_ie6) {
			cbb.addEvent(
			 oOuter, 
			 'mouseover', 
			 function() {
				 this.className = this.className.replace(/(\s|^)button(\s|$)/, "$1button-hover$2");
				}
			);
			cbb.addEvent(
			 oOuter, 
			 'mouseout', 
			 function() {
				 this.className = this.className.replace(/(^|\s)button-hover(\s|$)/, "$1button$2");
				 }
			);
		}
		
		// Add Disabling function
		oElement.setDisabled = function(disabled) {
			if (typeof disabled=="undefined")
				disabled=false;
			if (disabled != this.disabled)
				this.disabled = disabled;
			c = this.parentNode.className.replace(/(^|\s)(button|button-disabled)(\s|$)/, (this.disabled ? '$1button-disabled$3' : '$1button$3'));
			if (this.parentNode.className != c)
				this.parentNode.className = c;
		}
		oElement.setDisabled(oElement.disabled);


	},
	// addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	addEvent : function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	},
	is_disabled : function() {
		if (cbb.disabled=='undefined')
			cbb.disabled=0;
		return cbb.disabled;
	},
	init_ie6 : function() {
		cbb.is_ie6 = 0;
		if (navigator.appName=='Microsoft Internet Explorer') {
			v = navigator.appVersion.replace(/.*MSIE ([0-9.]+).*/, '$1');
			if (v < 7) {
				cbb.is_ie6 = 1;
				//alert('IE6');
			}
		}
	}
	
};

cbb.addEvent(window, 'load', cbb.init);
