// JavaScript Document


var h = new HistoryManager();

// VARIABLE DE DETECTION DU PREMIER LOADING
var firstload = true;
var gen_menu = true;
var nav_projet = $$('#nav_projet li a');


		
		
window.addEvent('domready', function() {
									 
	//$('content_realisations').empty();

	// REDIRECTION SI ON ARRIVE SUR HOME.PHP
	/*var url = window.location;
	var verif = /index.php$/;
	if (verif.exec(url) != null)
	{
		window.location.replace("home.php#nouveautes");
	}*/
	/*if(window.location == 'http://www.ultranoir.com/html_v2/') {
			window.location.replace("http://www.ultranoir.com/html_v2/nouveautes");
	}*/
	
	// GESTION DE L'HISTORIQUE ET DES REQUETES

	var data;
	
	// EXECUTION DE L'AJAX
	function loadHtml(page){
	
		if(page == 'nouveautes') {
			data = 'mode=new';
			changeNav('search_nouveautes');
			gen_menu = true;
		} else if(page == 'hall_of_fame') {
			data = 'mode=hof';
			changeNav('search_hof')
		} else if(page == 'tous_les_projets') {
			data = 'mode=tous';
			changeNav('search_tous')
		}
		
		var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_listing.php',
		onRequest: function() {
			$('content_realisations').fade('out');
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
				
				$('loading').empty();
				$('content_realisations').fade('in');
				$('content_realisations').innerHTML = response;
				loadeffect();
				$$('#list_clients li').addEvent('click', function(element) {				  
					//clearClients();
					//clearSecteurs();
					//this.firstChild.set('class','actif');
				});
				$$('#list_secteurs li').addEvent('click', function(element) {
					//clearClients();
					//clearSecteurs();
					//this.firstChild.set('class','actif');
				});
				Shadowbox.init({ skipSetup: true }); Shadowbox.clearCache(); Shadowbox.setup();  
			}
		});
		
		myRequest.send(data);
		h.addState(page);
	}
	

	// GESTION DES EVENEMENTS
	
	// CATEGORIES
	$('search_nouveautes').addEvent('click', function() {
		loadHtml('nouveautes');
		changeNav('search_nouveautes')
		
	});
	$('search_hof').addEvent('click', function() {
		loadHtml('hall_of_fame');
		changeNav('search_hof')
		
	});
	$('search_tous').addEvent('click', function() {
		loadHtml('tous_les_projets');
		changeNav('search_tous')
		
	});
	
	function verif_numeric(variable)
	{
		var exp = new RegExp("^[0-9]+$","g");
		return exp.test(variable);
	} 

	
	// METHODES A LANCER LORS DU CHANGEMENT DANS L'HISTORIQUE
	h.addEvent('onHistoryChange', function(page){
		// LES CAS GENERES LORS DU PREMIER CHARGEMENT	
		longueur_str = page.length
		search_str = page.lastIndexOf('_');
		pos_str = search_str+1;
		id = page.substring(pos_str,longueur_str)
		if(verif_numeric(id)) {
			id_exist = true;
		} else {
			id_exist = false;
		}
		
		if(id_exist == true && firstload == true) {
			if((page.indexOf('client'))> -1) {
				firstLoadFiltre(page);
			} else if((page.indexOf('secteur'))> -1) {
				firstLoadSecteur(page);
			} else if((page.indexOf('fiche'))> -1) {
				firstLoadFiche(page);
			} else {
				loadHtml(page); 
			}
			firstload = false;
		} else {
		
			// LES CAS GENERES LORS DE LA NAVIGATION	
			if((page.indexOf('client'))> -1) {
				loadFiltre(page);
			} else if((page.indexOf('secteur'))> -1) {
				loadSecteur(page);
			} else if((page.indexOf('fiche'))> -1) {
				loadFiche(page);
			} else {
				if(page == '') {
				loadHtml('nouveautes'); 
				} else {
				loadHtml(page); 
				}
			}
			firstload = false;
		}
		
	});
	
	// LORS DU CHANGEMENT, ON ENVOIE EN PARAM L'URL DE LA PAGE => PAS BESOIN DE RECUPERER L'ID
	h.fireEvent('onHistoryChange', [h.getCurrentLocation()]);
})

//#####################################################################################################################
//####################################### FILTRE CLIENTS ############################################

// FILTRE CLIENT
function loadFiltre(page){
	var id;
	var mode;

	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	
	// RECUPERER MODE
	if((page.indexOf('tous_les_projets')) > -1) {
		mode = '&mode=tous';
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
	}
	/*if((page.indexOf('nouveautes')) > -1) {
		mode = '&mode=new2';
		gen_menu = false;
	}*/
	if((page.indexOf('nouveautes')) > -1 && gen_menu == true) {
		mode = '&mode=new2';
		gen_menu = false;
	} else if((page.indexOf('nouveautes')) > -1 && gen_menu == false) {
		mode = '&mode=new3';
	}
	
	clearSecteurs();
	getNavClient(id);
	// REQUETE AJAX
	var data = 'c='+id+mode;
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_listing_filtre.php',
		onRequest: function() {
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			$('loading').empty();
			
			longueur_resp = response.length;
			search_resp = response.indexOf('redir');
			if(search_resp != -1) {
				var go_redir = true;
				search_link = response.indexOf('#');
				pos_resp = search_link+1;
				redir = response.substring(pos_resp,longueur_resp)
			}
			
			if(go_redir == true) {
				var url = '#'+redir;
				window.location.replace(url);
			} else {
				if(mode == '&mode=new3' || mode == '&mode=new2') {
					$('content_realisations').innerHTML = response;
				} else {
					$('listing').innerHTML = response;
				}
				loadeffect();
			}
		}
	});
		
	myRequest.send(data);
	h.addState(page);
	
}

// FIRST FILTRE CLIENT
function firstLoadFiltre(page){
	var id;
	var mode;
	
	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	// RECUPERER MODE
	if((page.indexOf('tous_les_projets')) > -1) {
		mode = '&mode=tous';
		changeNav('search_tous')
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
		changeNav('search_hof');
	}
	if((page.indexOf('nouveautes')) > -1) {
		mode = '&mode=new2';
		changeNav('search_nouveautes')
		gen_menu = false;
	}

	
	// REQUETE AJAX
	var data = 'c='+id+mode;
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_listing.php',
		onRequest: function() {
			$('content_realisations').fade('out');
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			$('loading').empty();
			$('content_realisations').fade('in');
			$('content_realisations').innerHTML = response;
			loadeffect();
		}
	});
		
	myRequest.send(data);
	h.addState(page);
}


//#####################################################################################################################
//####################################### FILTRE SECTEURS ############################################

// FILTRE CLIENT

function loadSecteur(page){
	var id;
	var mode;

	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	// RECUPERER MODE
	if((page.indexOf('tous_les_projets')) > -1) {
		mode = '&mode=tous';
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
	}
	/*if((page.indexOf('nouveautes')) > -1) {
		mode = '&mode=new2';
		gen_menu = false;
	}*/
	if((page.indexOf('nouveautes')) > -1 && gen_menu == true) {
		mode = '&mode=new2';
		gen_menu = false;
	} else if((page.indexOf('nouveautes')) > -1 && gen_menu == false) {
		mode = '&mode=new3';
	}
	clearClients();
	getNavSecteur(id);
	// REQUETE AJAX
	var data = 's='+id+mode;
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_listing_filtre.php',
		onRequest: function() {
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			/*$('loading').empty();
			if(mode == '&mode=new3' || mode == '&mode=new2') {
				$('content_realisations').innerHTML = response;
			} else {
				$('listing').innerHTML = response;
			}
			$$('#listing div a').addEvent('click', function(element) {
					//loadFiche(this.getAttribute('id'))
			});
			loadeffect();
			$$('#list_secteurs li').addEvent('click', function(element) {
				
				//clearClients();
				//clearSecteurs();									   
				//this.firstChild.set('class','actif');
			});*/
			longueur_resp = response.length;
			search_resp = response.indexOf('redir');
			if(search_resp != -1) {
				var go_redir = true;
				search_link = response.indexOf('#');
				pos_resp = search_link+1;
				redir = response.substring(pos_resp,longueur_resp)
			}
			
			if(go_redir == true) {
				var url = '#'+redir;
				window.location.replace(url);
			} else {
				if(mode == '&mode=new3' || mode == '&mode=new2') {
					$('content_realisations').innerHTML = response;
				} else {
					$('listing').innerHTML = response;
				}
				loadeffect();
			}
			
		}
	});
		
	myRequest.send(data);
	h.addState(page);
	
}

// FIRST FILTRE CLIENT
function firstLoadSecteur(page){
	var id;
	var mode;
	
	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	// RECUPERER MODE
	if((page.indexOf('tous_les_projets')) > -1) {
		mode = '&mode=tous';
		changeNav('search_tous')
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
		changeNav('search_hof')
	}
	if((page.indexOf('nouveautes')) > -1) {
		mode = '&mode=new2';
		gen_menu = false;
		changeNav('search_nouveautes')
	}
	
	// REQUETE AJAX
	var data = 's='+id+mode;
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_listing.php',
		onRequest: function() {
			$('content_realisations').fade('out');
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			$('loading').empty();
			$('content_realisations').fade('in');
			$('content_realisations').innerHTML = response;
			loadeffect();
		}
	});
		
	myRequest.send(data);
	h.addState(page);
}

//#####################################################################################################################
//####################################### FILTRE FICHE ############################################

// FILTRE FICHE
function loadFiche(page){
	var id;
	var mode;
	var url_xhr;
	
	
	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	
	
	// RECUPERER MODE
	if((page.indexOf('tous')) > -1) {
		mode = '&mode=tous';
		
		
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
	}
	
	if((page.indexOf('nouveautes')) > -1 && gen_menu == true) {
		mode = '&mode=new2';
		gen_menu = false;
	} else if((page.indexOf('nouveautes')) > -1 && gen_menu == false) {
		mode = '&mode=new3';
	}
	
	// REQUETE AJAX 
	var data = 'p='+id+mode;
	
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_fiche.php',
		onRequest: function() {
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			$('loading').empty();
			if(mode == '&mode=new3' || mode == '&mode=new2') {
				$('content_realisations').innerHTML = response;
			} else {
				$('listing').innerHTML = response;
			}
			Shadowbox.init({ skipSetup: true }); Shadowbox.clearCache(); Shadowbox.setup();  
			
		}
	});
		
	myRequest.send(data);
	h.addState(page);
	
}

// FIRST FILTRE FICHE
function firstLoadFiche(page){
	var id;
	var mode;
	// RECUPERER ID
	longueur_str = page.length
	search_str = page.lastIndexOf('_');
	pos_str = search_str+1;
	id = page.substring(pos_str,longueur_str)
	
	// RECUPERER MODE
	if((page.indexOf('tous_les_projets')) > -1) {
		mode = '&mode=tous';
		changeNav('search_tous')
		
	}
	if((page.indexOf('hall_of_fame')) > -1) {
		mode = '&mode=hof';
		changeNav('search_hof')
	}
	if((page.indexOf('nouveautes')) > -1) {
		mode = '&mode=new2';
		changeNav('search_nouveautes')
		gen_menu = false;
	}
	
	// REQUETE AJAX
	var data = 'p='+id+mode;
	var myRequest = new Request({
		method: 'post', 
		evalScripts: true,
		url: 'http://www.ultranoir.com/html/gene/ajax/gen_fiche_nav.php',
		onRequest: function() {
			$('content_realisations').fade('out');
			$('loading').innerHTML = '<img src="http://www.ultranoir.com/html/gene/image/loading.gif" />';
		},
		onSuccess: function(response) {
			$('loading').empty();
			$('content_realisations').fade('in');
			$('content_realisations').innerHTML = response;
			Shadowbox.init({ skipSetup: true }); Shadowbox.clearCache(); Shadowbox.setup(); 
		}
	});
		
	myRequest.send(data);
	h.addState(page);

}

function changeNav(current) {
	for(v=0;v<nav_projet.length;v++) {
		var id_nav = nav_projet[v].get('id');
		if(id_nav == current) {
			nav_projet[v].set('class','actif');
		} else {
			nav_projet[v].set('class','');
		}
	}
}

function clearClients() {
	listeold = $$('#list_clients li a');
	var nb = $$('#list_clients li a').length;
	for(i=0;i<nb;i++) {
		listeold[i].set('class','');
	}
}

function clearSecteurs() {
	listeold = $$('#list_secteurs li a');
	var nb = $$('#list_secteurs li a').length;
	for(i=0;i<nb;i++) {
		listeold[i].set('class','');
	}
}

function getNavClient(id) {
	listeID = $$('#list_clients li a');
	var nb = $$('#list_clients li a').length;
	var compare = 'client_'+id;
	for(i=0;i<nb;i++) {
		var recup = listeID[i].get('id');
		if(compare == recup) {
			listeID[i].set('class','actif');
		} else {
			listeID[i].set('class','');
		}
	}
}

function getNavSecteur(id) {
	listeID = $$('#list_secteurs li a');
	var nb = $$('#list_secteurs li a').length;
	var compare = 'secteur_'+id;
	for(i=0;i<nb;i++) {
		var recup = listeID[i].get('id');
		if(compare == recup) {
			listeID[i].set('class','actif');
		} else {
			listeID[i].set('class','');
		}
	}
}