var sg_inpObj = null;
var sg_n_list = null;
var sg_n_loader = null;
var sg_dtTimer = null;
var sg_mTimer = null;
var sg_sTimer = null;
var sg_actItem = null;

function sg_init(id) {
	var useSug = true;
	var rg = new RegExp('MSIE [4|5].*Windows 9[58]|Windows 9[58]|Windows NT.*MSIE [4|5]');
	if (rg.test(navigator.userAgent)) useSug = false;
	if (useSug) {
		var o = document.getElementById(id);
		if (o) {
			x = document.createElement('div')
			if (document.getElementById('sg_list')) { document.getElementById('sg_list').parentNode.removeChild(document.getElementById('sg_list')); }
			x.id = 'sg_list';
			o.parentNode.appendChild(x);
			sg_n_list = x;
			sg_n_list.style.visibility= "hidden";

			y = document.createElement('div');
			if (document.getElementById('sg_loader')) { document.getElementById('sg_loader').parentNode.removeChild(document.getElementById('sg_loader')); }
			y.id = 'sg_loader';
			o.form.appendChild(y);
			sg_n_loader = y;

			o.onkeyup = sg_keyup;
			if (document.all) o.onkeydown = sg_keyCatch;
			else o.onkeypress = sg_keyCatch;
			o.onclick = sg_unselectItem;
                        o.onblur = function () { sg_sTimer = setTimeout("sg_clear()",500);}
		}
		sg_inpObj = o;
	}
}


function sg_mouseout (obj) {
	if (sg_actItem != null) { sg_actItem.className=''; sg_actItem=null; }
}

function sg_mouseover(obj) {
	if (sg_actItem != null) { sg_actItem.className=''; }
	sg_actItem = obj;
	sg_actItem.className = 'act';
}

function sg_mouseclick(obj) {
	sg_inpObj.value = obj.childNodes[0].nodeValue;

	setTimeout("sg_inpObj.focus()",10);
	sg_getData();

	sg_inpObj.form.submit();
	return false;
}


function sg_unselectItem () {
	if (sg_actItem) {
		sg_actItem.className = '';
		sg_actItem = null;
	}
}

function sg_putSelected(key) {
	if (sg_actItem) {
		sg_inpObj.value = sg_actItem.childNodes[0].nodeValue;
		if (key != 13) { sg_getData(); }
		return true;
	} else return true;
}

function sg_getKey(e) {
	if (!e) e =  window.event;
	return (e.which) ? e.which : e.keyCode;
}

function sg_keyCatch(e) {
	keyId = sg_getKey(e);
	// enter
	if ((keyId == 13) || (keyId == 39)) { return sg_putSelected(keyId); }
	// up / down
	if ((keyId == 38) || (keyId == 40)) {
    	if (sg_actItem) {
	    	var id = parseInt(sg_actItem.id.substr(3,1));

			if (keyId == 40) var newId = id+1;
			else var newId = id-1;

			if (newId<0) newId = 0;
			if (!document.getElementById('sg-'+newId)) newId = id;
			sg_actItem.className = '';
    	} else {
	    	var newId = 0;
		}
    	if (document.getElementById('sg-'+newId)) {
	    	sg_actItem = document.getElementById('sg-'+newId);
		    sg_actItem.className = 'act';
		}
		return false;
	}
	// esc
	if (keyId == 27) { sg_clear(); }
}


function sg_keyup(e) {
	keyId = sg_getKey(e);
        if ((keyId>=48 && keyId<=57) ||(keyId>=97 && keyId<=122) || (keyId>=65 && keyId<=90) || (keyId==32)){
		clearTimeout(sg_dtTimer);
		sg_dtTimer = null;
		sg_dtTimer = setTimeout( function() { sg_getData(); },200);
	}

	if ((keyId == 8) || (keyId == 46)) {
		clearTimeout(sg_dtTimer);
		sg_dtTimer = null;
		sg_dtTimer = setTimeout( function() { sg_getData(); },100);
	}
}


function sg_getData(){
    // remove old script

    var dataLoader = document.getElementById('sg-scrLoader');

    //if (dataLoader) sg_n_loader.innerHTML = '';

    var word = sg_inpObj.value;
    if (word) {
        // pre-format request
        var nword = "";
		for (i=0; i<word.length;i++){
			letter=word.charAt(i);
			letter = letter.toLowerCase();
			char_code=word.charCodeAt(i);
                        //if ( (char_code>=97 && char_code<=122) || (char_code>=65 && char_code<=90)|| (char_code>=1488 && char_code<=1515) ){
				nword += letter;
                        //} else {
                        //        alert(char_code)
                        //        nword += '_'+char_code + "$";
                        //}
		}

	    // add new script
		s = document.createElement('script');
		s.id = 'sg-scrLoader';
                s.src = "http://www.portelo.co.il/suggest/s.asp?key=" + escape(nword);
                //s.src = "http://www.portelo.co.il/suggest/s.asp?key=" + escape(nword);
		sg_n_loader.appendChild(s);
	} else {
		sg_clear();
	}
}

function sg_show(phs,ress){
	clearTimeout(sg_sTimer);
	sg_sTimer = null;

	if (!sg_n_list.hasChildNodes()) {
		if (phs.length>0) sg_n_list.style.visibility= "visible";
		d = document.createElement('div');
		d.className = 'cont';

		sg_n_listMain = sg_n_list.appendChild(d);

		for (i=0;i<phs.length;i++) {
			var p = document.createElement("p");
			var pText = document.createTextNode(phs[i]);
			p.id = 'sg-'+i;

			p.onmouseover = function () { clearTimeout(sg_mTimer); sg_mouseover(this); };
			p.onmouseout = function () { sg_mTimer = setTimeout("sg_mouseout(this)",100); };
			p.onclick = function () { return sg_mouseclick(this); };

			p.appendChild(pText);

                        //var s = document.createElement("span");
                        //var sText = document.createTextNode('חיפשו החודש: '+sg_formatNum(ress[i]));
                        //s.appendChild(sText);

                        //p.appendChild(s);
                        sg_n_listMain.appendChild(p);
		}

	} else sg_clear();
}

function sg_formatNum(str){
    if(str){
        if(str.length > 3) str = str.substring(0, str.length - 3) + "," + str.substring(str.length - 3, str.length);
        if(str.length > 7) str = str.substring(0, str.length - 7) + "," + str.substring(str.length - 7, str.length);
        if(str.length > 11) str = str.substring(0, str.length - 11) + "," + str.substring(str.length - 11, str.length);
    }
    return str;
}

function sg_clear() {
    sg_n_list.innerHTML = '';
	sg_n_list.style.visibility= "hidden";
};

sg_SetData=function(phrase,phrases,results){
    sg_clear();
    sg_show(phrases, results);

}

