if (typeof addLoadEvent == 'function')
{
  addLoadEvent(initAnnouncement);
}
else
{
  window.onload = initAnnouncement;
}

function TheNodeFilter() {
   
}
TheNodeFilter.SHOW_ALL = 0;
TheNodeFilter.SHOW_ELEMENT = 1;
TheNodeFilter.SHOW_TEXT = 3;
TheNodeFilter.SHOW_COMMENT = 8;
TheNodeFilter.FILTER_ACCEPT = true;
TheNodeFilter.FILTER_SKIP = false;

//TheTreeWalker---------------------------------------------------------------
//  TheNodeFilter and TheNodeWalker replecate the functionality of the
//  w3c NodeFilter and NodeWalker objects.  They may be used in place, if
//  a browser does not support the w3c objects.
//-----------------------------------------------------------------------------
function TheTreeWalker(root, show, filter) {
	//properties---------------------------------------------------------------
	this.nodes;
	this.current;
	
	//constructor--------------------------------------------------------------
	this.nodes = methodWalkChildNodes(root, show, filter);
	this.current = 0;
	
	//methods------------------------------------------------------------------
	this.firstChild = methodFirstChild;
	this.lastChild = methodLastChild;
	this.nextNode = methodNextNode;
	this.walkChildNodes = methodWalkChildNodes;

	function methodFirstChild() {
		return this.nodes[0];
	}

	function methodLastChild() {
		return this.nodes[this.nodes.length - 1];
	}

	function methodNextNode() {
		if (this.current < this.nodes.length) {
			return this.nodes[this.current++];
		}
		return null;
	}

	function methodWalkChildNodes(root, show, filter, depth) {
       var obj;
       if (root) {
           if (typeof root == "string") {
               obj = parent.main.document.getElementById(root);
           } else {
               obj = root;
           }
       } else {
           obj = (parent.main.document.body.parentElement) ? 
               parent.main.document.body.parentElement : parent.main.document.body.parentNode;
       }
       var output = new Array();
       var i, group, txt;
       if (!depth) {
           depth = 0;
       }
       group = obj.childNodes;
       for (i = 0; i < group.length; i++) {
           switch (group[i].nodeType) {
               case 1:
                  if (show == TheNodeFilter.SHOW_ALL || show == TheNodeFilter.SHOW_ELEMENT) {
                     if (filter) {
                        if (filter(group[i])) {
                           output.push(group[i]);
                        }
                     } else {
                        output.push(group[i]);
                     }
                  }
                  break;
               case 3:
                  if (show == TheNodeFilter.SHOW_ALL || show == TheNodeFilter.SHOW_TEXT) {
                     if (filter) {
                        if (filter(group[i])) {
                           output.push(group[i]);
                        }
                     } else {
                        output.push(group[i]);
                     }

                  }
                  break;
               case 8:
                  if (show == TheNodeFilter.SHOW_ALL || show == TheNodeFilter.SHOW_COMMENT) {
                     if (filter) {
                        if (filter(group[i])) {
                           output.push(group[i]);
                        }
                     } else {
                        output.push(group[i]);
                     }

                  }
                  break;
               default:
                  if (show == TheNodeFilter.SHOW_ALL) {
                     if (filter) {
                        if (filter(group[i])) {
                           output.push(group[i]);
                        }
                     } else {
                        output.push(group[i]);
                     }

                  }
                  break;
           }
           if (group[i].childNodes.length > 0) {
               var temp = methodWalkChildNodes(group[i], show, filter, depth + 1);
               for (var j = 0; j < temp.length; j++) {
                  output.push(temp[j]);
               }
           }
       }
       return output;
	} 
}

function initAnnouncement() {
	var announcements = document.getElementById('announcements');
	
	if (announcements) {
		var length = document.getElementById("announcement-max-length").value;
		var li = announcements.firstChild;
		
		while (li) {
			var contents = li.firstChild;
			while(contents) {
				if (contents.className == "announcement-content") {
					
					break;
				}
				contents = contents.nextSibling;
			}
			if (contents && length) {
				li.fullContent = contents;
				var full = traverseDOM(contents, length);
				
				if (full == false) {
					appendShow(contents);
				}
				
				//showAll(contents);
			}
			li = li.nextSibling;
		}
	}
}

function appendShow(element) {
	var div = document.createElement("div");
	var anchor = document.createElement("a");
	var text = document.createTextNode("[show full text]");
	var br = document.createElement("br");
	anchor.setAttribute("href", "javascript:void(null)");
	anchor.onclick = function() {
		showAll(element);
		div.style.display = "none";
	}
	anchor.appendChild(text);
	anchor.style.display = "block";
	div.appendChild(anchor);
	div.appendChild(br);
	element.appendChild(div);
}


function traverseDOM(start, limit) {
	var chars = 0;
	var walker = new TheTreeWalker(start, TheNodeFilter.SHOW_ALL);
	var full = true;
	var current = null;
	while (current = walker.nextNode()) {
		if (current.nodeType == 3) {
			re = /\S/;
			if (re.test(current.data)) {
				chars += current.length;
			}
			
		}
		if (chars > limit) {
			if (current.nodeType == 1) {
				current.style.display = "none";
				full = false;
			}
		}
	}
	return full;
}

function showAll(start) {
	var walker = new TheTreeWalker(start, TheNodeFilter.SHOW_ALL);
	
	var current = null;
	while (current = walker.nextNode()) {
		if (current.nodeType == 1) {
			current.style.display = "";
		}
	}
}


