/*************************************************************************************************
	JavaScript required for Department Of Justice Internet
	- Stylesheet Switcher Script
	- Minimum Width
	- Onload functions
	- Automatic Menu Remover
	- Automatic Publication Space Remover
*************************************************************************************************/

/*------------------------------------------- Stylesheet Switcher Script -------------------------
	documented at: http://www.alistapart.com/articles/alternate/
*/

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


/*------------------------------------ Multiple Onloads Script -----------------------------------
	documented by Simon Willson at: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*/

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/* onLoad events */
//addLoadEvent(something);

/* Start the Stylesheet Switcher Script */
addLoadEvent(function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
});



/*-------------------------------------- Automatic Menu Remover  --------------------------------*/

function removeMenus(columns){	
			
			for (var h = 0; h < columns.length; h++){
		
			var currentColumn = document.getElementById(columns[h]);
			if (currentColumn != null) {
				var children = currentColumn.childNodes;
				for (var i =0;i < children.length;i++){
					//alert(children[i].className);
				
					var currentNode = children[i];
					if (currentNode.className == 'box'){
						//alert('it is a box');
						var boxULs = currentNode.getElementsByTagName('ul');
						//alert("box has " + boxULs.length +" menus");
						for (var j = 0; j < boxULs.length; j++){
							var ulNode = boxULs[j];
							var LIs = ulNode.getElementsByTagName('li');
							//alert("box " + i + " ul " + j + " has " +  LIs.length + " entries");
							if (LIs.length == 0) {
								currentNode.style.display = 'none';
								
							}
						}
					}
				}
			}
			
			
	}
}


/*-------------------------------------- Automatic Publication Space Remover  --------------------------------*/

function hideEmptyAttachments(){
		//this function is used to hide any empty publicactions.
		// if an empty A element exists in an LI in publication div then the LI is set to
		//display none.
	var publ_DIV = document.getElementById('publication');

	if (publ_DIV != null) {
		var firstChild = publ_DIV.childNodes[0]; //this should be the ul element
		var li_elements = firstChild.getElementsByTagName('li');
		for( var i = 0; i < li_elements.length; i++) {
			var li_e = li_elements[i];
			var a_element = li_e.childNodes[0]; //should only ne one in here
			if (a_element.innerHTML == '') {
				//hide
				li_e.style.display='none';
			}
			
		}

	}

}

/*-------------------------------------- Automatic FAQ Remover  --------------------------------*/

function hideEmptyFAQs(){

	var faqParentElement = document.getElementById('two-column-c');
	if (faqParentElement != null) {
		var faqElements = faqParentElement.childNodes;
		for (var i = 0; i < faqElements.length; i++){
	  		var faq_div = faqElements[i];
			//alert(faq_div.outerHTML);
			if (faq_div.className == 'faq'){
				var faq_colElements = faq_div.childNodes;
				var rightCol = faq_colElements[1];
				//alert(rightCol.innerHTML);
				if (rightCol.innerHTML == '') {
					faq_div.style.display = 'none';
				}
			}
		}
	}

}
