var onglet_courant = 1;

//gestion de l historique de navigation
function PromptMe()
{
	// some application vars
	var stateVar = "nothin'", displayDiv = $("HistoryState");
		
	this.historyListener = function(historyHash) {
		//boucle pour trouver les valeurs
		$('url_depuis_exterieur').value = 'pas_vide';
		var numero_variable = 0;
		var chaine_recuperee = '';
		var fonction = '';
		var nom_page = '';
		var id_systeme = '';
		var lettre_choisie = '';
		var id_jeu = '';
		var numero_page = '';
		for(var i=0; i<historyHash.length; i++)
		{
			if( historyHash.charAt(i) != '|' )
				chaine_recuperee += historyHash.charAt(i);
			else
			{						
				if ( numero_variable == 0 )
					fonction = chaine_recuperee;
				if ( numero_variable == 1 )
					nom_page = chaine_recuperee;
				if ( numero_variable == 2 )
					id_systeme = chaine_recuperee;
				if ( numero_variable == 3 )
					lettre_choisie = chaine_recuperee;
				if ( numero_variable == 4 )
					id_jeu = chaine_recuperee;
				if ( numero_variable == 5 )
					numero_page = chaine_recuperee;
				
				chaine_recuperee = '';
				numero_variable++;
			}
		}
		if ( fonction == 'mise_a_jour_page' && nom_page != '' && nom_page != undefined )
			mise_a_jour_page(nom_page, id_systeme, lettre_choisie, id_jeu, numero_page);
		if ( fonction == 'mise_a_jour_recherche' )
		{
			$('mot').value = nom_page;
			champ_ecrire_valeur( $('systeme'), id_systeme);
			champ_ecrire_valeur( $('genre'), lettre_choisie);
			mise_a_jour_recherche();
		}
	};
	// subscribe to unFocus.History
	unFocus.History.addEventListener('historyChange', this.historyListener);
	
	this.historyListener(unFocus.History.getCurrent());
};

var demoApp;

function champ_ecrire_valeur( objet, valeur )
{
	var liste_elements = null;
	if( objet == null )
		return null;

	if( objet.tagName == "SELECT" )
	{
		if( objet.options.length == 0 )
		{
			objet.selectedIndex = -1;
			return;
		}

		for( var i=0 ; i<objet.options.length ; i++ )
			if( objet.options[i].value == valeur )
				objet.selectedIndex = i;

		if( objet.options[objet.selectedIndex].value != valeur )
			objet.selectedIndex = -1;
	}

	if( objet.tagName == "TEXTAREA" )
		objet.value = valeur;

	if( objet.tagName == "INPUT" )
	{
		if( ( objet.type == "text" )
			|| ( objet.type == "password" )
			|| ( objet.type == "hidden" )
			|| ( objet.type == "button" ) )
			objet.value = valeur;

		if( ( objet.type == "radio" ) || ( objet.type == "checkbox" ) )
		{
			liste_elements = getElementsByAttribute( "name", objet.name );
			for( num_element=0 ; num_element<liste_elements.length ; num_element++ )
				if( liste_elements[num_element].value == valeur )
					liste_elements[num_element].checked = true;
				else
					liste_elements[num_element].checked = false;
		}

		if( objet.type == "file" )
		{
			var parent = objet.parentNode;
			parent.innerHTML = parent.innerHTML;
			objet = parent.firstChild;
		}
	}
	return 0;
}

	var scroll = "";
	var lettre_choisie2 = "";
var nom_page2 = "";

function initialiser()
	{
		num_case = 1;
	taille_des_div = 45;
	nombre_totale_icone = 0;
	demoApp = new PromptMe();
	}

function MoveToInitial()
{
	num_case = 1;
	new Effect.Move('contenu', { mode: 'absolute', x: 0, y: 0});	
}
	
function MoveTo(valeur)
{
	if ( valeur == 'previous' )
		numero_de_la_case = num_case - 1;
	else
		numero_de_la_case = num_case + 1;

	if ( ( num_case != numero_de_la_case ) && ( numero_de_la_case > 0 ) && ( numero_de_la_case + 19 <= nombre_totale_icone ) )
	{
		//calcul du scroll à appliquer			
		var difference_entre_cases = (numero_de_la_case-1) * taille_des_div;
		num_case = numero_de_la_case;
		new Effect.Move('contenu', { mode: 'absolute', x: -difference_entre_cases, y: 0, duration: 0.4, transition: Effect.Transitions.linear });
	}			
}

function lancer_scroll(valeur)
{
	scroll = setInterval("MoveTo('"+valeur+"')", 400);
}

function arreter_scroll()
{
	clearInterval(scroll);
}

function vider_icone_en_fond()
{
	$('bloc_icone_systeme').style.backgroundImage = 'url()';	
	$('bloc_infos_systeme').innerHTML = '';	
	$('bloc_photo_systeme').innerHTML = '';	
	$('bloc_nom_systeme').innerHTML = '';	
}
			
function mise_a_jour_page(nom_page, id_systeme, lettre_choisie, id_jeu, numero_page)
{
	$('boxarts').innerHTML = '';
	
	if ( nom_page == 'page_du_systeme' )
		lettre_choisie2 = lettre_choisie;
	else
		lettre_choisie2 = '';

	nom_page2 = nom_page;

	//mise a jour de la page
	var url = 'ajax_maj_page.php';
	var parametres = 'nom_page=' + nom_page + '&id_systeme=' + id_systeme + '&lettre_choisie=' + lettre_choisie + '&id_jeu=' + id_jeu + '&numero_page=' + numero_page;
	
	new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: parametres,
			onComplete: afficher_la_page
		});
	
	//si on affiche la "page_du_jeu" alors on cherche si on affiche les boxarts
	if ( nom_page == 'page_du_jeu' )
	{
		var url = 'ajax_maj_boxarts.php';
		var parametres = 'id_jeu=' + id_jeu;
		new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: parametres,
				onComplete: afficher_les_boxarts
			});
	}
	else
		$('boxarts').innerHTML = '';
}

function mettre_recherche_dans_history()
{
	unFocus.History.addHistory('mise_a_jour_recherche|' + $('mot').value + '|' + $('systeme').value + '|' + $('genre').value + '|');
}

function mise_a_jour_recherche()
{			
	var lancer_formulaire = true;
	
	if ( ( $('mot').value.length > 0 ) && ( $('mot').value.length < 3 ) )
		lancer_formulaire = false;

	if ( lancer_formulaire == true )
	{
		vider_icone_en_fond();
		$('boxarts').innerHTML = '';
		//mise à jour de la page
		var url = 'ajax_maj_page.php';
		var parametres = 'nom_page=page_de_recherche&mot=' + $('mot').value + '&systeme=' + $('systeme').value + '&genre=' + $('genre').value;
		new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: parametres,
				onComplete: afficher_la_page
			});
	}
	else
		alert('More than 3 characters');
}

function modifier_la_lettre( lettre_choisie )
{
	if ( lettre_choisie != 'undefined' )
	{
		if ( ( lettre_choisie == '0-9' ) || ( lettre_choisie == '' ) ) 
			lettre_choisie = '0';
		$('caractere_'+lettre_choisie).style.backgroundImage='url(imgs/lettres/'+lettre_choisie+'_1.png)';
		$('caractere_'+lettre_choisie).onmouseover = '';
		$('caractere_'+lettre_choisie).onmouseout = '';
	}	
}

function afficher_la_page(requete)
{
	$('menu_central').innerHTML = '';	
	new Insertion.Bottom( 'menu_central', requete.responseText );
	if ( nom_page2 == 'page_du_systeme' )
	{
		if ( lettre_choisie2 == '' )
			lettre_choisie2 = $('lettre_choisie2').value;
		modifier_la_lettre( lettre_choisie2 );
	}
	new Effect.ScrollTo('ancre_haut', {duration: .7} );
}

function afficher_les_boxarts(requete)
{
	$('boxarts').innerHTML = '';	
	new Insertion.Bottom( 'boxarts', requete.responseText );
}

function afficher_icone_selectionnee(element, dropon, event)
{
	$('bloc_icone_systeme').style.backgroundImage = 'url(imgs/deroulant/'+element.id+')';
	//ajax pour maj la fiche du systeme
	var url = 'ajax_infos_systeme.php';
	var parametres = 'ajax_id_systeme='+element.id;
	new Ajax.Updater( 'bloc_infos_systeme', url, { parameters: parametres } );
	//ajax pour maj le nom du systeme + logo marque
	var url = 'ajax_nom_systeme.php';
	var parametres = 'ajax_id_systeme='+element.id;
	new Ajax.Updater( 'bloc_nom_systeme', url, { parameters: parametres } );
	//ajax pour maj la photo du systeme
	var url = 'ajax_photo_systeme.php';
	var parametres = 'ajax_id_systeme='+element.id;
	new Ajax.Updater( 'bloc_photo_systeme', url, { parameters: parametres } );
	//ajax affichage de la page du systeme
	//mise_a_jour_page('page_du_systeme', element.id);
	unFocus.History.addHistory( 'mise_a_jour_page|page_du_systeme|' + element.id + '|');
}

function afficher_icone_selectionnee_clic(id)
{
	$('bloc_icone_systeme').style.backgroundImage = 'url(imgs/deroulant/'+id+')';
	//ajax pour maj la fiche du systeme
	var url = 'ajax_infos_systeme.php';
	var parametres = 'ajax_id_systeme='+id;
	new Ajax.Updater( 'bloc_infos_systeme', url, { parameters: parametres } );
	//ajax pour maj le nom du systeme + logo marque
	var url = 'ajax_nom_systeme.php';
	var parametres = 'ajax_id_systeme='+id;
	new Ajax.Updater( 'bloc_nom_systeme', url, { parameters: parametres } );
	//ajax pour maj la photo du systeme
	var url = 'ajax_photo_systeme.php';
	var parametres = 'ajax_id_systeme='+id;
	new Ajax.Updater( 'bloc_photo_systeme', url, { parameters: parametres } );
	//ajax affichage de la page du systeme
	//mise_a_jour_page('page_du_systeme', element.id);
	unFocus.History.addHistory( 'mise_a_jour_page|page_du_systeme|' + id + '|');
}

function afficher_icone_dans_recherche(element, dropon, event)
{
	$('bloc_icone_recherche').style.backgroundImage = 'url(imgs/deroulant/'+element.id+')';
	var reg = new RegExp(".png", "g");
	var chaine = element.id;
	champ_ecrire_valeur( $('systeme'), chaine.replace(reg,""));
}

function afficher_icone_dans_recherche_apres_onchange()
{
	$('bloc_icone_recherche').style.backgroundImage = 'url(imgs/deroulant/'+$('systeme').value+'.png)';
}

function mise_a_jour_onglet(valeur)
{
	afficher_le_menu( 'encart_onglets_' + valeur );
	onglet_courant = valeur;
	//selectionner les bonnes icones
	var url = 'ajax_maj_icones.php';
	var parametres = 'ajax_type=' + valeur;
	new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: parametres,
			onComplete: afficher_les_icones
		});
}

function afficher_les_icones(requete)
{
	var tab = eval('(' + requete.responseText + ')');
	$('contenu').innerHTML = '';
	new Insertion.Bottom( 'contenu', tab['html'] );			
	nombre_totale_icone = tab['id'].length;
	for ( var i=0; i<nombre_totale_icone; i++ )
	{
		new Draggable(tab['id'][i], {
			revert: true,
			onStart : function(){ vider_icone_en_fond() } 
			} );
	}
	MoveToInitial();
}

function changer_lettre_choisie(id_systeme, lettre_choisie)
{
	//mise_a_jour_page('page_du_systeme', id_systeme, lettre_choisie);
	unFocus.History.addHistory( 'mise_a_jour_page|page_du_systeme|' + id_systeme + '|' + lettre_choisie + '|' );
}		

function calculeOffsetTop(r)
{
	return calculeOffset(r,"offsetTop")
}

function calculeOffset(element,attr)
{
	var offset=0;
	while(element)
	{
		offset+=element[attr];
		element=element.offsetParent
	}
	return offset
}

function afficher_screens_recherche(id_systeme,chemin_du_systeme,id_du_jeu,id_du_parent)
{
	var test = calculeOffsetTop( $('ancre_recherche_'+id_du_jeu) ) - calculeOffsetTop( $('ancre_debut_'+id_systeme) );
	new Effect.Move('scroll_recherche_screens_'+id_systeme, { mode: 'absolute', x: 0, y: test});
	var url = 'ajax_maj_recherche_screens.php';
	var parametres = 'chemin_du_systeme=' + chemin_du_systeme + '&id_du_jeu=' + id_du_jeu + '&id_systeme=' + id_systeme + '&id_du_parent=' + id_du_parent;
	new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: parametres,
			onComplete: afficher_screens_recherche_final
		});
}

function afficher_screens_recherche_final(requete)
{
	var tab = eval('(' + requete.responseText + ')');
	$('recherche_screens_'+tab['id_systeme']).innerHTML = '';
	$('recherche_screens_'+tab['id_systeme']).innerHTML = tab['html'];
	$('id_du_jeu_cache_'+tab['id_systeme']).value = tab['id_du_jeu'];
}

function afficher_page_du_jeu_depuis_recherche(id_systeme)
{
	if ( $('id_du_jeu_cache_'+id_systeme).value != '' )
	{
		//mise_a_jour_page('page_du_jeu', id_systeme, 'undefined', $('id_du_jeu_cache_'+id_systeme).value);
		unFocus.History.addHistory( 'mise_a_jour_page|page_du_jeu|' + id_systeme + '|' + $('lettre_du_jeu_cache_'+id_systeme).value + '|' + $('id_du_jeu_cache_'+id_systeme).value + '|' );
	}
}

function mise_a_jour_langue(langue)
{
	//mise a jour de la page
	var url = 'ajax_maj_langue.php';
	var parametres = 'langue=' + langue;
	//alert(parametres);
	new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: parametres,
			onComplete: function() { window.location.href='index.php'; }
		});
}