// JavaScript Document
function SearchAssist() {
	this.numResult = 0
	this.maxRow = 10
	this.currentRow = 0
	this.offsetRow = 0
	this.containerRef = null //document.getElementById('div_result_container')
	this.inputRef = null
	this.divRowName = 'div_keyword_result_'
	this.searchPath = 'http://www.sheckys.com/modules/AjaxModules/webservice.asp'
	this.keyword = null
	this.searchTimer = null
	this.dHeight = 20
	this.resultKeywords
	this.autoScrollTimer = null
}

SAObj = new SearchAssist

SearchAssist.prototype.initializeSA = function() {
	document.write('<div id="search_assist_container" style="position:absolute;left: 0px; top: 26px; display: none; text-align:left;width:250px;z-index:99999999">')
	document.write('<iframe id="result_iframe" style="display:block;width:265px; height:240px; position:absolute; top:0; left:0; z-index:-1;  "></iframe>')
	document.write('<div style="border:2px solid #006699;background-color:#ffffff; padding:5px;z-index:999;text-align:left;width:100%">')
	
	document.write('<div style="position:relative; width:100%; height:20px;"><font style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#006699; font-size:12px;background-color: #ffffff"> Search Suggestion</font><div style="position:absolute; right:0px; top:0px"><a href="" onclick="javascript: SAObj.HideSearchAssist();return false">Close X</a></div></div><hr style="height:1px">')
	document.write('<div style="position:relative;float: left; width:225px; height:200px; overflow:hidden;background-color:#ffffff;">')
	document.write('<div id="div_result_container" style="text-align:left;  position:relative ">	')
	document.write('</div>')
	//document.write('<div style="width:40px;float:right">')
	//document.write('<a href="" onclick="javascript: SAObj.ScrollUp();return false">UP</a><br />')
	//document.write('<a href="" onclick="javascript: SAObj.ScrollDown();return false">Down</a>')
	//document.write('</div>')
	document.write('</div><div id="sa_auto_scroll" style="text-align:center; display:none; float: right;" >')
	document.write('<a href="" onMouseOver="javascript:SAObj.AutoScrollUp();document.getElementById(\'sa_up_arrow\').src = \'http://www.sheckys.com/images/buttons/arrow_up.gif\'" onMouseOut="javascript: SAObj.ClearAutoScroll();document.getElementById(\'sa_up_arrow\').src = \'http://www.sheckys.com/images/buttons/arrow_up_1.gif\'" onclick="return false">')
	document.write('<img id="sa_up_arrow" src="http://www.sheckys.com/images/buttons/arrow_up_1.gif" border="0">')
	document.write('</a>')
	document.write('<br /><br />')
	document.write('<a href="" onMouseOver="javascript:SAObj.AutoScrollDown();document.getElementById(\'sa_down_arrow\').src = \'http://www.sheckys.com/images/buttons/arrow_down.gif\'" onMouseOut="javascript: SAObj.ClearAutoScroll();document.getElementById(\'sa_down_arrow\').src = \'http://www.sheckys.com/images/buttons/arrow_down_1.gif\'" onclick="return false">')
	document.write('<img id="sa_down_arrow" src="http://www.sheckys.com/images/buttons/arrow_down_1.gif" border="0">')
	document.write('</a>')
		
	document.write('</div><div style="clear:both"></div></div></div>')
	this.containerRef = document.getElementById('div_result_container')
}

SearchAssist.prototype.NoEnter = function(e) {
	if (e.keyCode == 13) {
		if (this.currentRow >= 1) {
			this.inputRef.value = this.resultKeywords[this.currentRow-1]
			this.inputRef.form.submit();
			this.HideSearchAssist()
			return false
		} else {
			return true
		}
	} else {
		return true
	}
}

SearchAssist.prototype.ScrollResult = function(e) {
	if (e.keyCode == 40) {
		this.ScrollDown()
	}
	else if (e.keyCode == 38) {
		this.ScrollUp()
	}
}

SearchAssist.prototype.MouseOverResult = function(idx) {
	if (this.currentRow > 0 )
		document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword'
	this.currentRow = idx
	document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword_highlight'
}

SearchAssist.prototype.MouseClickResult = function(idx) {
	this.inputRef.value = this.resultKeywords[idx-1]
	this.inputRef.form.submit();
	this.HideSearchAssist()
}

SearchAssist.prototype.ScrollDown = function() {
	if (this.currentRow < this.numResult)	{
		if (this.currentRow > 0 )
			document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword'
		this.currentRow = this.currentRow + 1;
	}
	if (this.currentRow <= (this.numResult) && (this.currentRow - this.maxRow) > this.offsetRow ) { 
		this.containerRef.style.top = eval(this.containerRef.style.top.replace("px", "").replace("pt", "") - this.dHeight)+"px";
		this.offsetRow = this.offsetRow + 1
	}
	document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword_highlight'
	this.inputRef.value = this.resultKeywords[this.currentRow-1]
}

SearchAssist.prototype.ScrollUp = function() {
	//if (this.currentRow == 1 && this.offsetRow == 0) {
	//	document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword'
	//	this.currentRow = this.currentRow - 1;
	//}
	if (this.currentRow > 1) {
		document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword'
		this.currentRow = this.currentRow - 1;
	}
	if (this.currentRow >= 1 && (this.currentRow <= this.offsetRow)  ) {
		this.containerRef.style.top = eval(parseInt(this.containerRef.style.top.replace("px", "").replace("pt", "")) + this.dHeight)+"px";
		this.offsetRow = this.offsetRow - 1
	} 
	if (this.currentRow > 0) {
		document.getElementById(this.divRowName+this.currentRow).className = 'sa_keyword_highlight'
		this.inputRef.value = this.resultKeywords[this.currentRow-1]
	}
}

SearchAssist.prototype.SearchName = function(ref, e, param) {
	this.inputRef = ref
	clearTimeout(this.searchTimer)
	//this.searchTimer = setTimeout(''+this.RunSearchName(ref.value, e.keyCode, param)+'', 700)
	this.searchTimer = setTimeout('SAObj.RunSearchName(\''+ref.value+'\', \''+e.keyCode+'\', \''+param+'\')', 700)
}

SearchAssist.prototype.RunSearchName = function(ref, e, param) {
	if (e != 40 && e != 38  && e !=37 && e!= 39 && e !=13) {
		this.ResetResultArea()
		this.keyword = ref;
		PassAjaxResponseToFunction(this.searchPath+'?kw='+escape(this.keyword)+''+param, 'SAObj.UpdateResult') 
	}
}

SearchAssist.prototype.ResetResultArea = function() {
	// reset the result area
	this.numResult = 0
	this.containerRef.innerHTML = ''
	this.currentRow = 0
	this.offsetRow = 0
	this.containerRef.style.top = 0
	this.keyword = null;
	this.resultKeywords = null;
	document.getElementById('sa_auto_scroll').style.display = 'none'
	// end reset
}

SearchAssist.prototype.UpdateResult = function(txt) {
	this.resultKeywords = txt.split(":,")
	var strRows = '' 
	if (txt != "" )
		this.numResult = this.resultKeywords.length;
	else
		this.numResult = 0
	//var hlKeyword
	//var kwLength = this.keyword.length
	for (var i = 0; i <= this.numResult-1; i++) {
		//var splitKeyword = resultKeywords[i].split("|.|")
		//hlKeyword = ''+splitKeyword[1].substr(0, kwLength)+''
		//strRows = strRows + '<div class="sa_keyword" id="'+this.divRowName+eval(i+1)+'" onMouseOver="javascript: SAObj.MouseOverResult('+eval(i+1)+')" onClick="javascript: window.location=\''+splitKeyword[0]+'\' ">' + hlKeyword+splitKeyword[1].substring(kwLength) + '</div>'
		strRows = strRows + '<div class="sa_keyword" id="'+this.divRowName+eval(i+1)+'" onMouseOver="javascript: SAObj.MouseOverResult('+eval(i+1)+')" onClick="javascript:SAObj.MouseClickResult('+eval(i+1)+')">' + this.resultKeywords[i]+ '</div>'
	}
	
	if (this.numResult > 0 ) {
		this.containerRef.innerHTML = strRows
		//this.ScrollUp();
		this.DisplaySearchAssist()
	} else {
		this.HideSearchAssist()
	}

}

SearchAssist.prototype.DisplaySearchAssist = function() {
	document.getElementById('search_assist_container').style.display = 'block'
	if (this.numResult > this.maxRow)
		document.getElementById('sa_auto_scroll').style.display = 'block'
}

SearchAssist.prototype.HideSearchAssist = function() {
	document.getElementById('search_assist_container').style.display = 'none'
	this.ResetResultArea()
	this.inputRef.focus();
	this.inputRef = null
}

SearchAssist.prototype.AutoScrollDown = function() {
	this.ScrollDown();
	this.autoScrollTimer = setTimeout('SAObj.AutoScrollDown()', 100)	
}

SearchAssist.prototype.AutoScrollUp = function() {
	this.ScrollUp();
	this.autoScrollTimer = setTimeout('SAObj.AutoScrollUp()', 100)	
}

SearchAssist.prototype.ClearAutoScroll = function() {
	clearTimeout(this.autoScrollTimer )
}


