
/***************************************************************************
 *				javascript.js
 *				-------------
 *	Cree le		: 8 Novembre 2007
 *	Derniere modif.	: 10 Decembre 2007
 *	Auteur		: Agence ici - Asselin Benoit
 *	Site		: http://www.agenceici.com/
 *
 ***************************************************************************/



function ClassIndexFade() {
	this.id = ClassIndexFade.arguments;	// div
	this.idOpacity = new Array();		// %
	this.active = null;			// inc
	
	this.step = 2;
	this.timeMs = 10;
	this.opactityMax = 100;
	this.opactityMin = 60;
	
	this.getElements();
	this.setEvents();
}
ClassIndexFade.prototype = {
	getElements : function() {
		var v_array = this.id;
		for(var v_i = 0, v_l = v_array.length; v_i < v_l; v_i++) {
			this.id[v_i] = Doc.id(v_array[v_i]);
			this.idOpacity[v_i] = this.opactityMax;
		}
	},
	setEvents : function() {
		document.getElementsByTagName('body').item(0).onmouseover = this.mousemove._bindEvent(this);
	},
	css : function() {
		var v_stopTimeout = 0;
		
		for(var v_i = 0, v_l = this.id.length; v_i < v_l; v_i++) {
			this.idOpacity[v_i] += (this.active == v_i || this.active == -1) ? (this.step) : (-this.step);
			
			if(this.idOpacity[v_i] > this.opactityMax) { this.idOpacity[v_i] = this.opactityMax; v_stopTimeout++; }
			if(this.idOpacity[v_i] < this.opactityMin) { this.idOpacity[v_i] = this.opactityMin; v_stopTimeout++; }
			
			var v_css = '';
			v_css += 'filter:alpha(opacity=' + this.idOpacity[v_i] + ');';	/* mie */
			v_css += '-moz-opacity:' + (this.idOpacity[v_i] / 100) + ';';	/* moz */
			v_css += 'opacity:' + (this.idOpacity[v_i] / 100) + ';';	/* css */
			this.id[v_i].style.cssText = v_css;
			
		}
		
		if(v_stopTimeout != this.id.length) {
			setTimeout(this.css._bind(this), this.timeMs);
		}
	},
	mousemove : function(p_event) {
		var v_element = FixEvent.element(p_event);
		while(v_element != null) {
			for(var v_i = 0, v_l = this.id.length; v_i < v_l; v_i++) {
				if(v_element == this.id[v_i]) {
					this.active = v_i;
					this.css();
					return ;
				}
			}
			
			v_element = v_element.parentNode;
		}
		
		// RAZ
		this.active = -1;
		this.css();
		return ;
	}
}



function ClassMenus() {
	this.menuHide = null;
	this.menuShow = null;
	
	this.effectHide = new ClassMenu();
	this.effectShow = new ClassMenu();
}
ClassMenus.prototype = {
	click : function(p_id) {
		SSmenu();
		if(!this.effectHide.running && !this.effectShow.running) {
			this.menuHide = this.menuShow;
			this.menuShow = (this.menuHide == p_id) ? (null) : (p_id);
			
			if(this.menuHide != null) {
				this.effectHide.start(this.menuHide, false);
			}
			if(this.menuShow != null) {
				this.effectShow.start(this.menuShow, true);
			}
		}
	}
}


function ClassMenu() {
	this.menu = null;
	this.smenu = null;
	
	this.mode = null;
	this.running = false;
	this.offsetHeight = 0;
	
	this.step = 30;
	this.timeMs = 10;
}
ClassMenu.prototype = {
	start : function(p_id, p_mode) {
		if(!this.running) {
			this.menu = Doc.id(p_id);
			this.smenu = Doc.id('s' + p_id);
			if(!this.smenu) { return false; } /* menu.empty */ 
			
			this.getHeight();
			
			this.mode = p_mode;
			
			this.menu.className = (this.mode) ? ('active') : ('');
			
			this.running = true;
			this.css();
		}
		return false;
	},
	getHeight : function() {
		var v_li = this.smenu.getElementsByTagName('li');
		
		this.offsetHeight = 0;
		for(var v_i = 0, v_l = v_li.length; v_i < v_l; v_i++) {
			if(v_li[v_i].offsetHeight > 0) {
				this.offsetHeight += v_li[v_i].offsetHeight;
			}
		}
	},
	css : function() {
		var v_height = this.smenu.offsetHeight;
		v_height = (this.mode) ? (v_height + this.step) : (v_height - this.step);
		
		if(this.mode && v_height >= this.offsetHeight) {
			this.running = false;
			this.smenu.style.height = this.offsetHeight + 'px';
			return;
		} else if(!this.mode && v_height <= 0) {
			this.running = false;
			this.smenu.style.height = '0' + 'px';
			return;
		}
		
		this.smenu.style.height = v_height + 'px';
		setTimeout(this.css._bind(this), this.timeMs);
	}
}

function SSmenu(p_element) {
	var v_fix = Doc.id('menu-fix');
	var v_fixout = Doc.id('menu-fixout');
	
	if(p_element && p_element.parentNode.getElementsByTagName('ul').item(0)) {
		var v_top = p_element.parentNode.offsetTop + p_element.parentNode.parentNode.offsetTop;
		
		var v_innerHTML = '<ul>' + p_element.parentNode.getElementsByTagName('ul').item(0).innerHTML + '<\/ul>';
		
		v_fix.style.display = 'none';
		v_fix.innerHTML = v_innerHTML;
		v_fix.style.top = v_top + 'px';
		v_fix.style.display = 'block';
		v_fixout.style.display = 'block';
	} else {
		v_fix.style.display = 'none';
		v_fixout.style.display = 'none';
		v_fix.innerHTML = '';
	}
}

var myMenu = new ClassMenus();



var myShowImage = 1;
function showImage(p_id) {
	if(Doc.id('image-txt-' + myShowImage)) { Doc.id('image-txt-' + myShowImage).style.display = 'none'; }
	if(Doc.id('image-min-' + myShowImage)) { Doc.id('image-min-' + myShowImage).className = ''; }
	if(Doc.id('image-png-' + myShowImage)) { Doc.id('image-png-' + myShowImage).className = ''; }
	
	myShowImage = p_id;
	
	if(Doc.id('image-txt-' + myShowImage)) { Doc.id('image-txt-' + myShowImage).style.display = 'block'; }
	if(Doc.id('image-min-' + myShowImage)) { Doc.id('image-min-' + myShowImage).className = 'active'; }
	if(Doc.id('image-png-' + myShowImage)) { Doc.id('image-png-' + myShowImage).className = 'active'; }
}



var myPilote = 0;
function showPilote(p_id) {
	if(Doc.id('pilote-' + myPilote)) { Doc.id('pilote-' + myPilote).style.display = 'none'; }
	myPilote = p_id;
	if(Doc.id('pilote-' + myPilote)) { Doc.id('pilote-' + myPilote).style.display = 'block'; }
}



var myMapPaysXHR = null;
function mapPaysClick(p_element) {
	if(Doc.id('map')) {
		var v_xhrPost = 'pays=' + encodeURIComponent(p_element.value);
		myMapPaysXHR = new ClassAJAX();
		myMapPaysXHR.send('distributeurs.xml', mapPaysState, v_xhrPost);
		
		Doc.id('map').SetVariable('pays', p_element.value);
	}
}

function mapPaysState() {
	if(myMapPaysXHR.sendReturn()) {
		var v_xml = myMapPaysXHR.xml;
		
		var v_pays = v_xml.getElementsByTagName('pays').item(0);
		var v_departements = v_xml.getElementsByTagName('departement');
		var v_villes = v_xml.getElementsByTagName('ville');
		
		
		if(v_departements.length > 0) {
			var v_xhtml = '';
			v_xhtml += '<select id="departement" onchange="mapDepartementClick(this);">';
			v_xhtml += '<option value="">&nbsp;<\/option>';
			for(var v_i = 0, v_l = v_departements.length; v_i < v_l; v_i++) {
				var v_code = fixHTML(getNodeValue(v_departements.item(v_i), 'code'));
				var v_nom = fixHTML(getNodeValue(v_departements.item(v_i), 'nom'));
				
				v_xhtml += '<option value="' + v_code + '">' + v_nom + '<\/option>';
			}
			v_xhtml += '<\/select>';
			
			Doc.id('select-2').innerHTML = v_xhtml;
			Doc.id('villes').innerHTML = '';
			Doc.id('adresses').innerHTML = '';
			
			
		} else if(v_villes.length > 0) {
			
			var v_xhtml = '';
			v_xhtml += '<div class="pays">' + fixHTML(v_pays.firstChild.nodeValue) + '<\/div>';
			
			v_xhtml += '<div class="villes">';
			for(var v_i = 0, v_l = v_villes.length; v_i < v_l; v_i++) {
				v_xhtml += '<p onclick="mapAdresseClick(this);" onmouseover="this.className=\'mouseover\';" onmouseout="this.className=\'mouseout\';">' + fixHTML(v_villes.item(v_i).firstChild.nodeValue) + '<\/p>';
			}
			v_xhtml += '<\/div>';
			
			if(v_pays.firstChild.nodeValue != 'France') { /* fix */
				Doc.id('select-2').innerHTML = '';
			}
			Doc.id('villes').innerHTML = v_xhtml;
			Doc.id('adresses').innerHTML = '';
			
			
		} else {
			Doc.id('select-2').innerHTML = '';
			Doc.id('villes').innerHTML = '';
			Doc.id('adresses').innerHTML = '';
		}
		
	} else {
		/* Loading... */
	}
}



function mapDepartementClick(p_element) {
	if(Doc.id('map')) {
		var v_xhrPost = 'pays=' + encodeURIComponent(Doc.id('pays').value);
		v_xhrPost += '&departement=' + encodeURIComponent(Doc.id('departement').value);
		
		myMapPaysXHR = new ClassAJAX();
		myMapPaysXHR.send('distributeurs.xml', mapPaysState, v_xhrPost);
	}
}



var myMapAdresseXHR = null;
function mapAdresseClick(p_element) {
	if(Doc.id('map')) {
		var v_xhrPost = 'pays=' + encodeURIComponent(Doc.id('pays').value);
		v_xhrPost += '&ville=' + encodeURIComponent(p_element.innerHTML);
		
		myMapAdresseXHR = new ClassAJAX();
		myMapAdresseXHR.send('distributeurs.xml', mapAdresseState, v_xhrPost);
	}
}

function mapAdresseState() {
	if(myMapAdresseXHR.sendReturn()) {
		var v_xml = myMapAdresseXHR.xml;
		
		var v_adresses = v_xml.getElementsByTagName('adresse');
		if(v_adresses.length) {
			var v_xhtml = '';
			
			var v_ville = fixHTML(getNodeValue(v_adresses.item(0), 'ville'));
			var v_codepostal = fixHTML(getNodeValue(v_adresses.item(0), 'codepostal'));
			v_xhtml += '<div class="ville">' + v_ville + ' (' + v_codepostal + ')' + '<\/div>';
			
			v_xhtml += '<div class="adresses">';
			for(var v_i = 0, v_l = v_adresses.length; v_i < v_l; v_i++) {
				var v_intitule = fixHTML(getNodeValue(v_adresses.item(v_i), 'intitule'));
				var v_adresse = fixHTML(getNodeValue(v_adresses.item(v_i), 'adresse_'));
				var v_telephone = fixHTML(getNodeValue(v_adresses.item(v_i), 'telephone'));
				
				v_xhtml += '<p onmouseover="this.className=\'mouseover\';" onmouseout="this.className=\'mouseout\';">';
				v_xhtml +=	'<strong class="intitule">' + v_intitule + '<\/strong>';
				v_xhtml +=	'<span class="adresse"> ' + v_adresse + ' <\/span>';
				v_xhtml +=	'<span class="telephone">' + v_telephone + '<\/span>';
				v_xhtml += '<\/p>';
				
			}
			v_xhtml += '<\/div>';
			
			Doc.id('adresses').innerHTML = v_xhtml;
			
		} else {
			Doc.id('adresses').innerHTML = '';
		}
		
	} else {
		/* Loading... */
	}
}



function getNodeValue(p_node, p_tagName) {
	if(p_node.getElementsByTagName(p_tagName).item(0) != null && p_node.getElementsByTagName(p_tagName).item(0).firstChild != null) {
		return p_node.getElementsByTagName(p_tagName).item(0).firstChild.nodeValue;
	} else {
		return '';
	}
}

function fixHTML(p_text) {
	p_text = p_text.replace(/&/gi, '&amp;');
	p_text = p_text.replace(/"/gi, '&quot;');
	p_text = p_text.replace(/</gi, '&lt;');
	p_text = p_text.replace(/>/gi, '&gt;');
	return p_text;
}



function popup(p_element,p_mode) {
	if(p_mode == 'catalogues') {
		var v_w = 1000;
		var v_h = 700;
	} else {
		var v_h = 540;
		var v_w = 540;
	}
	var v_l = (screen.width - v_w) / 2;
	var v_t = (screen.height - v_h) / 3;
	window.open(p_element.href,'','scrollbars=no,status=no,toolbar=no,width='+v_w +',height='+v_h +',left='+v_l +',top='+v_t).focus;
	return false;
}


