/*
Foundation.Completer
Copyright © 2005-2010 Scott Nesin, all rights reserved.
Documentation: http://FoundationDotJS.org/Foundation.Completer.htm
*/
/*
   ***** Preprocessed code, any modifications will be lost. *****
     Foundation.js, object oriented JavaScript framework:
     http://FoundationDotJS.org/
     Preprocessed for speed:
     http://FoundationDotJS.org/Foundation.Preprocessor.htm
*/
Foundation.Completer=function()
{
   Foundation.FloatControl.apply(this,arguments);
   this.setCaseSensitive(this.resource("caseSensitive"));
   this.setMinimumCheckLength(this.resource("minimumCheckLength"));
   this.setMaximumListLength(this.resource("maximumListLength"));
   this.setMaximumDisplayLength(this.resource("maximumDisplayLength"));
   this.matchList=new Array();
   this.activeIndex=-1;
};
Foundation.Completer.$parentClass=Foundation.FloatControl;
if (!Foundation.FloatControl.$childClasses) Foundation.FloatControl.$childClasses=new Array();
Foundation.FloatControl.$childClasses.push(Foundation.Completer);
Foundation.Completer.$constructor=Foundation.FloatControl.$constructor ? Foundation.FloatControl.$constructor : function(){};
Foundation.Completer.$interfaces=new Array();
Foundation.Completer.$name="Completer";
Foundation.Completer.$childClasses=new Array();
Foundation.Completer.$container=Foundation;
Foundation.Completer.prototype={
   constructor:Foundation.Completer,   
   bindFloatEvents:function(element)
   {
      Foundation.FloatControl.prototype.bindFloatEvents.call(this,element
                            ,"onkeyup","floatBoundOnKeyUp(event)"
                            ,arguments
                            );
   },   
   getCaseSensitive:function()
   {
      return this.disabled;
   },   
   setCaseSensitive:function(caseSensitive)
   {
      if (caseSensitive!=this.caseSensitive)
      {
         var oldValue=this.caseSensitive;
         this.caseSensitive=caseSensitive ? true : false;
         if (typeof(oldValue)!="undefined")
            this.onCaseSensitiveChanged(this.caseSensitive,oldValue);
      }
   },   
   onCaseSensitiveChanged:function(newValue,oldValue)
   {
      this.sortValues();
   },   
   getMaximumListLength:function()
   {
      return this.maximumListLength;
   },   
   setMaximumListLength:function(maximumListLength)
   {
      if (maximumListLength!=this.maximumListLength)
      {
         var oldValue=this.maximumListLength;
         this.maximumListLength=maximumListLength;
         if (typeof(oldValue)!="undefined")
            this.onMaximumListLengthChanged(this.maximumListLength,oldValue);
      }
   },   
   onMaximumListLengthChanged:function(newValue,oldValue)
   {
   },   
   getMaximumDisplayLength:function()
   {
      return this.maximumDisplayLength;
   },   
   setMaximumDisplayLength:function(maximumDisplayLength)
   {
      if (maximumDisplayLength!=this.maximumDisplayLength)
      {
         var oldValue=this.maximumDisplayLength;
         this.maximumDisplayLength=maximumDisplayLength;
         if (typeof(oldValue)!="undefined")
            this.onMaximumDisplayLengthChanged(this.maximumDisplayLength,oldValue);
      }
   },   
   onMaximumDisplayLengthChanged:function(newValue,oldValue)
   {
   },   
   getMinimumCheckLength:function()
   {
      return this.minimumCheckLength;
   },   
   setMinimumCheckLength:function(minimumCheckLength)
   {
      if (minimumCheckLength!=this.minimumCheckLength)
      {
         var oldValue=this.minimumCheckLength;
         this.minimumCheckLength=minimumCheckLength;
         if (typeof(oldValue)!="undefined")
            this.onminimumCheckLengthChanged(this.minimumCheckLength,oldValue);
      }
   },   
   onMinimumCheckLengthChanged:function(newValue,oldValue)
   {
   },   
   canShowFloat:function()
   {
      return this.matchList.length>0 && Foundation.FloatControl.prototype.canShowFloat.call(this);
   },   
   sortValues:function()
   {
      if (this.getCaseSensitive())
         this.value.sort();
      else
         this.value.sort(Foundation.caseInsensitiveSorter);
   },   
   setValue:function(value)
   {
      if (!this.value)
         this.value=new Array();
      for (var i=0;i<value.length;i++)
         this.value.push(value[i]);
      this.sortValues();
   },   
   floatBoundOnKeyDown:function(event)
   {
      var retVal=true;
      var shown=this.isFloatShown();
      if (shown && event.keyCode==40)
         this.activateNextEntry();
      else if (shown && event.keyCode==38)
         this.activatePreviousEntry();
      else if (shown && event.keyCode==13)
         retVal=!this.selectActiveEntry();
      return Foundation.FloatControl.prototype.floatBoundOnKeyDown.call(this,event) && retVal;
   },   
   floatBoundOnKeyUp:function(event)
   {
      if (event.keyCode!=40 &&
          event.keyCode!=38 &&
          event.keyCode!=37 &&
          event.keyCode!=39 &&
          event.keyCode!=13)
         this.queueUpdate(event.srcElement || event.target);
      return true;
   },   
   queueUpdate:function(element)
   {
      if (this.updateQueueHandle)
         window.clearTimeout(this.updateQueueHandle);
      var delay=this.resource("updateDelay");
      if (delay>0)
         this.updateQueueHandle=window.setTimeout(this.event("queuedUpdate('"+element.id.cEncode()+"')"),1);
      else
         this.updateFloat(element);
   },   
   queuedUpdate:function(elementId)
   {
      this.updateQueueHandle=null;
      this.updateFloat(document.getElementById(elementId));
   },   
   synchFloat:function(element)
   {
      if (this.slideHandle)
         this.slideHandle=window.clearInterval(this.slideHandle);
      var value=element.value.toString();
      if (value.length>=this.getMinimumCheckLength())
      {
         var maximumListLength=this.getMaximumListLength();
         var list=new Array();
         var lesserList=new Array();
         var matchLength=value.length;
         var flags=this.getCaseSensitive() ? "" : "i";
         var begining=new RegExp("(^|\\W)"+value.regExpEncode(),flags);
         var middle=new RegExp(value.regExpEncode(),flags);
         var matchIndex;
         for (var i=0;i<this.value.length;i++)
            if (this.value[i].length>=matchLength)
               if ((matchIndex=this.value[i].search(begining))>=0)
                  list.push(this.createEntry(this.value[i],matchIndex+RegExp.$1.length,matchLength));
               else
                  if (list.length<maximumListLength &&
                      (matchIndex=this.value[i].search(middle))>=0)
                     lesserList.push(this.createEntry(this.value[i],matchIndex,matchLength));
         list.sort(this.entrySorter);
         if (list.length>=maximumListLength)
            list.length=maximumListLength;
         else
         {
            lesserList.sort(this.entrySorter);
            for (var i=0;list.length<maximumListLength && i<lesserList.length;i++)
               list.push(lesserList[i]);
         }
         this.matchList=list;
      }
      else
         this.matchList.length=0;
      this.activeIndex=this.matchList.length>0 ? 0 : -1;
   },   
   createEntry:function(value,index,length)
   {
      return {value:value,index:index,length:length};
   },   
   entrySorter:function(a,b)
   {
      return a.index-b.index;
   },   
   getFloatHtml:function()
   {
      var html="";
      var maxDisplay=this.getMaximumDisplayLength();
      html+="<table id=\""+this.elementId("listTable")+"\" border=0 cellpadding=0 cellspacing=0 style=\"cursor:default\">";
      for (var i=0;i<this.matchList.length;i++)
      {
         var m=this.matchList[i];
         html+="<tr"+(i>=maxDisplay ? " style=\"display:none\"" : "")+"><td onmouseover=\""+this.event("entryOnMouseOver(event,"+i+")")+"\" onmouseout=\""+this.event("entryOnMouseOut(event,"+i+")")+"\" onclick=\""+this.event("entryOnClick(event,"+i+")")+"\" "+(i==this.activeIndex ? "style=\""+this.resource("activeStyle")+"\"" : "")+">";
         html+=this.resource("matchHtml"
                             ,'lead',m.value.substr(0,m.index).htmlEncode()
                             ,'match',m.value.substr(m.index,m.length).htmlEncode()
                             ,'tail',m.value.substr(m.index+m.length).htmlEncode()
                            );
         html+="</td></tr>";
      }
      html+="</table>";
      return html;
   },   
   entryOnMouseOver:function(event,index)
   {
      if (this.slideHandle)
         this.slideHandle=window.clearInterval(this.slideHandle);
      this.activateEntry(index);
      var table=this.getElement("listTable");
      var ticks=this.resource("slideTicks");
      if (table && ticks>0)
         if (this.activeIndex>0 &&
             table.rows[this.activeIndex-1].style.display=="none")
            this.slideHandle=window.setInterval(this.event("activatePreviousEntry()"),ticks);
         else
            if (this.activeIndex<table.rows.length-1 &&
                table.rows[this.activeIndex+1].style.display=="none")
               this.slideHandle=window.setInterval(this.event("activateNextEntry()"),ticks);
   },   
   entryOnMouseOut:function(event,index)
   {
      if (this.slideHandle)
         this.slideHandle=window.clearInterval(this.slideHandle);
   },   
   entryOnClick:function(event,index)
   {
      var e=this.getActiveFloatBoundElement();
      if (e)
         e.focus();
      window.setTimeout(this.event("selectEntry("+index+")"),1);
   },   
   activateNextEntry:function()
   {
      if (this.activeIndex<this.matchList.length-1)
         this.activateEntry(this.activeIndex+1);
   },   
   activatePreviousEntry:function()
   {
      if (this.activeIndex>0)
         this.activateEntry(this.activeIndex-1);
   },   
   activateEntry:function(index)
   {
      var table=this.getElement("listTable");
      if (this.activeIndex>=0)
         table.rows[this.activeIndex].cells[0].style.cssText="";
      this.activeIndex=index;
      if (this.activeIndex>=0)
      {
         table.rows[this.activeIndex].cells[0].style.cssText=this.resource("activeStyle");
         if (table.rows[this.activeIndex].style.display=="none")
         {
            var firstDisplayed=0;
            var maxDisplay=this.getMaximumDisplayLength();
            while (table.rows[firstDisplayed].style.display=="none")
               firstDisplayed++;
            var dir=(firstDisplayed<this.activeIndex ? -1 : 1);
            var index=this.activeIndex;
            var display=document.all ? "" : "table-row";
            for (var i=0;i<maxDisplay;i++)
            {
               table.rows[index].style.display=display;
               index+=dir;
            }
            while (index>=0 && index<table.rows.length)
            {
               table.rows[index].style.display="none";
               index+=dir;
            }
         }
      }
   },   
   selectActiveEntry:function()
   {
      if (this.activeIndex>=0 && this.activeIndex<this.matchList.length)
         return this.selectEntry(this.activeIndex);
      return false;
   },   
   selectEntry:function(index)
   {
      var e=this.getActiveFloatBoundElement();
      if (e)
      {
         e.value=this.matchList[index].value;
         if (e.onkeyup)
            e.onkeyup();
         if (e.onchange)
            e.onchange();
         this.hideFloat();
         e.focus();
         return true;
      }
      return false;
   },
   dispose:function()
   {
      if (Foundation.FloatControl.prototype.dispose) Foundation.FloatControl.prototype.dispose.call(this);
   },
   resource:Foundation.FloatControl.prototype.resource,
   event:Foundation.FloatControl.prototype.event,
   elementId:Foundation.FloatControl.prototype.elementId,
   getElement:Foundation.FloatControl.prototype.getElement,
   getElementValue:Foundation.FloatControl.prototype.getElementValue,
   parseElementId:Foundation.FloatControl.prototype.parseElementId,
   attachEvent:Foundation.FloatControl.prototype.attachEvent,
   detachEvent:Foundation.FloatControl.prototype.detachEvent,
   bindToElement:Foundation.FloatControl.prototype.bindToElement,
   bindEvents:Foundation.FloatControl.prototype.bindEvents,
   unbindFromElement:Foundation.FloatControl.prototype.unbindFromElement,
   unbindEvents:Foundation.FloatControl.prototype.unbindEvents,
   getScriptSearchKeys:Foundation.FloatControl.prototype.getScriptSearchKeys,
   getResourceFolder:Foundation.FloatControl.prototype.getResourceFolder,
   getResourcePath:Foundation.FloatControl.prototype.getResourcePath,
   compareValues:Foundation.FloatControl.prototype.compareValues,
   onValueChanged:Foundation.FloatControl.prototype.onValueChanged,
   getValue:Foundation.FloatControl.prototype.getValue,
   parseValue:Foundation.FloatControl.prototype.parseValue,
   getDisabled:Foundation.FloatControl.prototype.getDisabled,
   setDisabled:Foundation.FloatControl.prototype.setDisabled,
   onDisabledChanged:Foundation.FloatControl.prototype.onDisabledChanged,
   getInnerHtml:Foundation.FloatControl.prototype.getInnerHtml,
   getHtml:Foundation.FloatControl.prototype.getHtml,
   getActiveFloatBoundElement:Foundation.FloatControl.prototype.getActiveFloatBoundElement,
   floatBoundOnFocus:Foundation.FloatControl.prototype.floatBoundOnFocus,
   floatBoundOnBlur:Foundation.FloatControl.prototype.floatBoundOnBlur,
   getFloatActivationSelected:Foundation.FloatControl.prototype.getFloatActivationSelected,
   setFloatActivationSelected:Foundation.FloatControl.prototype.setFloatActivationSelected,
   floatActivationSelectedOnChange:Foundation.FloatControl.prototype.floatActivationSelectedOnChange,
   activateFloat:Foundation.FloatControl.prototype.activateFloat,
   focusToFloat:Foundation.FloatControl.prototype.focusToFloat,
   getActivateFloatHtml:Foundation.FloatControl.prototype.getActivateFloatHtml,
   getFloatElement:Foundation.FloatControl.prototype.getFloatElement,
   combineFloatStyle:Foundation.FloatControl.prototype.combineFloatStyle,
   setFloatFocusEvents:Foundation.FloatControl.prototype.setFloatFocusEvents,
   floatChildOnFocus:Foundation.FloatControl.prototype.floatChildOnFocus,
   floatChildOnBlur:Foundation.FloatControl.prototype.floatChildOnBlur,
   saveFloatData:Foundation.FloatControl.prototype.saveFloatData,
   setFloatHtml:Foundation.FloatControl.prototype.setFloatHtml,
   startFloatActivation:Foundation.FloatControl.prototype.startFloatActivation,
   floatActivationPeriodExpired:Foundation.FloatControl.prototype.floatActivationPeriodExpired,
   floatActivationActivity:Foundation.FloatControl.prototype.floatActivationActivity,
   maybeShowFloatActivation:Foundation.FloatControl.prototype.maybeShowFloatActivation,
   maybePopulateFloat:Foundation.FloatControl.prototype.maybePopulateFloat,
   createFloat:Foundation.FloatControl.prototype.createFloat,
   getActivationFloatSize:Foundation.FloatControl.prototype.getActivationFloatSize,
   getFloatSize:Foundation.FloatControl.prototype.getFloatSize,
   getFloatPosition:Foundation.FloatControl.prototype.getFloatPosition,
   positionFloat:Foundation.FloatControl.prototype.positionFloat,
   isFloatShown:Foundation.FloatControl.prototype.isFloatShown,
   updateFloat:Foundation.FloatControl.prototype.updateFloat,
   showFloat:Foundation.FloatControl.prototype.showFloat,
   setFloatBoundElementValue:Foundation.FloatControl.prototype.setFloatBoundElementValue,
   floatBoundElementOnChange:Foundation.FloatControl.prototype.floatBoundElementOnChange,
   abortFloat:Foundation.FloatControl.prototype.abortFloat,
   hideFloat:Foundation.FloatControl.prototype.hideFloat,
   floatOnFocus:Foundation.FloatControl.prototype.floatOnFocus,
   floatOnBlur:Foundation.FloatControl.prototype.floatOnBlur,
   floatOnMouseDown:Foundation.FloatControl.prototype.floatOnMouseDown,
   floatOnMouseUp:Foundation.FloatControl.prototype.floatOnMouseUp,
   floatOnMouseOver:Foundation.FloatControl.prototype.floatOnMouseOver,
   floatOnMouseMove:Foundation.FloatControl.prototype.floatOnMouseMove,
   floatOnMouseOut:Foundation.FloatControl.prototype.floatOnMouseOut,
   focusToDefaultElement:Foundation.FloatControl.prototype.focusToDefaultElement,
   synchActiveFloatBoundElement:Foundation.FloatControl.prototype.synchActiveFloatBoundElement,
   floatOnDoubleClick:Foundation.FloatControl.prototype.floatOnDoubleClick,
   incrementFloatCount:Foundation.FloatControl.prototype.incrementFloatCount,
   decrementFloatCount:Foundation.FloatControl.prototype.decrementFloatCount,
   resetFloatCount:Foundation.FloatControl.prototype.resetFloatCount
};
Foundation.Completer.resourcePack={
      value:[],
      caseSensitive:false,
      matchHtml:"%lead<b>%match</b>%tail",
      activeStyle:"background-color:#ddddff",
      delimeters:";,",
      updateDelay:0,
      minimumCheckLength:1,
      maximumListLength:20,
      maximumDisplayLength:10,
      slideTicks:100,
      disabled:false
   }
;
Foundation.Completer.getTypePath=Foundation.FloatControl.getTypePath;
Foundation.Completer.resource=Foundation.FloatControl.resource;
Foundation.Completer.getById=Foundation.FloatControl.getById;
Foundation.Completer.getFirst=Foundation.FloatControl.getFirst;
Foundation.Completer.getNext=Foundation.FloatControl.getNext;
Foundation.Completer.isInstanceOf=Foundation.FloatControl.isInstanceOf;
Foundation.Completer.resourcePackFromElement=Foundation.FloatControl.resourcePackFromElement;
Foundation.Completer.bindToElement=Foundation.FloatControl.bindToElement;
Foundation.Completer.getCssName=Foundation.FloatControl.getCssName;
Foundation.Completer.bindToElements=Foundation.FloatControl.bindToElements;
Foundation.Completer.getCssRules=Foundation.FloatControl.getCssRules;
Foundation.Completer.writeToPage=Foundation.FloatControl.writeToPage;
Foundation.Completer.$constructor();

