// addLoadEvent function as seen at http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// allows you to stack functions and apply them to the onload event and also means you 
// can abstract onload functions from the html

function addLoadEvent(func) {
	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
// ---
function $(strId){
	return document.getElementById(strId);
}
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---


function highlight_subnav() {
	// Highlight the right subnav item by comparing the URL with
	// the href of each <a> elt in the <div id="subnav">, or by
	// using the hint if supplied as arguments[0]. The hint
	// specifies a part of the URL, useful for actions with
	// lots of paramters
	
	var this_page = document.location.href;
	
	// Remove anchor if there is one -- anything following #, including #
	this_page = this_page.replace( /\#.*/, '' );
	
	var hint = "";
	if (arguments[0]) {
		hint = arguments[0];
	}
	
	
	// Try both subnav and subnavIndent for our item to highlight
	
	var divs = [ "header" ];
	for (var d = 0; d < divs.length; d++) {
	
		if (document.getElementById(divs[d])) {
			var subnav_elts = document.getElementById(divs[d]);
			var aTags = subnav_elts.getElementsByTagName("A");
 			var i = 0;
			for (i = 0; i < aTags.length; i++) {
				// Is this a link?
				var elt = aTags[i]
 					if (elt.href == this_page || (hint && elt.href.indexOf(hint) != -1) ) {
						elt.className = elt.className + " currentsection";
					 
						
						
					}
 			}
		}
	}
		

} 

addLoadEvent(highlight_subnav);


 
function toggle(){
	
	if (!document.getElementById('edit_form')) return false;
	if (!document.getElementById('add_item_btn')) return false;

 	var elm = document.getElementById('edit_form');
	var btn = document.getElementById('add_item_btn');
	var doc = document.getElementById('li_doc');
	
	if (doc.value != ""){
		document.getElementById('doc_preview').innerHTML = doc.value.split("/")[2];
	}

	// if we're editing an item then show the edit area
	var in_edit = new RegExp('ecco_edit_item.py/edit');
	var in_copy = new RegExp('ecco_edit_item.py/copy');
 	var href = document.location.href;
 	if ((in_edit.test(href) == false) && (in_copy.test(href)==false)) {
   	   	elm.style.display = 'none';
	}
	
	// show/hide the content area depending on the current display value
  	btn.onclick = function() {
 		elm.style.display = (elm.style.display=='none') ? 'block' : 'none';
 	}
	
	

}




