function enableTextSelection(flag){
	var fn=flag?null:function(){return false};
	document.body.ondrag=fn;
	document.body.onselectstart=fn;
	var mozclass=flag?"":"noselection";
	document.body.className=mozclass;
}
function RightPanelControls(){
	this.holder=null;
	this.txt=null;
	this.html=null;
	this.type=null;
	this.subtype=null;
	this.visible=false;
	this.ready=false;
	this.optslistlength=0;
	this.getHTML=function(){
		if(!this.html){
			_localizer.openGroup('customizer');
			var ropts=[];
			if(!this.isDisabledModule('dir')) ropts.push({id:"dir",tit:_localizer.tr('optcatalogtitle'),txt:_localizer.tr('optcatalogtext')});
			if(!this.isDisabledModule('live')) ropts.push({id:"live",tit:_localizer.tr('optonairtitle'),txt:_localizer.tr('optonairtext')});
			if(!this.isDisabledModule('ebay')) ropts.push({id:"ebay",tit:_localizer.tr('optebaytitle'),txt:_localizer.tr('optebaytext')});
			if(!this.isDisabledModule('tod')) ropts.push({id:"tod",tit:_localizer.tr('opttopofthedaytitle'),txt:_localizer.tr('opttopofthedaytext')});
			var h="";
			//h+="<div class=cbox_a><table class=ctbl cellpadding=0 cellspacing=0><tr valign=top><td class=chdr style=\"padding-left:20px;cursor:default\"><a class=chdrtitle>"+_localizer.tr('paneltitle')+"</a></td><td valign=middle><a href=\"#\" onclick=\"return _controls.hide()\"><img class=iboxclose src=\""+pictv41_dir+"hm_box_close.gif\" width=19 height=15 border=0></a></td></tr></table><div class=ctrlstop>"+_localizer.tr('panelhelp')+" :</div><div class=boxctrlscontent><table width=100%><tr><td>";
			h+="<div class=cbox_a><div class=\"boxctrlshelp\">"+_localizer.tr('paneltitle')+"</div><div class=boxctrlscontent><table width=100%><tr valign=top><td><div class=\"panelhelp\">"+_localizer.tr('panelhelp')+" :</div>";
			for(var i=0;i<ropts.length;i++){h+="<div class=ctrlsbox id=\"opts_"+ropts[i].id+"\" href=# onclick=\"return _controls.setContentType('"+ropts[i].id+"')\" onmouseover=\"_controls.boxov('"+ropts[i].id+"')\" onmouseout=\"_controls.boxout('"+ropts[i].id+"')\"><div class=ctrlsboxin id=\"optsi_"+ropts[i].id+"\"><div class=ctrlsboxt>"+ropts[i].tit+"</div><div class=ctrlsboxi>"+ropts[i].txt+"</div></div></div>";}
			h+="</td>";
			h+="</tr></table>";
			h+="</div></div>";
			this.html=h;
		}
		return this.html;
	}
	this.isDisabledModule=function(mod_id){
		if(typeof(wdgtdsbl)=='undefined') return false;
		for(var i=0;i<wdgtdsbl.length;i++){if(mod_id==wdgtdsbl[i]) return true;}
		return false;
	}
	this.setContentType=function(type){
		if(this.type==type){this.type=null;this.closeOptions();}
		else{this.closeOptions(true);this.type=type;this.showOptions();}
		return false;
	}
	this.boxov=function(type){
		if(b=gel("optsi_"+type)){b.style.padding=0;b.style.border="2px solid black";}
	}
	this.boxout=function(type){
		if(this.type!=type){
			if(b=gel("optsi_"+type)){b.style.border="";b.style.padding="2px";}
		}
	}
	this.getOptionsContent=function(){
		this.subtype = -1;
		var h="";
		h+="<table cellpadding=0 cellspacing=0><tr valign=top><td>";
		switch(this.type){
			default:
				h+="<div id=ctrlsoptcontent style=\"padding-top:3px;padding-bottom:2px\">";
		}
		h+="<table cellpadding=0 cellspacing=0 align=center><tr><td align=center><a class=\"txtbtn\" href=# onclick=\"return _controls.addContent()\">"+_localizer.tr('addmodule').toUpperCase()+"</a></td></tr></table></div></td></tr></table>";
		return h;
	}
	this.setOptionsIndex=function(idx){
		this.subtype=idx;
		for(var i=0;i<this.optslistlength;i++){
			/*if(o=gel("ctrlsoptidx"+i+"_on")){o.style.display=(i==idx?"none":"block");}
			if(o=gel("ctrlsoptidx"+i+"_off")){o.style.display=(i==idx?"block":"none");}*/
			if(o=gel("ctrlsoptidx"+i)){o.style.fontWeight=(i==idx?"bold":"normal");}
		}
		return false;
	}
	this.showOptions=function(){
		//this.closeOptions();
		var ref=gel("opts_"+this.type);var refpos=getPos(ref);
		var opts=document.createElement("div");
		opts.setAttribute("id","ctrlsopt");opts.innerHTML=this.getOptionsContent();opts.style.left=(refpos.x+1)+"px";opts.style.top=(refpos.y+33)+"px";
		this.holder.appendChild(opts);
		if(document.attachEvent)document.attachEvent("onclick",_controls.clickOutside);
		else if(document.addEventListener)document.addEventListener("click",function(e){_controls.clickOutside(e)},false);
		return false;
	}
	this.closeOptions=function(out){
		if(o=gel("ctrlsopt")){this.holder.removeChild(o);}
		if(out){var ptype=this.type;this.type=null;this.boxout(ptype)};
		return false;
	}
	this.clickOutside=function(e){
		var srcEle=null;
		var ev=window.event?window.event:e;
		ev.cancelBubble=true;
		if(window.event) srcEle = window.event.srcElement;
		else if(e) srcEle = e.target;
		if(srcEle!=gel("opts_"+_controls.type)&&!isChild(srcEle,gel("opts_"+_controls.type))&&!isChild(srcEle,gel("ctrlsopt"))){
			if(document.detachEvent)document.detachEvent("onclick",_controls.clickOutside);
			else if(document.removeEventListener)document.removeEventListener("click",function(e){_controls.clickOutside(e)},false);
			_controls.closeOptions(true);
		}
		
	}
	this.addContent=function(){
		switch(this.type){
			case "news":case "charts":_mngr.addModule("main",this.type,this.subtype);break;
			case "live":case "dir":case "ebay":case"tod":_mngr.addModuleEx("right",this.type);break;
			case "rss":if(this.subtype > -1){var urss=this.getRSSSources()[this.subtype]["uid"];_mngr.addModule("right",this.type,urss)}else _mngr.addModuleError(this.type);break;
		}
		return false;
	}
	this.show=function(){
		this.holder.innerHTML=this.getHTML();
		this.holder.style.display="block";
		this.txt.innerHTML=_localizer.tr('customizer/close');
		this.txt.style.color = "#282828";
		this.txt.style.backgroundColor = "#adadac";
		this.visible=true;
		return false;
	}
	this.hide=function(){
		this.holder.style.display="none";
		this.txt.innerHTML=_localizer.tr('customizer/personalizemodules');
		this.txt.style.color = "#282828";
		this.txt.style.backgroundColor = "#adadac";
		this.visible=false;
		return false;
	}
	this.hide2=function(){
		if(this.ready && this.visible) this.hide();
	}
	this.showhide=function(){
		if(this.visible) this.hide();
		else this.show();
	}
	this.init=function(holderId,txtId){
		this.holder=gel(holderId);
		this.txt=gel(txtId);
		this.ready=true;
	}
}
function RightPanelManager(){
	this.cfg=null;
	this.f=null;
	this.ftxt=null;
	this.mholder=null;
	this.rholder=null;
	this.mdup=null;
	this.rdup=null;
	this.evtmanager={};
	this.cfgref={};
	this.init=function(f_id,scfg){
		this.f=gel(f_id);
		this.ftxt=gel(f_id+"txt");
		this.initCfg(scfg);
		this.ftxt.value=scfg;
		this.initModules();
	}
	this.initCfg=function(scfg){
		this.cfg=_s2o.unserialize(scfg);
	}
	this.initModules=function(){
		if(this.mholder=gel("mpanel")){
			var voids=[];
			var count=0;
			for(var i=0;i<this.mholder.childNodes.length;i++){
				var c=this.mholder.childNodes[i];
				if(c.nodeType==3) voids.push(c);
				else if(c.id.substr(0,3)=="box"){c.style.width="100%";this.setDraggable(c.id);this.initEventResponder(c);this.cfgref[c.id]="main_"+count;count++}
			}
			for(var i=0;i<voids.length;i++) this.mholder.removeChild(voids[i]);
		}
		if(this.rholder=gel("rpanel")){
			var voids=[];
			var count=0;
			for(var i=0;i<this.rholder.childNodes.length;i++){
				var c=this.rholder.childNodes[i];
				if(c.nodeType==3) voids.push(c);
				else if(c.id.substr(0,3)=="box"){c.style.width="100%";this.setDraggable(c.id);this.initEventResponder(c);this.cfgref[c.id]="right_"+count;count++}
			}
			for(var i=0;i<voids.length;i++) this.rholder.removeChild(voids[i]);
		}
	}
	this.moveModule=function(e,dragger){
		var srcEle = null;
		if(window.event) srcEle = window.event.srcElement;
		else if(e) srcEle = e.target;
		if(srcEle.tagName && srcEle.tagName == "A") return false;
		var mod=gel(dragger.id.split("_")[0]);
		if(mod.id.substr(3,1)=="m") this.onPanelStartMove("main");
		else this.onPanelStartMove("right");
		this.dup=this.duplicate(mod);this.cmod=mod;
		document.onmousemove=this.onDocMouseMove;
		document.onmouseup=this.onDocMouseUp;
	}
	this.onDocMouseMove=function(e){
		var ev=window.event?window.event:e;
		_mngr.doMove(ev);
	}
	this.onDocMouseUp=function(e){
		var ev=window.event?window.event:e;
		_mngr.doMouseUp(ev);
	}
	this.onPanelStartMove=function(ptype){
		enableTextSelection(false);
		for(var ref in this.cfg[ptype]){
			if(this.cfg[ptype][ref]["t"]=="ad"){var adcontent=gel("boxcontentr"+ref);this.save_ad=adcontent.innerHTML;adcontent.innerHTML="<div style=\"height:250px\"></div>"};
		}
	}
	this.onPanelEndMove=function(ptype){
		enableTextSelection(true);
		for(var ref in this.cfg[ptype]){
			if(this.cfg[ptype][ref]["t"]=="ad"){var adcontent=gel("boxcontentr"+ref);adcontent.innerHTML=this.save_ad};
		}
		this.makeNewCfg();
		this.setNewCfgAsync();
	}
	this.doMove=function(ev){
		if(this.dup){
			var ws=getwinsize();var ys=0;var t=(ws.sy+ev.clientY-10);
			if(t>ws.sy+ws.h-150)ys=10;else if(t<ws.sy+50) ys=-10;
			if(ys) window.scrollBy(0,ys);
			this.dup.style.top=(t+ys)+"px";
			this.organize();
		}
	}
	this.doMouseUp=function(ev){
		document.onmousemove=null;
		document.onmouseup=null;
		if(this.dup){this.dup.style.display="none";this.cholder.replaceChild(this.cmod,this.cplace);}
		if(this.cmod){
			if(this.cmod.id.substr(3,1)=="m") this.onPanelEndMove("main");
			else this.onPanelEndMove("right");
		}
	}
	this.isBox=function(mod){
		return (mod.id.substr(0,3)=="box");
	}
	this.getBoxes=function(){
		var mods=[];
		for(var i=0;i<this.cholder.childNodes.length;i++){var c=this.cholder.childNodes[i];if(this.isBox(c))mods.push(c);else if(c==this.cplace) mods.push(c);}
		mods=mods.slice(2);
		return mods;
	}
	this.organize=function(){
		var dist=-1;
		var nearest=null;
		var debug="";
		var mods=this.getBoxes();
		for(var i=0;i<mods.length;i++){
			var pos=getPos(mods[i]);
			var dpos=getPos(this.dup);
			debug+=mods[i].id+": "+Math.abs(dpos.y - pos.y)+" ";
			if(dist==-1){nearest=mods[i];dist=Math.abs(dpos.y - pos.y)}else if(Math.abs(dpos.y - pos.y)<dist){nearest=mods[i];dist=Math.abs(dpos.y - pos.y)};
		}
		if(nearest){
			if(nearest==mods[mods.length-1]){
				this.cholder.insertBefore(this.cplace,nearest.nextSibling);
			}
			else{
				if(this.cplace==nearest.nextSibling) this.cholder.insertBefore(this.cplace,nearest);
				else this.cholder.insertBefore(nearest,this.cplace);
			}
		}
	}
	this.duplicate=function(mod){
		this.cholder=(mod.id.substr(3,1)=="m"?this.mholder:this.rholder);
		if(this.cholder==this.mholder){
			if(!this.mdup){this.mdup=document.createElement("div");this.mdup.style.position="absolute";this.mdup.style.zIndex=1000;this.mholder.appendChild(this.mdup);}
			var pos=getPos(mod);var md=this.mdup;
			md.style.left=pos.x+"px";md.style.top=pos.y+"px";md.className=mod.className;md.style.width=mod.offsetWidth+"px";md.style.display="block";md.style.opacity=.9;md.style.filter="alpha(opacity=90)";md.innerHTML=mod.innerHTML;
			if(!this.mplace){this.mplace=document.createElement("div");this.mplace.setAttribute("id","mplace");this.mplace.style.border="1px dashed black";this.mplace.style.marginBottom="10px";}
			var mp=this.mplace;
			mp.style.width=(mod.offsetWidth-4)+"px";mp.style.height=(mod.offsetHeight-4)+"px";
			this.mholder.replaceChild(mp,mod);
			this.cplace=this.mplace;
			return this.mdup;
		}
		if(this.cholder==this.rholder){
			if(!this.rdup){this.rdup=document.createElement("div");this.rdup.style.position="absolute";this.rdup.style.zIndex=1000;this.rholder.appendChild(this.rdup);}
			var pos=getPos(mod);var rd=this.rdup;
			rd.style.left=pos.x+"px";rd.style.top=pos.y+"px";rd.className=mod.className;rd.style.width=mod.offsetWidth+"px";rd.style.display="block";rd.style.opacity=.9;rd.style.filter="alpha(opacity=90)";rd.innerHTML=mod.innerHTML;
			if(!this.rplace){this.rplace=document.createElement("div");this.rplace.setAttribute("id","rplace");this.rplace.style.border="1px dashed black";this.rplace.style.marginBottom="10px";}
			var rp=this.rplace;
			rp.style.width=(mod.offsetWidth-4)+"px";rp.style.height=(mod.offsetHeight-4)+"px";
			this.rholder.replaceChild(rp,mod);
			this.cplace=this.rplace;
			return this.rdup;
		}
	}
	this.setDraggable=function(mod_id){
		if(d=gel(mod_id+"_drag")){
			d.onmousedown=function(e){_mngr.moveModule(e,this)};
		}
	}
	this.initEventResponder=function(mod){
		this.evtmanager[mod.id]={onExpand:function(){},onCollapse:function(){}};
	}
	this.addBoxEvent=function(mod_id,eventName,fn){
		this.evtmanager[mod_id][eventName]=fn;
	}
	this.setBoxBehavior=function(mod_id,btype,o){
		if(btype=="spy"){
			this.addBoxEvent(mod_id,'onCollapse',function(){o.spyPlay(false)});
			this.addBoxEvent(mod_id,'onExpand',function(){o.spyPlay(true)});
		}
	}
	this.setNewCfg=function(){
		var scfg=_o2s.serialize(this.cfg);
		this.ftxt.value=scfg;
		this.f.submit();
	}
	this.setNewCfgAsync=function(unsilent){
		var scfg=_o2s.serialize(this.cfg);var d=new Date();var aj=new ajax();
		if(unsilent) aj.setResultHandler(this.cfgChanged);
		aj.call("homecfg.ajax.php?scfg="+scfg+"&mk="+d.getTime());
	}
	this.makeNewCfg=function(){
		var debug = "";
		var ncfg={};ncfg["main"]=this.cfg["main"];ncfg["right"]=[];
		var nref={};var count=0;
		for(var i=0;i<this.rholder.childNodes.length;i++){var c=this.rholder.childNodes[i];if(this.isBox(c)){var cref=this.cfgref[c.id].split("_");ncfg["right"].push(this.cfg[cref[0]][cref[1]]);nref[c.id]="right_"+count;count++}}
		this.cfg=ncfg;
		this.cfgref=nref;
	}
	this.cfgChanged=function(){
		document.location.href=document.location.href;
	}
	this.saveModule=function(mod_id){
		var cref=this.cfgref["box"+mod_id].split("_");
		if(mobj=this.cfg[cref[0]][cref[1]]){
			var mf =gel("form_"+mod_id);var mtype=mobj["t"];
			switch(mtype){
				case "charts": mobj["n"]=mf.nb.value; mobj["r"]=mf.type.value; mobj["p"]=mf.period.value; break;
				case "news": mobj["n"]=mf.nb.value; mobj["r"]=mf.type.value; break;
			}
			this.cfg[cref[0]][cref[1]]=mobj;
			this.setNewCfg();
		}
	}
	this.switchModuleState=function(mod_id,x){
		var cref=this.cfgref["box"+mod_id].split("_");
		if(mobj=this.cfg[cref[0]][cref[1]]){mobj["x"]=x;this.cfg[cref[0]][cref[1]]=mobj;this.setNewCfgAsync();}
		if(x=="x") this.evtmanager["box"+mod_id].onExpand();
		else if(x=="c") this.evtmanager["box"+mod_id].onCollapse();
	}
	this.getSize=function(panel){
		var s=0;for(var ref in this.cfg[panel]) s++;return s;
	}
	this.addModule=function(panel,t,s){
		var mod={t:t,x:"x","n":3};
		switch(t){
			case "charts": mod["r"]="c"; mod["s"]=s; break;
			case "news": mod["r"]="c"; mod["s"]=s; mod["p"]="wk"; break;
			case "rss": mod["u"]=s;break;
			case "live": mod["n"]=3;break;
		}
		var ncfg=[];ncfg[0]=mod;for(var ref in this.cfg[panel]) ncfg.push(this.cfg[panel][ref]);this.cfg[panel]=ncfg;this.setNewCfgAsync(true);
	}
	this.addModuleEx=function(panel,t,s){
		if(!this.checkModuleEx(panel,t,s)) this.addModule(panel,t,s);else this.addModuleError("exclusive");
	}
	this.addModuleError=function(t){
		switch(t){
			case "exclusive": alert(_localizer.tr('customizer/errormoduleexclusive'));break;
			case "rss": alert("Choisissez l'un des fils RSS proposés ou entrez son adresse pour l'ajouter.");break;
		}
		return false;
	}
	this.checkModuleEx=function(panel,t,s){
		var is_in=false;	for(var ref in this.cfg[panel]){if(this.cfg[panel][ref]["t"]==t) is_in=true;}return is_in;
	}
	this.removeModule=function(mod_id){
		var cref=this.cfgref["box"+mod_id].split("_");
		this.cfg["deleted"]=this.cfg[cref[0]][cref[1]]["t"];
		var ncfg=[];
		for(var ref in this.cfg[cref[0]]){if(ref!=cref[1]) ncfg.push(this.cfg[cref[0]][ref]);}
		this.cfg[cref[0]]=ncfg;
		this.setNewCfgAsync(true);
		//holder.removeChild(gel("box"+mod_id));
	}
}
function boxdel(count){
	var msg=_localizer.tr('customizer/confirmdelmodule');
	if(t=gel("box"+count+"title")){msg=_localizer.tr('customizer/confirmdelthismodule', "\""+gtxt("box"+count+"title")+"\"");}
	if(confirm(msg)){_mngr.removeModule(count);}
	return false;
}
function boxclex(count){
	var c=gel("boxcontent"+count);
	var ed=gel("boxedit"+count);
	if(i=gel("iboxclex"+count)){
		if(i.src.indexOf("clps") > -1){
			i.src=i.src.replace("clps","expd");
			_mngr.switchModuleState(count,"c");
			if(c) c.style.display="none";
			if(ed) ed.style.display="none";
		}
		else{
			i.src=i.src.replace("expd","clps");
			_mngr.switchModuleState(count,"x");
			if(c) c.style.display="block";
			if(ed) ed.style.display="inline";
		}
	}
	return false;
}
function openControls(holderId,txtId){
	if(!_controls.ready) _controls.init(holderId,txtId);
	_controls.showhide();
	return false;
}
var _mngr=new RightPanelManager();
var _controls=new RightPanelControls();