/*
	This heading must remain intact at all times.
	Copyright (c) 2010 Mark Mason.

	File:	Q-Cogo-Sugg.js
	Use:	To provide intelligent suggest behaviors for Q-Cogo, <http://www.q-cogo.com/>.
	Ver:	1.2 (Beta)

	Created by Mark Mason. Latest version available from <http://www.q-cogo.com/>.



	This file is part of Q-Cogo.
	
	Q-Cogo is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Q-Cogo is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Q-Cogo.  If not, see <http://www.gnu.org/licenses/>.
*/

function autoCompleteDB(){this.aNames=new Array();}
autoCompleteDB.prototype.assignArray=function(aList){this.aNames=aList;};autoCompleteDB.prototype.getMatches=function(str,aList,maxSize){var ctr=0;for(var i in this.aNames){if(this.aNames[i].indexOf(str)==0){aList.push(this.aNames[i]);ctr++;}if(ctr==(maxSize-1))break;}};function autoComplete(aNames,oText,oDiv,maxSize,mult){this.oText=oText;this.oDiv=oDiv;this.maxSize=maxSize;this.cur=-1;this.preText='';this.mult=mult;this.db=new autoCompleteDB();this.db.assignArray(aNames);oText.onkeyup=this.keyUp;oText.onkeydown=this.keyDown;oText.autoComplete=this;oText.onblur=this.hideSuggest;}
autoComplete.prototype.hideSuggest=function(){this.autoComplete.oDiv.style.visibility='hidden';};autoComplete.prototype.selectText=function(iStart,iEnd){if(this.oText.createTextRange){var oRange=this.oText.createTextRange();oRange.moveStart('character',iStart);oRange.moveEnd('character',iEnd-this.oText.value.length);oRange.select();}
else if(this.oText.setSelectionRange)this.oText.setSelectionRange(iStart,iEnd);this.oText.focus();};autoComplete.prototype.textComplete=function(sFirstMatch){if(this.oText.createTextRange||this.oText.setSelectionRange){var iStart=this.oText.value.length;this.oText.value=sFirstMatch;this.selectText(iStart,sFirstMatch.length);}};autoComplete.prototype.keyDown=function(oEvent){oEvent=window.event||oEvent;iKeyCode=oEvent.keyCode;switch(iKeyCode){case 38:this.autoComplete.moveUp();break;case 40:this.autoComplete.moveDown();break;case 13:this.blur();break;}};autoComplete.prototype.moveDown=function(){var oThis=this;if(this.oDiv.childNodes.length>0&&this.cur<(this.oDiv.childNodes.length-1)){++this.cur;for(var i=0;i<this.oDiv.childNodes.length;i++){if(i==this.cur){this.oDiv.childNodes[i].className='over';this.oText.value=this.preText+this.oDiv.childNodes[i].innerHTML;}
else{this.oDiv.childNodes[i].className='';}}}
setCursorAtEnd(oThis.oText);};autoComplete.prototype.moveUp=function(){var oThis=this;if(this.oDiv.childNodes.length>0&&this.cur>0){--this.cur;for(var i=0;i<this.oDiv.childNodes.length;i++){if(i==this.cur){this.oDiv.childNodes[i].className='over';this.oText.value=this.preText+this.oDiv.childNodes[i].innerHTML;}
else{this.oDiv.childNodes[i].className='';}}}
setCursorAtEnd(oThis.oText);};autoComplete.prototype.keyUp=function(oEvent){oEvent=oEvent||window.event;var iKeyCode=oEvent.keyCode;if(iKeyCode==8||iKeyCode==46){this.autoComplete.onTextChange(false);}
else if(iKeyCode<32||(iKeyCode>=33&&iKeyCode<=46)||(iKeyCode>=112&&iKeyCode<=123)){}
else{this.autoComplete.onTextChange(false);}};autoComplete.prototype.positionSuggest=function(){var oNode=this.oText;var x=0,y=oNode.offsetHeight;while(oNode.offsetParent&&oNode.offsetParent.tagName.toUpperCase()!='BODY'){x+=oNode.offsetLeft;y+=oNode.offsetTop;oNode=oNode.offsetParent;}
x+=oNode.offsetLeft;y+=oNode.offsetTop;y-=getScrollXY()[1];this.oDiv.style.top=y+'px';this.oDiv.style.left=x+'px';}
autoComplete.prototype.onTextChange=function(bTextComplete){var txt=this.oText.value;var oThis=this;this.cur=-1;var LastChar=Math.max(txt.lastIndexOf(','),txt.lastIndexOf('-'))+1;var LStar=txt.lastIndexOf('*');var BStar=((LStar>=0)?txt.substring(LStar-1,LStar):'');LStar=((LStar==0||BStar=='-'||BStar==',')?LStar:-1)+1;var Last=Math.max(LastChar,LStar);if(this.mult){this.preText=txt.substring(0,Last);txt=txt.substring(Last);txt=FormatString(txt,1,0);}
if(txt.length>=0){while(this.oDiv.hasChildNodes())this.oDiv.removeChild(this.oDiv.firstChild);var aStr=new Array();this.db.getMatches(txt,aStr,this.maxSize);if(!aStr.length){oThis.oDiv.style.visibility='hidden';return;}
if(aStr.length==1&&aStr[0]==txt){oThis.oDiv.style.visibility='hidden';return;}
if(bTextComplete&&txt.length>0)this.textComplete(this.preText+aStr[0]);this.positionSuggest();for(i in aStr){var oNew=document.createElement('div');this.oDiv.appendChild(oNew);oNew.style.paddingLeft='2px';oNew.onmouseover=oNew.onmouseout=oNew.onmousedown=function(oEvent){oEvent=window.event||oEvent;oSrcDiv=oEvent.target||oEvent.srcElement;if(oEvent.type=='mousedown'){oThis.oText.value=oThis.preText+this.innerHTML;setCursorAtEnd(oThis.oText);}
else if(oEvent.type=='mouseover'){this.className='over';}
else if(oEvent.type=='mouseout'){this.className='';}
else{this.oText.focus();}};oNew.innerHTML=aStr[i];}
this.oDiv.style.visibility='visible';setCursorAtEnd(oThis.oText);}
else{this.oDiv.innerHTML='';this.oDiv.style.visibility='hidden';}};function createAutoComplete(InputID,DivID,index,mult){var pointsMatrix=ParsePoints();var lP=pointsMatrix.length-1;if(index==0){var aNames=new Array(lP);for(var i=0;i<lP;i++)aNames[i]=pointsMatrix[i][index];}
else{var aNames=new Array();for(var i=0;i<lP;i++){var Exists=0;var lN=aNames.length;for(var j=0;j<lN;j++){if(aNames[j]==pointsMatrix[i][index]){Exists=1;break;}}
if(!Exists)aNames[i]=pointsMatrix[i][index];}}
var W=$('#'+InputID).outerWidth();$('#'+DivID).css('width',W+12);Sugg=new autoComplete(aNames,document.getElementById(InputID),document.getElementById(DivID),15,mult);Sugg.onTextChange(false);}
function setCursorAtEnd(elem){var CharPos=elem.value.length;if(elem!=null){var IE=false;if(IE){var range=elem.createTextRange();range.move('character',CharPos);setTimeout(function(){range.select();},10);}
else if(elem.selectionStart){setTimeout(function(){elem.focus();},10);setTimeout(function(){elem.setSelectionRange(CharPos,CharPos);},10);var CEvent=document.createEvent("KeyboardEvent");if(CEvent.initKeyEvent){CEvent.initKeyEvent('keypress',true,true,null,false,false,false,false,0,32);elem.dispatchEvent(CEvent);CEvent=document.createEvent('KeyboardEvent');CEvent.initKeyEvent('keypress',true,true,null,false,false,false,false,8,0);elem.dispatchEvent(CEvent);}}
else{setTimeout(function(){elem.focus();},10);setTimeout(function(){elem.value=elem.value;},10);}}}
function getScrollXY(){if(typeof(window.pageYOffset)=='number'){var scrOfY=window.pageYOffset,scrOfX=window.pageXOffset;}
else if(document.body&&(document.body.scrollLeft||document.body.scrollTop)){var scrOfY=document.body.scrollTop,scrOfX=document.body.scrollLeft;}
else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)){var scrOfY=document.documentElement.scrollTop,scrOfX=document.documentElement.scrollLeft;}
return[((!isNaN(scrOfX))?scrOfX:0),((!isNaN(scrOfY))?scrOfY:0)];}