// LiveLookup
// (c) 2007 University of Cincinnati
// constructor : new LiveLookup(textbox)
// mode = what we're looking up.
// 1 = CEEB
// 2 = Majors
// textbox = reference to the textbox element to attach to
// dropText = the "start typing..." text

var robj=new Array();

function LLVal(value, name, city, state) {
this.Value=value;
this.Name=name;
this.City=city;
this.State=state;
}

Function.prototype.bind = function (object) {
 var method = this;
 return function () {
 	return method.apply(object, arguments);
 };
};

function LiveLookup(mode, textbox, dropText, defaultValues, nextEl, clgFilter) {
this.q=textbox;
this.m=null;
this.mi=null;
this.mp=null;
this.kc=null;
this.si=null;
this.stmr=null;
this.lastSearch=null;
this.selVal=0;
this.selName=null;
this.results=new Array();
this.mf=null;
this.wClick=false;
this.shiftDown=false;
this.r=Math.random();
this.dt=dropText;
this.def=defaultValues;
this.nel=nextEl;
this.clg=clgFilter;

this._mode=mode;



this.m = document.createElement("div");
this.m.id="qmenu" + this.r;
this.m.className="qmenu";
this.m.innerHTML='<i style="color:#a0a0a0;">' + this.dt + '</i>';
if (typeof this.def == 'object') {
if (this.def != null) {
for (i=0; i<this.def.length; i++) {
this.LLresult(this.def[i].Value, this.def[i].Name, this.def[i].City, this.def[i].State);
}
}
}
//document.getElementById('hdr').appendChild(this.m);
document.body.appendChild(this.m);
this.q.autocomplete='off';

this.q.blur();
this.m.style.display='none';



var _qf=this.qFocus.bind(this);
if (window.attachEvent) {
	this.q.attachEvent("onfocus", _qf);
} else {
	this.q.addEventListener("focus", _qf, false);
}

var _qb=this.qBlur.bind(this);
if (window.attachEvent) {
	this.q.attachEvent("onblur", _qb);
} else {
	this.q.addEventListener("blur", _qb, false);
}

var _qkd=this.qKeyDown.bind(this);
if (window.attachEvent) {
	this.q.attachEvent("onkeydown", function() { _qkd(window.event); });
} else {
	this.q.addEventListener("keydown", _qkd, false);
}
var _qku=this.qKeyUp.bind(this);
if (window.attachEvent) {
	this.q.attachEvent("onkeyup", function() { _qku(window.event); });
} else {
	this.q.addEventListener("keyup", _qku, false);
}

var _mev=this.mouseevent.bind(this);
if (document.addEventListener) {
document.addEventListener("mousedown", _mev, false );
} else if (document.attachEvent) {
document.attachEvent("onmousedown", function () { _mev(window.event); } );
}


}

LiveLookup.prototype.Dispose=function() {  // Works fine in Mozilla but gets all kinds of wacky behavior in IE7 if you reattch a new LL object to a field that had a disposed object.
var _qf=this.qFocus.bind(this);
if (window.detachEvent) {
	this.q.detachEvent("onfocus", _qf);
} else {
	this.q.removeEventListener("focus", _qf, false);
}

var _qb=this.qBlur.bind(this);
if (window.detachEvent) {
	this.q.detachEvent("onblur", _qb);
} else {
	this.q.removeEventListener("blur", _qb, false);
}

var _qkd=this.qKeyDown.bind(this);
if (window.detachEvent) {
	this.q.attachEvent("onkeydown", function() { _qkd(window.event); });
} else {
	this.q.removeEventListener("keydown", _qkd, false);
}
var _qku=this.qKeyUp.bind(this);
if (window.detachEvent) {
	this.q.detachEvent("onkeyup", function() { _qku(window.event); });
} else {
	this.q.removeEventListener("keyup", _qku, false);
}

var _mev=this.mouseevent.bind(this);
if (document.addEventListener) {
document.removeEventListener("mousedown", _mev, false );
} else if (document.detachEvent) {
document.detachEvent("onmousedown", function () { _mev(window.event); } );
}
}

LiveLookup.prototype.qFocus=function() {
this.wClick=false;
if (this.selVal != 0) this.q.select(); else this.q.value='';
this.mSelect(0); this.qKeyUp();
}

LiveLookup.prototype.qBlur=function() {
if (!this.wClick) this.m.style.display='none';
if (this.selVal != 0 && this.q.value == this.selName) this.q.value=this.selName; else { this.q.value=''; this.selVal=0; }
}

LiveLookup.prototype.mSelect=function(i) {
try { document.getElementById('cr'+this.r+this.si).className='qmenuitem'; } catch(e) { }
try { document.getElementById('cr'+this.r+i).className='qmenuitemselected'; } catch(e) { }
//try { } catch(e) { }
 if (i > 0) this.m.scrollTop=document.getElementById('cr'+this.r+i).offsetTop - 40;
this.si=i;
}

LiveLookup.prototype.qKeyDown=function(e) {
this.kc=e.keyCode;
var ni;
if (this.kc == 13) {
		  e.cancelBubble = true;
 		  e.returnValue = false;
		  
		  this.setVal(this.si);
		  
} else if (this.kc == 9) { //tab
		  e.cancelBubble = false;
 		  e.returnValue = true;
		  
		  if (this.si > 0) this.setVal(this.si);
		  
		  //if (this.shiftDown) { $('zip').focus(); shiftDown=false; } else $('gradyear').focus();
		  
} else if (this.kc == 16) { //shift
this.shiftDown=true;
} else if (this.kc == 40) { //dn
		  e.cancelBubble = true;
 		  e.returnValue = false;
ni=this.si+1;
if (ni > this.mi) ni=this.mi;
this.mSelect(ni);
} else if (this.kc == 38) { //up
		  e.cancelBubble = true;
 		  e.returnValue = false;
ni=this.si-1;
if (ni < 1) ni=1;
this.mSelect(ni);
}
}

LiveLookup.prototype.qKeyUp=function(e) {
try { this.kc=e.keyCode; } catch(e) { this.kc=0; }
if (this.kc == 16) this.shiftDown=false;
if (this.kc != 13 && this.kc != 40 && this.kc != 38) {
this.m.style.display='block';
if (this.q.value.length > 2) {
this.positionDrop();
if (this.m.style.display != 'block') { this.m.innerHTML='<i style="color:#a0a0a0;">Loading...</i>'; }
this.qv=this.q.value;
clearTimeout(this.stmr);
this.stmr=setTimeout(this.goSearch.bind(this), 350);
} else {
this.positionDrop();
this.LLinit();
this.m.innerHTML='<i style="color:#a0a0a0;">' + this.dt + '</i>';
if (typeof this.def == 'object') {
if (this.def != null) {
for (i=0; i<this.def.length; i++) {
this.LLresult(this.def[i].Value, this.def[i].Name, this.def[i].City, this.def[i].State);
}
}
}
}
}
}

LiveLookup.prototype.goSearch=function() {
clearTimeout(this.stmr);
if (this.lastSearch != 0) cancelRequest(this.lastSearch);
var rr=0;
while (rr == 0) {
rr=Math.random();
}
this.lastSearch=rr;
if (this.qv.length > 2) {
this.LLinit();
this.m.innerHTML='<i style="color:#a0a0a0;">Loading...</i>';
robj.push([rr, this]);
loadScript("api.asp?mode=LL&m=" + this._mode + ((this.clg > 0) ? '&c=' + this.clg : '') + "&s=" + encodeURIComponent(this.qv), rr);
} else {
this.m.style.display='none';
}
}

LiveLookup.prototype.LLinit=function() {
this.mi=0;
this.mSelect(0);
while (this.m.childNodes.length > 0) this.m.removeChild(this.m.childNodes[0]);
this.results=new Array();
}
LiveLookup.prototype.LLresult=function(value, name, city, state) {
var mv=document.createElement("a");
if (this._mode == 1) mv.innerHTML=city + ', ' + state + ': ' + name; else mv.innerHTML=((typeof city == 'string') ? name + ' <span class="llclgname">' + city + '</span>' : name);
mv.className='qmenuitem';
var _mvo=this.mSelect.bind(this);
mv.onmouseover=function() { _mvo(0); }
this.mi++;
var x=this.mi;
var _mvc=this.setVal.bind(this);
mv.onclick=function() { _mvc(x); return false; }
this.results[this.mi]=new LLVal(value, name, city, state);
mv.id='cr'+this.r+this.mi;
mv.href="#";
mv.title='';
this.m.appendChild(mv);
if (this._mode == 1) mv.title=name + '\nCEEB: ' + value;

}

LiveLookup.prototype.LLloaded=function() {
this.lastSearch=0;
//if (this.results.length == 0) {
var mv=document.createElement("a");
mv.style.color='#a7a7ff';
mv.style.marginTop='10px';
//mv.style.marginLeft='100px';
mv.style.textAlign='center';
mv.style.width='100%';
if (this._mode == 1) mv.innerHTML='<i>Can\'t find your high school?&nbsp;</i>';
//mv.className='qmenuitem';
var _mvo=this.mSelect.bind(this);
//mv.onmouseover=function() { _mvo(0); }
//this.mi++;
//var x=this.mi;
var _mvc=this.setVal.bind(this);
var _evhandler=this.onschoolnotfound.bind(this);
mv.onclick=function() { _mvc(-1); _evhandler(); return false; }
mv.id='cr'+this.r+'-1';
mv.href="#";
this.m.appendChild(mv);
//}
}


LiveLookup.prototype.setVal=function(i) {
if (i > 0) {
this.selVal=this.results[i].Value;
this.selName=this.results[i].Name;
this.q.value=this.results[i].Name;
} else if (i == -1) {
this.q.value='(couldn\'t find high school)';
this.selVal=0;
} else {
this.q.value='';
this.selVal=0;
}
this.q.blur();
if (this.nel != null) this.nel.focus();
this.m.style.display='none';
}


LiveLookup.prototype.positionDrop=function() {
this.m.style.left = eX(this.q) + "px";
this.m.style.top = eY(this.q) + this.q.offsetHeight + "px";
this.m.style.width = this.q.offsetWidth-2 + "px";
}



LiveLookup.prototype.mouseevent=function(e) {
if (this.m.style.display == 'block') {

    var obj,len,el,i,isWidgetClick;
isWidgetClick=false;
    el = e.target ? e.target : e.srcElement;


    while (el.parentNode != document && el.parentNode != null) {
    if (el.id.substr(0,5) == "qmenu" || el.id == this.q.id) { isWidgetClick=true; break; }
el = el.parentNode; 
}


    if (!isWidgetClick) {
	this.m.style.display='none';
	} //else if (this.wClick) {
	//this.wClick=false;
	//this.qBlur();
	//}
}

this.wClick=(isWidgetClick);
}
LiveLookup.prototype.onschoolnotfound=function(e) {
return false;
}

function HTMLEncode(s) {
        var str = new String(s);
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
}

function eX(obj) {
var x = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }
  else if (obj.x) x = obj.x;
  
  return x;
}

function eY(obj) {
var y = 0;
  if(obj.offsetParent) {
    while (obj.offsetParent) {
      y += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }
  else if (obj.y) y = obj.y;
  return y; 
}

function LLinit(r) {
//dbg(r);
//dbg(robj);
for (i=0; i<robj.length; i++) {
if (robj[i][0] == r) {
//dbg(i + ':' + r);
robj[i][1].LLinit();
break;
}
}
}

function LLresult(r, value, name, city, state) {
for (i=0; i<robj.length; i++) {
if (robj[i][0] == r) {
//dbg(i + ':' + r + '\n' + r + value + name + city + state);
robj[i][1].LLresult(value, name, city, state);
break;
}
}
}

function LLloaded(r) {
for (i=0; i<robj.length; i++) {
if (robj[i][0] == r) {
robj[i][1].LLloaded();
robj.splice(i, 1);
break;
}
}
}
