var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 500;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acResultsDiv  = null;
function setAutoComplete(field_id, results_id, get_url){
	acSearchId  = "#" + field_id;
	acResultsId = "#" + results_id;
	acURL 		= get_url;
	$("body").append('<div id="' + results_id + '"></div>');
	acSearchField	= $(acSearchId);
	acResultsDiv	= $(acResultsId);
	repositionResultsDiv();
	acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) });
	acSearchField.keyup(function (e) {
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();
		if(updownArrow(keyCode)){
			return;
		}
		if(keyCode == 13 || keyCode == 27){
			clearAutoComplete();
			return;
		}
		setTimeout(function () {autoComplete(lastVal)}, acDelay);
	});
}
function autoComplete(lastValue)
{
	var part = acSearchField.val();
	if(part == '' || part.length < 3){
		clearAutoComplete();
		return;
	}
	if(lastValue != part){
		return;
	}
	$.getJSON(acURL + part, function(json){
		var ansLength = acListTotal = json.length;
		//var firstLetter = part.substr(0, 1);
		//var str=firstLetter.toUpperCase() + part.substr(1);
		//var myNewString=str;
		//myNewString.setAttribute("class", hilight);
		//.replace(str,myNewString)
		if(ansLength > 0){
			var newData = '';
			for(i=0; i < ansLength; i++) {
				newData += '<div class="unselected">'+ json[i] +'</div>';
			}
			acResultsDiv.html(newData);
			acResultsDiv.css("display","block");
			var divs = $(acResultsId + " > div");
			divs.mouseover( function() {
				divs.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
			divs.click( function() {
				acSearchField.val(this.childNodes[0].nodeValue);
				clearAutoComplete();
			});
		} else {
			clearAutoComplete();
		}
	});
}
function clearAutoComplete()
{
	acResultsDiv.html('');
	acResultsDiv.css("display","none");
}
function repositionResultsDiv()
{
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;
	var sf_height = acSearchField.height();
	var sf_width  = acSearchField.width();
	acResultsDiv.css("position","absolute");
	acResultsDiv.css("left", sf_left - 1);
	acResultsDiv.css("top", sf_top + sf_height + 5);
	acResultsDiv.css("width", sf_width + 6);
}
function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){
		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}
		acResultsDiv.children().each(function(i){
			if(i == acListCurrent){
				acSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});
		return true;
	} else {
		acListCurrent = -1;
		return false;
	}
}
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('1.a.1g=1.a.f;1.a.Y=1.a.t;1.a.f=e(){6(2[0]==g)9 r.T||1.p&&7.z.1b||7.d.1b;6(2[0]==7)9 14.11(7.d.1n,7.d.G);9 2.1g(W[0])};1.a.t=e(){6(2[0]==g)9 r.V||1.p&&7.z.U||7.d.U;6(2[0]==7)9 14.11(7.d.1j,7.d.D);9 2.Y(W[0])};1.a.T=e(){9 2[0]==g||2[0]==7?2.f():2.3(\'o\')!=\'B\'?2[0].G-(4(2.3("k"))||0)-(4(2.3("1d"))||0):2.f()+(4(2.3("N"))||0)+(4(2.3("1c"))||0)};1.a.V=e(){9 2[0]==g||2[0]==7?2.t():2.3(\'o\')!=\'B\'?2[0].D-(4(2.3("i"))||0)-(4(2.3("18"))||0):2.f()+(4(2.3("J"))||0)+(4(2.3("16"))||0)};1.a.1s=e(){9 2[0]==g||2[0]==7?2.f():2.3(\'o\')!=\'B\'?2[0].G:2.f()+(4(2.3("k"))||0)+(4(2.3("1d"))||0)+(4(2.3("N"))||0)+(4(2.3("1c"))||0)};1.a.1r=e(){9 2[0]==g||2[0]==7?2.t():2.3(\'o\')!=\'B\'?2[0].D:2.f()+(4(2.3("i"))||0)+(4(2.3("18"))||0)+(4(2.3("J"))||0)+(4(2.3("16"))||0)};1.a.h=e(){6(2[0]==g||2[0]==7)9 r.1p||1.p&&7.z.h||7.d.h;9 2[0].h};1.a.j=e(){6(2[0]==g||2[0]==7)9 r.1l||1.p&&7.z.j||7.d.j;9 2[0].j};1.a.1k=e(c,F){u x=0,y=0,8=2[0],5=2[0],s=E,C=E,n,l=0,m=0,c=1.S({R:q,P:q,1h:E,A:q},c||{});1f{x+=5.1x||0;y+=5.1w||0;6(1.b.v||1.b.M){u L=4(1.3(5,\'k\'))||0;u K=4(1.3(5,\'i\'))||0;x+=K;y+=L;6(1.b.v&&5!=8&&1.3(5,\'1a\')!=\'19\'){x+=K;y+=L}6(1.3(5,\'w\')==\'1e\')s=q;6(1.3(5,\'w\')==\'1v\')C=q}6(c.A){n=5.17;1f{l+=5.h||0;m+=5.j||0;5=5.1u;6(1.b.v&&5!=8&&5!=n&&1.3(5,\'1a\')!=\'19\'){x+=4(1.3(5,\'i\'))||0;y+=4(1.3(5,\'k\'))||0}}15(n&&5!=n)}Q 5=5.17;6(5&&(5.13.12()==\'d\'||5.13.12()==\'1q\')){6((1.b.H||(1.b.M&&1.p))&&1.3(8,\'w\')!=\'1e\'){x+=4(1.3(5,\'10\'))||0;y+=4(1.3(5,\'Z\'))||0}6((1.b.v&&!s)||(1.b.M&&1.3(8,\'w\')==\'1o\'&&(!C||!s))){x+=4(1.3(5,\'i\'))||0;y+=4(1.3(5,\'k\'))||0}1m}}15(5);6(!c.R){x-=4(1.3(8,\'10\'))||0;y-=4(1.3(8,\'Z\'))||0}6(c.P&&(1.b.H||1.b.O)){x+=4(1.3(8,\'i\'))||0;y+=4(1.3(8,\'k\'))||0}Q 6(!c.P&&!(1.b.H||1.b.O)){x-=4(1.3(8,\'i\'))||0;y-=4(1.3(8,\'k\'))||0}6(c.1h){x+=4(1.3(8,\'J\'))||0;y+=4(1.3(8,\'N\'))||0}6(c.A&&1.b.O&&1.3(8,\'o\')==\'1t\'){l-=8.h||0;m-=8.j||0}u I=c.A?{X:y-m,1i:x-l,j:m,h:l}:{X:y,1i:x};6(F){1.S(F,I);9 2}Q{9 I}};',62,96,'|jQuery|this|css|parseInt|parent|if|document|elem|return|fn|browser|options|body|function|height|window|scrollLeft|borderLeftWidth|scrollTop|borderTopWidth|sl|st|op|display|boxModel|true|self|absparent|width|var|mozilla|position|||documentElement|scroll|none|relparent|offsetWidth|false|returnObject|offsetHeight|safari|returnValue|paddingLeft|bl|bt|msie|paddingTop|opera|border|else|margin|extend|innerHeight|clientWidth|innerWidth|arguments|top|_width|marginTop|marginLeft|max|toLowerCase|tagName|Math|while|paddingRight|offsetParent|borderRightWidth|visible|overflow|clientHeight|paddingBottom|borderBottomWidth|absolute|do|_height|padding|left|scrollWidth|offset|pageYOffset|break|scrollHeight|static|pageXOffset|html|outerWidth|outerHeight|inline|parentNode|relative|offsetTop|offsetLeft'.split('|'),0,{}))
