// OHSU common.js
// Paul Farning- ISITE Design

// Event Listener
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		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];
	}
}

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);

// dropdowns for ie
/*startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace(" over", "");
   }
   }
  }
 }
}
addEvent(window,'load',startList);
*/

// set/reset value of inputs
// convert to jquery
function resetinputs() {
	if (!document.getElementById) return ;
	if (!document.getElementById("search")) return false;
	var searchbox = document.getElementById("search");
	if (searchbox) {
		searchbox.onfocus = function () {
			if(this.value == 'Search') { this.value = ''; }
		}
		searchbox.onblur = function() {
			if(this.value.length == 0) { this.value = 'Search'; }
		}		
	}

	if (!document.getElementById("person")) return false;
	var personbox = document.getElementById("person");
	if (personbox) {
		personbox.onfocus = function () {
			if(this.value == 'Enter Name') { this.value = ''; }
		}
		personbox.onblur = function() {
			if(this.value.length == 0) { this.value = 'Enter Name'; }
		}
	}

	if (!document.getElementById("search_dept")) return false;
	var personbox = document.getElementById("search_dept");
	if (personbox) {
		personbox.onfocus = function () {
			if(this.value == 'Search Healthcare') { this.value = ''; }
		}
		personbox.onblur = function() {
			if(this.value.length == 0) { this.value = 'Search Healthcare'; }
		}
	}
}



addEvent(window,'load',resetinputs);


function addButtonHover() {
  if (!document.getElementsByTagName) return false;
  var buttons  = document.getElementsByTagName('button');
  if (buttons[0]) {
    for (var i = 0; i < buttons.length; i++){
      buttons[i].onmouseover  = function() { this.className += " over"; }
      buttons[i].onmouseout   = function() { this.className = this.className.replace("over",""); }
      buttons[i].onfocus      = function() { this.className += " over"; }
      buttons[i].onblur	      = function() { this.className = this.className.replace("over",""); }
    }
  }
}
addEvent(window,'load',addButtonHover);

// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
// Use this function when you have a SELECT box of values and a text
// input box with a fill-in value. Often, onChange of the SELECT box
// will fill in the selected value into the text input (working like
// a Windows combo box). Using this function, typing into the text
// box will auto-select the best match in the SELECT box and do
// auto-complete in supported browsers.
// Arguments:
// field = text input field object
// select = select list object containing valid values
// property = either "text" or "value". This chooses which of the
// SELECT properties gets filled into the text box -
// the 'value' or 'text' of the selected option
// forcematch = true or false. Set to 'true' to not allow any text
// in the text box that does not match an option. Only
// supported in IE (possible future Netscape).
// -------------------------------------------------------------------
function autoComplete (field, select, property, forcematch) {
 	var found = false;
 	for (var i = 0; i < select.options.length; i++) {
 	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
 	found=true; break;
 	}
 }
 	if (found) { select.selectedIndex = i; }
	 else { select.selectedIndex = -1; }
 		if (field.createTextRange) {
 		if (forcematch && !found) {
 		field.value=field.value.substring(0,field.value.length-1);
 	return;
 }
 	var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		 if (cursorKeys.indexOf(event.keyCode+";") == -1) {
 	var r1 = field.createTextRange();
 	var oldValue = r1.text;
 	var newValue = found ? select.options[i][property] : oldValue;
	 if (newValue != field.value) {
 		field.value = newValue;
	 var rNew = field.createTextRange();
 		rNew.moveStart('character', oldValue.length) ;
 		rNew.select();
 			}
 		}
 	}
}

//start jquery functions
$(document).ready(function() {

		//open links with rel="external" in a new window.  ick.
		$("a[@rel='external'], a[@href^=http://www]").not( $("a[@href^=http://www.ohsu.edu]") ).click( function () {
			if(this.className == 'prevnextbutton' || this.id == 'picohelp') return;
			window.open(this.href);
			return false;
		});


}); //doc ready


//indicate that external sidebar links are external.
// background image was set on LI elements so we're using parent() to get to it.


