// JavaScript Document

// ---------------------------------------------------------------------------------------------------- //
// Global
// ---------------------------------------------------------------------------------------------------- //

// Vérifie le navigateur
function Navigateur() {	
	// Firefox
	if(window.XMLHttpRequest) 
		return new XMLHttpRequest();
	// Internet Explorer
	else if(window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	// XMLHttpRequest non suporté par le navigateur
	else return null;	
}

// Affichage
function aff_c(elt,bloc,type) {	
	elt = document.getElementById(bloc);	
	if(type == "show") elt.style.display = 'block';
	else elt.style.display = 'none';	
}


// ---------------------------------------------------------------------------------------------------- //
// Menu
// ---------------------------------------------------------------------------------------------------- //
// Fond des éléments du menu
function bgMenu(bloc,type) {

	elt = document.getElementById(bloc);
	
	if(type == "show") {
		/*elt.style.backgroundColor = '#0672B3';
		elt.style.backgroundImage = 'url(/global/img/bg/fond-bleu-2.jpg)';
		elt.style.backgroundRepeat = 'repeat-y';*/
	}
	else {
		/*elt.style.backgroundColor = 'transparent';
		elt.style.backgroundImage = 'none';*/
	}

}

// Position des sous-catégories
function posMenu(bloc1,bloc2) {

	var menu = document.getElementById(bloc1);
	var smenu = document.getElementById(bloc2);

	var widthM2 = Math.round(menu.offsetWidth / 2);
	var widthSM2 = Math.round(smenu.offsetWidth / 2);

	var posLeft = widthM2 - widthSM2;
	smenu.style.left = posLeft + "px";
	
}

// ---------------------------------------------------------------------------------------------------- //
// Compte client
// ---------------------------------------------------------------------------------------------------- //
// Champs pour la connexion au compte client
function connexionIn(bloc,texte) {	
	if(bloc.value == texte) bloc.value = "";	
}

// ---------------------------------------------------------------------------------------------------- //
// Panier
// ---------------------------------------------------------------------------------------------------- //
// Quantité
function quantiteA(bloc,type) {
	
	elt = document.getElementById(bloc);
	
	if(type == "plus") var qte = parseFloat(elt.value) + 1;
	else var qte = parseFloat(elt.value) - 1;
	
	if(qte < 1) qte = "1";
	else if(qte > 99) qte = "99";
	
	elt.value = qte;
	
}

// Ajouter au panier
function ajoutPanier(article) {
	
	var method = "GET";
	
	// Quantité
	if(document.getElementById("qte")) var qte = document.getElementById("qte").value;
	else var qte = 1;
	
	xhr_object = Navigateur();
	// Objet
	xhr_object.open(method, "/global/includes/ajax/ajout-panier.php?id=" + article + "&qte=" + qte); 
	xhr_object.onreadystatechange = 
		function()
		{ 
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;
				retour = retour.split("|");
				document.getElementById("cd_panier_texte").innerHTML = retour[0];
				alert(retour[1]);
			}
		}
	
	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(null);
	
}

// Modification du panier
function modifPanier(article,type) {
	
	var method = "GET";
	
	xhr_object = Navigateur();
	// Objet
	xhr_object.open(method, "/global/includes/ajax/modif-panier.php?id=" + article + "&type=" + type); 
	xhr_object.onreadystatechange = 
		function() 
		{ 
			if(xhr_object.readyState == 4) {
				var retour = xhr_object.responseText;
				document.getElementById("panier").innerHTML = retour;					
			}
		}
	
	// Header 
	if(method == "POST")
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	xhr_object.send(null);
	
}

// ---------------------------------------------------------------------------------------------------- //
// Chrono
// ---------------------------------------------------------------------------------------------------- //
function afficheChrono(cadre_chrono, chrono) {
	
	js_cadre_chrono = cadre_chrono;
	js_chrono = "";
	cadre_chrono = cadre_chrono.split(";");
	chrono = chrono.split(";");
	
	for(i=0;i<cadre_chrono.length;i++) {
		
		if(cadre_chrono[i] != "" && chrono[i] != "") {
		
			ts = chrono[i];
			ts_1 = chrono[i] - 1;
			js_chrono = js_chrono + ts_1 + ";";
			// Reste
			reste = ts % 3600;
			// Heures (avec les jours)
			heures = (ts - reste) / 3600;
			// Secondes
			ts = reste % 60;
			// Minutes
			minutes = (reste - ts) / 60;
			// Jours
			jours = Math.floor(heures / 24);
			// Heures (sans les jours)
			heures = heures % 24;
			
			// Affichage
			temps = "";
			if(jours > 0) temps = jours + "jrs ";
			if(heures > 0) temps += heures + "h ";
			if(minutes > 0) temps += minutes + "m ";
			if(ts < 10) ts = "0" + ts;
			temps += ts + "s ";
			$('.' + cadre_chrono[i]).html(temps);
			
		}
		
	}
	
	setTimeout("afficheChrono(js_cadre_chrono, js_chrono)", 1000);
	
}

// ---------------------------------------------------------------------------------------------------- //
// Google Map
// ---------------------------------------------------------------------------------------------------- //
function load() {
	
	if (GBrowserIsCompatible()) {
		
		var map = new GMap2(document.getElementById("plan"));	
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl());
        map.setCenter(new GLatLng(48.83018997916966, 7.739911079406738), 12);
		
		// Creates a marker at the given point with the given number label
		function createMarker(point, number) {
		var marker = new GMarker(point);
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<strong>Dema France</strong><br />8 rue de la Zinsel<br />67590 Schweighouse sur Moder<br>Tél. : 03.88.06.24.24</span><br>E-mail : <a href=\"mailto:info@dema-france.com\" title=\"info@dema-france.com\">info@dema-france.com</a><br>");
		});
		return marker;
		}
		
		// Add marker
		var point = new GLatLng(48.83018997916966, 7.739911079406738);
		map.addOverlay(createMarker(point, 1));
		
	}
	
}

// ---------------------------------------------------------------------------------------------------- //
// Slider
// ---------------------------------------------------------------------------------------------------- //
// The auto-scrolling function
function slide(){
  $('#next').click(); 
}
// Launch the scroll every 2 seconds
var intervalle = window.setInterval(slide, 5000);  
// On user click deactivate auto-scrolling
$(document).ready(function() {
	$('#next, #previous').click(
		function(event){
			if(event.originalEvent){
				window.clearInterval(intervalle);
			}
		}
	);
});
