
var cWidth = 0;;
var cHeight = 0;
var imgWidth = 0;
var imgHeight = 0;
var contentWidth = 0;
var contentHeight = 0;
var cShow = 1;
var switchok = 1;
var cSlide = 0;
var timerId = 0;

function switchDisplay() {
//alert(cShow+'-'+switchok);
    if (cShow == 1) {
        if (switchok == 1) {
            hidediv('content');
            cShow = 0;
        }
    }
    else {
        showdiv('content');
        cShow = 1;  
    }       
    switchok = 1;
}

function switchKO() {
    switchok = 0;
}

function hidediv(myId) {
    if (document.getElementById) { // DOM3 = IE5, NS6
      if (document.getElementById(myId)) document.getElementById(myId).style.visibility = 'hidden';
    }
    else {
        if (document.layers) { // Netscape 4
          if (document.layer[myId]) document.layer[myId].visibility = 'hidden';
        }
        else { // IE 4
          if (document.all[myId]) document.all[myId].style.visibility = 'hidden';
        }
    }
}

function showdiv(myId) {
    if (document.getElementById) { // DOM3 = IE5, NS6
        if (document.getElementById(myId)) document.getElementById(myId).style.visibility = 'visible';
    }
    else {
        if (document.layers) { // Netscape 4
          if (document.layer[myId]) document.layers[myId].visibility = 'visible';
        }
        else { // IE 4
          if (document.all[myId]) document.all[myId].style.visibility = 'visible';
        }
    }
}

// - - - - - - - - - - - - - Slider - - - - - - - - - - - - - - - - - - -

  var slider = new Array();
            
  // scroll the element vertically based on its width and the slider maximum value
  function scrollVertical(value, element, nom) {
    element.scrollTop = Math.round(value*slider[nom].scrollMax);
  }
  
  function monter(nom) {
    slider[nom].setValueBy(-40/slider[nom].scrollMax);
  }
            
  function descendre(nom) {
    slider[nom].setValueBy(40/slider[nom].scrollMax);
  }
            
  // mouse wheel code from http://adomas.org/javascript-mouse-wheel/
  function handle(delta,nom) {
    slider[nom].setValueBy(-(delta*60)/slider[nom].scrollMax);
  }
        
  function initSlider0(adiv) {
    var max = $(adiv+'_main').scrollHeight-$(adiv+'_main').offsetHeight;
    s = new Control.Slider(adiv+'_slider_ascenseur', adiv+'_slider_chemin', {
      axis: 'vertical',
      onSlide: function(v) { scrollVertical(v, $(adiv+'_main'), adiv);  },
      onChange: function(v) { scrollVertical(v, $(adiv+'_main'), adiv); }
    });
    s.scrollMax = max;
    slider[adiv]=s;
  }
        
  function wheelRight(event) {
    var delta = 0;
    if (!event) /* For IE. */
      event = window.event;
      if (event.wheelDelta) { /* IE/Opera. */
        delta = event.wheelDelta/120;
      /** In Opera 9, delta differs in sign as compared to IE. */
      if (window.opera)
        delta = -delta;
    }
    else if (event.detail) {
      /** Mozilla case. */
      /** In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3. */
      delta = -event.detail/3;
    }
        
    /** If delta is nonzero, handle it.
    * Basically, delta is now positive if wheel was scrolled up,
    * and negative, if wheel was scrolled down. */
    if (delta)
      handle(delta,'boxright');
        
    /** Prevent default actions caused by mouse wheel.
    * That might be ugly, but we handle scrolls somehow
    * anyway, so don't bother here.. */
    if (event.preventDefault)
      event.preventDefault();
                
    event.returnValue = false;
  }

  function wheelPhoto(event) {
    var delta = 0;
    if (!event) /* For IE. */
      event = window.event;
      if (event.wheelDelta) { /* IE/Opera. */
        delta = event.wheelDelta/120;
      /** In Opera 9, delta differs in sign as compared to IE. */
      if (window.opera)
        delta = -delta;
    }
    else if (event.detail) {
      /** Mozilla case. */
      /** In Mozilla, sign of delta is different than in IE. Also, delta is multiple of 3. */
      delta = -event.detail/3;
    }

    /** If delta is nonzero, handle it.
    * Basically, delta is now positive if wheel was scrolled up,
    * and negative, if wheel was scrolled down. */
    if (delta)
      handle(delta,'boxphoto');
        
    /** Prevent default actions caused by mouse wheel.
    * That might be ugly, but we handle scrolls somehow
    * anyway, so don't bother here.. */
    if (event.preventDefault)
      event.preventDefault();
                
    event.returnValue = false;
  }

  function initMouseWheel0(adiv) {
    // mozilla
    if (adiv == "boxright")
      Event.observe(adiv+'_main', 'DOMMouseScroll', wheelRight);
    else if (adiv == "boxphoto")
      Event.observe(adiv+'_main', 'DOMMouseScroll', wheelPhoto);

    // IE/Opera
    if (adiv == "boxright")
      Event.observe(adiv+'_main', 'mousewheel', wheelRight);
    else if (adiv == "boxphoto")
      Event.observe(adiv+'_main', 'mousewheel', wheelPhoto);
  }
        
  function initAscenseur(adiv,largeur) {
    if ($(adiv+'_main').scrollHeight > $(adiv+'_main').offsetHeight) {
      $(adiv+'_slider').style.display='block';
      divTexte = $(adiv+'_main');
      //largeur = divTexte.offsetWidth;
      //largeur = 400;
      //largeur2 = largeur-22;
      largeur2 = largeur-15;
      divTexte.style.width = largeur2+'px';
      initSlider0(adiv);
      initMouseWheel0(adiv);
    }
  }

// - - - - - - - - - - - - - Image resize - - - - - - - - - - - - - - - - - - -

// Done By Samuel Liew (http://www.samliew.com)
// Detect Browser
var detect = navigator.userAgent.toLowerCase();
var browser,thestring;
if (checkIt('msie')) { browser = "Internet Explorer"; }
else if (!checkIt('compatible')) { browser = "Netscape Navigator"; }
else browser = "An unknown browser";
function checkIt(string) {
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
}

// Resize Image
//var ow = 0;
//var oh = 0;

var ratio = 1.5;

function stretch(largeur) {
//  if (ow == 0) ow = document.getElementById("image").width;
//  if (oh == 0) oh = document.getElementById("image").height;

  ow = document.getElementById("image").width;
  oh = document.getElementById("image").height;

  var x,y;
  if (self.innerHeight) { // all except Explorer
    x = self.innerWidth;
    y = self.innerHeight-40-20;
  }
  else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight-40-20;
  }
  else if (document.body) { // other Explorers
    x = document.body.clientWidth;
    y = document.body.clientHeight-40-20;
  }

  cWidth = x;
  cHeight = y;

// content (ratio)
//  var contentWidth, contentHeight;
  contentWidth = x;
  contentHeight = y;

  if (contentWidth < document.getElementById("minimg").width)   contentWidth = document.getElementById("minimg").width;
  if (contentHeight < document.getElementById("minimg").height) contentHeight = document.getElementById("minimg").height;
  if (contentWidth > document.getElementById("maximg").width)   contentWidth = document.getElementById("maximg").width;
  if (contentHeight > document.getElementById("maximg").height) contentHeight = document.getElementById("maximg").height;

  if ((contentWidth / contentHeight) < ratio) {
    // Image verticale
    contentHeight = Math.round(contentWidth / ratio);
  }
  else if ((contentWidth / contentHeight) > ratio) {
    // image horizontale
    contentWidth = Math.round(contentHeight * ratio);
  }

// image (conserve ratio, display center top in content)
  var w,h;
//alert(contentWidth + "-" + ow + "-" + contentHeight + "-" + oh);

//alert("contentWidth="+contentWidth+", contentHeight="+contentHeight+"\now="+ow+", oh="+oh);

  if ((contentWidth/ow) <= (contentHeight/oh)) {
    // bordure haut et bas
    w = contentWidth;
    h = Math.round((contentWidth*oh)/ow);
  }
  else { // Horizontal
    // bordure gauche et droite
    h = contentHeight;
    w = Math.round((contentHeight*ow)/oh);
  }
  imgWidth = w;
  imgHeight = h;
//alert("imgWidth="+imgWidth+", imgHeight="+imgHeight);

  document.getElementById("image").width = w;
  document.getElementById("image").height = h;
  document.getElementById("header").style.width = contentWidth + 'px';
  document.getElementById("footer").style.width = contentWidth + 'px';
  document.getElementById("content").style.width = contentWidth + 'px';
  document.getElementById("content").style.height = contentHeight + 'px';

  if (document.getElementById("photoCommand")) {
    var cmdWidth = 200;
    var cmdHeight = 40;

    var newLeft = Math.round((contentWidth-cmdWidth)/2);
    var newTop = (contentHeight-cmdHeight-20);
//alert('photoCommand OK : w='+contentWidth+', h='+contentHeight+', left='+newLeft+', top='+newTop);
    //document.getElementById("photoCommand").style.left = newLeft+'px';
    document.getElementById("photoCommand").style.left = '0px';
    document.getElementById("photoCommand").style.top = newTop+'px';

  }

  if (document.getElementById("boxright")) {
    var ihbox = document.getElementById("boxright").style.height;
    var hbox = h-30-30;

    if (ihbox != hbox + 'px') {
      document.getElementById("boxright").style.height = hbox + 'px';
      hbox = hbox-20;
      document.getElementById("boxright_slider").style.height = hbox + 'px';
      document.getElementById("boxright_main").style.height = hbox + 'px';
      hbox = hbox-20;
      document.getElementById("boxright_slider_chemin").style.height = hbox + 'px';

      initAscenseur('boxright',largeur);
    }
  }

  if (document.getElementById("boxphoto")) {
    var ihbox2 = document.getElementById("boxphoto").style.height;
    var hbox2 = h-30-30;

    if (ihbox2 != hbox2 + 'px') {
      document.getElementById("boxphoto").style.height = hbox2 + 'px';
      hbox2 = hbox2-20;
      document.getElementById("boxphoto_slider").style.height = hbox2 + 'px';
      document.getElementById("boxphoto_main").style.height = hbox2 + 'px';
      hbox2 = hbox2-20;
      document.getElementById("boxphoto_slider_chemin").style.height = hbox2 + 'px';

      initAscenseur('boxphoto',240);
    }
  }

  showdiv('main');
}

// Ajax
function abGetUrlDiv(id,url) {
    var xReq=getXmlHttpRequest();
    var obj=document.getElementById(id);

    abShowWaiting(obj);

    xReq.open("GET",url,true);
    xReq.onreadystatechange=function(){abEventGetUrlDiv(xReq,id);};
    xReq.send(null);
}

function abPhoto(url) {
    abSlideShowStop();

//    showdiv('content');
    hidediv('boxright');
//    cShow = 1;

    var xReq=getXmlHttpRequest();
    xReq.open("GET",url + "&w=" + contentWidth + "&h=" + contentHeight,true);
    xReq.onreadystatechange=function(){abEventGetUrlDiv(xReq,'divimg');};
    xReq.send(null);

    var xReqMenu=getXmlHttpRequest();
    xReqMenu.open("GET",url + "&menu=1",true);
    xReqMenu.onreadystatechange=function(){abEventGetUrlDiv(xReqMenu,'menulbitem');};
    xReqMenu.send(null);
}

function abEventGetUrlDiv(xRequest,anId) {
    if (xRequest.readyState==4)
        document.getElementById(anId).innerHTML=xRequest.responseText;
}

function abShowWaiting(obj) {
    obj.innerHTML="<div style=\"width: 100%;text-align: center;\" align=\"center\"><br /><br />Loading...</div>";
}

function getXmlHttpRequest() {
    if (window.XMLHttpRequest) // Firefox
    {
       return(new XMLHttpRequest());
    }
    else if (window.ActiveXObject) // Internet Explorer
    {
        try
        {
            return(new ActiveXObject("Msxml2.XMLHTTP"));
        } 
        catch (e)
        {
            try
            {
                return(new ActiveXObject("Microsoft.XMLHTTP"));
            }
            catch (e)
            {
                alert("Your browser does not support XMLHTTPRequest...");
            }
        }
    }
    else
    { // XMLHttpRequest non supporté par le navigateur
       alert("Your browser does not support XMLHTTPRequest...");
    }
}

// Remove scrollbars if any
function removeScrollBars() {
    if (browser = "Internet Explorer") { document.body.scroll="no"; document.getElementsByTagName("body")[0].scroll="no"; }
    else if (browser = "Netscape Navigator") { 
        document.width  = window.innerWidth;
        document.height = window.innerHeight;
    }
}

// Close Window Script
function closeWindow() {
    try { window.opener=top; } catch(e){}
    try { window.close(); } catch(e){}
    try { close(opener=0); } catch(e){}
}

// No Right-Click
var message="";
function clickIE() {if (document.all) {(message);return false;}}

function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}

if (document.layers) {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else {document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
function disableselect(e) {return false;}
//document.onselectstart=new Function ("return false")
document.ondragstart=new Function ("return false")
//if (window.sidebar){document.onmousedown=disableselect;}




// Slideshow
function abSlideShow() {
    if (cSlide == 0) {
        if (arguments.length > 1) {
            var str = arguments[0]+',\''+arguments[1]+'\'';
            for(var i=2; i<arguments.length; i++) {
                str+=','+arguments[i];
            }
            setTimeout('abSlideShowGo('+str+')',500);
        }
    }
    else {
        abSlideShowStop();
    }
}

function abSlideShowGo() {
    if (arguments.length > 3) {
        var cId = arguments[0];
        var pTags = arguments[1];
        var nId = 0;
        var str = '';
        for(var i=2; i<arguments.length; i++) {
            if (i==2) nId = arguments[2];
            if ((arguments[i] == cId) && ((i+1)<arguments.length)) nId = arguments[i+1];
            str+=','+arguments[i];
        }
        if (nId > 0) {
            cSlide = 1;
            //var url = 'photo-'+nId+'-'+pTags;
            var url = 'default.php?fct=lbitem&i='+nId+'&t='+pTags;

            var xReq=getXmlHttpRequest();
            xReq.open("GET",url + "&w=" + contentWidth + "&h=" + contentHeight,true);
            xReq.onreadystatechange=function(){abEventGetUrlDiv(xReq,'divimg');};
            xReq.send(null);
            var xReqMenu=getXmlHttpRequest();
            xReqMenu.open("GET",url + "&menu=1",true);
            xReqMenu.onreadystatechange=function(){abEventGetUrlDiv(xReqMenu,'menulbitem');};
            xReqMenu.send(null);

            //hidediv('content');
            //cShow = 0;
            timerId = setTimeout('abSlideShowGo('+nId+',\''+pTags+'\''+str+')',5000);
        }
    }
}

function abSlideShowStop() {
    clearTimeout(timerId);
    //showdiv('content');
    //cShow = 1;
    cSlide = 0;
}

function abGuestbook(url) {
    var xReq=getXmlHttpRequest();
    xReq.open("GET",url,true);
    xReq.onreadystatechange=function(){abEventGetUrlDiv(xReq,'guestbookContent');};
    xReq.send(null);
//    hidediv('boxright_slider');
//    stretch(300);
}










function abSendAjaxFormTargetCommand(idForm,baseurl,targetId,preCommand,postCommand) {
  var xReq=getXmlHttpRequest();
  var i;
  var url="";
  var myForm=document.getElementById(idForm);

  if (myForm) {
    var first=true;
    for(i=0;i<myForm.elements.length;i++) {
      var elmt=myForm.elements[i];
      var type=elmt.nodeName.toLowerCase();
      var ok=true;
      if (type=="input") {
        switch (elmt.type.toLowerCase()) {
          case "radio":
          case "checkbox":
            if (!elmt.checked) ok=false;
            break;
          case "submit":
          case "image":
            ok=false;
            break;
        }
      }
      else if ((type!="select") && (type!="textarea")) ok=false;
      if (ok) {
        if (first) first=false;
        else url+="&";
        url+=elmt.name+"="+elmt.value.replace(/%/g,"%25").replace(/&/g,"%26").replace(/=/g,"%3D").replace(/ /g,"%20");
      }
    }
  }

  if (targetId && targetId!="") abShowWaiting(document.getElementById(targetId));
  eval(preCommand);
  xReq.open("POST",baseurl,true);
  xReq.onreadystatechange=function(){abEventSendAjaxForm(xReq,targetId,postCommand);};
  xReq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xReq.send(url);
}

function abEventSendAjaxForm(xRequest,targetId,postCommand) {
    if (xRequest.readyState==4) {
        if (targetId && targetId!="") document.getElementById(targetId).innerHTML=xRequest.responseText;
        eval(postCommand);
    }
}


