
var SAMEPAGE = {
		wiki : {}, 
		blog : {}, 
		utils: {
			i18n : {},
			toc : {
				arrayofTocs: new Array(),
				counter: 0
			},
			tree : {}
		} //end utils
	};


SAMEPAGE.utils.i18n.generalMsgLocalizer = {
	getMessage: function(key, args) {
		var _result;
		try {
			_result = generalMsgs[key];
		} catch(err) {
		}
		if (typeof _result !="string") {
			_result = key;
		}
		if (typeof args != "undefined" && 
					(args.constructor.toString().indexOf("Array") != -1)) {
			for (var i=0; i< args.length; i++) {
				var regEx = new RegExp("\\{"+i+"\\}");
				_result = _result.replace(regEx,args[i])
			}
		}
		return _result;
	}
}

SAMEPAGE.utils.i18n.editorCustomMsgLocalizer = {
	getMessage: function(key) {
		var _result;
		try{
		 	_result=editorI18nCustomMsgs[key];
		}catch (err) {
		}
		if (typeof _result !="string") {
			_result = key;
		}
		return _result;
	}
}

SAMEPAGE.utils.toc.createTableOfContents = function(showdiv,tocId,divborderwidth,divborderstyle,
			divbordercolor,divbgcolor,shownumbering,showheader,showanchor) {
	var style="";
	if(typeof divbgcolor == "undefined" || divbgcolor == "") {
	   divbgcolor="#f9f9f9";
	}
	if(typeof showheader == "undefined" || showheader == "") {
		showheader="no";
	}

	if(typeof showanchor == "undefined" || showanchor == "") {
		showanchor="no";
	}
	
	if(trim(showdiv.toLowerCase()) == "yes") {
		if(typeof divborderwidth == "undefined" || divborderwidth == "") {
		   divborderwidth="1px";
		}
		if(typeof divborderstyle == "undefined" || divborderstyle == "") {
		   divborderstyle="solid";
		}
		if(typeof divbordercolor == "undefined" || divbordercolor == "") {
		   divbordercolor="#CCCCCC";
		}
		if(typeof divbgcolor == "undefined" || divbgcolor == "") {
		   divbgcolor="#f9f9f9";
		}
		style="border: "+divborderwidth+" "+divborderstyle+" "+
			divbordercolor+";padding: 5px;font-size: 80%;background: "+
			divbgcolor+";"
	}
	
	document.write("<div id="+tocId+" style=\""+style+"\" class=toc></div>");
	var obj = new Object();
	obj.tocid = tocId;
	obj.shownumbering = shownumbering;
	obj.showheader = showheader;
	obj.showanchor = showanchor;
	this.arrayofTocs[arrayofTocs.length]=obj;

} 

SAMEPAGE.utils.tree.autoInit_tree = function (id)
{
	//alert("id "+id);
	var candidates = document.getElementsByTagName('ul');
	for(var i=0;i<candidates.length;i++) {
		if(candidates[i].className && candidates[i].className.indexOf('tree') != -1) {
			this.initTree(candidates[i]);
			candidates[i].className = candidates[i].className.replace(/ ?unformatted ?/, ' ');
		}
	}
}

/*
 * Initialise a tree node, converting all its LIs appropriately
 */
SAMEPAGE.utils.tree.initTree = function(el) {
	var i,j;
	var spanA, spanB, spanC;
	var startingPoint, stoppingPoint, childUL;
	
	// Find all LIs to process
	for(i=0;i<el.childNodes.length;i++) {
		if(el.childNodes[i].tagName && el.childNodes[i].tagName.toLowerCase() == 'li') {
			var li = el.childNodes[i];

			// Create our extra spans
			spanA = document.createElement('span');
			spanB = document.createElement('span');
			spanC = document.createElement('span');
			spanA.appendChild(spanB);
			spanB.appendChild(spanC);
			spanA.className = 'a ' + li.className.replace('closed','spanClosed');
			spanA.onMouseOver = function() {}
			spanB.className = 'b';
			spanB.onclick = this.treeToggle;
			spanC.className = 'c';
			
			
			// Find the UL within the LI, if it exists
			stoppingPoint = li.childNodes.length;
			startingPoint = 0;
			childUL = null;
			for(j=0;j<li.childNodes.length;j++) {
				if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'div') {
					startingPoint = j + 1;
					continue;
				}

				if(li.childNodes[j].tagName && li.childNodes[j].tagName.toLowerCase() == 'ul') {
					childUL = li.childNodes[j];
					stoppingPoint = j;
					break;					
				}
			}
				
			// Move all the nodes up until that point into spanC
			for(j=startingPoint;j<stoppingPoint;j++) {
				spanC.appendChild(li.childNodes[startingPoint]);
			}
			
			// Insert the outermost extra span into the tree
			if(li.childNodes.length > startingPoint) li.insertBefore(spanA, li.childNodes[startingPoint]);
			else li.appendChild(spanA);
			
			// Process the children
			if(childUL != null) {
				if(this.initTree(childUL)) {
					this.addClass(li, 'children', 'closed');
					this.addClass(spanA, 'children', 'spanClosed');
				}
			}
		}
	}
	
	if(li) {
		// li and spanA will still be set to the last item

		this.addClass(li, 'last', 'closed');
		this.addClass(spanA, 'last', 'spanClosed');
		return true;
	} else {
		return false;
	}
		
}

SAMEPAGE.utils.tree.addClass = function(el, cls, forceBefore) {
	if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) {
		el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore);

	} else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) {
		el.className += ' ' + cls;
		el.className = el.className.replace(/(^ +)|( +$)/g, '');
	}
}

/*
 * +/- toggle the tree, where el is the <span class="b"> node
 * force, will force it to "open" or "close"
 */
SAMEPAGE.utils.tree.treeToggle = function(el, force) {
	el = this;
	
	while(el != null && (!el.tagName || el.tagName.toLowerCase() != "li")) el = el.parentNode;
	
	// Get UL within the LI
	var childSet = SAMEPAGE.utils.tree.findChildWithTag(el, 'ul');
	var topSpan = SAMEPAGE.utils.tree.findChildWithTag(el, 'span');

	if( force != null ){
		
		if( force == "open"){
			SAMEPAGE.utils.tree.treeOpen( topSpan, el )
		}
		else if( force == "close" ){
			SAMEPAGE.utils.tree.treeClose( topSpan, el )
		}
		
	}
	
	else if( childSet != null) {
		// Is open, close it
		if(!el.className.match(/(^| )closed($| )/)) {		
			SAMEPAGE.utils.tree.treeClose( topSpan, el )
		// Is closed, open it
		} else {			
			SAMEPAGE.utils.tree.treeOpen( topSpan, el )
		}
	}
}

SAMEPAGE.utils.tree.treeOpen = function( a, b ){
	this.removeClass(a,'spanClosed');
	this.removeClass(b,'closed');
}
	
	
SAMEPAGE.utils.tree.treeClose = function( a, b ){
	this.addClass(a,'spanClosed');
	this.addClass(b,'closed');
}

/*
 * Find the a child of el of type tag
 */
SAMEPAGE.utils.tree.findChildWithTag = function (el, tag) {
	for(var i=0;i<el.childNodes.length;i++) {
		if(el.childNodes[i].tagName != null && el.childNodes[i].tagName.toLowerCase() == tag) return el.childNodes[i];
	}
	return null;
}

/*
 * Functions to add and remove class names
 * Mac IE hates unnecessary spaces
 */
SAMEPAGE.utils.tree.addClass = function(el, cls, forceBefore) {
	if(forceBefore != null && el.className.match(new RegExp('(^| )' + forceBefore))) {
		el.className = el.className.replace(new RegExp("( |^)" + forceBefore), '$1' + cls + ' ' + forceBefore);

	} else if(!el.className.match(new RegExp('(^| )' + cls + '($| )'))) {
		el.className += ' ' + cls;
		el.className = el.className.replace(/(^ +)|( +$)/g, '');
	}
}

SAMEPAGE.utils.tree.removeClass = function(el, cls) {
	var old = el.className;
	var newCls = ' ' + el.className + ' ';
	newCls = newCls.replace(new RegExp(' (' + cls + ' +)+','g'), ' ');
	el.className = newCls.replace(/(^ +)|( +$)/g, '');
} 

