// Design.js  2007-APR-22
window.onerror = trapErrors;
addStyleSheet('/inc/js/css/fixit.css');
showDesign(myDesign);

function myDesign() {
	// The following code block is required
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if (_timer) {
		clearInterval(_timer);
		_timer = null;
	}
	
	// Do DOM manipulation for this design
	mx('content','ppc',2,true,200); 

	// Important: uncloak any DIVs that were cloaked in the stylesheet
	cs('content','display','block');
}

function mx(containerId,mvId,pos,autoSw,iThresh,msg,subNode) {
	if(subNode) cont = subNode;
	else var cont = document.getElementById(containerId);
	var newElm = document.getElementById(mvId);
	if(!newElm || !cont || !pos) return;
	autoSw = autoSw ? autoSw : false;
	iThresh = iThresh ? iThresh : 200;
	var br_count=0,p_count=0,bChars=0,pChars=0,brFlag=false,pBased=false;
	pBased = (document.getElementsByTagName("P").length > 1) ? true : false;
		
	for(var i=0; i < cont.childNodes.length; i++) {
		if(msg) inspect(cont.childNodes[i]);
		cNode = cont.childNodes[i];
			
		// Check node names
		if(cNode.nodeName == 'BR') {
			if(brFlag) br_count++; 
			brFlag = !brFlag;
			
		} else if(cNode.nodeName == '#text') {
			var brProx = 2; // 2 BRs 2 chars apart or less qualify as a double BR. Opera(2), IE(0), Moz(1). Use max (2).
			if(cNode.nodeValue && (cNode.nodeValue.length > brProx)) {
				bChars += cNode.nodeValue.length; // Count chars
				brFlag = !brFlag;
			}
			
		} else if(cNode.nodeName == 'P') {
			p_count++; 
			lastParaNode = cNode;
			for(var j=0; j < cNode.childNodes.length; j++) { // Count chars
				if(cNode.childNodes[j].nodeName == '#text') {
					pChars += cNode.childNodes[j].nodeValue.length;
				}
			}
				
		} else if((cNode.nodeName == 'UL') || (cNode.nodeName == 'OL')) {
			// List encountered? Insert target at bottom of page.
			cont.appendChild(newElm);
			return; 
		}
			
		// If spacing thresholds are met, insert the content. We're done.
		if((pBased && !subNode && ((p_count == pos) || (autoSw && (pChars > iThresh)))) || 
		(!pBased && ((br_count == pos) || (autoSw && (bChars > iThresh)))))	{
			if(cont.childNodes[i + 1]) cNode.parentNode.insertBefore(newElm,cont.childNodes[i + 1]);
			else cont.appendChild(newElm);
			return;
		}
	}
		
	// Spacing thresholds not met. Try something else.
	if(p_count == 1) {
		// If content has only one P we may be dealing w/ double BR spacing. Recurse into P and look for double BRs.
		mx('',mvId,pos,autoSw,iThresh,msg,lastParaNode);
		return;
	} else {
		// Stumped? Insert target at bottom of page.
		cont.appendChild(newElm);
	}
}
	
function inspect(obj) {
	var t = '';
	t += 'nodeName: ' + obj.nodeName + "\n";
	t += obj.nodeValue ? 'nodeValue: ' + obj.nodeValue + "\n" + 'Length:' + obj.nodeValue.length : '';
	alert(t);
}

function cs(id,property,val) { // Change style
	var elm = document.getElementById(id);
	if(elm) elm.style[property] = val;
}

function showDesign(func) {
	if(typeof func != 'function') return;
	_timer = null; // Set global var
	/* for Mozilla */
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", func, false);
	}
	
	/* for Internet Explorer */
	/*@cc_on @*/
	/*@if (@_win32)
		document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
		var script = document.getElementById("__ie_onload");
		script.onreadystatechange = function() {
			if (this.readyState == "complete") {
				func(); // call the onload handler
			}
		};
	/*@end @*/
	
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
		_timer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
				func(); // call the onload handler
			}
		}, 10);
	}
	
	/* for other browsers */
	attachOnloadEvent(func);
}

// attach func function to window onload event (crossbrowser compatible)
function attachOnloadEvent(func) {
	if(typeof window.addEventListener != 'undefined') {
		// moz, saf1.2, ow5b6.1
		window.addEventListener('load', func, false);
	} else if (typeof document.addEventListener != 'undefined') {
		// MSN/OSX, op7.50, saf1.2, ow5b6.1
		document.addEventListener('load', func, false);
	} else if (typeof window.attachEvent != 'undefined') {
		// ie5.0w, ie5.5w, ie6w
		window.attachEvent('onload', func);
	} else {
		// all other browsers
		if (typeof window.onload == 'function') {
			var oldonload = onload;
			window.onload = function() {
				oldonload();
				func();
			};
		} else {
			window.onload = func;
		}
	}
}

function trapErrors() { return true; }

function includeScript(src) {
	document.write('<sc' + 'ript src="' + src + '" type="text/javascript"></sc' + 'ript>');
}

function addStyleSheet(uri) {
	document.write('<link rel="stylesheet" href="' + uri + '" type="text/css" media="all" />');
}
