function file(fichier)
{
	if(window.XMLHttpRequest) // FIREFOX
	xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	return('Erreur de chargement');
	
	xhr_object.open("GET", fichier, false);
	
	xhr_object.send(null);
	
	if(xhr_object.readyState == 4)
	return(xhr_object.responseText);
	else return("Erreur de chargement des données AJAX. Merci de prévenir un administrateur en copiant le texte suivant:<br />"+navigator.userAgent);
}

function return_message(content, filtre)
{
document.getElementById('information').style.display = 'block';
document.getElementById('information_message').innerHTML = content;
}

function information_message(contenu, id)
{
	if(contenu == 'poids')
	return_message(file('ajax.php?ajax=information&type=poids'), 1);
	else if(contenu == 'adopter' && id > 0)
	{
		if(file('ajax.php?ajax=adopter&cat_id='+escape(id)) != '1')
		return_message(file('ajax.php?ajax=adopter&cat_id='+escape(id)));
		else headers('mes_chats');
	}
	else if(contenu == 'caracteristiques')
	return_message(file('ajax.php?ajax=information&type=caracteristiques'));
	else if(contenu == 'manger' && id > 0)
	return_message(file('ajax.php?ajax=manger&cat_id='+escape(id)));
	else if(contenu == 'jouer' && id > 0)
	return_message(file('ajax.php?ajax=jouer&cat_id='+escape(id)));
	else if(contenu == 'bichonner' && id > 0)
	return_message(file('ajax.php?ajax=bichonner&cat_id='+escape(id)));
	else if(contenu == 'balade' && id > 0)
	return_message(file('ajax.php?ajax=balade&cat_id='+escape(id)));
	else if(contenu == 'rebaptiser' && id > 0)
	return_message(file('ajax.php?ajax=rebaptiser&cat_id='+escape(id)));
	else if(contenu == 'services' && id > 0)
	return_message(file('ajax.php?ajax=services&cat_id='+escape(id)));
}

function information_close()
{
document.getElementById('information').style.display = 'none';
}

function headers(location, timeout)
{
	if(timeout == '')
	timeout = 1000;
	
setTimeout("window.location='?p="+escape(location)+"'", timeout);
}

function utiliser_produit(cat_id, product_id, quantity, type)
{
return_message(file('ajax.php?ajax='+escape(type)+'&cat_id='+escape(cat_id)+'&produit_id='+escape(product_id)+'&quantite='+escape(quantity)));
}

var story_id_get = 0;
var cat_id_get = 0;
var story_page_get = 0;
function balade(story_id, cat_id, page)
{
// modifier le titre de la page: document.title = '';
content = file('ajax.php?ajax=balade&cat_id='+escape(cat_id)+'&story_id='+escape(story_id)+'&story_page='+escape(page));
document.getElementById('information_message').style.display = 'none';
document.getElementById('information').style.paddingTop = 0;
document.getElementById('information').style.display = 'block';
document.getElementById('information_balade').style.display = 'block';
document.getElementById('information_balade').innerHTML = content;

story_id_get = story_id;
story_page_get = page;
cat_id_get = cat_id;
}

function balade_keypress(key)
{
	if(file('ajax.php?ajax=balade&cat_id='+escape(cat_id_get)+'&story_id='+escape(story_id_get)+'&story_page='+escape(story_page_get+1)) != 'End')
	{
		if(key == 37) // Left
		{
		balade(story_id_get, cat_id_get, story_page_get-1);
		}
		else if(key == 39) // Right
		{
		balade(story_id_get, cat_id_get, story_page_get+1);
		}
	}
	else close_balade(story_id_get, cat_id_get);
}

function close_balade(story_id, cat_id)
{
document.getElementById('information_balade').style.display = 'none';
document.getElementById('information_balade').innerHTML = '';
document.getElementById('information_message').style.display = 'block';
return_message(file('ajax.php?ajax=story_ending&story_id='+escape(story_id)+'&cat_id='+escape(cat_id)));
}

function rebaptiser(cat_id, new_name)
{
return_message(file("ajax.php?ajax=rebaptiser&cat_id="+escape(cat_id)+"&new_name="+escape(new_name)+"&replace=1"));
}

function pendu(letter, new_letter)
{
document.getElementById('pendu_game').innerHTML = file('ajax.php?ajax=pendu&new_letter='+escape(new_letter)+'&letter='+escape(letter));
}

function cards(choice)
{
document.getElementById('cards_game').innerHTML = file('ajax.php?ajax=cards&cards_choice='+escape(choice));
}

var temps_a = 20;
function charades()
{
	if(temps_a >= 10)
	{
	str_temps_a = '"'+temps_a+'"';
	
	img_temps_a = '<img src="design/images/games/numbers/'+str_temps_a.substring(1, 2)+'.png" alt="'+str_temps_a.substring(1, 2)+'" /> <img src="design/images/games/numbers/'+str_temps_a.substring(2, 3)+'.png" alt="'+str_temps_a.substring(2, 3)+'" />';
	}
	else img_temps_a = '<img src="design/images/games/numbers/'+temps_a+'.png" alt="'+temps_a+'" />';
	
	// On écrit le temps restant
	document.getElementById('charades_temps_restant').innerHTML = '<img src="design/images/games/temps_restant.png" alt="Temps restant" /> '+img_temps_a;
	
	if(temps_a >= 11)
	{
		if(temps_a >= 12)
		s = 's';
		else s = '';
		
	aide = (temps_a-10)+' seconde'+s+' avant l\'aide';
	}
	else aide = file('ajax.php?ajax=charades&get_help=1');
	
	// On affiche l'aide à partir de 10 secondes restantes
	document.getElementById('temps_avant_aide').innerHTML = aide;
	
	// On lance l'horloge pour le décompte des 20 secondes
	if(temps_a > 0)
	charade_timeout_a = setTimeout("charades()", 1000);
	else
	{
	clearTimeout(charade_timeout_a);
	document.getElementById('charades_aide_verif').style.display = 'none';
	document.getElementById('reload_game').style.display = 'block';
	document.getElementById('temps_ecoule').style.display = 'block';
	charades_solutions();
	}
	temps_a--;
}

function charade_verif(verif)
{
	if(verif.length > 2)
	{
	aideverif = file('ajax.php?ajax=charades&aide_verif='+escape(verif));
	
		if(aideverif == '0')
		document.getElementById('aide_verif').innerHTML = '<span class="rouge">Faux. Réessaye !</span>';
		else if(aideverif == '1')
		{
		clearTimeout(charade_timeout_a);
		
		document.getElementById('aide_verif').innerHTML = '<span class="vert">Bravo ! Tu as trouvé le mot !</span>';
		document.getElementById('reponse_trouvee').style.display = 'block';
		
		document.getElementById('charades_aide_verif').style.display = 'none';
		document.getElementById('reload_game').style.display = 'block';
		
		charades_solutions();
		}
	}
}

function charades_solutions()
{
reps = new Array;

reps[1] = file('ajax.php?ajax=charades&fin_partie=1');

document.getElementById('mon_premier_rep').innerHTML = '<strong>Etait:</strong> '+reps[1];

reps[2] = file('ajax.php?ajax=charades&fin_partie=2');

document.getElementById('mon_second_rep').innerHTML = '<strong>Etait:</strong> '+reps[2];

reps[3] = file('ajax.php?ajax=charades&fin_partie=3');
	if(reps[3] != '')
	document.getElementById('mon_troisieme_rep').innerHTML = '<strong>Etait:</strong> '+reps[3];
	
reps[4] = file('ajax.php?ajax=charades&fin_partie=4');
	if(reps[4] != '')
	document.getElementById('mon_quatrieme_rep').innerHTML = '<strong>Etait:</strong> '+reps[4];
}

function prix_produit(product_id, quantity)
{
document.getElementById('prix_'+escape(product_id)).innerHTML = file('ajax.php?ajax=prix_produit&product_id='+escape(product_id)+'&quantity='+escape(quantity));
}

function acheter_produit(product_id, quantity)
{
	return_message(file('ajax.php?ajax=acheter_produit&product_id='+escape(product_id)+'&quantity='+escape(quantity)));
}

function acheter_race(race_id, cat_sexe, cat_name)
{
	return_message(file('ajax.php?ajax=acheter_race&race_id='+escape(race_id)+'&sexe='+escape(cat_sexe)+'&name='+escape(cat_name)));
}

var tab_window = new Array;
var tab_class = new Array;
var hr_display = new Array;
function bottom_bar_window(id)
{
	for(a = 1; a <= 3; a++)
	{
		// On ferme tous les onglets
		document.getElementById('bottom_bar_window_'+a).style.display = 'none';
		document.getElementById('bottom_bar_tab_'+a).className = ''
		document.getElementById('bottom_bar_hr_'+a).style.display = 'none';
		
		if(a != id)
		{
		tab_window[a] = 'none';
		tab_class[a] = '';
		hr_display[a] = 'none';
		}
	}
	
	if(tab_window[escape(id)] == 'block')
	tab_window[escape(id)] = 'none';
	else tab_window[escape(id)] = 'block';
	
	document.getElementById('bottom_bar_window_'+escape(id)).style.display = tab_window[escape(id)];
	
	if(tab_class[escape(id)] == 'bottom_bar_tab_selected')
	tab_class[escape(id)] = '';
	else tab_class[escape(id)] = 'bottom_bar_tab_selected';
	
	document.getElementById('bottom_bar_tab_'+escape(id)).className = tab_class[escape(id)];
	
	if(hr_display[escape(id)] == 'block')
	hr_display[escape(id)] = 'none';
	else hr_display[escape(id)] = 'block';
	
	document.getElementById('bottom_bar_hr_'+escape(id)).style.display = hr_display[escape(id)];
}

function ecuelle_remplir(cat_id, product_id, valid)
{
return_message(file('ajax.php?ajax=ecuelle&do=fill&cat_id='+escape(cat_id)+'&product_id='+escape(product_id)+'&valid='+escape(valid)));
}

function ecuelle_vider(cat_id, valid)
{
return_message(file('ajax.php?ajax=ecuelle&do=empty&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function ecuelle_nettoyer(cat_id, valid)
{
return_message(file('ajax.php?ajax=ecuelle&do=clean&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function bol_vider(cat_id, valid)
{
return_message(file('ajax.php?ajax=bol&do=empty&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function bol_nettoyer(cat_id, valid)
{
return_message(file('ajax.php?ajax=bol&do=clean&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function bol_remplir(cat_id, valid)
{
return_message(file('ajax.php?ajax=bol&do=fill&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function litiere_nettoyer(cat_id, valid)
{
return_message(file('ajax.php?ajax=litter&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function panier_nettoyer(cat_id, valid)
{
return_message(file('ajax.php?ajax=bed&cat_id='+escape(cat_id)+'&valid='+escape(valid)));
}

function ami_ajouter(friend_id)
{
return_message(file('ajax.php?ajax=ami_ajouter&friend_id='+escape(friend_id)));
}

function ami_accepter(friend_id)
{
return_message(file('ajax.php?ajax=ami_ajouter&friend_id='+escape(friend_id)));
}

function ami_refuser(friend_id)
{
return_message(file('ajax.php?ajax=ami_refuser&friend_id='+escape(friend_id)));
}

var notif_read = 0;

function notification_read()
{
file('ajax.php?ajax=notification_read');
	if(notif_read == 0)
	{
		for(a = 1; a <= 120; a++)
		{
		setTimeout('document.getElementById(\'new_notifications\').style.backgroundColor = \'rgb(255, 255, '+(135+a)+')\';', a*30); // 30 * 120 = 3600 = 3,6 secondes
		}
		notif_read = 1;
		document.getElementById('new_notifications_nb').style.display = 'none';
	}
}

function miaoubox_all()
{
	if(document.getElementById('miaoubox_all').style.display == 'block')
	display = 'none';
	else display = 'block';
	
	document.getElementById('miaoubox_all').style.display = display;
}

var miaoubox_now_id = 1;
function miaoubox(last)
{
	document.getElementById('miaoubox').innerHTML = document.getElementById('miaoumessage_'+miaoubox_now_id).innerHTML;
	document.getElementById('miaoubox').style.opacity = 0;
	b = 0;
	
	for(a=1; a<=120; a++)
	{
	setTimeout('document.getElementById(\'miaoubox\').style.opacity = '+b, a*20);
	b = b+0.01;
	}
	
	for(a = 1; a <= last; a++)
	{
	document.getElementById('miaoumessage_'+a).style.paddingLeft = '0px';
	}
	
	document.getElementById('miaoumessage_'+miaoubox_now_id).style.paddingLeft = '10px';
	
	if(miaoubox_now_id == last)
	miaoubox_now_id = 1;
	else miaoubox_now_id++;
	
	setTimeout('miaoubox('+last+')', 5000);
}

function news(id, nb)
{
	for(a = 1; a <= nb; a++)
	{
	document.getElementById('news_titre_'+a).style.color = '#2392ae';
	document.getElementById('news_titre_'+a).style.fontWeight = 'normal';
	document.getElementById('new_contenu_'+a).style.display = 'none';
	}
	
document.getElementById('news_titre_'+id).style.color = '#67a654';
document.getElementById('news_titre_'+id).style.fontWeight = 'bold';
document.getElementById('new_contenu_'+id).style.display = 'block';
}

function switch_mode(mode)
{
return_message(file('ajax.php?ajax=switch_mode&mode='+escape(mode)), 1);
}

var crazy_cats_score = 0;
function crazy_cats_points()
{
crazy_cats_score++;
}

zone_de_jeu_largeur = 350;
zone_de_jeu_hauteur = 350;
crazy_cat_img_largeur = 20;
crazy_cat_img_hauteur = 20;
limit = 0;
game_time = 20;
t = '';
function crazy_cat()
{
	if(limit <= game_time) // x secondes de jeu maximum
	{
		if(limit == 0)
		{
		document.getElementById('crazy_cat').style.display = 'block';
		document.getElementById('crazy_cats_start').style.display = 'none';
		}
		
	crazy_cat_pos_x = Math.random()*(zone_de_jeu_largeur-crazy_cat_img_largeur);
	crazy_cat_pos_y = Math.random()*(zone_de_jeu_hauteur-crazy_cat_img_hauteur);
	
	// On a les coordonnées du chat fou après démarrage, généré aléatoirement
	
	document.getElementById('crazy_cat').style.marginLeft = crazy_cat_pos_x+'px';
	document.getElementById('crazy_cat').style.marginTop = crazy_cat_pos_y+'px';
	
	temps_restant = '"'+(game_time-limit)+'"';
	
		if((game_time-limit) >= 10)
		numero = '<img src="design/images/games/numbers/'+temps_restant.substring(1, 2)+'.png" alt="'+temps_restant.substring(1, 2)+'" /> <img src="design/images/games/numbers/'+temps_restant.substring(2, 3)+'.png" alt="'+temps_restant.substring(2, 3)+'" />';
		else numero = '<img src="design/images/games/numbers/'+temps_restant.substring(1, 2)+'.png" alt="'+temps_restant.substring(1, 2)+'" />';
		
	document.getElementById('crazy_cats_timeout').innerHTML = '<img src="design/images/games/temps_restant.png" alt="Temps restant" /> '+numero;
	
	t=setTimeout("crazy_cat()", 1000);
	limit++;
	}
	else
	{
	clearTimeout(t);
	limit = 0;
	file('ajax.php?ajax=crazy_cats&do=insert_score&score='+escape(crazy_cats_score));
	resultat = file('ajax.php?ajax=crazy_cats&do=resultat');
	crazy_cats_load('', resultat, crazy_cats_score);
	crazy_cats_score = 0;
	}
}

function crazy_cats(defi_id)
{
	if(defi_id > 0)
	{
		if(file('ajax.php?ajax=crazy_cats&defi_id='+escape(defi_id)+'&do=check') != 1)
		{
		document.getElementById('crazy_cats_presentation').style.display = 'block';
		document.getElementById('crazy_cats_playing_link_'+escape(defi_id)).style.display = 'block';
		document.getElementById('crazy_cats_play_link_'+escape(defi_id)).style.display = 'none';
		}
		else alert('Défi déjà réalisé !');
	}
}

function crazy_cats_defier(player_id)
{
return_message(file('ajax.php?ajax=crazy_cats_defier&player_id='+escape(player_id)));
}

function crazy_cats_load(see, resultat, score)
{
document.getElementById('crazy_cats_load').innerHTML = file('ajax.php?ajax=crazy_cats_load&see='+escape(see)+'&resultat='+resultat+'&score='+score);
}

function crazy_cats_defi_delete(defi_id)
{
return_message(file('ajax.php?ajax=crazy_cats_defi_delete&defi_id='+escape(defi_id)));
}

function mission_actuelle()
{
return_message(file('ajax.php?ajax=mission_actuelle'));
}

function mes_chats(cat_num)
{
document.getElementById('mes_chats_liste').innerHTML = file('ajax.php?ajax=mes_chats&cat_num='+escape(cat_num));
}

function accoupler(cat_id, cat_id2)
{
return_message(file('ajax.php?ajax=accoupler&cat_id='+escape(cat_id)+'&cat_id2='+escape(cat_id2)));
}

function menu_compte()
{
	if(document.getElementById('menu_compte').style.display == 'block')
	fermer = 1;
	else fermer = 0;
	
	document.getElementById('menu_compte').style.display = 'none';
	
	if(fermer == 0)
	document.getElementById('menu_compte').style.display = 'block';
}

function afficher_bonus(id, idd)
{
	bloc_style = document.getElementById('bonus_'+escape(id)).style.display;
	
	for(a=1; a<=4; a++)
	{
	document.getElementById('bonus_'+a).style.display = 'none';
	}
	/*
	if(bloc_style == 'none')
	document.getElementById('bonus_'+escape(id)).style.display = 'block';
	
	document.getElementById('form_bonus_'+escape(id)).value = idd;
	*/
	
	document.getElementById('bonus_'+escape(id)).style.display = 'block';
}

function information_aide_hide(page)
{
	if(document.getElementById('information_aide_content').style.display == 'none')
	{
	hide = 0;
	information_display = 'block';
	display_afficher = 'none';
	display_cacher = 'block';
	}
	else
	{
	hide = 1;
	information_display = 'none';
	display_afficher = 'block';
	display_cacher = 'none';
	}
	
file('ajax.php?ajax=information_aide_hide&page='+escape(page)+'&hide='+hide);
document.getElementById('information_aide_content').style.display = information_display;
document.getElementById('information_aide_afficher').style.display = display_afficher;
document.getElementById('information_aide_cacher').style.display = display_cacher;
}

function partner_clic(partner_id)
{
file('ajax.php?ajax=partner_clic&partner_id='+escape(partner_id));
}

function background(choice)
{
	if(file('ajax.php?ajax=background_choice&background_choice='+escape(choice)) != 1)
	return_message(file('ajax.php?ajax=background_choice&background_choice='+escape(choice)));
	else document.body.style.backgroundColor = "#"+choice;
}

function profil(id, see)
{
document.getElementById('profil_contenu').innerHTML = file('ajax.php?ajax=profil&id='+escape(id)+'&see='+escape(see));
}

function calendrier_avent(day_id)
{
document.getElementById('calendrier_avent').innerHTML = file('ajax.php?ajax=calendrier_avent&day_id='+escape(day_id));
}

function accouplement(cat_id)
{
document.getElementById('accouplement').innerHTML = file('ajax.php?ajax=accouplement&cat_id='+escape(cat_id));
}

function rechercher_chat(nom)
{
document.getElementById('resultats_recherche').style.display = 'block';
document.getElementById('resultats_recherche').innerHTML = file('ajax.php?ajax=rechercher_chat&nom='+escape(nom));
}
function accoupler(cat_id)
{
	result_infos = file('ajax.php?ajax=accoupler&cat_id='+escape(cat_id)+'&return=result_infos');
	// Peut donner 01, 02, 11, 12 avec premier chiffre = resultat du traitement (0 = erreur; 1 = correct) et deuxième chiffre = position 1 ou 2 du chat
	
	document.getElementById('accouplement_chat'+result_infos.substring(1, 2)).innerHTML = file('ajax.php?ajax=accoupler&return=cat_infos');
	
	/*
	if(result_infos.substring(0, 1) == 1) // Traitement correct, on cache donc le chat de la liste des résultats
	document.getElementById('cat_'+escape(cat_id)).style.display = 'none';
	*/
	
	rechercher_chat(document.getElementById('rechercher_chat').value);
	if(result_infos.substring(1, 2) == 2 && result_infos.substring(0, 1) == 1) // On a sélectionné le 2e chat et le traitement est correct
	{
	document.getElementById('accouplement_valider').style.display = 'block';
	}
}

function accouplement_valider()
{
document.getElementById('accouplement_valider').innerHTML = file('ajax.php?ajax=accouplement_valider');
document.getElementById('accouplement_valider_submit').disabled = 'disabled';
}

function speed_info(content)
{
	if(content != '0')
	{
		if(content != '') // On ajoute un speed info
		{
		document.getElementById('speed_info').innerHTML = ajax('ajax.php?ajax=speed_info&content='+escape(content));
		var t=setTimeout("speed_info('')", 1000);
		}
	}
}
