﻿var regExps = null;

function JoueursList(){
	var thisObject 		= this;
	this.list 			= new Array();
	this.length 		= 0;
	
	this.joueursByName 			= new Array();
	this.joueursByActualRank 	= new Array();
	this.joueursByBetterRank 	= new Array();
	this.joueursByDate 			= new Array();
	
	this.add = function(idx, joueur){
		thisObject.list[idx] = joueur;
		thisObject.length++;
	};
	
	this.get = function(idx){
		return thisObject.list[idx];
	};
	
	this.getLength = function(){
		return thisObject.length;
	};
	
	this.toString = function(){
		var result = "";
		for(idx in thisObject.list){
			if(thisObject.list[idx] != undefined)
				result = result + "[" + idx + ":" + thisObject.list[idx] + "] ";
		}
		return result;
	};
	
	this.getThumbnailsHTML = function(sortedField){
	
		var thumbnailsHTML = "<table align='center' cellspacing='1' cellpadding='0' border='0'>";			
		var tdCount = 0;		
		
		var requestedSortedIdList;		
		
		switch (sortedField) 
		{ 
		case 'name': 
			requestedSortedIdList = thisObject.joueursByName;
			break; 
		case 'actualRank': 
			requestedSortedIdList = thisObject.joueursByActualRank;
			break; 
		case 'betterRank': 
			requestedSortedIdList = thisObject.joueursByBetterRank;
			break; 
		case 'age': 
			requestedSortedIdList = thisObject.joueursByDate;
			break; 
		} 
		
		for (var i=0; i < requestedSortedIdList.length; i++) {
			idx = requestedSortedIdList[i];
			if(thisObject.list[idx] != undefined){
				var joueur = thisObject.list[idx];				
				
				if(tdCount%6 == 0){
					thumbnailsHTML += "<tr>";
				}
				tdCount++;
				
				thumbnailsHTML += "<td style='vertical-align:top; text-align:center;'>"
				thumbnailsHTML += joueur.getThumbnails(sortedField);
				thumbnailsHTML += "</td>";
			}				
		}
		thumbnailsHTML += "</table>";
		return thumbnailsHTML;
	};
	
	this.getThisMonthBirthday = function(){

		var months = new Array("jan","fev","mars","avr","mai","juin","juil","ao&ucirc;t","sept","oct","nov","dec");
		var currentMonth = (new Date()).getMonth();
		var birthdayItems = new Array();
		
		for (var i=0; i < thisObject.joueursByDate.length; i++) {
			idx = thisObject.joueursByDate[i];
			if(thisObject.list[idx] != undefined){
				var joueur = thisObject.list[idx];			
				if(joueur.birthday != null && eval(joueur.birthday.substring(3,5)) == (currentMonth + 1)){				
					var birthdayItem = new BirthdayItem(joueur.getCompleteName(), joueur.birthday.substring(0,2))
					birthdayItems.push(birthdayItem);
				}				
			}				
		}
		birthdayItems.sort(sortBirthdayItem);
		
		var birthdayHTML = "<div style='width:186px;height:81px;padding:2px;background:transparent url(img/birthdaybg.jpg) no-repeat'>";//border:solid 1px gray
		birthdayHTML	+= "<span style='color:red;font-family:Arial;font-size:12px;font-weight:bold;'>Anniversaires du mois :</span>";
		birthdayHTML	+= "<div style='overflow:auto;height:60px;width:164px;vertical-align:bottom;'>";
		birthdayHTML	+= "<table align='center' cellspacing='1' cellpadding='0' border='0' style='color:#000000;font-family:Arial;font-size:9px;font-weight:bold;'>";		
		if(birthdayItems.length == 0){
			birthdayHTML += "<tr><td style='text-align:left;'>Aucun</td>";
			birthdayHTML += "<td style='text-align:left;color:#0336DB'></td></tr>";
		}else{
			for (var i=0; i < birthdayItems.length; i++) {
				var birthdayItem = birthdayItems[i];	
				birthdayHTML += "<tr><td style='text-align:left;'>" + birthdayItem.name + "</td>";
				birthdayHTML += "<td style='text-align:left;color:#0336DB'>" + birthdayItem.dayOfMonth + " " + months[currentMonth] + "</td></tr>";
			}
		}
		birthdayHTML += "</table></div></div>";
		return birthdayHTML;		
	};

	// id sorted list by name
	this.setJoueursByName = function(joueursByName){
		thisObject.joueursByName = joueursByName;
	};
	
	// id sorted list by actual rank
	this.setJoueursByActualRank = function(joueursByActualRank){
		thisObject.joueursByActualRank = joueursByActualRank;
	};

	// id sorted list by better rank
	this.setJoueursByBetterRank = function(joueursByBetterRank){
		thisObject.joueursByBetterRank = joueursByBetterRank;
	};
	
	// id sorted list by date
	this.setJoueursByDate = function(joueursByDate){
		thisObject.joueursByDate = joueursByDate;
	};
	
};

function BirthdayItem(name, dayOfMonth){
	this.name 		= name;
	this.dayOfMonth = dayOfMonth;
};

function sortBirthdayItem(item1, item2){
	return (item1.dayOfMonth - item2.dayOfMonth);
};

function Joueur(idx, firstname, name, 
				photopt, photogd, birthday, 
				actualWorldRank, actualFrenchRank, actualRank, actualNotClassified,
				betterWorldRank, betterFrenchRank, betterRank, betterNotClassified,
				pingBegin, clubBegin, previousClub, role){
	
	var thisObject = this;
	
	this.idx 				= idx; // key of this objeect in joueurs array
	this.name 				= name;
	this.firstname 			= firstname;
	
	this.photopt 			= photopt;
	this.photogd 			= photogd;
	this.birthday 			= birthday;
	
	this.actualWorldRank 	= actualWorldRank;
	this.actualFrenchRank 	= actualFrenchRank;
	this.actualRank 		= actualRank;
	this.actualNotClassified= actualNotClassified;
	if(this.actualWorldRank != null){
		this.actualRankLevel = 3;		// 3 : World Level
	}else if(this.actualFrenchRank != null){
		this.actualRankLevel = 2;		// 2 : french Level
	}else if(this.actualRank != null){
		this.actualRankLevel = 1;		// 1 : standard classification
	}else if(this.actualNotClassified == true){
		this.actualRankLevel = 0;		// 0 : Not Classified
	}else{
		this.actualRankLevel = -1;		// 0 : unknow
	}
		
	//alert("actualWorldRank " + this.actualWorldRank + "   /actualFrenchRank :" + this.actualFrenchRank + " /actualRank : " + this.actualRank + " /actualNotClassified : " + this.actualNotClassified + "  /actualRankLevel  : " + this.actualRankLevel );//--
		
	this.betterWorldRank 	= betterWorldRank;
	this.betterFrenchRank 	= betterFrenchRank;
	this.betterRank 		= betterRank;
	this.betterNotClassified= betterNotClassified;
	if(this.betterWorldRank != null){
		this.betterRankLevel = 3;		// 3 : World Level
	}else if(this.betterFrenchRank != null){
		this.betterRankLevel = 2;		// 2 : french Level
	}else if(this.betterRank != null){
		this.betterRankLevel = 1;		// 1 : standard classification
	}else if(this.betterNotClassified == true){
		this.betterRankLevel = 0;		// 0 : Not Classified
	}else{
		this.betterRankLevel = -1;		// 0 : unknow
	}

	//alert("betterWorldRank " + this.betterWorldRank + "   /betterFrenchRank :" + this.betterFrenchRank + " /betterRank : " + this.betterRank + " /betterNotClassified : " + this.betterNotClassified + "  /betterRankLevel  : " + this.betterRankLevel );//--
		
	this.pingBegin 			= pingBegin; 	// pingpong firts year
	this.clubBegin 			= clubBegin;	// club firts year
	this.previousClub 		= previousClub;	// previous club name
	this.role 				= role;			// role in the club

	this.decodedName 		= decodeString(name);
	this.decodedFirstname 	= decodeString(firstname);
	
	this.getCompleteName = function(){
		var completeName = "";
		
		if(thisObject.firstname != null)
			completeName += thisObject.firstname;
			
		if(thisObject.name != null)
			completeName += " " + thisObject.name;
			
		if(thisObject.completeName == "")
			completeName = "?";
			
		return completeName;
	};
	
	this.getActualRank = function(){
		if(	thisObject.actualWorldRank != null){
			return thisObject.actualWorldRank + "° joueur mondial";
		}else if(thisObject.actualFrenchRank != null){
			return thisObject.actualFrenchRank + "° joueur français";
		}else if(thisObject.actualRank != null){
			return thisObject.actualRank;
		}else if(thisObject.actualNotClassified == true){
			return "non classé";
		}else{
			return "inconnu";
		}
	};
	
	this.getBetterRank = function(){
		if(	thisObject.betterWorldRank != null){
			return thisObject.betterWorldRank + "° joueur mondial";
		}else if(thisObject.betterFrenchRank != null){
			return thisObject.betterFrenchRank + "° joueur français";
		}else if(thisObject.betterRank != null){
			return thisObject.betterRank;
		}else if(thisObject.betterNotClassified == true){
			return "non classé";
		}else{
			return "inconnu";
		}
	};
	
	this.getThumbnails = function(sortedField){
			
		var photopt 	= thisObject.photopt != null ? thisObject.photopt : "_sansphoto.jpg";
		var informations = thisObject.getCompleteName();		
		
		switch (sortedField) 
		{ 
		case 'name': 
			// no completion
			break; 
		case 'actualRank': 
			informations += "<br><span style='font-size:10px'>" + thisObject.getActualRank() + "</span>";
			break; 
		case 'betterRank': 
			informations += "<br><span style='font-size:10px'>" + thisObject.getBetterRank() + "</span>";
			break; 
		case 'age': 
			informations += "<br><span style='font-size:10px'>" + (thisObject.birthday != null ? thisObject.birthday : "inconnu") + "</span>";
			break; 
		} 
		
		//var linkBegin 	= thisObject.photogd == null ? "" : "<a href='javascript:openDetails("+this.idx+")' title='Voir la fiche' >";
		//var linkEnd 	= thisObject.photogd == null ? "" : "</a>";
		var linkBegin 	= "<a href='javascript:openDetails("+this.idx+")' title='Voir la fiche' >";
		var linkEnd 	= "</a>";
		
		var html = 	"<table border='0' cellspacing='0' cellpadding='0' class='vi_table'>";
		html += 	"<tr><td>";
		html += 		"<table class='vi_1' cellspacing='0' cellpadding='0' style='width:100%'>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_1_tl'/>";
		html += 		 	"<td class='vi_1_t'/>";
		html += 		 	"<td class='vi_1_tr'/>";
		html += 		 "</tr>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_1_l'/>";		
		html += 			"<td class='vi_1_m'>"+linkBegin+"<img border=0 class='image' src='photos/"+ photopt +"'/>"+linkEnd+"</td>";
		html += 		 	"<td class='vi_1_r'/>";
		html += 		 "</tr>";
		html += 		"</table>";
		html += 	"</td></tr>";
		html += 	"<tr><td>";
		html += 		"<table class='vi_2' cellspacing='0' cellpadding='0' style='width:100%'>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_2_l'/>";
		html +=		 	"<td class='vi_2_m'>"+linkBegin + informations + linkEnd+"</td>";			
		html += 		 	"<td class='vi_2_r'/>";
		html += 		 "</tr>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_2_bl'/>";
		html += 		 	"<td class='vi_2_b'/>";
		html += 		 	"<td class='vi_2_br'/>";
		html += 		 "</tr>";
		html += 		"</table>";
		html += 	"</td></tr>";
		html += 	"</table>";
		return html;
	};
	
	this.getDetails = function(){
		
		var completeName	= this.getCompleteName();
		var photopt 		= thisObject.photopt 		!= null ? thisObject.photopt : "_sansphoto_gd.jpg";
		var photogd 		= thisObject.photogd 		!= null ? thisObject.photogd : photopt;
		var birthday 		= thisObject.birthday 		!= null ? thisObject.birthday : "-";
		
		var actualRank 		= thisObject.getActualRank();
		var betterRank 		= thisObject.getBetterRank();
		
		var pingBegin 		= thisObject.pingBegin 		!= null ? thisObject.pingBegin : "-";
		var clubBegin 		= thisObject.clubBegin 		!= null ? thisObject.clubBegin : "-";
		var previousClub	= thisObject.previousClub 	!= null ? thisObject.previousClub : "-";
		
				
		var html = "<table class='fiche_table' cellspacing='0' cellpadding='0' style='width:100%'>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_1_tl'/>";
		html += 		 	"<td class='vi_1_t'/>";
		html += 		 	"<td class='vi_1_tr'/>";
		html += 		 "</tr>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_1_l'/>";
		html += 		 	"<td>";
		html += 		 		"<table cellspacing='0' cellpadding='0' border=0>";
		html += 		 		"<tr>";
		html += 		 			"<td class='vi_1_m'><img src='photos/"+photogd+"'></td>";
		html += 		 			"<td style='vertical-align:top'>";
		html += 		 				"<table class='fiche_table' style='width:160px' border=0>";
		html += 		 					"<tr><td class='fiche_cell fiche-name'>"+ completeName +"</td></tr>";
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Né(e) le :</span><br><span class='fiche-data'>"+ birthday +"</span></td></tr>";
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Classement actuel :</span><br><span class='fiche-data'>"+ actualRank +"</span></td></tr>";
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Meilleur classement :</span><br><span class='fiche-data'>"+ betterRank +"</span></td></tr>";
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Joue au ping depuis :</span><br><span class='fiche-data'>"+ pingBegin +"</span></td></tr>";
		if(role != null){
			html += 		 				"<tr><td class='fiche_cell'><span class='fiche-label'>Fonction au Club :</span><br><span class='fiche-data'>"+ role +"</span></td></tr>";
		}
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Au club depuis :</span><br><span class='fiche-data'>"+ clubBegin +"</span></td></tr>";
		html += 		 					"<tr><td class='fiche_cell'><span class='fiche-label'>Ancien Club :</span><br><span class='fiche-data'>"+ previousClub +"</span></td></tr>";
		html += 		 				"</table>";
		html += 		 			"</td>";
		html += 		 		"</tr>";
		html += 		 		"</table>";
		html += 		 	"</td>";
		html += 		 	"<td class='vi_1_r'/>";
		html += 		 "</tr>";
		html += 		 "<tr>";
		html += 		 	"<td class='vi_2_bl'/>";
		html += 		 	"<td class='vi_2_b'/>";
		html += 		 	"<td class='vi_2_br'/>";
		html += 		 "</tr>";
		html += 	"</table>";
		
		return html;
	};
	

};

function openDetails(idx){
	var popup = window.open("fiche.htm?idx="+idx,"Fiche_du_joueur","menubar=no, status=no, scrollbars=no, width=500px, height=400px");
	if(popup.window.focus){popup.window.focus();}
};

function gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
};

function decodeString(chaine){
	if(chaine == null){
		return null;
	}
	var result = chaine;	
	result = result.replace(/[çÇ]/gm,"c");
	result = result.replace(/[ÈÉÊËèéêë]/gm,"e");
	result = result.replace(/[ÀÁÂÃÄÅàáâãäå]/gm,"a");
	result = result.replace(/æ/gm,"ae");
	result = result.replace(/[ÌÍÎÏìíîï]/gm,"i");
	result = result.replace(/[ÒÓÔÕÖòóôõö]/gm,"o");
	result = result.replace(/Œœ/gm,"oe");	
	result = result.replace(/[ÙÚÛÜùúûüü]/gm,"u");
	return result;
};                               
 


