// GLOBALE
var indik=0;

var pagination=0;
var nbpreviouscollections=0;

/******************************************/
/******* FONCTIONS UTILISATEURS *******/
/******************************************/
function entrer_image_previouscollection(num, contenulangue, annee) {

	if(indik==0) document.getElementById('previouscollections_img_'+num).innerHTML="<div style='height:300px;padding-top:130px;width:300px;color:#FFFCDF;font-size:25px;font-weight:bold;'>"+contenulangue+"<br />"+annee+"</div>";
	indik++;
}

function sortir_image_previouscollection(num, lien) {
	document.getElementById('previouscollections_img_'+num).innerHTML="<img style='border:0;position:relative;' src='"+lien+"'></img>";
	indik=0;
}

function entrer_image_pressrelease(num) {

	if(indik==0) {
		document.getElementById('pressrelease_'+num+'_1').style.display="none";
		document.getElementById('pressrelease_'+num+'_1').style.zIndex="20000";
		document.getElementById('pressrelease_'+num+'_2').style.display="";
		document.getElementById('pressrelease_'+num+'_2').style.zIndex="21000";
	}
	indik++;
}

function sortir_image_pressrelease(num) {
	document.getElementById('pressrelease_'+num+'_2').style.zIndex="20000";
	document.getElementById('pressrelease_'+num+'_2').style.display="none";
	document.getElementById('pressrelease_'+num+'_1').style.zIndex="21000";
	document.getElementById('pressrelease_'+num+'_1').style.display="";
	
	indik=0;
}

function maj_nbpreviouscollections() {
	var xhr=null;
	
	xhr=ajax_init();
	
	ajax_open(xhr, 'php/maj_nbpreviouscollections.php', 'POST', false);
	
	nbpreviouscollections=xhr.responseText;
}

function afficher_navigation_gauche() {
	if(pagination==0) document.getElementById('btn_previouscollections_gauche').style.display='none';
	else document.getElementById('btn_previouscollections_gauche').style.display='block';
}

function afficher_navigation_droite() {
	if((parseInt(pagination)+4)<parseInt(nbpreviouscollections)) document.getElementById('btn_previouscollections_droite').style.display='block';
	else document.getElementById('btn_previouscollections_droite').style.display='none';

	if(nbpreviouscollections==0) document.getElementById('btn_previouscollections_droite').style.display='none';
}

function afficher_accueil() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// on cache le pied
	document.getElementById('pied').style.display='none';
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/accueil.php', 'POST', true);
}

function afficher_contacts() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// les boutons
	resetbtn();
	document.getElementById('btn_contacts').style.fontSize='20px';
	document.getElementById('btn_contacts').style.fontWeight='bold';
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/contacts.php', 'POST', true);
}

function afficher_pressrelease() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// les boutons
	resetbtn();
	indik=0;
	document.getElementById('btn_pressrelease').style.fontSize='20px';
	document.getElementById('btn_pressrelease').style.fontWeight='bold';
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue))
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/pressrelease.php', 'POST', true);
}

function afficher_pressrelease2(idpressrelease) {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));
	tab_params.push(new param("idpressrelease",idpressrelease));
	
	// les boutons
	indik=0;
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/pressrelease2.php', 'POST', true);
}

function afficher_lastcollection() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// les boutons
	resetbtn();
	document.getElementById('btn_lastcollection').style.fontSize='20px';
	document.getElementById('btn_lastcollection').style.fontWeight='bold';
	
	// le loading
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));

	// on affiche la page
	xhr.onreadystatechange = function() { ajax_response_lastcollection(xhr, 'corp'); };
	ajax_open(xhr, 'php/lastcollection.php', 'POST', true);
}

function afficher_previouscollections() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// les boutons
	resetbtn();
	indik=0;
	document.getElementById('btn_previouscollections').style.fontSize='20px';
	document.getElementById('btn_previouscollections').style.fontWeight='bold';
	
	// le loading
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));

	// on affiche la page
	xhr.onreadystatechange = function() { ajax_response_previouscollections(xhr, 'corp'); };
	ajax_open(xhr, 'php/previouscollections.php', 'POST', true);
}

function afficher_boutique() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// le loading
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));

	// on affiche la page
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/boutique.php', 'POST', true);
}

function afficher_previouscollections2() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// le loading
	document.getElementById('conteneur_previouscollections').innerHTML="<img style='margin-top:357px;' src='images/ajax/ajax-loader1.gif' />";
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));
	tab_params.push(new param("pagination",pagination));
	
	// les navigations
	afficher_navigation_gauche();
	afficher_navigation_droite();
	
	// on affiche la page
	xhr.onreadystatechange = function() { ajax_response(xhr, 'conteneur_previouscollections'); };
	ajax_open(xhr, 'php/previouscollections2.php', 'POST', true);
}

function afficher_previouscollections3(idcollection) {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// le loading
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";
	
	var langue=document.getElementById('langue').value;
	tab_params.push(new param("langue",langue));
	tab_params.push(new param("idcollection",idcollection));
	
	// on affiche la page
	xhr.onreadystatechange = function() { ajax_response_lastcollection2(xhr, 'corp'); };
	ajax_open(xhr, 'php/previouscollections3.php', 'POST', true);
}

function afficher_portrait() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";

	// on cache le pied
	document.getElementById('pied').style.display='none';
	
	var langue=document.getElementById('langue').value;
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/portrait_'+langue+'.php', 'POST', true);
}

function afficher_accueil2() {
	var xhr=null;
	tab_params=new Array();

	xhr=ajax_init();
	
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";

	// on cache le pied
	document.getElementById('pied').style.display='none';
	
	var langue=document.getElementById('langue').value;
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/portrait2.php', 'POST', true);
}

function afficher_portrait2() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// les boutons
	resetbtn();
	document.getElementById('btn_portrait').style.fontSize='20px';
	document.getElementById('btn_portrait').style.fontWeight='bold';
	
	document.getElementById('corp').innerHTML="<center><img style='margin-top:330px;' src='images/ajax/ajax-loader.gif' /></center>";
	
	var langue=document.getElementById('langue').value;
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'corp'); };
	ajax_open(xhr, 'php/portrait_'+langue+'.php', 'POST', true);
}

function afficher_pied() {
	var xhr=null;
	tab_params=new Array();
	
	xhr=ajax_init();
	
	// on cache le pied
	document.getElementById('pied').style.display='block';
	
	document.getElementById('pied').innerHTML="<center><img style='margin-top:9px;position:relative;' src='images/ajax/ajax-loader1.gif' /></center>";

	var langue=document.getElementById('langue').value;
	
	tab_params.push(new param("langue",langue));
	
	xhr.onreadystatechange = function() { ajax_response(xhr, 'pied'); };
	ajax_open(xhr, 'php/pied.php', 'POST', true);
}

/******************************************/
/******* FONCTIONS SYSTEME AJAX *******/
/*****************************************/
function param(libelle, valeur) {
	this.libelle = libelle;
    this.valeur = valeur;
}
  
function ajax_init(xhr) {
	if (navigator.appName=="Microsoft Internet Explorer") {
		return (new ActiveXObject("Microsoft.XMLHTTP"));
	} else if (navigator.appName=="Netscape") {
		return (new XMLHttpRequest());
	} else {
		return null;
	}
}

function ajax_open(xhr, fichier, mode_transmission, asynchrone) {
	xhr.open(mode_transmission, fichier, asynchrone);
	
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	if ( tab_params.length>0 ) {
		var retour="";
		var i=1;
		
		retour=(tab_params[0].libelle+"="+tab_params[0].valeur);
		
		for(i;i<tab_params.length;i++) retour = retour + ("&"+(tab_params[i].libelle)+"="+(tab_params[i].valeur));

		xhr.send(retour);
	} else {
		xhr.send(null);
	}
}

function setOpacity(obj, opacity) {
	if(obj!=null) {
		opacity = (opacity == 100)?99.999:opacity;
		  
		// IE/Win
		obj.style.filter = "alpha(opacity:"+opacity+")";
		  
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;
		  
		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;
		  
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	}
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function fadeOut(objId,opacity,idpressrelease) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity = opacity-10;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+",'"+idpressrelease+"')", 100);
    } else {
		document.getElementById('corp').innerHTML="";
		afficher_pressrelease2(idpressrelease);
		fadeIn('corp',0);
	}
  }
}

function fadeOut2(objId,opacity) {
obj = document.getElementById(objId);
  if (document.getElementById) {
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity = opacity-10;
      window.setTimeout("fadeOut2('"+objId+"',"+opacity+")", 100);
    } else {
		if(obj!=null) obj.style.display='none';
	}
  }
}

// on renseigne un zone d'un formulaire HTML avec sa valeur texte
function writediv(id_div, texte_div) {
	setInnerHTML(document.getElementById(id_div), texte_div);
	
	if(id_div=='pied') {
		document.getElementById('btn_portrait').style.fontSize='20px';
		document.getElementById('btn_portrait').style.fontWeight='bold';
	}
}

function ajax_response(xhr, div_conteneur) {
	
	if (xhr.readyState==4)  {
		if (xhr.status == 200 ) {
			var resultat= xhr.responseText;
			writediv(div_conteneur,resultat);
		}
	}
	
}

function ajax_response_lastcollection(xhr, div_conteneur) {
	
	if (xhr.readyState==4)  {
		if (xhr.status == 200 ) {
			var resultat= xhr.responseText;
			writediv_lastcollection(div_conteneur,resultat);
			
			setTimeout("ajax_response_lastcollection_fin()",4000);
		}
	}
	
}

function ajax_response_lastcollection_fin() {
	fadeOut2('div_1',100);
	fadeOut2('div_2',100);
	fadeOut2('div_3',100);
}

function ajax_response_lastcollection2(xhr, div_conteneur) {
	
	if (xhr.readyState==4)  {
		if (xhr.status == 200 ) {
			var resultat= xhr.responseText;
			writediv_lastcollection(div_conteneur,resultat);
			
			setTimeout("ajax_response_lastcollection2_fin()",4000);
		}
	}
	
}

function ajax_response_lastcollection2_fin() {
	fadeOut2('div_1',100);
	fadeOut2('div_2',100);
}

function ajax_response_previouscollections(xhr, div_conteneur) {
	
	if (xhr.readyState==4)  {
		if (xhr.status == 200 ) {
			var resultat= xhr.responseText;
			writediv(div_conteneur,resultat);
			
			afficher_previouscollections2();
		}
	}
	
}

function writediv_lastcollection(id_div, texte_div) {
	setInnerHTML(document.getElementById(id_div), texte_div);
	
	conf_reflection_p = 0.5;     
	conf_focus = 4;             
	conf_slider_width = 14;
	conf_images_cursor = 'pointer'; 
	conf_slider_cursor = 'default';

	conf_imageflow = 'imageflow'
	conf_loading = 'loading'; 
	conf_images = 'images';    
	conf_captions = 'captions';
	conf_scrollbar = 'scrollbar';
	conf_slider = 'slider';
	conf_nom = 'nom';

	caption_id = 0;
	new_caption_id = 0;
	current = 0;
	target = 0;
	mem_target = 0;
	timer = 0;
	array_images = new Array();
	new_slider_pos = 0;
	dragging = false;
	dragobject = null;
	dragx = 0;
	posx = 0;
	new_posx = 0;
	xstep = 150;

	rafraichir();
}

// setInnerHTML Sécurisé
function setInnerHTML(divContent, HTML) {
    divContent.innerHTML=HTML;
    try {
      var All=divContent.getElementsByTagName("*");
      for (var i=0; i<All.length; i++) {
        All[i].id=All[i].getAttribute("id")
        All[i].name=All[i].getAttribute("name")
      }
    } catch (ex) {}
    try {
      var AllScripts=HTML.extractTags("script");
      AllScripts.forEach(function (v) {
        eval(v);
      })
    } catch (ex) {}
    try {
      var AllStyles=HTML.extractTags("style");
      AllStyles.forEach(function (v) {
        var s=document.createStyleSheet()
        s.cssText=v;
        s.enabled=true;
      }, true)
    } catch (ex) {}
}

String.prototype.extractTags=function(tag) {
    var matchAll = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'img');
    var matchOne = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'im');
    return (this.match(matchAll) || []).map(function(scriptTag) {
      return (scriptTag.match(matchOne) || ['', ''])[1];
    });
}

//Detect IE5.5+
version=0;
if(navigator.appVersion.indexOf("MSIE")!=-1)
{
temp=navigator.appVersion.split("MSIE");
version=parseFloat(temp[1]);
}
// NON IE browser will return 0
if(version>=5.5)
{
Object.prototype.forEach=function(delegate, ownpropertiesonly) {
    if (typeof(delegate)=="function") {
        if (this instanceof Array && typeof(ownpropertiesonly)=="undefined") {
            ownpropertiesonly=true;
        }
        for (key in this) {
            var ok = (!ownpropertiesonly);
            if (!ok) {
                try {
                    ok=this.hasOwnProperty(key)
                } catch (ex) {}
            }
            if (ok) {
                try { delegate(this[key], key, this) } catch(e) {
                    // ...
                }
            }
        }
    }
    return false;
}

Object.prototype.map=function(iterator) {
    var results = [];
    this.forEach(function(value, index) {
      results.push(iterator(value, index));
    });
    return results;
}
}

function resetbtn() {
	document.getElementById('btn_accueil').style.fontSize='18px';
	document.getElementById('btn_lastcollection').style.fontSize='18px';
	document.getElementById('btn_previouscollections').style.fontSize='18px';
	document.getElementById('btn_portrait').style.fontSize='18px';
	document.getElementById('btn_pressrelease').style.fontSize='18px';
	document.getElementById('btn_contacts').style.fontSize='18px';
	
	document.getElementById('btn_accueil').style.fontWeight='normal';
	document.getElementById('btn_lastcollection').style.fontWeight='normal';
	document.getElementById('btn_previouscollections').style.fontWeight='normal';
	document.getElementById('btn_portrait').style.fontWeight='normal';
	document.getElementById('btn_pressrelease').style.fontWeight='normal';
	document.getElementById('btn_contacts').style.fontWeight='normal';
}