//
// typeahead class. for... typing ahead

function invite(){
window.location="http://about.schooltipline.com/invite-my-school";	
}
function typeaheadpro(obj, source,css,global) {

  // h4x. don't do u\a checking until we need to.
  if (!typeaheadpro.prototype.hacks) {
    // safari gets two hacks in this library. the first hack is for up\down errors... they send us two events per key press typeaheadpro.prototype.should_check_double_fire    =
    // the second hack is for missing keypress events. if you type really fast and hit enter at the same time as a letter itll forget
    //   to send us a keypress for the enter and we cant cancel the form submit. this hack introduces another bug where if you hold down
    //   a key and the blur off the input you cant submit the form, but thats the lesser of two evils in this case.
    typeaheadpro.prototype.should_check_missing_events = navigator.userAgent.indexOf('AppleWebKit/4')!=-1;
    // MSIE will make select boxes shine through our div unless we cover up with an iframe
    typeaheadpro.prototype.should_use_iframe = navigator.userAgent.indexOf('MSIE 6.0')!=-1;
    typeaheadpro.prototype.hacks=true;
  }

  // setup pointers every which way
  this.obj=obj;
  this.obj.typeahead=this;
  this.clear_placeholder();
  if (source) {
    this.set_source(source);
  }

  // attach event listeners where needed
  this.obj.onfocus=function() {
    return this._onfocus();
  }.bind(this);

  this.obj.onblur=function() {
    return this._onblur();
  }.bind(this);

  this.obj.onkeyup=function(event) {
    return this._onkeyup(event ? event : window.event);
  }.bind(this);

  this.obj.onkeydown=function(event) {	
    return this._onkeydown(event ? event : window.event);
  }.bind(this);

  this.obj.onkeypress=function(event) {
    return this._onkeypress(event ? event : window.event);
  }.bind(this);

  // setup container for results
  this.list=document.createElement('div');
  if(css){
	 this.list.className=css;
  }else{
  	this.list.className='typeahead_list';
  }
  if(global){
	  this.globalLogin=true;
  }else{
  	this.globalLogin=false;
  }
  this.list.style.width=(this.obj.offsetWidth-2)+'px'; // assumes a border of 2px
  if (this.obj.nextSibling) {
    this.obj.parentNode.insertBefore(this.list, this.obj.nextSibling);
  }
  else {
    this.obj.parentNode.appendChild(this.list);
  }

   this.obj.parentNode.appendChild(this.list);
  if (this.should_use_iframe && !typeaheadpro.iframe) {
    typeaheadpro.iframe=document.createElement('iframe');
    typeaheadpro.iframe.src="/js/blank.html";
    typeaheadpro.iframe.className='typeahead_iframe';
    typeaheadpro.iframe.style.display='none';
    typeaheadpro.iframe.frameBorder=0;
    document.body.appendChild(typeaheadpro.iframe);
  }
  this.focused=true;

  // get this party started
  if (this.source) {
    this.selectedindex=-1;
    this._onkeyup();
    this.set_class('');
    this.capture_submit();
  }
  else {
    this.hide();
  }
}
typeaheadpro.prototype.max_results=10;
typeaheadpro.prototype.allow_placeholders=true;

// set a source for this typeahead
typeaheadpro.prototype.set_source=function(source) {
  this.source=source;
  this.source.set_owner(this);
  this.status=0;
  this.cache={};
  this.last_search=0;
  this.suggestions=[];
}

// event handler when the input box receives a key press
typeaheadpro.prototype._onkeyup=function(e) {
  this.last_key=e ? e.keyCode : -1;

  // safari h4x
  if (this.key_down==this.last_key) {
    this.key_down=0;
  }

  switch (this.last_key) {
    case 27: // esc
      this.selectedindex=-1;
      this._onselect(false);
      this.hide();
     break;

    case undefined: // haha, what?
    case 0: // whoops
    case 13: // enter
    case 37: // left
    case 38: // up
    case 39: // right
    case 40: // down
      break;

    default: // some other key
      this.dirty_results();
      if (this.should_check_missing_events) {
        setTimeout(function(){this.dirty_results()}.bind(this), 50);
      }
      break;
  }
}

// event handler when a key is pressed down on the text box
typeaheadpro.prototype._onkeydown=function(e) {
  this.key_down=this.last_key=e ? e.keyCode : -1;

  switch (this.last_key) {
    case 9: // tab
      this.select_suggestion(this.selectedindex);
      break;

    case 13: // enter
     this.select_suggestion(this.selectedindex);
     this.hide();
     // we capture the return of _onsubmit here and return it onkeypress to prevent the form from submitting
     return this.submit_keydown_return=this._onsubmit(this.get_current_selection());

    case 38: // up	 
      if (this.check_double_fire()) return;
      this.set_suggestion(this.selectedindex-1);
      return false;

    case 40: // down		
	  if (this.check_double_fire()) return;
      this.set_suggestion(this.selectedindex+1);
      return false;
  }
}

// event handler for when a key is pressed
typeaheadpro.prototype._onkeypress=function(e) {
  this.last_key=e ? e.keyCode : -1;

  switch (this.last_key) {
    case 38: // up
    case 40: // down
      return false;

    case 13: // enter
      return this.submit_keydown_return;
  }
  return true;
}

// event handler when a match is found (happens a lot)
typeaheadpro.prototype._onfound=function(obj) {
  return this.onfound ? this.onfound.call(this, obj) : true;
}

// event handler when the user submits the form
typeaheadpro.prototype._onsubmit=function(obj) {
  if (this.onsubmit) {
    var ret=this.onsubmit.call(this, obj);

    if (ret && this.obj.form) {
      if (!this.obj.form.onsubmit || this.obj.form.onsubmit()) {
        this.obj.form.submit();
      }
      return false;
    }
    return ret;
  } else {
    this.advance_focus();
    return false;
  }
}

// event handler when the user selects a suggestions
typeaheadpro.prototype._onselect=function(obj) {
	
  if (this.onselect) {
    this.onselect.call(this, obj);
	
  }
  document.schools.school.value = this.suggestions[this.selectedindex].i;
   document.globlogin.school.value = this.suggestions[this.selectedindex].i;
 // document.getElementById('school-id').innerHTML = this.suggestions[this.selectedindex].i;
}

// event handler when obj gets focus
typeaheadpro.prototype._onfocus=function() {
  this.focused=true;
  this.clear_placeholder();
  this.results_text='';
  this.set_class('');
  this.dirty_results();
  this.show();
  this.capture_submit();
}

// event handler when focus is lost
typeaheadpro.prototype._onblur=function() {
  if (!this.suggestions) {
    this._onselect(false);
  }
  this.focused=false;
  this.hide();
  this.update_class();
  if (!this.get_value()) {
    var noinput=this.allow_placeholders ? '' : this.source.gen_noinput();
    this.set_value(noinput ? noinput : '');
    this.set_class('typeahead_placeholder')
  }
}

// steals the submit event of the parent form (if any). see should_check_missing_events
typeaheadpro.prototype.capture_submit=function() {
  if (!this.should_check_missing_events) return;

  if ((!this.captured_form || this.captured_substitute != this.captured_form.onsubmit) && this.obj.form) {
    this.captured_form=this.obj.form;
    this.captured_event=this.obj.form.onsubmit;
    this.captured_substitute=this.obj.form.onsubmit=function() {
      return ((this.key_down && this.key_down!=13 && this.key_down!=9) ? this.submit_keydown_return : (this.captured_event ? this.captured_event.apply(arguments, this.captured_form) : true)) ? true : false;
    }.bind(this);
  }
}

// checks to see if this event fired twice. see should_check_double_fire
typeaheadpro.prototype.check_double_fire=function() {
  if (!this.should_check_double_fire) {
    return false;
  }
  else {
    this.double_fire++;
    return this.double_fire % 2 == 1;
  }
}
typeaheadpro.prototype.double_fire=0;

// sets the current selected suggestion. error checking is done here, so you can pass this pretty much anything.
typeaheadpro.prototype.set_suggestion=function(index) {
	
  if (!this.suggestions || this.suggestions.length <= index) { return }
  this.selectedindex=(index <= -1) ? -1 : index;
  var nodes=this.list.childNodes;
  
  for (var i=0; i<nodes.length; i++) {
    if (this.selectedindex==i) {
		
      nodes[i].className=nodes[i].className.replace(/\btypeahead_not_selected\b/, 'typeahead_not_selected');
	 
    }
    else {
      nodes[i].className=nodes[i].className.replace(/\btypeahead_selected\b/, 'typeahead_not_selected');
    }

  }

  this._onfound(this.get_current_selection());
}

// gets the current selection
typeaheadpro.prototype.get_current_selection=function() {
  return this.selectedindex==-1 ? false : this.suggestions[this.selectedindex];
}

// sets the class if we've found a suggestions
typeaheadpro.prototype.update_class=function() {
  if (this.suggestions && this.selectedindex!=-1 && this.get_current_selection().t.toLowerCase() == this.get_value().toLowerCase()) {
    this.set_class('typeahead_found');
  }
  else {
    this.set_class('');
  }
}

// fun little bug in safari which makes us need to do this... basically it pulls out the <br> and puts it back
typeaheadpro.prototype.replace_break=function() {
  this.obj.parentNode.removeChild(this.obj.nextSibling);
  this.obj.parentNode.insertBefore(document.createElement('br'), this.list);
}

// selects this suggestion... it's a done deal
typeaheadpro.prototype.select_suggestion=function(index) {
  if (!this.suggestions || index==undefined || index===false || this.suggestions.length <= index || index < 0) {
    this._onfound(false);
    this._onselect(false);
    this.selectedindex=-1;
  }
  else {
    this.selectedindex=index;
    this.set_value(this.suggestions[index].t);
    this.set_class('typeahead_found');
    this._onfound(this.suggestions[this.selectedindex]);
    this._onselect(this.suggestions[this.selectedindex]);
  }
}

// sets the value of the input
typeaheadpro.prototype.set_value=function(value) {
  this.obj.value=value;
}

// gets the value of the input
typeaheadpro.prototype.get_value=function() {
 return this.obj.value;
}

// called by source in response to search_value
typeaheadpro.prototype.found_suggestions=function(suggestions, text, fake_data) {
  if (!fake_data) {
    this.status=0;
    this.add_cache(text, suggestions);
  }

  if (this.get_value()==this.results_text) { // if we already have a perfect match
    return;
  }

  if (!fake_data) {
    this.results_text=text.toLowerCase();
  }

  this.suggestions=suggestions;
  this.selectedindex=-1;

  if (suggestions.length>0) {
    var perfect_match=false;
	
	
	//this.list.innerHTML='';
    this.list.innerHTML='';
	
	var bubble=document.createElement('em');;
	if(this.globalLogin==true){
		bubble.innerHTML='Find your school below <br>';
	}else{
		bubble.innerHTML='Find your school below <br><a href="javascript:" > Not listed? <u>Add here</u></a>';
	}
	bubble.onmousedown=function(){invite()};
	this.list.appendChild(bubble);
    
	
	var ttext=this.get_value().split(' ');
    for (var i=0; i<suggestions.length; i++) {
      var node=document.createElement('div');
      node.typeahead=this;
      if (!perfect_match && typeahead_source.check_match(ttext, suggestions[i].t)) {
        perfect_match=suggestions[i];
        this.selectedindex=i;
        node.className='typeahead_suggestion typeahead_not_selected';
      }
      else {
        node.className='typeahead_suggestion typeahead_not_selected';
      }
      node.i=i;
      node.onmouseout=function(){this.typeahead.set_suggestion(-1)};
      node.onmouseover=function(){this.typeahead.set_suggestion(this.i)};
      node.onmousedown=function(){this.typeahead.select_suggestion(this.i)};
	 
      node.innerHTML=this.source.gen_html(suggestions[i], this.get_value());
      this.list.appendChild(node);
    }
    this.show();
    this.reset_iframe();
    this._onfound(suggestions[0]);
  }
  else {
    this.selectedindex=-1;
    this.set_message(this.status==0 ? this.source.gen_nomatch() : this.source.gen_loading());
    this._onfound(false);
  }

  if (!fake_data && this.results_text!=this.get_value().toLowerCase()) {
    this.dirty_results();
  }
}

// searches the local cache for the text
typeaheadpro.prototype.search_cache=function(text) {
  return this.cache[text.toLowerCase()];
}

// adds a value to the local cache
typeaheadpro.prototype.add_cache=function(text, results) {
  if (this.source.cache_results) {
    this.cache[text.toLowerCase()]=results;
  }
}

// called by source when it's done loading
typeaheadpro.prototype.source_loaded=function() {
  if (!this.get_value().length) {
    this.set_message(this.source.gen_placeholder());
  }
  if (this.status==2) {
    this.status=0;
  }
  this.dirty_results();
}

// sets the class on the textbox while maintaining ones this object didn't fool around with
typeaheadpro.prototype.set_class=function(name) {
  this.obj.className=(this.obj.className.replace(/typeahead_[^\s]+/g, '') + ' ' + name).replace(/ {2,}/g, ' ');
}

// dirties the current results... fetches new results if need be
typeaheadpro.prototype.dirty_results=function() {
  if (this.get_value().replace(' ', '')=='') {
    this.set_message(this.source.gen_placeholder());
    this.suggestions=[];
    this.selectedindex=-1;
    this.results_text=this.get_value();
    return;
  }
  else if (this.results_text==this.get_value().toLowerCase()) {
    return; // just kidding! don't dirty!
  }

  var time=(new Date).getTime();
  var cache;
  if (this.last_search <= (time - this.source.search_limit) && this.source.status==0 && this.status==0) { // ready
    this.perform_search();
  }
  else {
    if (this.status==0 && this.source.status==1) {
      this.set_message(this.source.gen_loading());
      this.status=2; // waiting for source
    }
    else if (this.status==0 && this.source.status==0) {
      if (!this.search_timeout) {
        this.search_timeout=setTimeout(function() {
          this.search_timeout=false;
          if (this.status==0 && this.source.status==0) {
            this.perform_search();
          }
        }.bind(this), this.source.search_limit - (time - this.last_search));
      }
    }
  }

  if (this.suggestions) {
    var match=-1;
    var ttext=this.get_value().split(' ');
    this.found_suggestions(this.suggestions, this.get_value(), true); // update the highlighting
    for (var i=0; i<this.suggestions.length; i++) {
      if (typeahead_source.check_match(ttext, this.suggestions[i].t)) {
        match=i;
        break;
      }
    }
    if (match!=0) {
      this.set_suggestion(match);
    }
  }
}

// runs a search for the current search text
typeaheadpro.prototype.perform_search=function() {
  if (this.get_value()==this.results_text) {
    return;
  }

  if (!this.get_value().length) { // empty text box
    this.set_message(this.source.gen_placeholder());
    this.suggestions=[];
    this.results_text='';
    this.selectedindex=-1;
  }
  else if ((cache=this.search_cache(this.get_value()))!==undefined) { // found in local cache
    this.found_suggestions(cache, this.get_value(), false);
    this.show();
  }
  else if (!this.source.search_value(this.get_value())) { // if this isn't going to return instantly then we need to pretend to do something
    this.status=1;
    this.last_search=(new Date).getTime();
  }
}

// sets a message for the results
typeaheadpro.prototype.set_message=function(text) {
  if (text) {
    this.list.innerHTML='<div class="typeahead_message">' + text + '</div>';
    this.reset_iframe();
  }
  else {
    this.hide();
  }
}

// moves the iframe to where it needs to be
typeaheadpro.prototype.reset_iframe=function() {
  if (!this.should_use_iframe) { return }
  typeaheadpro.iframe.style.top=elementY(this.list)+'px';
  typeaheadpro.iframe.style.left=elementX(this.list)+'px';
  typeaheadpro.iframe.style.width=this.list.offsetWidth+'px';
  typeaheadpro.iframe.style.height=this.list.offsetHeight+'px';
  typeaheadpro.iframe.style.display='';
}

// advances the form to the next available input
typeaheadpro.prototype.advance_focus=function() {
  setTimeout(function() {
    var inputs=this.obj.form ? this.obj.form : document.getElementsByTagName('input');
    var found=false;
    for (var i=0; i<inputs.length; i++) {
      if (inputs[i]==this.obj) {
        found=true;
      }
      else if (found) {
        if (inputs[i].type!='hidden' && inputs[i].offsetParent) {
          try {
            inputs[i].focus();
            return;
          }
          catch (e) {
          }
        }
      }
    }
  }.bind(this), 0);
}

// clears out the placeholder if need be
typeaheadpro.prototype.clear_placeholder=function() {
  if (this.obj.className.indexOf('typeahead_placeholder')!=-1) {
    this.set_value('');
    this.set_class('');
  }
}

// clear the input
typeaheadpro.prototype.clear=function() {
  this.set_value('', true);
  this.set_class('');
  this.dirty_results();
}

// hide the suggestions
typeaheadpro.prototype.hide=function() {
  this.list.style.display='none';
  this.list.innerHTML='';
  if (this.should_use_iframe) {
    typeaheadpro.iframe.style.display='none';
  }
}

// show the suggestions
typeaheadpro.prototype.show=function() {
  if (this.list.style.display!='' && this.focused) {
    this.replace_break();
    this.list.style.display='';
    if (this.should_use_iframe) {
      typeaheadpro.iframe.style.display='';
      this.reset_iframe();
    }
  }
}

// kills an input's typeahead obj (if there is one)
/* static */ typeaheadpro.kill_typeahead=function(obj) {
  if (obj.typeahead) {
    obj.parentNode.removeChild(obj.nextSibling); // <br />
    obj.parentNode.removeChild(obj.nextSibling); // <div>
    if (obj.typeahead.source) {
      obj.typeahead.source=obj.typeahead.source.owner=null;
    }
    obj.onfocus=obj.onblur=obj.onkeypress=obj.onkeyup=obj.onkeydown=obj.typeahead=null;
  }
}

//
// a typeahead in textarea form
function typeaheadbox(obj, source) {
  this.parent.construct(this, obj, source);
}
typeaheadbox.extend(typeaheadpro);
typeaheadbox.prototype.max_results=5;
typeaheadbox.prototype.allow_placeholders=false;

// extend _onkeydown to treat commas like enter
typeaheadbox.prototype._onkeydown=function(e) {
  if (e) {
    switch (e.keyCode) {
      case 13: // if they press "," pretend it's a comma
      case 188:
        this.select_suggestion(this.selectedindex);
        return this.submit_keydown_return=false;
    }
  }
  else {
    return this.parent._onkeydown(e);
  }
}

// extend _onkeypress to treat commas like enter
typeaheadbox.prototype._onkeypress=function(e) {
  if (e) {
    switch (e.keyCode) {
      case 13: // if they press "," pretend it's a comma
      case 188:
        return this.parent._onkeypress({keyCode: 13});
    }
  }
  else {
    return this.parent._onkeypress(e);
  }
}

// extend the parent's _onfocus
typeaheadbox.prototype._onfocus=function() {
  if (this.obj.value) {
    this.obj.value=trim(trim(this.obj.value),',') + ', ';
    this.parent._onfocus();
    this.set_selection({offset: this.obj.value.length, length: 0});
    this.dirty_results();
  }
  else {
    this.parent._onfocus();
  }
}

// totally override the parent class's _onblur
typeaheadbox.prototype._onblur=function() {
  this.focused=false;
  this.hide();
  this.obj.value=trim(trim(this.obj.value), ',');
}

// overrides get_value to only get what's in between ,'s
typeaheadbox.prototype.get_value=function() {
  var selection=this.get_selection();
  var start=Math.max(this.obj.value.lastIndexOf(',', selection.offset), -1) + 1;
  var end=this.obj.value.indexOf(',', selection.offset+selection.length);
  end=end==-1 ? this.obj.value.length : end;
  return trim(this.obj.value.substring(start, end));
}

// overrides set_value to only set in between ,'s
typeaheadbox.prototype.set_value=function(text, clear) {
  if (clear) {
    this.obj.value=text;
  }
  else {
    var selection=this.get_selection();
    var start=Math.max(this.obj.value.lastIndexOf(',', selection.offset), 0);
    var end=this.obj.value.indexOf(',', selection.offset+selection.length);
    end=end==-1 ? this.obj.value.length : end;
    this.obj.value=this.obj.value.substring(0, start) + (start==0 ? '' : ', ') + text + this.obj.value.substring(end, this.obj.value.length) + ', ';
    this.set_selection({offset: text.length + (start==0 ? 2 : 4) + start, length: 0});
  }
}

// sets the selection area of the textarea
typeaheadbox.prototype.set_selection=function(dimensions) {
  this.obj.setSelectionRange(dimensions.offset, dimensions.offset+dimensions.length);
}

// gets the dimensions of the current selection in the textarea
typeaheadbox.prototype.get_selection=function() {
  return {offset: this.obj.selectionStart, length: this.obj.selectionEnd - this.obj.selectionStart};

}

// extend parent to dirty the results after something is selected
typeaheadbox.prototype.select_suggestion=function(i) {
  this.parent.select_suggestion(i);
  this.dirty_results();
}

// this crap doesn't apply to typeaheadbox
typeaheadbox.prototype.capture_submit=function() {};
typeaheadbox.prototype.update_class=function() {};
typeaheadbox.prototype.set_class=function() {};

//
// typeahead source generic class
function typeahead_source() {
}
typeahead_source.prototype.cache_results=true; // may the owner cache results?
typeahead_source.prototype.search_limit=10;    // how often can we run a query?

// basically a tokenized search
/* static */ typeahead_source.check_match=function(search, value) {
  search.sort(function(a,b){return b.length-a.length});
  value=value.split(' ');
  for (var i in search) {
    if (search[i].length) { // do we want to count this piece as a search token?
      var found=false;
      for (var j in value) {
        if (value[j].length >= search[i].length && value[j].substring(0, search[i].length).toLowerCase() == search[i].toLowerCase()) {
          found=true;
          value[j]=''; // prevent this piece of the name from being matched again
          break;
        }
      }
      if (!found) {
        return false;
      }
    }
  }
  return true;
}

// sets the owner (i.e. typeahead) of this source
typeahead_source.prototype.set_owner=function(obj) {
  this.owner=obj;
}

// highlights found text with searched text
typeahead_source.prototype.highlight_found=function(result, search) {
  var html=new Array();
  result = result.split(' ');
  search = search.split(' ');
  search.sort(function(a,b){return a.length<b.length?1:-1}); // do this to make sure the larger piece gets matched first
  for (var i in result) {
    var found=false;
    for (var j in search) {
      if (search[j].length && result[i].length >= search[j].length && result[i].substring(0, search[j].length).toLowerCase() == search[j].toLowerCase()) {
        html.push('<em>', htmlspecialchars(result[i].substring(0, search[j].length)), '</em>', htmlspecialchars(result[i].substring(search[j].length, result[i].length)), ' ');
        found=true;
        break;
      }
    }
    if (!found) {
      html.push(htmlspecialchars(result[i]), ' ');
    }
  }
  return html.join(''); 
}

// returns error text for when nothing was found
typeahead_source.prototype.gen_nomatch=function() {
	if(this.owner.globalLogin==true){
  		return this.text_nomatch != null ? this.text_nomatch : 'No matches found.';
	}else{		
		return this.text_nomatch != null ? this.text_nomatch : 'No matches found. <br><a href="http://about.schooltipline.com/invite-my-school">  <u>Add school here!</u>';
	}
 }

// returns message in case the selector is still loading
typeahead_source.prototype.gen_loading=function() {
  return this.text_loading != null ? this.text_loading : 'Loading...';
}

// returns filler text for when the user hasn't typed anything in
typeahead_source.prototype.gen_placeholder=function() {
  return this.text_placeholder != null ? this.text_placeholder : 'Start typing...';
}

// returns filler text for when the user hasn't typed anything in
typeahead_source.prototype.gen_noinput=function() {
  return this.text_noinput != null ? this.text_noinput : 'Start typing...';
}

//
// friend source for typeaheads
function friend_source(get_param) {
  this.status=1; // loading
  var ajax=new Ajax(
    function(obj, text) {
      text = text.substring(9);
      eval(text);
      this.friends=friends;
      this.status=0; // ready
      if (this.owner && this.owner.source_loaded) {
        this.owner.source_loaded();
      }
    }.bind(this));
  ajax.get('/ajax/typeahead_friends.php?' + get_param);
  this.parent.construct(this);
}
friend_source.extend(typeahead_source);
friend_source.prototype.text_noinput=friend_source.prototype.text_placeholder='Start typing a friend\'s name';

// searches the friend list for some text and returns those to the typeahead
friend_source.prototype.search_value=function(text) {
  
  if (this.status==0) {
    var results=new Array();
    var ttext=text.split(' ');
    for (var i in this.friends) {
      if (typeahead_source.check_match(ttext, this.friends[i].t)) {
        results.push(this.friends[i]);
      }
      
      // if we have X matches, that's enough
      if (results.length >= this.owner.max_results) {
        break;
      }
    }
    this.owner.found_suggestions(results, text, false);
    return true;
  }
}

// generates html for this friend's typeahead
friend_source.prototype.gen_html=function(friend, highlight) {
  var html=new Array('<div>');
  html.push(this.highlight_found(friend.t, highlight), '</div><div><small>', friend.n, '</small></div>');
  return html.join('');
}

//
// candidate source for typing ahead
function candidate_source(get_param) {
  this.parent.construct(this, 'candidate&' + get_param);
}
candidate_source.extend(friend_source);
candidate_source.prototype.text_noinput=candidate_source.prototype.text_placeholder='Start typing a candidate\'s name';

// generates html for this candidate's typeahead
candidate_source.prototype.gen_html=function(friend, highlight) {
  var html=new Array('<div>');
  html.push(this.highlight_found(friend.t, highlight));
  return html.join('');
}

//
// network source for networks and stuff... when needed this should be further abstracted to ajax_source -> network_source
function network_source(get_param) {
  this.get_param=get_param ? get_param : '';
  this.status=0;
  this.parent.construct(this);
}
network_source.extend(typeahead_source);
network_source.prototype.search_limit=200;   // how often can we run a query?
network_source.prototype.text_placeholder=network_source.prototype.text_noinput='Type school name or city or zip';

// sends a query to look for the network. the owner won't call this until we respond with found_suggestions, so we don't have to implement any kind of throttling here.
network_source.prototype.search_value=function(text) {
  this.search_text=text;
  var ajax = new Ajax(
    function(ajax, text) {
      eval(text); // extracts var results
      this.owner.found_suggestions(results, this.search_text, false);
    }.bind(this),
    function() {
      this.owner.found_suggestions(false, this.search_text, false);
    }.bind(this)
  );
  
  	
	if(this.owner.globalLogin==true)
		ajax.get('/lib/ajax/school.php?extra=true&q='+encodeURIComponent(text));
	else
		ajax.get('/lib/ajax/school.php?q='+encodeURIComponent(text));
	
  
  
}

// generates html for this result
network_source.prototype.gen_html=function(result, highlight) {
	
	
  var html=new Array('<div>');
  html.push(this.highlight_found(result.t, highlight), '</div><div><small>', result.l, '</small></div>');
  return html.join('');
}

//
// custom source -- pass it an array of stuff and it'll autocomplete from the list
function custom_source(options) {
  this.status=0; // ready
  this.options=options;
  this.parent.construct(this);
}
custom_source.extend(typeahead_source);
custom_source.prototype.search_limit=10;     // how often can we run a query?
custom_source.prototype.text_placeholder=custom_source.prototype.text_noinput=false;

// searches the friend list for some text and returns those to the typeahead
custom_source.prototype.search_value=function(text) {
  var results=new Array();
  var ttext=text.split(' ');
  for (var i in this.options) {
    if (typeahead_source.check_match(ttext, this.options[i].t)) {
      if (this.options[i].i) {
        results.push(this.options[i]);
      }
      else {
        results.push({t:this.options[i].t, i:this.options[i].i});
      }
    }
    
    // if we have X matches, that's enough
    if (results.length >= this.owner.max_results) {
      break;
    }
  }
  this.owner.found_suggestions(results, text, false);
  return true;
}

// generates html for this result
custom_source.prototype.gen_html=function(result, highlight) {
  var html=new Array('<div>');
  html.push(this.highlight_found(result.t, highlight), '</div>');
  if (result.s) {
    html.push('<div><small>', friend.n, '</small></div>');
  }
  return html.join('');
}

//
// concentration source, for college majors\minors. this one is kind of interesting because we will probably have more than one from the same college on the page at once.




function search_selector_onfound(result) {
  var n;
  if (!(n=ge('n'))) {
    return;
  }
  n.value = result ? result.i : -1;
}

function search_result_selector_onsubmit(result) {
  ge('search').submit();
  this.hide();
  this.advance_focus();
}