var arrowDiv ="up";
jQuery(function() {
	window.ob = new Array();
	if( jQuery.browser.mozilla ) {
		ob.push(new Observe(jQuery("input:text").get(2)));
	}
});
function autoComplete(event, listboxDiv, query, count, inverse, formobj, id) {
	

	var keyCodeNum = 0;

	if( jQuery.browser.msie ) {
		keyCodeNum = event.keyCode;
	}
	else {
		keyCodeNum = event.which;
	}
	if (keyCodeNum == 8 || (keyCodeNum > 45 && keyCodeNum < 91) || (keyCodeNum > 95 && keyCodeNum < 112))
	{
		if (query.length > 0)
		{
			jQuery.get("./wordComplete/wordcomplete.jsp",
			//jQuery.post("../_icssadm/ajax/auto.do",
					{ count: count, inverse: inverse, query: query, autoId: id },
					function(xml) {
						var keywords = $(xml).find("keyword");
						if( keywords.length > 0 ) {
							listboxDiv.find("li").remove();

							keywords.each(function() {
								var click = "onclick='" + 'queryFunc("' + $(this).text() + '", "' + formobj.selector + '"); ' + 'jQuery("' + formobj.selector + '").submit();' + "'";
								if( !jQuery.browser.mozilla )
									var q = " onmouseover='" + 'queryFunc("' + $(this).text() + '", "' + formobj.selector + '");';
								else
									var q = "";

								q += "'>";
								$("ul").append('<li ' + click + q +"<a>" + $(this).text().replace(query, '<strong>' + query + '</strong>') + '</a></li>');
								//listboxDiv.find("ul").append('<li ' + click + q +"<a>" + $(this).text().replace(query, '<strong>' + query + '</strong>') + '</a></li>');
							});
							listboxDiv.find("li").mouseover(function() {
								$(this).addClass("selected");
								listboxDiv.find("li").not($(this)).removeClass("selected");
							}).mouseout(function() {
								$(this).removeClass("selected");
							});

							setQueryDisplayOn();
						}
						else {
							setQueryDisplayOff();
						}
					},
			"xml");
		}
		else {
			setQueryDisplayOff();
		}
	}
	else if (keyCodeNum > 32 && keyCodeNum < 41 && listboxDiv.find("li").length > 0)
	{
		var li = $("ul li", formobj);
		switch(keyCodeNum)
		{
		case 36:	//home
		case 33:	//pageup
			li.each(function(i) {
				if( i == 0 ) {
					$(this).addClass("selected");
					$(this).mouseover();
				}
				else {
					$(this).removeClass("selected");
				}
			});
			break;
		case 35:	//end
		case 34:   //pagedn
			li.each(function(i) {
				if( i == li.length-1 ) {
					$(this).addClass("selected");
					$(this).mouseover();
				}
				else {
					$(this).removeClass("selected");
				}
			});
			break;
		case 38:      // arrow up
			if( !li.hasClass("selected") ) {
				li.first().addClass("selected");
				li.first().mouseover();
			}
			else {
				li.each(function(i) {
					if( $(this).hasClass("selected") ) {
						var moveidx = li.length-1;

						if( i != 0 )
							moveidx = i-1;

						li.eq(moveidx).addClass("selected");
						li.eq(moveidx).mouseover();
						$(this).removeClass("selected");
						return false;
					}
				});
			}
			break;
		case 40:  // arrow down
			if( !li.hasClass("selected") ) {
				li.last().addClass("selected");
				li.first().mouseover();
			}
			else {
				li.each(function(i) {
					if( $(this).hasClass("selected") ) {
						var moveidx = 0;

						if( i != li.length-1 )
							moveidx = i+1;

						li.eq(moveidx).addClass("selected");
						li.eq(moveidx).mouseover();
						$(this).removeClass("selected");
						return false;
					}
				});
			}
			break;
		}
	}
}
function queryFunc(query, obj) {
	jQuery("[name='icss:search:query']", obj).val(query);
}

function setArrToggle() {
	if (arrowDiv=="up") {
		setQueryDisplayOn()
	} else {
		setQueryDisplayOff();
	}
}

function setQueryDisplayOff() {
	document.getElementById("scboxlist").style.display = "none";
	document.all.s_arrow.src="http://img.sbsgolf.com/www/front/search/se_arrow_down2.gif";
	orderNum=-1;
	arrowDiv="up";
}

function setQueryDisplayOn() {
//	if (listCnt>0) {
		document.getElementById("scboxlist").style.display = "block";
		document.all.s_arrow.src="http://img.sbsgolf.com/www/front/search/se_arrow_up2.gif";
		arrowDiv="down";
//	}
}

function uriEncode(data) {
    if(data != "") {
        var encdata = '';
        var datas = data.split('&');
        
        for(i=1;i<datas.length;i++) {
            var dataq = datas[i].split('=');
            encdata += '&'+encodeURIComponent(dataq[0])+'='+encodeURIComponent(dataq[1]);
        }
    } else {
        encdata = "";
    }
    return encdata;
}


