/*
 * Widget: BoxWidget
 * Base widget for all widgets using the infrastructure for compiletime box
 * generation. It is important that we are rendered with a width of at least
 * this.left + this.right.
 */

window.theme = "AudentisV";
window.styles = {};
window.development = document.location.href.indexOf('.audentis.eu')>=0;
var nav = navigator.userAgent.toLowerCase();
window.ddie = nav.indexOf("msie") >= 0 && document.all;
window.ddie6 = nav.indexOf("msie 6.") >= 0;
window.ddpng = !ddie6 && document.location.hash != 'png';

$(document).ready(function() {
	var recurse = function(e)
	{
		if (e.className) {
			var cs = e.className.split(' ');
			for(var i=0; i<cs.length; i++)
				if (styles[cs[i]]) styles[cs[i]].call(e);
		}
		for(var n, f=e.firstChild; f; f=n) {
			n= f.nextSibling;
			recurse(f);
		}
	};
	recurse(document.body);
});


var findClass = function(element,className)
{
	if (element.className==className) return element;
	element = element.firstChild;
	while(element) { 
		var r = findClass(element, className);
		if (r) return r;
		element = element.nextSibling;
	};
};

/*
 * Style element using box.
 * mode can be:
 *    'inside' -> embed the box in the element
 *    'replace' -> replace the element with the box (default)
 *    'around' -> wrap the element in the box
 */
var makeBox = function(element, box, mode)
{
	var q="margin:"+((box.textTop!=undefined)?box.textTop-box.top:0)+"px "+((box.textRight!=undefined)?box.textRight-box.borderRight:0)+"px "+((box.textBottom!=undefined)?box.textBottom-box.bottom:0)+"px "+((box.textLeft!=undefined)?box.textLeft-box.borderLeft:0)+"px;position: relative;"+(ddie6?"display: inline-block;":"")+"";
	var p="",l="";
	if(box.ver){
		var s="background: url(/Images/"+theme+"/"+box.ver+") repeat-y top ";
		p=s+"left;";
		l=s+"right;"
	}
	else{
		if(box.borderColor) p=l="background: "+box.borderColor+";"
	}
	var r=box.left-box.borderLeft;
	var k=box.right-box.borderRight;
	if(r<0||k<0 && window.development) {
		alert("BoxWidget ["+(box.anchorName||box.content||"unknown")+"] WARNING: leftDif || rightDif < 0!");
	}
	var cor = "/Images/"+theme+"/"+box.cor;
	var n=box.renderedCols={};
	var m=box.renderedRows={};
	if(box.top!=0){
		m.top="<tr>"+(box.borderLeft||r?'<td colspan="'+((box.borderLeft?1:0)+(r?1:0))+'" style="background: url('+cor+") no-repeat top left; height: "+box.top+'px;"></td>':"")+'<td colspan="'+(1+(box.borderRight?1:0)+(k?1:0))+'" style="background: url('+cor+") no-repeat top right; height: "+box.top+'px;"></td></tr>'
	}
	if(box.bottom!=0){
		m.bottom="<tr>"+(box.borderLeft||r?'<td colspan="'+((box.borderLeft?1:0)+(r?1:0))+'" style="background: url('+cor+") no-repeat bottom left; height: "+box.bottom+'px;"></td>':"")+'<td colspan="'+(1+(box.borderRight?1:0)+(k?1:0))+'" style="background: url('+cor+") no-repeat bottom right; height: "+box.bottom+'px;"></td></tr>'
	}
	
	var tmp = document.createElement('span');
	tmp.innerHTML = "<table cellspacing='0' cellpadding='0' style='border: 0; table-layout: "+(ddie ? "auto" : "fixed")+";'><tbody><tr>"+(box.borderLeft?(n[0]='<td style="height: 1px;" width="'+box.borderLeft+'"></td>'):"")+""+(r?(n[1]='<td style="height: 1px;" width="'+r+'"></td>'):"")+'<td style="height: 1px;" width="*"></td>'+(k?(n[3]='<td style="height: 1px;" width="'+k+'"></td>'):"")+""+(box.borderRight?(n[4]='<td style="height: 1px;" width="'+box.borderRight+'"></td>'):"")+"</tr>"+(m.top||"")+"<tr>"+(box.borderLeft?'<td style="'+p+'"></td>':"")+'<td colspan="'+(1+(r?1:0)+(k?1:0))+'" style="background: '+(box.color||"transparent")+'; "><div class="@inner" style="'+q+'"></div></td>'+(box.borderRight?'<td style="'+l+'"></td>':"")+"</tr>"+(m.bottom||"")+"</tbody></table>";
	var root = tmp.firstChild;

	if (mode!="around" && mode!="inside" && mode!="dropClass") root.className = element.className;

	var inner = findClass(root,'@inner');
	
	if (mode!='around') {
		for(var ne, e = element.firstChild; e; e = ne) {
			ne = e.nextSibling;
			inner.appendChild(e);
		}
	}

	if (mode=='inside') element.appendChild(root);
	else {
		element.parentNode.replaceChild(root, element);
		if (mode=='around') inner.appendChild(element);
	}

	return root;
};


// This is the output of makeBoxes.. DO NOT EDIT.
var boxMainTitle = {right:13,borderLeft:7,"top":126,borderRight:13,bottom:12,color:"#0194d9",cor:(ddpng?"BlueBoxC.png":"BlueBoxBffffff.png"),"name":"boxMainTitle",left:7,textTop:7,ver:(ddpng?"BlueBoxV.png":"BlueBoxBffffff.png")};
var boxMain = {right:13,borderLeft:7,"top":101,borderRight:13,bottom:12,color:"#0194d9",cor:(ddpng?"BlueBoxNoTitleC.png":"BlueBoxNoTitleBffffff.png"),"name":"boxMain",left:7,textTop:7,ver:(ddpng?"BlueBoxNoTitleV.png":"BlueBoxNoTitleBffffff.png")};
var boxPage = {right:10,textRight:0,borderLeft:10,"top":10,textTop:0,borderRight:10,bottom:10,textBottom:7,color:"#ffffff",cor:(ddpng?"WhiteSquareC.png":"WhiteSquareB070c0f.png"),"name":"boxPage",left:10,textLeft:0,ver:(ddpng?"WhiteSquareV.png":"WhiteSquareB070c0f.png")};
var boxButton = {right:4,textRight:6,borderLeft:4,"top":14,textTop:3,borderRight:4,bottom:4,textBottom:3,color:"#0898db",cor:(ddpng?"ButtonC.png":"ButtonBffffff.png"),"name":"boxButton",left:4,textLeft:6,ver:(ddpng?"ButtonV.png":"ButtonBffffff.png")};
var boxMenu = {right:6,textBottom:3,borderLeft:4,"top":20,textLeft:1,borderRight:6,bottom:5,textRight:4,color:"#f68624",cor:(ddpng?"OrangeMenuBarC.png":"OrangeMenuBarBffffff.png"),"name":"boxMenu",left:4,textTop:0,ver:(ddpng?"OrangeMenuBarV.png":"OrangeMenuBarBffffff.png")};


styles.page = function()
{
	this.style.backgroundColor = 'transparent';
	makeBox(this, boxPage, 'inside');
};

styles.contentBlockTable1 = function()
{
	$(this).removeClass('contentBlockTable1');
	makeBox(this, boxMainTitle, 'around');
};

styles.bordered = function()
{
	makeBox(this, boxMain, 'around');
};

styles.buttonBig = function()
{
	this.style.backgroundColor = 'transparent';
	if (ddie) {
		// <utter nonsense>
		this.onclick = function() { document.location.href = this.href; };
		// </utter nonsense>
	}
	makeBox(this, boxButton, 'inside');
};

styles.mainMenu0 = function() {
	var e = makeBox(this, boxMenu);
	e.style.backgroundColor = 'transparent';
}


