var page = "";
var new_search = false;
if(typeof(cuniv)=="undefined") var cuniv="music";
var defSearchText={music:'rechercher un artiste, un album, un titre',videos:'rechercher un artiste',mix:'tapez un nom d\'artiste'};
isSearchHelp=function(txt){
	var issh=false;
	for(var ref in defSearchText){
		if(txt==defSearchText[ref]) issh=true;
	}
	return issh;
}
setUniverse=function(uniId){
	if(uniId!=cuniv){
		var uniEnable,uniDisable;
		if(uniId=='videos'){
			uniEnable = gel('suni_videos');
			uniDisable = gel('suni_music');
		}
		else{
			uniEnable = gel('suni_music');
			uniDisable = gel('suni_videos');
		}
		uniEnable.className = 'searchfilteron';
		uniDisable.className = 'searchfilter';
		if(uniId=='videos'){
			uniDisable.style.borderLeft='';
			uniDisable.style.borderRight='1px solid #808080';
			if(nav=gel('topsubnav')) nav.style.display='none';
		}
		else{
			uniEnable.style.borderRight='';
			if(nav=gel('topsubnav')) nav.style.display='block';
		}
		if(unifield=gel('suni')) unifield.value=uniId;
		if(s=gel('mmsearch')){
			if(s.value=='' || s.value==defSearchText[cuniv]) s.value=defSearchText[uniId];
		}
		cuniv=uniId;
		if(uniId=='videos'){
			document.location.href="/videos/";
		}
		else{
			document.location.href="/";
		}
	}
	return false;
}
clkUniverse=function(uniId){
	return true;
}
initSearchBox=function(){
	if(s=gel('mmsearch')){
		blurSearch(s);
	}
}
searchMusicMe=function(){
	var f = document.f;
	if(s=gel('mmsearch')){
		if(s.value==defSearchText[cuniv]){
			s.value='';
		}
	}
	f.submit();
	return false;
}
chngSearch=function(){
	new_search = true;
}
focusSearch=function(o){
	if(o.value==defSearchText[cuniv] || (o.id=="qmix"&&o.value==defSearchText['mix'])){
		o.value = '';
		o.style.color='#282828';
	}
}
blurSearch=function(o){
	if(o.value==''){
		if(o.id=="qmix"){
			o.value = defSearchText['mix'];
		}
		else{
			o.value = defSearchText[cuniv];
			o.style.color='#999999';
		}
	}
}
setSearch=function(mode,onoff){
	if(eval(document.getElementById) != null){
		var cell = gel('cell_' + mode);
		var img = gel('img_' + mode);
		if(onoff == "on"){
			cell.className = "searchnav_" + mode + "_on";
			img.src = pict_dir + "gl_" + mode + "_arrow.gif";
		}
		else{
			cell.className = "searchnav";
			img.src = pict_dir + "gl_g_spacer.gif";
		}
	}
}
cmmz=function(_mmz){
	if(_mmz == mmz || (page == "home" && !new_search)){
		var h;
		if(_mmz == "ul") h = "/illimite";
		else if(_mmz == "dl") h = "/telechargements";
		else if(_mmz == "rt") h = "/sonneries";
		else h = "/";
		document.location.href= h;
		return false;
	}
	if(!new_search && (page == "artist" || page == "title" || page == "news" || page == "charts" || page == "catalog" || page == "cd")) return true;
	var f = document.f;
	f.mmz.value = _mmz;
	searchAMB();
	return false;
}
goCharts=function(){
	var loc = document.location.href;
	var page = loc.substr(0,loc.indexOf(".php"));
	page = page.substr(page.lastIndexOf("/") + 1);
	var charts = "charts.php";
	if(page == "artist") charts += "?fp=" + fpage;
	else if(page == "title") charts += "?fp=" + fpage;
	document.location.href = charts;
}

var globs={};
// Predictive search
PredictiveSearch=function(uid){
	var me=this;
	this.res=[];
	this.refIdx=[];
	this.offsetCoords={x:0,y:0};
	if(uid==null || uid.toString()=="")
		this.uid=Math.random()*1000000;
	else
		this.uid = uid;
	this.pr_aj = new ajax();
	this.pr_aj.setResultHandler(function(raw){me.showPredict(raw)});
	globs[this.uid]=this;
}
PredictiveSearch.prototype={
tprd: 0,
prdID: null,
prdDelay: 250,
nbk: 0,
nbq: 0,
prdCurv: null,
showMode: "ajax",
resCur: -1,
inFld: null,
subFunc: null,
fldType: null,
fldId: null,
resType: "all",
viewMode: "detailed",
attach: null,
isDisabled: false,
showResult: function(res){
	res=res.split("{").join("<b>");
	res=res.split("}").join("</b>");
	if(res.indexOf('[')>-1) res=res.substr(0,res.indexOf('['));
	return res;
},
cleanResult: function(res){
	res=res.split("{").join("");
	res=res.split("}").join("");
	return res;
},
setCurrentResult: function(idx){
	var d=this.res[idx].split('|');
	if(this.fldType) this.fldType.value=d[0];
	this.inFld.value=this.cleanResult(d[1]);
	if(this.fldId) this.fldId.value=d[3];
},
resetCurrentResult: function(){
	if(this.fldType) this.fldType.value="";
	this.inFld.value=this.prdCurv;
	if(this.fldId) this.fldId.value="";
},
showPredict: function(raw){
	var pw;
	var me=this;
	me.resetList();
	if(raw!=""){
		var r=raw.split(";");
		for(var i=0 ; i<r.length ; i++){if(r[i]!="") me.res.push(r[i]);}
		if(pw=gel('pwres'+me.uid)){
			var sorted = [[],[],[]];
			var sortedIdRef = [];
			pw.style.display="block";
			if(me.res.length>0){
				for(var i=0 ; i<me.res.length ; i++){
					var data=me.res[i].split('|');
					sorted[data[0]].push(data);
					sortedIdRef[data[0]+","+(sorted[data[0]].length-1)]=i;
				}
				if(sorted.length > 0){
					var tbl='<table>';
					var ps=-1;
					var idx=0;
					for(var s=0 ; s<sorted.length ; s++){
						var items=sorted[s];
						for(var i=0 ; i<items.length ; i++){
							me.refIdx[idx]=sortedIdRef[s+","+i];
							var stl;
							var data=items[i];
							var dispName=me.showResult(data[1]);
							var dispType;
							var dispDetails="";
							if(data[0]==0){
								dispType='Artistes';
								dispDetails=data[4]+' album'+(data[4]>1?'s':'');
							}
							else if(data[0]==1){
								dispType='Albums';
							}
							else if(data[0]==2){
								dispType='Titres';
								dispDetails=data[4]+' version'+(data[4]>1?'s':'');
							}
							var stl="";
							if(s!=ps){
								if(ps>=0) stl=" style=\"border-top:1px solid #cccccc\"";
								//tbl+='<tr><td'+stl+'><span class="pwrestype">'+dispType+'</span></td></tr>';
							}
							if(me.viewMode=="detailed")
								tbl+='<tr><td class="pwrestext"'+stl+' id="pwres'+this.uid+'_'+idx+'" onmouseover="globs[\''+me.uid+'\'].resOver(this)" onclick="globs[\''+me.uid+'\'].resClick(this)">'+dispName+'</td><td class="pwrestype"'+stl+' id="pwresd'+this.uid+'_'+idx+'" onmouseover="globs[\''+me.uid+'\'].resOver(this)" onclick="globs[\''+me.uid+'\'].resClick(this)">'+dispDetails+'</td></tr>';
							else
								tbl+='<tr><td class="pwrestext"'+stl+' id="pwres'+this.uid+'_'+idx+'" onmouseover="globs[\''+me.uid+'\'].resOver(this)" onclick="globs[\''+me.uid+'\'].resClick(this)">'+dispName+'</td></tr>';
							idx++;
							ps=s;
						}
					}
					if(me.viewMode=="detailed")
						tbl+='<tr><td colspan=2 align="right"><a href="#" onclick="return globs[\''+me.uid+'\'].disablePredict()" style="font-size:10px;color:#999999">Désactiver les suggestions</a></td></tr>';
					tbl+='</table>';
					pw.innerHTML=tbl;
				}
				else{
					pw.innerHTML='';
				}
			}
		}
	}
	else{
		me.hidePredict();
	}
},
hidePredict: function(){
	this.resetList();
	this.res=[];
	if(pw=gel('pwres'+this.uid)){
		pw.style.display="none";
	}
},
enablePredict: function(){
	deletecookie('prddsbl_'+this.uid);
	this.isDisabled = false;
},
disablePredict: function(){
	this.hidePredict();
	this.isDisabled = true;
	setcookie('prddsbl_'+this.uid,1,3600*24*30);
},
togglePredict: function(){
	if(this.isDisabled)
		this.enablePredict();
	else
		this.disablePredict();
},
resetList: function(){
	this.res=[];
	this.refIdx=[];
	this.resCur=-1;
},
initPredictWin: function(o,frmOrFunc,ht,hi,resType,viewMode,attach,offsetCoords){
	if(getcookie('prddsbl_'+this.uid)==1){
		this.isDisabled = true;
		return;
	}
	this.inFld=o;
	if(typeof(frmOrFunc)=='function')
		this.subFunc=frmOrFunc;
	else
		this.subFunc=function(){frmOrFunc.submit()};
	if(ht!=null && ht.toString()!="")
		this.fldType=ht;
	if(hi!=null && hi.toString()!="")
		this.fldId=hi;
	this.resType=resType;
	this.viewMode=viewMode;
	this.attach=attach;
	if(typeof(offsetCoords)!="undefined"){
		this.offsetCoords=offsetCoords;
	}
	var pw = document.createElement('DIV');
	pw.setAttribute("id","pwres"+this.uid);
	var w=(this.uid=='mix')?230:260;
	pw.style.width=w+"px";
	pw.className="pwres";
	var pwObj = document.body.appendChild(pw);
	this.inFld.setAttribute("autocomplete","off");
	this.posWin();
	var obj=this;
	addEvent(this.inFld,"keydown",function(evt){obj.keyPressDown(this,evt)});
	addEvent(this.inFld,"keyup",function(evt){obj.keyPressUp(this,evt)});
	addEvent(document,"click",function(evt){obj.clickDoc(this,evt)});
	addEvent(window,"resize",function(){obj.posWin()});
},
posWin: function(){
	var pw;
	var pos=getPos(this.attach);
	//alert(this.uid+": "+pos.x+"/"+pos.y+" ("+this.inFld.id+")");
	var ie_xoff=(this.uid=='mix')?-8:1;
	var ie_yoff=(this.uid=='mix')?-4:2;
	if(pw=gel('pwres'+this.uid)){
		pw.style.left=(this.offsetCoords.x+pos.x-4+(is_ie?ie_xoff:0))+"px";
		pw.style.top=(this.offsetCoords.y+pos.y+22+(is_ie?ie_yoff:0))+"px";
	}
},
clickDoc: function(doc,e){
	var targ;
	var ev=(window.event)?event:e;
	if(ev.target) targ = ev.target;
	else if(ev.srcElement) targ = ev.srcElement;
	if(targ.nodeType == 3) targ = targ.parentNode;
	var inClick=false;
	var id=targ.id,pid;
	if(id.substr(0,5)=="pwres"||id==this.fldId)
		inClick=true;
	if(!inClick && targ.parentNode){
		if(pid=targ.parentNode.id){
			if(pid.substr(0,5)=="pwres"||pid==this.fldId)
				inClick=true;
		}
	}
	if(!inClick)
		this.hidePredict();
},
predict: function(a){
	clearTimeout(this.prdID);
	if(this.isDisabled)
		return;
	if(a==1){
		v=this.prdCurv;
	}
	else{
		v=this.inFld.value;
	}
	var t=(new Date()).getTime();
	var datePrint=Math.floor(t/(3600*1000*24));
	if((t-this.tprd)>this.prdDelay||v==""){
		this.tprd=t;
		if(v!=""){
			this.pr_aj.abort();
			this.pr_aj.call("predictive.ajax.php?q="+v+"&t="+this.resType+"&r="+datePrint);
		}
		else{
			this.hidePredict();
		}
		this.nbq++;
	}
	else{
		clearTimeout(this.prdID);
		var obj=this;
		this.prdID=setTimeout(function(){obj.predict(1)},(t-this.tprd));
	}
},
hiliteRes: function(w,updFld){
	var idx,itm,itmd,pCur=-1;
	if(this.res.length>0){
		pCur=this.resCur;
		if(this.resCur==-1){
			if(w==-1) idx=this.res.length-1;
			else idx=0;
		}
		else{
			idx=this.resCur+w;
		}
		if(idx>=0 && idx<this.res.length){
			this.resCur=idx;
			if(itm=gel('pwres'+this.uid+'_'+this.resCur)){
				itm.style.backgroundColor="#dddddd";
				if(itmd=gel('pwresd'+this.uid+'_'+this.resCur))
					itmd.style.backgroundColor="#dddddd";
				if(updFld){
					this.setCurrentResult(this.refIdx[idx]);
				}
			}
		}
		else{
			this.resCur=-1;
			if(updFld){
				this.resetCurrentResult();
			}
		}
		if(pCur>-1){
			if(itm=gel('pwres'+this.uid+'_'+pCur)){
				itm.style.backgroundColor="#ffffff";
				if(itmd=gel('pwresd'+this.uid+'_'+pCur))
					itmd.style.backgroundColor="#ffffff";
			}
		}
	}
},
sub: function(){
	this.hidePredict();
	this.subFunc.apply();
	return false;
},
keyPressUp: function(fld,e){
	var keyID = (window.event)?event.keyCode:e.keyCode;
	if(keyID==13)
		this.sub();
	else if(keyID==40){}
	else if(keyID==38){}
	else if(keyID<16 || keyID>20){
		this.nbk++;
		this.prdCurv=this.inFld.value;
		this.predict(0);
	}
},
resOver: function(td){
	var itm,itmd;
	if(this.res.length>0){
		var id=td.id.split('_')[1];
		for(var i=0 ; i<this.res.length ; i++){
			if(itm=gel('pwres'+this.uid+'_'+i)){
				if(i==id){
					itm.style.backgroundColor="#dddddd";
					if(itmd=gel('pwresd'+this.uid+'_'+i))
						itmd.style.backgroundColor="#dddddd";
					this.resCur=i;
				}
				else{
					itm.style.backgroundColor="#ffffff";
					if(itmd=gel('pwresd'+this.uid+'_'+i))
						itmd.style.backgroundColor="#ffffff";
				}
			}
		}
	}
},
resClick: function(td){
	if(this.res.length>0){
		var id=td.id.split('_')[1];
		this.setCurrentResult(this.refIdx[id]);
		this.inFld.focus();
		this.sub();
		this.hidePredict();
	}
},
keyPressDown: function(fld,e){
	var keyID = (window.event)?event.keyCode:e.keyCode;
	if(keyID==40)
		this.hiliteRes(1,1);
	else if(keyID==38)
		this.hiliteRes(-1,1);
},
textChanged: function(){
}
}

MixSearch=function(){
	var me=this;
	this.mix_aj=new ajax();
	this.mix_aj.setResultHandler(function(raw){me.searchDone(raw)});
	this.suggest_aj=new ajax();
	this.suggest_aj.setResultHandler(function(raw){me.suggestDone(raw)});
}
MixSearch.prototype={
frm: null,
fld: null,
btn: null,
res: null,
ldr: null,
initinfo: null,
delaySuggestID: null,
suggestQuery: '',
suggestType: 0,
initSearch: function(f,fld,btn,res,ldr,initinfo){
	var obj=this;
	this.frm=f;
	this.fld=fld;
	this.btn=btn;
	this.res=res;
	this.ldr=ldr;
	this.initinfo=initinfo;
},
doSearch: function(evt){
	var v=this.fld.value;
	var t=(new Date()).getTime();
	var datePrint=Math.floor(t/(3600*1000*24));
	if(v!="" && v!="tapez un nom d'artiste"){
		this.mix_aj.abort();
		this.mix_aj.call("mixid.ajax.php?q="+escape(v)+"&r="+datePrint);
		this.initinfo.style.display="none";
		this.res.innerHTML="";
		this.res.style.display="none";
		this.ldr.style.display="block";
	}
	else{
		this.noResults(-1);
		this.doSuggest('',0);
	}
	return false;
},
searchDone: function(raw){
	this.ldr.style.display="none";
	this.res.style.display="none";
	if(raw==""||raw==0){
		this.noResults(0);
	}
	else{
		var data=raw.split("|");
		if(data.length<2)
			this.noResults(0);
		else if(data[1]==0)
			this.noResults(data[0]);
		else{
			streamMusicMix(data[0]);
			this.doSuggest(this.fld.value,2);
		}
	}
},
doSuggest: function(q,type){
	if(typeof(q)=="undefined"){
		q="";
	}
	this.suggestQuery=q;
	this.suggestType=type;
	this.initinfo.innerHTML="";
	this.initinfo.style.display="block";
	var t=(new Date()).getTime();
	this.suggest_aj.abort();
	this.suggest_aj.call("mixsuggest.ajax.php?q="+escape(q)+"&r="+t);
},
suggestDone: function(raw){
	if(raw!=""){
		var data=raw.split(";");
		var h="";
		if(this.suggestType==0)
			h+="Exemples";
		else if(this.suggestType==1)
			h+="Essayez";
		else if(this.suggestType==2)
			h+="Essayez aussi";
		h+=" : ";
		for(var i=0 ; i<data.length ; i++){
			var art=data[i];
			var escArt1=art.split('"').join('&quot;');
			var escArt2=escArt1.split("'").join("?");
			h+="<a title=\"Ecouter le musicMix de "+escArt1+"\" href=\"#\" onclick=\"_mSearch.autoSearch('"+escArt2+"');return false\">"+art+"</a>";
			if(i<data.length-1)
				h+=", ";
			else
				h+=",&nbsp;";
		}
		h+="<a title=\"Voir d'autres suggestions\" href=\"#\" onclick=\"_mSearch.refreshSuggest();return false\">...</a>";
		this.initinfo.innerHTML=h;
	}
},
refreshSuggest: function(){
	this.doSuggest(this.suggestQuery,this.suggestType);
},
noResults: function(aid){
	this.initinfo.style.display="none";
	this.ldr.style.display="none";
	this.res.style.display="block";
	if(aid==0){
		this.res.innerHTML="Cet artiste est introuvable !";
		this.doSuggest('',1);
	}
	else if(aid==-1)
		this.res.innerHTML="Veuillez taper un nom d'artiste";
	else{
		this.res.innerHTML="musicMix n'est pas encore disponible pour cet artiste";
		this.doSuggest(this.fld.value,1);
	}
},
sub: function(){
	this.doSearch();
	return false;
},
autoSearch: function(val){
	val=val.split('?').join("'");
	this.fld.value=val;
	this.doSearch();
}
}
var _prdSearch = new PredictiveSearch('search');