var json;
var duree = 8;
var selectClan;
	
function upSpecTable(type){
	if(type == "clan"){
		var id = selectClan.options[selectClan.selectedIndex].value;
		var clanList = selectClan.options;
		for(var i=0; i < clanList.length; i++){			
			if(document.getElementById("clan_"+clanList[i].value) != null){
				if(clanList[i].value != id && id != ""){
					document.getElementById("clan_"+clanList[i].value).style.display = "none";
				}
				else{
					document.getElementById("clan_"+clanList[i].value).style.display = "block";
				}
			}
		}
	}
	
}

function getImgEvo(evo){
	var className = "";
	if(evo > 30) {
		className = "plusplus";
	}
	else if(evo > 10) {
		className = "plus";
	}
	else if(evo <= 10 && evo >= -10){
		className = "equal";
	}
	else if(evo < -30) {
		className = "minusminus";
	}
	else if(evo < -10) {
		className = "minus";
	}
	

	return className;
}

function getStars(n){
	var stars = "";
	for(n; n>0; n--){
		stars += star_model;
	}
	return stars;
}

var modeleTdNiveau = document.createElement("td");
var graphEvolutions	= {};
function buildLvl(div, idPers){
	var table = table_model.cloneNode(true);
	var tBody = table.getElementsByTagName("tbody")[0];
	
	
	div.appendChild(table);
	
	var n = 1;
	// Loop for levels
	for(n; n<6; n++){
		var d = 1;
		
		if(json[idPers].n["n_"+n] != null){
			var minVal = 0;
			var avgVal = 0;
			var maxVal = 0;
			var countIte = 0
			
			var newestDayFound = null;
			var oldestDayFound = null;
			
			// Loop for days
			var tempArray = [];
			for(d; d < duree; d++){
				if(json[idPers].n["n_"+n]["d_"+d] != null){
					//if(n==1) alert(d+" "+json[idPers].n["n_"+n]["d_"+d].mi)
					minVal += parseInt(json[idPers].n["n_"+n]["d_"+d].mi);
					avgVal += parseInt(json[idPers].n["n_"+n]["d_"+d].mo);
					maxVal += parseInt(json[idPers].n["n_"+n]["d_"+d].ma);
					countIte ++;
					if(newestDayFound == null) newestDayFound = parseInt(json[idPers].n["n_"+n]["d_"+d].mi);
					oldestDayFound = parseInt(json[idPers].n["n_"+n]["d_"+d].mi);
									
					tempArray.push([json[idPers].n["n_"+n]["d_"+d].mi, json[idPers].n["n_"+n]["d_"+d].mo, json[idPers].n["n_"+n]["d_"+d].ma]);						
				
				}
			}
			tempArray.reverse();
			graphEvolutions[idPers+"_"+n] = tempArray;
		//	alert(idPers+"_"+n)
			minVal = Math.round(minVal/countIte);
			avgVal = Math.round(avgVal/countIte);
			maxVal = Math.round(maxVal/countIte);
			
			//alert(n+" "+newestDayFound+" "+oldestDayFound)
			var evo = Math.round(newestDayFound * 100 / oldestDayFound) - 100;			
			//alert(evo)
			var trNiveau = tr_template.evaluate({
				stars: getStars(n),
				min: minVal,
				avg: avgVal,
				max: maxVal,
				evo: getImgEvo(evo),
				evo_num: evo,
				idPers: idPers,
				namePers: charList[idPers].name,
				lvl: n
			});
			tBody.innerHTML += trNiveau;
		}
	
	}
		
		
	
}

function openLvl(id, idPers){
	var div = $(id);
	if(div.style.display != "block"){
		var div = document.getElementById(id);
		buildLvl(div, idPers);
		div.previous().removeClassName("bottom-red-line");
		div.style.display = "block";
		
	}
	else{
		div.previous().addClassName("bottom-red-line");
		div.style.display = "none";
		div.innerHTML = "";
		
	}
}


function goTo(id){
	new Effect.ScrollTo($(id));
}


/* GRAPHIQUE */
var isOpen = false;
var isAnimating = false;
function openGraph(id, lvl){
	var jsonGraph = {"array":[]};
	if(typeof(lvl)== "undefined"){
		var d = duree;
		for(d; d > 0; d--){
			var n = 1;
			var tempArray = [0,0,0];
			var countIte = 0;
			for(n; n<6; n++){
				if(("n_"+n) in json[id].n && ("d_"+d) in json[id].n["n_"+n]){
					tempArray[0] += parseInt(json[id].n["n_"+n]["d_"+d].mi);
					tempArray[1] += parseInt(json[id].n["n_"+n]["d_"+d].mo);
					tempArray[2] += parseInt(json[id].n["n_"+n]["d_"+d].ma);
					countIte++;
				}
			}
			if(countIte != 0){
				tempArray[0] = Math.round(tempArray[0] / countIte); 
				tempArray[1] = Math.round(tempArray[1] / countIte);
				tempArray[2] = Math.round(tempArray[2] / countIte);
				jsonGraph.array.push(tempArray);
			}
		}
	}
	else{
		jsonGraph.array = graphEvolutions[id+"_"+lvl];
	}
	

	if(!isAnimating){
		Event.stopObserving(document, "graph:closed")
		if(isOpen){
			openGraph = openGraph.bind(this, id);
			Event.observe(document, "graph:closed", openGraph.bind(this, id))
			closeGraph();
		}
		else{
			isOpen = true;
			isAnimating = true;
			
			var so = new SWFObject(url+"media/Graph.swf", "ClintzFlashFl", "100%", "100%", "9", "#DBE0E4");
			so.addParam("align", "top"); 
			so.addParam("allowScriptAccess", "always");
			so.addParam("wmode", "opaque");
			so.addVariable("langage", lang);
			// '{"array":[[240,245,249],[100,235,245],[220,235,245],[220,235,245],[220,235,245],[220,235,245]]}'
			
			so.addVariable("points", $H(jsonGraph).toJSON(true).replace(/"/g, "'"));
			
			so.write("ClintzFlash");
			
			new Effect.Move($("ClintzFlashContainer"),
							{ 	x: 0,
								y: 510,
								mode: 'fixed',
								afterFinish: function(){
									isAnimating = false;
								}
							}
						);
		}
	}
	
}

function closeGraph(){
	if(!isAnimating){
		isAnimating = true;
		new Effect.Move($("ClintzFlashContainer"), 
						{ 	x: 0,
							y: -510,
							mode: 'fixed',
							afterFinish: function(){
								isOpen = false;
								isAnimating = false;
								document.fire("graph:closed");
							}
						}
					);
	}
}
