// JavaScript Document

window.onload=function(){//everything that should happen on load
	var default_text_on_leave = function(){
		if (this.value==''){this.value=this.getAttribute('default_text');}	
	}
	var default_text_on_enter = function(){
		if (this.value==this.getAttribute('default_text')){this.value="";}	
	}
	var inputs = getElementsByClass('default_text');
	add_function(inputs,'blur',default_text_on_leave);
	add_function(inputs,'focus',default_text_on_enter);
}

function add_function(objs,type,fn){
	if (typeof(objs)=='string'){objs = document.getElementsById(objs);}
	if (get_type(objs)=='object'){objs = [objs];}
	for(var i=0;i<objs.length;i++){
		addEvent(objs[i],type,fn);
	}
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		 if (type === 'mouseenter')
			 {obj.addEventListener('mouseover', mouseEnter(fn),false); }
		  else if (type === 'mouseleave')
			 {obj.addEventListener('mouseout', mouseEnter(fn),false); }
		  else {obj.addEventListener( type, fn, false );}
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

/* This non-prototype Mouseenter/Mouseleave function is based on stchur's code
http://blog.stchur.com/2007/03/15/mouseenter-and-mouseleave-events-for-firefox-and-other-non-ie-browsers/
Perhaps I should consider using his whole addEvent function rather than mixing? */

function mouseEnter(_fn){
   return function(_evt){
      var relTarget = _evt.relatedTarget;
      if (this === relTarget || isAChildOf(this, relTarget))
         { return; }

      _fn.call(this, _evt);
   }
};

function isAChildOf(_parent, _child)
{
   if (_parent === _child) { return false; }
      while (_child && _child !== _parent)
   { _child = _child.parentNode; }

   return _child === _parent;
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

//******************* GET ELEMENTS BY CLASSNAME ***********************
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null ) {node = document;}
	if (typeof(node)=='string'){node=document.getElementById(node);}
	if ( tag == null ) {tag = '*';}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function move_node(obj,new_parent){
	var old_node = obj.parentNode.removeChild(obj);
	new_parent.appendChild(old_node);
}

function get_type(v) { /*thanks John Combe! */
  if (typeof(v) == "object") {
    if (v === null) return "null";
    if (v.constructor == (new Array).constructor) return "array";
    if (v.constructor == (new Date).constructor) return "date";
    if (v.constructor == (new RegExp).constructor) return "regex";
    return "object";
  }
  return typeof(v);
}

function get_style(obj,style_property){ //thanks to quirksmode for this one
	if (typeof(obj)=='string'){obj=document.getElementById(obj);};
	if (obj.currentStyle)
		var y = obj.currentStyle[style_property];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(obj,null).getPropertyValue(style_property);
	return y;
}

function show_hide(obj){
	if (typeof(obj)=='string'){obj=document.getElementById(obj);};
	if (obj){
		if (obj.style.display=='block'){obj.style.display='none';}
		else {obj.style.display='block';}
	}
}

