//  (c) 2008, OneTech Computing

      // ** create global variables **
      
      var typeaheads = new Array();


      // ** fix IE missing array.indexOf() **

      if (! Array.indexOf) {
        Array.prototype.indexOf = function (obj, start) {
          for (var i = (start || 0); i < this.length; i++) {
            if (this[i] == obj) return i;
          }
        }
      }


      // ** add missing object.elemCount() **

      function elemCount(obj) {
        var tot = 0;
        for (var i in obj) tot++;

        return tot;
      }

//      if (! Object.elemCount) {
//        Object.prototype.elemCount = function (obj) {
//          var tot = 0;
//          for (var i in this) tot++;

//          return tot;
//        }
//      }


      // ** fix IE missing event.preventDefault() **

      function ErrPreventDefault(e) {
        if (! e.preventDefault) {
          e.returnValue = false;
        } else {
          e.preventDefault();
        }
      }


      // ** add missing string.trim() **

      if (! String.trim) {
        String.prototype.trim = function() {
          a = this.replace(/^\s+/, '');
          return a.replace(/\s+$/, '');
        }
      }


      // ** setup namespace **

      var useOTnamespace;

      if (useOTnamespace) {
      	 if (typeof(ot) == "undefined") ot = {}
      	 var _ot = ot;
      } else {
	        var _ot = this;
      }


      // ** ImagePreloader class **

      _ot.ImagePreloader = function (newsitems, callback) {
        this.callback = callback;
        this.nImages = newsitems.length;
        this.aImages = new Array();
        this.nLoaded = 0;
        this.nProcessed = 0;
  
        for (var i=0; i<this.nImages; i++) {
          this.preload(newsitems[i]);
        } 
      }

      _ot.ImagePreloader.prototype.preload = function(newsitem) {
        var oImage = new Image;
        this.aImages.push(oImage);
        
        oImage.onload = ImagePreloader.prototype.onload;
        oImage.onerror = ImagePreloader.prototype.onerror;
        oImage.onabort = ImagePreloader.prototype.onabort;
        oImage.onclick = ImagePreloader.prototype.onclick;
        
        oImage.oImagePreloader = this;
        oImage.bLoaded = false;
        
        oImage.clickref = newsitem['url'];

        if (oImage.clickref != '') {
          oImage.style.cursor = 'pointer';
        }

        oImage.src = newsitem['pic'];
      }

      _ot.ImagePreloader.prototype.onComplete = function() {
        this.nProcessed++;

        if (this.nProcessed == this.nImages) {
          this.callback(this.aImages, this.nLoaded);
        }
      }

      _ot.ImagePreloader.prototype.onclick = function() {
        if (this.clickref != '') {
          if (this.clickref.toLowerCase().match(window.location.hostname)) {
            location.href = this.clickref;
          } else {
            window.open(this.clickref, '_new').focus();
          } 
        }
      }

      _ot.ImagePreloader.prototype.onload = function() {
        this.bLoaded = true;
        this.oImagePreloader.nLoaded++;
        this.oImagePreloader.onComplete();
      }

      _ot.ImagePreloader.prototype.onerror = function() {
        this.oImagePreloader.bError = true;
        this.oImagePreloader.onComplete();
      }

      _ot.ImagePreloader.prototype.onabort = function() {
        this.oImagePreloader.bAbort = true;
        this.oImagePreloader.onComplete();
      }


      // ** Crossfader class **
      
      _ot.Crossfader = function (divs, fadetime, delay, callback) {
         this.change_cb = callback;
	       this.nAct = -1;
	       this.aDivs = divs;

	       for (var i=0;i<divs.length;i++) {
	         var div = document.getElementById(divs[i]);
	         if (div) {
             div.style.opacity = 0;
             div.style.position = "absolute";
             div.style.filter = "alpha(opacity=0)";
             div.style.visibility = "hidden";
           }
	       }

      	this.nDur = fadetime;
        this.nDelay = delay;
        this._newfade();
      }

      _ot.Crossfader.prototype._newfade = function() {
        this.ispaused = false;

        if (this.nID1) clearInterval(this.nID1);
	      this.nOldAct = this.nAct;
        this.nAct++;

       	if (!this.aDivs[this.nAct])	this.nAct = 0;
 	      if (this.nAct == this.nOldAct) return false;

        var div = document.getElementById(this.aDivs[this.nAct]);
        if (div) div.style.visibility = "visible";

        this.nInt = 50;
        this.nTime = 0;
        var p=this;
        this.nID2 = setInterval(function() { p._fade() }, this.nInt);
      }

      _ot.Crossfader.prototype._fade = function() {
	      if (this.nTime == 0) {
          if (this.change_cb) {
            var olddiv = null;
            if (this.nOldAct > -1) olddiv = this.aDivs[this.nOldAct]; 
            this.change_cb(this.aDivs[this.nAct], olddiv);
          }
        }

        this.nTime += this.nInt;

 	      var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 );
 	      var op = ieop / 100;
 	      var div = document.getElementById(this.aDivs[this.nAct]);
        if (div) { 
 	        div.style.opacity = op;
 	        div.style.filter = "alpha(opacity="+ieop+")";
        }

        if (this.nOldAct > -1) {
          var div = document.getElementById(this.aDivs[this.nOldAct]);
          if (div) { 
            div.style.opacity = 1 - op;
  		      div.style.filter = "alpha(opacity="+(100 - ieop)+")";
  	      }
	      }

	      if (this.nTime == this.nDur) {
		      clearInterval(this.nID2);

          if (this.nOldAct > -1) {
            var div = document.getElementById(this.aDivs[this.nOldAct]); 
            if (div) div.style.visibility = "hidden";
          }

          var p=this;
          this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
        }
      }

      _ot.Crossfader.prototype._easeInOut = function(t,b,c,d) {
        return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
      }

      _ot.Crossfader.prototype.select = function(div) {
        this.ispaused = false;

        if (this.nID1) clearInterval(this.nID1);
        if (this.nID2) clearInterval(this.nID2);
	      this.nOldAct = this.nAct;
        this.nAct = this.aDivs.indexOf(div);

       	if (this.nAct == -1) this.nAct = 0;
 	      if (this.nAct == this.nOldAct) return false;

        var div = document.getElementById(this.aDivs[this.nAct]);
        if (div) div.style.visibility = "visible";

        this.nInt = 50;
        this.nTime = 0;
        var p=this;
        this.nID2 = setInterval(function() { p._fade() }, this.nInt);
      }

      _ot.Crossfader.prototype.pause = function() {
        this.ispaused = true;
        this.resumeID1 = null;
        this.resumeID2 = null;

        if (this.nID1) {
          this.resumeID1 = true;
          clearInterval(this.nID1);
        }

        if (this.nID2) {
          this.resumeID2 = true;
          clearInterval(this.nID2);
        }
      }

      _ot.Crossfader.prototype.resume = function() {
        this.ispaused = false;
        var p=this;

        if (this.resumeID1) this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
      }


      // ** typeahead (auto-complete) class **

      _ot.typeahead = function (obj, options, idxattribute) {
        this.obj = obj;
        
        this.obj.onfocus = typeahead.prototype.onfocus;
        this.obj.obblur = typeahead.prototype.onblur;
        this.obj.onkeyup = typeahead.prototype.onkeyup;
        this.obj.onkeydown = typeahead.prototype.onkeydown;
        this.obj.onkeypress = typeahead.prototype.onkeypress;
        this.obj.otypeahead = this;
        this.otypeahead = this;
        this.showing = false;

        this.list = document.createElement('div');
        this.list.className = 'th_list';
        this.list.style.width = parseInt(this.obj.offsetWidth) -2 + 'px';

//  this puts the list in the right place on the page as far as IE is concerned but extends page length
//  this.list.style.position = 'relative';

        this.list.style.display = 'none';

        this.obj.parentNode.insertBefore(this.list, this.obj.nextSibling);
        
        this.focused = true;
        this.options = options;
        this.selectedindex = -1;
        this.idxattribute = idxattribute;
        
        this.obj.className = this.obj.className.replace(/th_placeholder/, '').trim();
        this.obj.value = '';

        var itemindex = -1;
        if (this.idxattribute) {
          itemindex = this.obj.getAttribute(this.idxattribute);
          if (itemindex > -1) {
            this.obj.value = this.options[itemindex];
            this.selectedindex = 0;
          }
        }

        typeaheads.push(this);

        this.update_results();
        this.show();
      }
      
      _ot.typeahead.prototype.max_results = 5;
      
      _ot.typeahead.prototype.onfocus = function(e) {
        var evt = e || window.event;

        this.otypeahead.focused = true;
      	this.otypeahead.update_results();
        this.otypeahead.obj.className = this.otypeahead.obj.className.replace(/th_found/, '').trim();
      	this.otypeahead.show();
      }
      
      _ot.typeahead.prototype.onblur = function(e) {
        var evt = e || window.event;

        this.otypeahead.focused = true;
      	this.otypeahead.hide();
      }
      
      _ot.typeahead.prototype.onkeyup = function(e) {
        var evt = e || window.event;

        switch (evt.keyCode) {
      	  case 27: // escape
            this.otypeahead.hide();
            this.otypeahead.obj.className = this.otypeahead.obj.className.replace(/th_found/, '').trim();
      		  break;
      
      		case 0:
      		case 9:  // tab
      		case 13: // enter
      		case 37: // left
      		case 38: // up
      		case 39: // right
      		case 40: // down
      		  break;

      		default:
      			this.otypeahead.update_results();
      			this.otypeahead.show();
            this.otypeahead.obj.className = this.otypeahead.obj.className.replace(/th_found/, '').trim();
      			break;
      	}
      }
      
      _ot.typeahead.prototype.onkeydown = function(e) {
        var evt = e || window.event;

      	switch (evt.keyCode) {
      	  case 9:  // tab
      		case 13: // enter
      		  if (this.otypeahead.results[this.otypeahead.selectedindex]) {
              this.otypeahead.obj.className = this.otypeahead.obj.className.replace(/th_found/, '').trim();
              this.otypeahead.obj.className += ' th_found';
              this.otypeahead.obj.value = this.otypeahead.results[this.otypeahead.selectedindex];
              if (this.otypeahead.idxattribute) {
                this.otypeahead.obj.setAttribute(this.otypeahead.idxattribute, this.otypeahead.absoluteIndex(this.otypeahead.selectedindex));
              }
              this.otypeahead.hide();
              if (evt.keyCode != 9) ErrPreventDefault(evt);
      			}
      			break;
      
      		case 38: // up
      		  if (this.otypeahead.selectedindex > 0) this.otypeahead.select(this.otypeahead.selectedindex - 1);
      			ErrPreventDefault(evt);
      			break; 
      
      		case 40: // down
      		  if (this.otypeahead.selectedindex < this.otypeahead.results.length -1) this.otypeahead.select(this.otypeahead.selectedindex + 1);
      			ErrPreventDefault(evt);
      			break;
      	}
      }
      
      _ot.typeahead.prototype.onkeypress = function(e) {
        var evt = e || window.event;

      	switch (evt.keyCode) {
      	  case 9:  // tab
      	    this.otypeahead.cancelPopup();
      		  break;

      	 	case 13: // return
      		case 38: // up
      		case 40: // down
      			ErrPreventDefault(evt);
      			break;
      	}
      }

      _ot.typeahead.prototype.cancelPopup = function() {
        if ((this.results.indexOf(this.obj.value) > -1) || (this.selectedindex > -1)) {
        	var selected = -1;

          selected = this.selectedindex;
          if (selected == -1) selected = this.results.indexOf(this.obj.value);
              
          if ((selected > -1) && (this.results[selected] == this.obj.value)) {
            this.selectedindex = selected;
            if (this.results[this.selectedindex]) {
              this.obj.className = this.obj.className.replace(/th_found/, '').trim();
              this.obj.className += ' th_found';
            }
          }
        }

        if (this.idxattribute) {
          this.obj.setAttribute(this.idxattribute, this.absoluteIndex(this.selectedindex));
        }

        this.hide();
      }

      _ot.typeahead.prototype.absoluteIndex = function(index) {
        var result = index;
        if (index > -1) {
          result = this.options.indexOf(this.results[index]);
        }

        return result;
      }

      _ot.typeahead.prototype.select = function(index) {
      	var children = this.list.childNodes;
      	var found = false;

      	for (var i = 0; i < children.length; i++) {
          children[i].className = children[i].className.replace(/th_selected/, '').trim();
      	}

        if ((index >= 0) && (index < children.length)) {
          children[index].className += ' th_selected';
      		this.selectedindex = index;
      		found = true;
      	}      		

      	if (!found && children[this.selectedindex]) {
          children[this.selectedindex].className = children[this.selectedindex].className.replace(/th_selected/, '').trim();
          children[this.selectedindex].className += ' th_selected';
      	}
      }

      _ot.typeahead.prototype.update_results = function() {
      	var results = [];
      	var val = this.obj.value.toLowerCase();
      	this.selectedindex = -1;
      
      	for (var i = 0; i < this.options.length; i++) {
      		var prefix = this.options[i].substring(0, val.length).toLowerCase();
      		if (prefix == val) {
      			results.push(this.options[i]);
      			if (results.length >= this.max_results) {
      				break;
      			}
      		}
      	}
      
      	this.list.innerHTML = '';
      
        for (var i = 0; i < results.length; i++) {
          var lidiv = document.createElement('div');
          var p = this;

          lidiv.className = 'th_suggestion';
          lidiv.index = i;

          lidiv.onmouseover = function() {
            p.select(this.index);
          };
      
          lidiv.onmousedown = function() {
            p.obj.className = p.obj.className.replace(/th_found/, '').trim();
            p.obj.className += ' th_found';
            p.obj.value = p.results[this.index];
            if (p.idxattribute) {
              p.obj.setAttribute(p.idxattribute, p.absoluteIndex(this.index));
            }
            p.hide();
          };

          liem = document.createElement('em');
          liem.innerHTML = results[i].substring(0, val.length);

          lispan = document.createElement('span');
          lispan.innerHTML = results[i].substring(val.length);

          lidiv.appendChild(liem);
          lidiv.appendChild(lispan);

          this.list.appendChild(lidiv);
      	}

      	this.results = results;
      }
      
      _ot.typeahead.prototype.show = function() {
        this.showing = true;
        this.list.style.display = 'block';
      }
      
      _ot.typeahead.prototype.hide = function() {
        this.showing = false;
      	this.list.style.display = 'none';
      }
