// JavaScript Document

var filterstatus = {facturado:true,
					final:true,
					ok:true,
					error:true,
					empty:true,
					prize0:true,
					prize1:true,
					prize2:true,
					prize3:true,
					prize4:true,
					prize5:true,
					inactive:true,
					prizeyes:true,
					prizeno:true,
					noprize:true,
					prizeabs:true,
					yesvoted:true,
					novoted:true};
var typesempty = {facturado:false,
					final:false,
					ok:false,
					error:false,
					empty:false};
function resetFilterStats(){
	filterstatus = {facturado:true,
					final:true,
					ok:true,
					error:true,
					empty:false};
}
function refilterCats(){
	for(var status in filterstatus){
		filterCats(status,filterstatus[status])
	}
	for(var status in filefilterstatus){
		filterFiles(status,filefilterstatus[status])
	}
}
function filterCats(type,val,skip){
	if(val==null && !skip){
		val = !filterstatus[type];
	}
	filterstatus[type] = val;
	var caters = dojo.byId("cats");
	if(caters==null) return;
	var filtbut = dojo.byId("filter"+type);
	if(filtbut!=null){
		while(filtbut.className.indexOf("disabled")!=-1){
			filtbut.className = filtbut.className.replace("disabled","");
		}
		filtbut.className += val?"":" disabled";
	}
	var cats = dojo.query(".cat",caters);
	var showbigadd = true;
	var fullcount = 0;
	for(var i=0;i<cats.length;i++){
		var cat = cats[i];
		if(type=="empty"){
			if(cat.className.indexOf(type)!=-1){
				cat.style.display=val?'':'none';
			}
			showbigadd = !val&&typesempty.facturado&&typesempty.final&&typesempty.ok&&typesempty.error;
		}else if(type=="yesvoted" || type=="novoted"){
			if(cat.className.indexOf(type)!=-1){
				cat.style.display=val?'':'none';
			}
		}else{
			var lines = dojo.query(".catentry",cat);
			var count = 0;
			for(var j=0;j<lines.length;j++){
				var line = lines[j];
				if(line.className.indexOf(type)!=-1){
					line.style.display=val && (filterstatus['inactive'] || line.className.indexOf('inactive')==-1)?'':'none';
					if(val){
						count++;
						fullcount++;
					}
				}else{
					if(line.style.display=='') count++;
				}
			}
			if(cat.className.indexOf("empty")==-1 && cat.className.indexOf("search")==-1){
				cat.style.display=count>0?'':'none';
			}
			showbigadd = count==0 && showbigadd && !filterstatus.empty;
		}
	}
	typesempty[type] = fullcount==0;
	var bigadd = dojo.byId('bigadd');
	//if(bigadd!=null) bigadd.style.display=showbigadd?'block':'none';
	var filterinput = dojo.byId('entriesfilterinput');
	if(filterinput) filterEntriesList(filterinput.value,true)
}
function filterEntriesList(val,skip){
	if(val=="" && skip) return;
	var lines = dojo.query(".catentry",dojo.byId("myfiles"));
	val = val.toLowerCase();
	while(val.length>0 && val.lastIndexOf(" ")==val.length-1){
		val=val.substr(0,val.length-1);
	}
	while(val.indexOf("  ")!=-1){
		val = val.replace("  "," ");
	}
	var valparts = val.split(" ");
	var cats = dojo.query(".cat",dojo.byId("cats"));
	var showbigadd = true;
	for(var i=0;i<cats.length;i++){
		var cat = cats[i];
		if(cat.className.indexOf("judgecat")!=-1) continue;
		var lines = dojo.query(".catentry",cat);
		var count = 0;
		for(var j=0;j<lines.length;j++){
			var line = lines[j];
			var ltype = line.className;
			while(ltype.indexOf(" ")!=-1){
				ltype = ltype.replace(" ","");
			}
			var lsearch = "catentry";
			ltype = ltype.substr(ltype.indexOf(lsearch)+lsearch.length);
			var lstatus = filterstatus[ltype];
			var linedata = stripHTML(line.innerHTML).toLowerCase();
			linedata = stripHTML(linedata);
			var lineok = lstatus;
			for(k=0;k<valparts.length && lineok;k++){
				if(linedata.indexOf(valparts[k])==-1){
					lineok=false;
				}
			}
			if(lineok){
				line.style.display='';
				count++;
			}else{
				line.style.display='none';
			}
		}
		if(cat.className.indexOf("empty")==-1){
			cat.style.display=count>0?'':'none';
		}
		showbigadd = count==0 && showbigadd && !filterstatus.empty;
	}
	var bigadd = dojo.byId('bigadd');
	//if(bigadd!=null) bigadd.style.display=showbigadd?'block':'none';
}

function showAllEntries(val,uone){
	var cats = dojo.query(".forallscats");
	val = val==null?true:val;
	for(var i=0;i<cats.length;i++){
		var code = cats[i].id.substr(3);
		toggleSuperCat(code,val);
	}
	
	var scats = dojo.query(".forinnercats");
	for(var i=0;i<scats.length;i++){
		var code = scats[i].id.substr(3);
		toggleCat(code,val);
	}
	
	if(val){
		filterstatus.facturado = uone!=null?uone=='facturado':true;
		filterstatus.final = uone!=null?uone=='final':true;
		filterstatus.ok = uone!=null?uone=='ok':true;
		filterstatus.error = uone!=null?uone=='error':true;
		filterstatus.empty = false;
		refilterCats();
		filterEntriesList("");
		var ein = dojo.byId('entriesfilterinput');
		if(ein) ein.value = "";
	}
}
function closeAllEntries(){
}
function hideFilters(){
	var filters = dojo.query(".filtertype");
	for(var i=0;i<filters.length;i++){
		var filt = filters[i];
		filt.style.display = "none";
	}
}
function showFilter(which,votetype){
	//filteringcats
	var filters = dojo.query(".filtertype");
	for(var i=0;i<filters.length;i++){
		var filt = filters[i];
		filt.style.display = filt.id==which?"":"none";
	}
	if(votetype!=null){
		var filbuts = dojo.query(".filtbuts");
		for(var i=0;i<filbuts.length;i++){
			var fbut = filbuts[i]
			fbut.style.display = fbut.id=="fbuts"+votetype?"":"none";
		}
	}
}

var catrellast = "data.php";
var datatype;
function reloadCats(url,type){
	if(url == null && type == null){
		url = catrellast;
	}else{
		url = url==null?"data.php":url;
		url += type==null?"":"?t="+type;
		datatype = type;
	}
	fjudgedid = "reloadCats('"+url+"','"+type+"');";
	setMenuActive(type==null?'cat':type);
	showFilter('filteringuser');
	catrellast = url;
  	var catcontainer = dojo.byId('maincontents');
	catcontainer.innerHTML = getLoadingHTML();
	dojo.xhrGet({
		url: url,
		type: type,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				if(this.url!=catrellast){ return responseObject; }
				catcontainer.innerHTML = responseObject;
				refilterCats();
				countEntries();
				countFiles();
				reopenCats();
				var frp = dojo.query(".filereplacer",catcontainer);
				return responseObject;
			},
		error: function(response, ioArgs){
			console.log("ERROR LOADING EDITOR",response, ioArgs);
			container.innerHTML = getLoadingErrorHTML(true);
			//reloadCats();
			return response;
		},
		handleAs: "text"
	});
}
function reopenCats(){
	if(openedCats.length==0){
		showAllEntries(true);
	}else{
		for(var id in openedCats){
			var cdiv = dojo.byId('cat'+id);
			if(openedCats[id].type=='super'){
				toggleSuperCat(id,openedCats[id].val);
			}else{
				toggleCat(id,openedCats[id].val);
			}
		}
	}
}


function loadResume(){
	var url = "entries/resume.php";
	setMenuActive('resumen');
	catrellast = url;
  	var catcontainer = dojo.byId('maincontents');
	catcontainer.innerHTML = getLoadingHTML();
	dojo.xhrGet({
		url: url,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				if(this.url!=catrellast){ return responseObject; }
				catcontainer.innerHTML = responseObject;
				//refilterCats();
				//countEntries();
				var frp = dojo.query(".filereplacer",catcontainer);
				return responseObject;
			},
		error: function(response, ioArgs){
			console.log("ERROR LOADING EDITOR",response, ioArgs);
			container.innerHTML = getLoadingErrorHTML(true);
			//reloadCats();
			return response;
		},
		handleAs: "text"
	});
}

function countEntries(){
	var statuses = ["facturado","final","ok","error","inactive"];
	for(var stat in statuses){
		var cstat = statuses[stat];
		var entries = dojo.query(".catentry."+cstat);
		var entriesdown = dojo.query(".catentry."+cstat+".inactive");
		var countph = dojo.byId('count'+cstat);
		if(countph){
			if(cstat!="inactive"){
				countph.innerHTML = entries.length-entriesdown.length;
			}else{
				countph.innerHTML = entries.length;
			}
		}
	}
	
	var prizes = ["prizeabs","noprize","prize0","prize1","prize2","prize3","prize4","prize5","prizeyes","prizeno"];
	for(var prize in prizes){
		var cprize = prizes[prize];
		var entries = dojo.query(".catentry."+cprize);
		var countph = dojo.byId('count'+cprize);
		if(countph){
			countph.innerHTML = entries.length;
		}
	}
}
function reloadPart(id){
	url = "data.php?oo=1";
	url += "&id="+id;
	if(datatype!=null) url+="&t="+datatype;
	var saveimg = dojo.byId('savingbill'+id);
	saveimg.style.display = 'block';
  	var catcontainer = dojo.byId('catph'+id);
	//catcontainer.innerHTML = getLoadingHTML();
	catcontainer.className += " loading";
	dojo.xhrGet({
		url: url,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				catcontainer.innerHTML = responseObject;
				refilterCats();
				countEntries();
				while(catcontainer.className.indexOf("loading")!=-1){
					catcontainer.className = catcontainer.className.replace("loading","");
				}
				saveimg.style.display = 'none';
				return responseObject;
			},
		error: function(response, ioArgs){
			console.log("ERROR LOADING EDITOR",response, ioArgs);
			container.innerHTML = getLoadingErrorHTML(true);
			//reloadCats();
			return response;
		},
		handleAs: "text"
	});
}


function loadFileReplacer(which){
	var rpfvars = {width:176,height:20,
				   userid:iuid};
	var rpparams = {allowScriptAccess:'always',allowNetworking:'all',wmode:'transparent'};
	var rpargs = {};
	var phid = 'filereplacer'+which;
	var it = dojo.byId(phid);
	it.onclick = null;
	rpfvars.fileid = which;
	swfobject.embedSWF('fileReplacer.swf', phid, '176','20', '9.0.0','expressInstall.swf', rpfvars, rpparams, rpargs);
}

var openedCats = Array();
function toggleSuperCat(id,val){
	var cdiv = dojo.byId('cat'+id);
	var nVal = val!=null?val:cdiv.style.display=="none";
	if(cdiv) cdiv.style.display = nVal?"block":"none";
	openedCats[id] = {val:nVal, type:'super'};
	var toggler = dojo.byId('cattoggler'+id);
	if(toggler){
		while(toggler.className.indexOf("closed")!=-1){
			toggler.className = toggler.className.replace("closed","");
		}
		if(!nVal) toggler.className += " closed";
	}
}

function toggleCat(id,val){
	var cdiv = dojo.byId('catentries'+id);
	var nVal = val!=null?val:cdiv.style.display=="none";
	if(cdiv) cdiv.style.display = nVal?"block":"none";
	openedCats[id] = {val:nVal, type:'normal'};
	var toggler = dojo.byId('cattoggler'+id);
	if(toggler){
		while(toggler.className.indexOf("closed")!=-1){
			toggler.className = toggler.className.replace("closed","");
		}
		if(!nVal) toggler.className += " closed";
	}
}



var fjudgedid;
var catjudgelast;
function showVotingCat(catid,votetype){
	if(catid == null){
		return;
	}
	fjudgedid = "showVotingCat('"+catid+"','"+votetype+"');";
	var url = "entries/judging.cat.php?id="+catid;
	setMenuActive();
	showFilter('filteringents',votetype);
	catjudgelast = url;
  	var catcontainer = dojo.byId('maincontents');
	catcontainer.innerHTML = getLoadingHTML();
	dojo.xhrGet({
		url: url,
		load: function(responseObject, ioArgs){
				if(responseObject==null){ this.error(); return; }
				if(this.url!=catjudgelast){ return responseObject; }
				catcontainer.innerHTML = responseObject;
				refilterCats();
				countEntries();
				return responseObject;
			},
		error: function(response, ioArgs){
			console.log("ERROR LOADING EDITOR",response, ioArgs);
			container.innerHTML = getLoadingErrorHTML(true);
			//reloadCats();
			return response;
		},
		handleAs: "text"
	});
}
