/*
9 TAB
13 ENTER
27 ESC
38 UP
40 DOWN
*/

function wordSuggest(id , type ,  width , top , left)
{

	this.id = id;
	this.type = type;
	this.input = $(id+"_"+type);
	if(!this.input)
	{
		this.input = $(id);
	}
	this.width = width;
	if(!document.all)
	{
		this.width = this.width +2;
	}
	
	this.top = top;
	if(this.top == undefined)
	{
		this.top = 23;
	}
	this.left = left;
	if(this.left == undefined)
	{
		this.left = 0;
	}
	
	var html = '<div id="'+this.id+'_suggest" class="sgt_o" style="width:'+this.width+'px; top:'+this.top+'px; left:'+this.left+'px;display:none;z-index:3;"></div>';
	
	this.input.parentNode.id = this.id+"_parent";
	new Insertion.After(this.id+"_parent" , html);
	
}

function wordsuggest_inputOnfocus(thisobj , evnt) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}

	//thisobj.className='it2';
	if(thisobj.value == "")
	{
		var namelist = {"product":"product name"};
		var html = '<div id="'+id+'_suggest_word" style="width:'+(parseInt($(id+"_suggest").style.width)-10)+'px; border-bottom:1px solid #eee; padding:0px 5px; color:#666; background:#eee;" >';
		html += 'Please Enter product info';
		html += '</div>';
		//html += '<iframe style="position:absolute; visibility:inherit; top:0px; left:0px; width:300px; height:' + (1 * 20 + 5) + 'px; z-index:-1; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';" frameborder=0></iframe>';
		$(id+"_suggest").style.display = "block";
		
		$(id+"_suggest").parentNode.style.position = "relative";
		$(id+"_suggest").style.background = "#eee";
		$(id+"_suggest").innerHTML = html;
		
	
	}
	
}

function wordsuggest_inputOnblur(thisobj , evnt) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}
	
	$(id+"_suggest").style.display = "none";
	$(id+"_suggest").parentNode.style.position = "";
}


function wordsuggest_inputOnkeyup(thisobj , evnt,languages_id) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}
	
	var suggestid = id+"_suggest";
	var num=0;
	var obj;
	var hotobj = "";
	var hotnum = "";

	while((obj = $(suggestid+"_"+num)) != null)
	{
		if(obj.className == "sgt_on")
		{
			hotobj = obj.id;
		}
		num++;
	}
		
	if(hotobj != "")
	{
		var arr = hotobj.split('_');
		hotnum = parseInt(arr[2]);
		
	}

	//DOWN
	if(evnt.keyCode == 40)
	{
		if($(id+"_suggest_0") != null && $(id+"_suggest").style.display == "block")
		{
			if(hotobj == "")
			{
				$(id+"_suggest_0").className = "sgt_on";
				
				if ('function' == typeof(wordsuggest_seling))
				{
					wordsuggest_seling($(id+"_suggest_0").childNodes[0].nodeValue);
				}
				else
				{
					thisobj.value = $(id+"_suggest_0").childNodes[0].nodeValue;
				}
			}
			else
			{
				var nextobj = suggestid+"_"+ (hotnum == num-1 ? 0 : hotnum+1);
				$(hotobj).className = "sgt_of";
				$(nextobj).className = "sgt_on";
				if ('function' == typeof(wordsuggest_seling))
				{
					wordsuggest_seling($(nextobj).childNodes[0].nodeValue);
				}
				else
				{
					thisobj.value = $(nextobj).childNodes[0].nodeValue;
				}
				
			}
			return false;
		}
	}
	//UP
	else if(evnt.keyCode == 38)
	{
		if($(id+"_suggest_"+(num-1)) != null && $(id+"_suggest").style.display == "block")
		{
			if(hotobj == "")
			{
				$(id+"_suggest_"+(num-1)).className = "sgt_on";
				if ('function' == typeof(wordsuggest_seling))
				{
					wordsuggest_seling($(id+"_suggest_"+(num-1)).childNodes[0].nodeValue);
				}
				else
				{
					thisobj.value = $(id+"_suggest_"+(num-1)).childNodes[0].nodeValue;
				}
			}
			else
			{
				var preobj = suggestid+"_"+ (hotnum == 0 ? num-1 : hotnum-1);
				$(hotobj).className = "sgt_of";
				$(preobj).className = "sgt_on";
				if ('function' == typeof(wordsuggest_seling))
				{
					wordsuggest_seling($(preobj).childNodes[0].nodeValue);
				}
				else
				{
					thisobj.value = $(preobj).childNodes[0].nodeValue;
				}
			}
		}
	}
	else if (evnt.keyCode != 9 && evnt.keyCode != 27)
	{
		if (evnt.keyCode == 13)
		{
			if ($(id+"_suggest").style.display == "block")
			{
				if ('function' == typeof(wordsuggest_selafter))
				{
					wordsuggest_selafter($(id+"_suggest_"+hotnum).childNodes[0].nodeValue);
				}
				else
				{
				//	thisobj.value = $(id+"_suggest_"+hotnum).childNodes[0].nodeValue;
				}
				
				$(id+"_suggest").style.display = "none";
				
			}
			else
			{
				wordsuggest_ajax_submit(thisobj.value , id , type,languages_id);
			}
		}
		else
		{
			wordsuggest_ajax_submit(thisobj.value , id , type,languages_id);
		}
	}
	return false;
}


function wordsuggest_inputOnkeydown(thisobj , evnt) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	
	if (evnt.keyCode == 13)
	{
		$('quick_find').submit();
	}
	else if(evnt.keyCode == 9 || evnt.keyCode == 27)
	{
		$(id+"_suggest").style.display = "none";
	}
	return true;
}




function wordsuggest_ajax_submit(text , id , type,languages_id)
{
	var url = "word_suggest.php";
	var puretext = text;
	var text = encodeURIComponent(text);

	var pars = "keywords=" + text + "&languages_id=" + languages_id;
	var myAjax = new Ajax.Request(url, {method: "get", parameters: pars, onComplete: function (req) { wordsuggest_ajax_show(req , id , puretext); } });
}

function wordsuggest_ajax_show(req , id , text)
{
	var arr = eval(req.responseText);

	if(arr.length == 0)
	{
		$(id+"_suggest").style.display = "none";
		return;
	}
	if(arr.length == 1 && arr[0] == text)
	{
		$(id+"_suggest").style.display = "none";
		return;
	}
	
	$(id+"_suggest").style.display = "block";
	$(id+"_suggest").parentNode.style.position = "relative";
	$(id+"_suggest").style.background = "#fff";
	var html = "";
	for(var i=0 ; i<arr.length ; i++)
	{
		html += '<div id="'+id+'_suggest_'+i+'" class="sgt_of" style="width:'+(parseInt($(id+"_suggest").style.width)-10)+'px" onmouseover="javascript:return wordsuggest_suggestOnmouseover(this);" onmousedown="javascript:wordsuggest_suggestOnclick(this);">'+arr[i]+'</div>';
	}
	//html += '<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px; width:300px; height:' + (arr.length * 23 + 23) + 'px; z-index:-1; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';" frameborder=0></iframe>';
	$(id+"_suggest").innerHTML = html;
	
	if($(id+"_suggest_0") != null && $(id+"_suggest").style.display == "block")
	{
		$(id+"_suggest_0").className = "sgt_on";
	}
}


function wordsuggest_suggestOnclick(thisobj)
{
	var objid = thisobj.id;
	var arr = objid.split('_');
	var id = arr[0];

	if ('function' == typeof(wordsuggest_selafter))
	{
		wordsuggest_selafter(thisobj.innerHTML);
	}
	else
	{
		$(id+"_product").value = thisobj.childNodes[0].nodeValue;
	/*	var childNodes = extractNodes(thisobj.parentNode.parentNode);
		childNodes = extractNodes(childNodes[0]);
		childNodes[0].value = thisobj.innerHTML;*/
	}
	//thisobj.parentNode.parentNode.childNodes[0].childNodes[0].value = thisobj.innerHTML;	

	
	$(id+"_suggest").style.display = "none";
	
	return false;
}

function wordsuggest_suggestOnmouseover(thisobj)
{
	var objid = thisobj.id;
	var arr = objid.split('_');
	var id = arr[0];
	var suggestid = arr[0]+'_'+arr[1];
	var objnum = arr[2];

	var num = 0;
	var obj;
	while((obj = $(suggestid+"_"+num)) != null)
	{
		obj.className = "sgt_of";	
		num ++;
	}

	thisobj.className = "sgt_on";

	return false;
}
