
var Tb="div";
var Kb="span";
var theForm;
var theField;
var theDiv;
var theDummyIFDiv;

var ea=1;
var Ma=1;

var tagw; // will be filled from hidden iframe...
var tagc; // ...

function iTS(frm,fld_tag)
{
	theForm=frm;
	theField=fld_tag;

	theField.autocomplete="off";

	theDiv=document.createElement("DIV");
	theDiv.id="completeDiv";
	theDiv.style.borderRight="black "+ea+"px solid";
	theDiv.style.borderLeft="black "+ea+"px solid";
	theDiv.style.borderTop="black "+Ma+"px solid";
	theDiv.style.borderBottom="black "+Ma+"px solid";
	theDiv.style.zIndex="10";
	theDiv.style.paddingRight="2";
	theDiv.style.paddingLeft="2";
	theDiv.style.paddingTop="2";
	theDiv.style.paddingBottom="2";
	da();
	theDiv.style.visibility="hidden";
	theDiv.style.position="absolute";
	theDiv.style.backgroundColor="white";
	theDiv.style.fontFamily="Geneva, Verdana, sans-serif";
	theDiv.style.fontSize="10px";
	theDiv.style.textAlign="justify";
	document.body.appendChild(theDiv);

	theField.onblur=hideTheDiv;
	theField.onfocus=fiOF;
	theField.onkeyup=suggestTags;

	//dummy for IE zIndex problem
	theDummyIFDiv=document.createElement("IFRAME");
	theDummyIFDiv.id="dummy";
	theDummyIFDiv.src="tagsuggest_ifr_hidden.php?uid="+uid;
	theDummyIFDiv.scrolling="no";
	theDummyIFDiv.frameborder="0";
	theDummyIFDiv.style.position="absolute";
	theDummyIFDiv.style.left="0";
	theDummyIFDiv.style.top="0";
	theDummyIFDiv.style.visibility="hidden";
	document.body.appendChild(theDummyIFDiv);

	window.onresize=da;
}
function fiOF()
{
	theField.select();
	updateTagDiv();
	//showTheDiv();
}
function hideTheDiv(){
	theDummyIFDiv.style.visibility="hidden"
	theDiv.style.visibility="hidden"
}
function showTheDiv(){
	var wi;
	var he;
	da();

	if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){
		 wi=theDiv.offsetWidth-ea*2;
		 he=theDiv.offsetHeight-ea*2;
	}else{
		wi=theDiv.offsetWidth;
		he=theDiv.offsetHeight;
	}
	theDummyIFDiv.style.width = wi;
    theDummyIFDiv.style.height = he;
    theDummyIFDiv.style.top = theDiv.style.top;
    theDummyIFDiv.style.left = theDiv.style.left;
    theDummyIFDiv.style.zIndex = theDiv.style.zIndex - 1;
	theDummyIFDiv.style.visibility="visible";

	theDiv.style.visibility="visible";
}
function zb(s){
	return kb(s,"offsetLeft")
}
function kb(s,na){
	var wb=0;
	while(s){
		wb+=s[na];
		s=s.offsetParent
	}
	return wb
}
function Yb(s){
	return kb(s,"offsetTop")
}
function da(){
	if(theDiv){
		theDiv.style.left=zb(theField)+"px";
		theDiv.style.top=Yb(theField)+theField.offsetHeight-1+"px";
		theDiv.style.width=Ta()+"px";
		theDiv.style.zIndex=10;
	}
}
function Ta(){
	if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){
		return theField.offsetWidth-ea*2
	}else{
		return theField.offsetWidth
	}
}

///////////////////

var currTags;
var currTagSel=-1;


function createTagList()
{
	var num=0;
	var tmp;
	currTags=new Array();

	for(var i=0;i<tagw.length;i++){
		currTags[i]=new Object();
		currTags[i]['tag']=tagw[i];
		currTags[i]['cnt']=tagc[i];
	}
	currTagSel=-1;
}

function updateTagDiv()
{
	var sugg=currTags;
	var tmp;
	var at='';

	if(!sugg.length){
		hideTheDiv();
		return;
	}

	at='<table cellspacing="0" cellpadding="0" width="100%" class="info">';
	for(var i=0;i<sugg.length;i++){
		if(i==currTagSel){
			tmp="<tr span style='background-color: #999999;'><td style='color: #ffffff;'>"+sugg[i]['tag']+"</td><td align='right' style='color: #ffffff;'>~"+sugg[i]['cnt']+"</td></tr>";
		}else{
			tmp="<tr onMouseOver='currTagSel="+i+"; selectFromTagSuggestionlist(); updateTagDiv();'><td>" + sugg[i]['tag']+"</td><td align='right'>~"+sugg[i]['cnt']+"</td></tr>";
		}
		at+=tmp;
	}
	at+='</table>';

	document.getElementById('completeDiv').innerHTML=at;
	if(at){
		da();
		showTheDiv();
	}else{
		hideTheDiv();
	}
}
function suggestTags(ev)
{
	//
	if(!ev){
		ev=window.event;
	}
	var key=ev.keyCode;
	if(key==38){
		if(currTagSel>0){
			currTagSel-=1;
		}else{
			currTagSel=currTags.length -1;
		}
		updateTagDiv();
		selectFromTagSuggestionlist();
		return;
	}else if(key==40){
		if(currTagSel<(currTags.length-1)){
			currTagSel+=1;
		}else{
			currTagSel=0;
		}
		updateTagDiv();
		selectFromTagSuggestionlist();
		return;
	}
	var val=theField.value;
	//new
	tellEggFrame('dummy', 'tagsuggest_ifr_hidden.php?uid='+uid+'&like='+val+"&update=1");
}
function selectFromTagSuggestionlist()
{
	if(isdefined(currTags[currTagSel])){
		theField.value=currTags[currTagSel]['tag'];
	}
}
function isdefined(v)
{
    return (typeof(v) == "undefined")?  false: true;
}


