
var nbLines;
var currentText="";
var currentIndex;
var edition;
var onClickItem=null;
var onEnter=null;
var onEscape=null;
var onClean=null;
var _list;
var _codes;
var _artist;
var lock=0;

function response(obj)
{
	lock=0;
	eval(obj.responseText);
	
	if (edition != 0){
		document.getElementById("_TextBoxSearch").value=_list[0];
	}
	
	if (_list.length <= 1 ) 
	{
		document.getElementById("_ResultList").style.visibility='hidden';
		edition=0;
		return;
	}
	
	document.getElementById("_ResultList").style.visibility='visible';
	
	nbLines=_list.length-1;
	html="";
	
	currentArtist="";
	for(i=1; i <= nbLines;i++)
	{
		if (mode==1 && _artist[i] != currentArtist)
		{
			currentArtist=_artist[i];
			html+="<span  class=\"artistItem\" Id=\"a"+i+"\">"+_artist[i]+"</span><br>";
		}
		html+="<span onclick=\"clickOnItem('"+i+"')\" class=\"listItem\" Id=\"l"+i+"\">"+_list[i]+"</span><br>";
	}
	document.getElementById("_ResultList").innerHTML=html;
	
	edition=1;
	if (currentText.length>=_list[0].length) edition=0;
	currentText=_list[0];
	
	document.getElementById("l1").style.backgroundColor=selectedBgColor;
	document.getElementById("l1").style.color=selectedColor;
	currentIndex=1;
	
	if (edition == 1 )selectText();
}


function selectText()
{
	var textBoxSearch=document.getElementById("_TextBoxSearch");
	if (mode == 0 )
	{
		
		var ligne=document.getElementById("l"+currentIndex).innerHTML;
		//textBoxSearch.value=ligne;
		
		/*
		myRE = new RegExp(currentText, "i")
		results = ligne.search(myRE)
		
		pos=results+currentText.length;
		
		if (textBoxSearch.setSelectionRange){
			textBoxSearch.setSelectionRange(pos,ligne.length);
		}
		else{
			selectedText=textBoxSearch.createTextRange();
			selectedText.moveStart("character", pos);
			selectedText.moveEnd("textedit");
			selectedText.select();
		}
		*/
		
	}
	textBoxSearch.focus();
}


function next()
{
	nextIndex=currentIndex+1;
	if (nextIndex > nbLines)nextIndex=1;
	document.getElementById("l"+currentIndex).style.backgroundColor=document.getElementById("l"+nextIndex).style.backgroundColor;
	document.getElementById("l"+currentIndex).style.color=document.getElementById("l"+nextIndex).style.color;
	document.getElementById("l"+nextIndex).style.backgroundColor=selectedBgColor;
	document.getElementById("l"+nextIndex).style.color=selectedColor;
	currentIndex=nextIndex;
	selectText();
}

function prev()
{
	prevIndex=currentIndex-1;
	if (prevIndex<1)prevIndex=nbLines;
	document.getElementById("l"+currentIndex).style.backgroundColor=document.getElementById("l"+prevIndex).style.backgroundColor;
	document.getElementById("l"+currentIndex).style.color=document.getElementById("l"+prevIndex).style.color;
	document.getElementById("l"+prevIndex).style.backgroundColor=selectedBgColor;
	document.getElementById("l"+prevIndex).style.color=selectedColor;
	currentIndex=prevIndex;
	selectText();
}





function keyEvent(e){

	
	switch(parseInt(e.keyCode))
	{
		case 40:
			//next();
			setTimeout("next()",10);
			break;
		case 38:
			//prev();
			setTimeout("prev()",10);
			break;
		case 8:
			edition=0;
			setTimeout("_productList()",10);
			break;
		case 13:
			if (onEnter) {
				onEnter(_list[currentIndex],_codes[currentIndex]);
			}
			document.getElementById("_ResultList").style.visibility='hidden';
			break;
		case 27:
			clean();
			if (onEscape) {
				onEscape();
			}
			
			break;
		case 16:
		case 17:
		case 18:
		case 20:
		case 37:
		case 39:
			break;
		default:
			setTimeout("_productList()",10);
		break;
	}
}

function _productList() {
	
	if (lock == 1 ) {
		document.getElementById("_ResultList").style.visibility='visible';
		document.getElementById("_ResultList").innerHTML='<span class="artistItem" >wait ...</span>';
		return;
	}
	
    var XHR = new XHRConnection();
    XHR.appendData("l", languageId);
    XHR.appendData("p", publicId);
    XHR.appendData("s", encodeURI(document.getElementById("_TextBoxSearch").value));
	
	lock=0;
	if (mode == 0 ){
		XHR.appendData("t", "t");
	}
	else{
		XHR.appendData("t", "a");
	}
	XHR.sendAndLoad("/XHRSearch4.ashx", "POST", response);
}

function clean(){
	document.getElementById("_ResultList").style.visibility='hidden';
	document.getElementById("_TextBoxSearch").value='';
	document.getElementById("_ResultList").innerHTML='';
	if (onClean)onClean();
}

function clickOnItem(index){
	if (onClickItem)onClickItem(_list[index],_codes[index]);
	document.getElementById("_ResultList").style.visibility='hidden';
}
 

document.write('<div id="_Player" style="position:absolute;height:0px;width:0px;"></div>');

function PlayerStop(){
	document.getElementById("_Player").innerHTML="";
}

function PlayerStart(sound){
	document.getElementById("_Player").innerHTML="<embed src='http://content.mkmb.net/p.ashx?id="+sound+"&t=1.mp3' hidden=true autostart=true loop=false>";
} 
