
/*
** Initialisation générale : Installation des gestionnaires d'évènement, création des scriptlets...
** ~~~~~~~~~~~~~~~~~~~~~~~
*/
window.onload = function () {
  InitStyleSwitcher('StyleSW');
  InitGallery('portefolio');
  
}

var IsSupported = (document.createElement && document.getElementsByTagName);


/*
** Gestion des feuilles de styles mutiples par l'utilisateur
** ---------------------------------------------------------
**
** Crédits : Paul Sowden in "Alternative Style: Working With Alternate Style Sheets"
**           http://www.alistapart.com/articles/alternate/
**
*/

function InitStyleSwitcher(sId) {
  if (IsSupported) {
    var switcherCookie, aoLinkElements;
    // On retrouve l'élément identifié par "sId"
    oElem = document.getElementById(sId);
    // On insère le gestionnaire de style dans cet élément
    if (oElem) {
      CreateSwitcher(oElem);
      // On regarde si un cookie existe ....
      var switcherCookie = readCookie("switcherCookie");
      if (switcherCookie != null) {
        // On utilise le style stocké
        for(var i=0; (aoLinkElements = document.getElementsByTagName("link")[i]); i++) {
          if(aoLinkElements.getAttribute("rel").indexOf("style") != -1 && aoLinkElements.getAttribute("title")) {
            aoLinkElements.disabled = true;
            if (aoLinkElements.getAttribute("title") == switcherCookie) {
              aoLinkElements.disabled = false;
            }
          }
        }
      }
    }
  }
}

function InitGallery(sId) {
  if (IsSupported) {
    var oElem;
    // On retrouve l'élément identifié par "sId"
    oElem = document.getElementById(sId);
    // On insère le gestionnaire de style dans cet élément
    if (oElem) {
      fillup();
    }
  }
}

function CreateSwitcher(oElem) {
  if(document.createElement){
    var aAttrs, oNewElement, oNewElement2, aoLinkElements;
    var sDefaultStyle = null;

    // Création de <form id='StyleSwitcher'>
    aAttrs = {'id':'StyleSwitcher','action' : ''};
    oNewElement = createElement("form",aAttrs);
    oElem.appendChild(oNewElement);
    oNewElement2 = oNewElement;
    // Création de fieldset
    aAttrs = { 'id' : 'sw'};
    oNewElement = createElement('div');
    oNewElement2.appendChild(oNewElement);
    oNewElement2 = oNewElement;
    // Création label
    aAttrs = { 'for' : 'swlblStyle','text' : 'Selectionnez un style : ', 'id' : 'swlblStyle' };
    oNewElement = createElement('label', aAttrs);
    oNewElement2.appendChild(oNewElement);
    oNewElement2 = oNewElement;
    // Création de la liste déroulante
    aAttrs = { 'id' : 'cmbStyle' };
    oNewElement = createElement('select', aAttrs);
    oNewElement2.appendChild(oNewElement);
    oNewElement2 = oNewElement;
    // On la charge avec les styles définis
    var switcherCookie = readCookie("switcherCookie");
    if (switcherCookie != null) {
      sDefaultStyle = switcherCookie;
    }
    // Chargement de la combo
    for(var i=0; (aoLinkElements = document.getElementsByTagName("link")[i]); i++) {      
      if(aoLinkElements.getAttribute("rel").indexOf("style") != -1 && aoLinkElements.getAttribute("title")) {
        aAttrs = { 'value' : aoLinkElements.getAttribute("rel"), 'text' : aoLinkElements.getAttribute("title") };
        oNewElement = createElement('option', aAttrs);
        oNewElement2.appendChild(oNewElement);        
        if (sDefaultStyle == null && aoLinkElements.getAttribute("rel").indexOf("alt") == -1) {          
          sDefaultStyle = aoLinkElements.getAttribute("title");
        }
        if (aoLinkElements.getAttribute("title") == sDefaultStyle ) oNewElement.selected = true;
      }
    }
    // Création du gestionnaire d'évènement "onChange" sur la combo
    oNewElement2.onchange = function () {
      var aoLinkElements;
      for(var i=0; (aoLinkElements = document.getElementsByTagName("link")[i]); i++) {
        if(aoLinkElements.getAttribute("rel").indexOf("style") != -1 && aoLinkElements.getAttribute("title")) {
          aoLinkElements.disabled = true;
          if (aoLinkElements.getAttribute("title") == this.options[this.options.selectedIndex].text) {
            aoLinkElements.disabled = false;
            createCookie('switcherCookie', this.options[this.options.selectedIndex].text, 90)
          }
        }
      }
    }
  }
}

// --------------------------------------------------- //
// Fonctions utilitaires                               //
// --------------------------------------------------- //

/*
** Création d'un élément dans le DOM du navigateur
*/
function createElement(sElementName, aAttributes) {
  var oNewElement = null;
  var bNameSpace = false;
  sElementName = sElementName.toLowerCase( );
  // Création du nouvel élement
  if (typeof document.createElementNS != 'undefined') {
    // Pour les documents XML/XHTML
    oNewElement = document.createElementNS('http://www.w3.org/1999/xhtml',sElementName);
    bNameSpace = true;
  } else {
    // Pour le reste...
    oNewElement = document.createElement(sElementName);
  }
  if (oNewElement != null && typeof aAttributes != 'undefined' ) {
    // Création des attributs du nouvel élément qui sont
    // passés sous la forme d'un tableau associatif.
    for (var attr in aAttributes) {
      switch ( true ) {
        case ( attr == 'text' )  :
          oNewElement.appendChild( document.createTextNode( aAttributes[attr] ) );
          break;
        case ( attr == 'class' ) :
          oNewElement.className = aAttributes[attr];
          break;
        case ( attr == 'style') :
          // ce code ne marche pas très bien... sauf sous IE !
          try {
            if ( bNameSpace ) {
              oNewElement.setAttributeNS( "http://www.w3.org/1999/xhtml", attr, '' );
            } else {
              oNewElement.setAttribute( attr, '' );
            }
            oNewElement[attr] = aAttributes[attr];
          }
          catch(e) {
            oNewElement.style.setAttribute( "cssText", aAttributes[attr],0);
          }
          break;
        case ( attr == 'for') :
          oNewElement.setAttribute('htmlFor',aAttributes[attr]);
          break;
        default :
          if ( bNameSpace ) {
            oNewElement.setAttributeNS( "http://www.w3.org/1999/xhtml", attr, '' );
          } else {
            oNewElement.setAttribute( attr, '' );
          }
          oNewElement[attr] = aAttributes[attr];
          break;
      }
    }
  }
  return oNewElement;
}

/*
** Lecture du cookie "sName"
*/
function readCookie(sName) {
  this.cookie = null;
  if(document.cookie) {
    if(document.cookie.indexOf(sName)!=-1) {
      this.cookie = document.cookie.split(sName+'=');
      this.cookie = this.cookie[1].split(';');
      this.cookie = this.cookie[0].replace(/#/g,' ');// Remplace les # par espace
    }
  }
  return this.cookie;
}

/*
** Création du cookie "sName" stockant la valeur sValue durant iDays jours...
*/
function createCookie(sName, sValue, iDays) {
  this.date = new Date();
  this.date.setTime(this.date.getTime() + ( iDays * 86400000));
  this.value = sValue.replace(/ /g,'#'); // Remplace les espaces par #
  if(this.value == '') { this.date.setTime(0); } // si pas de valeur on expire le cookie de suite !
  document.cookie = sName+'=' + this.value + '; expires=' + this.date.toGMTString() + '; path=/';
}


/***********************************************
* CMotion Image Gallery- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* Last updated Mar 15th, 04'. Added "End of Gallery" message.
* This copyright notice must stay intact for legal use
***********************************************/

var restarea=6 //1) width of the "neutral" area in the center of the gallery in px
var maxspeed=7 //2) top scroll speed in pixels. Script auto creates a range from 0 to top speed.
//var endofgallerymsg="<span style='font-size: 11px'>End of Gallery</span>" //3) message to show at end of gallery. Enter "" to disable message.
var endofgallerymsg=""

function enlargeimage(path, optWidth, optHeight){ //function to enlarge image. Change as desired.
  var actualWidth=typeof optWidth!="undefined" ? optWidth : "600px" //set 600px to default width
  var actualHeight=typeof optHeight!="undefined" ? optHeight : "500px" //set 500px to  default height
  var winattributes="width="+actualWidth+",height="+actualHeight+",resizable=yes"
  window.open(path,"", winattributes)
}

function changeImage(filename,titre) {
  var oElem;
  oElem = document.getElementById('titrephoto');
  if (titre.length) {
    oElem.innerHTML = titre;
  } else {
    oElem.innerHTML = '';
  }
  
  document.photo.src = filename;
}

////NO NEED TO EDIT BELOW THIS LINE////////////
var iedom=document.all||document.getElementById
var scrollspeed=0
var movestate=""

var actualwidth=''
var cross_scroll, ns_scroll
var loadedyes=0

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function creatediv(){
statusdiv=document.createElement("div")
statusdiv.setAttribute("id","statusdiv")
document.body.appendChild(statusdiv)
statusdiv=document.getElementById("statusdiv")
statusdiv.innerHTML=endofgallerymsg
}

function positiondiv(){
menuheight=parseInt(crossmain.offsetHeight)
mainobjoffsetH=getposOffset(crossmain, "top")
statusdiv.style.left=mainobjoffset+(menuwidth/2)-(statusdiv.offsetWidth/2)+"px"
statusdiv.style.top=menuheight+mainobjoffsetH+"px"
}

function showhidediv(what){
if (endofgallerymsg!="")
statusdiv.style.visibility=what
}

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function moveleft(){
if (loadedyes){
movestate="left"
if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth)){
cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px"
showhidediv("hidden")
}
else
showhidediv("visible")
}
lefttime=setTimeout("moveleft()",10)
}

function moveright(){
if (loadedyes){
movestate="right"
if (iedom&&parseInt(cross_scroll.style.left)<0){
cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px"
showhidediv("hidden")
}
else
showhidediv("visible")
}
righttime=setTimeout("moveright()",10)
}

function motionengine(e){
var dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
var dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
var curposy=window.event? event.clientX : e.clientX? e.clientX: ""
curposy-=mainobjoffset-dsocx
var leftbound=(menuwidth-restarea)/2
var rightbound=(menuwidth+restarea)/2
if (curposy>rightbound){
scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed
if (window.righttime) clearTimeout(righttime)
if (movestate!="left") moveleft()
}
else if (curposy<leftbound){
scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed
if (window.lefttime) clearTimeout(lefttime)
if (movestate!="right") moveright()
}
else
scrollspeed=0
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function stopmotion(e){
if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
if (window.lefttime) clearTimeout(lefttime)
if (window.righttime) clearTimeout(righttime)
movestate=""
}
}

function fillup(){
if (iedom){
crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer
menuwidth=parseInt(crossmain.style.width)
mainobjoffset=getposOffset(crossmain, "left")
cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery
actualwidth=document.all? cross_scroll.offsetWidth : document.getElementById("trueContainer").offsetWidth

crossmain.onmousemove=function(e){
motionengine(e)
}

crossmain.onmouseout=function(e){
stopmotion(e)
showhidediv("hidden")
}
}
loadedyes=1
if (endofgallerymsg!=""){
creatediv()
positiondiv()
}
}
