//

var leftOn = false;
var rightOn = false;
var scrollRight = false;
var slow = 2;
var fast = 10;
var speed = slow;
var contDiv = null;
var contWidth = 0;
var photosWidth = 0;
var debugDiv = null;
var counter = 0;
var withDebug = false;
var scrollOn = false;
var scrollAsk = true;
var sensStr ='non';
var ieVer = 21;

//
function initGalerie()
{
  init();
  calculatePhotosSize();
  scroll();
}

function getIEVersion()
{
  if (!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1))
     {
     var ver = navigator.userAgent.match(/MSIE ([0-9.]+);/);
     if (ver != null && ver[1] != undefined)
        {
        ver = parseInt(ver[1]);
        if (ver == 7)
           {
           if (typeof (window.external.AddToFavoritesBar) != "undefined" || typeof (window.external.AddService) != "undefined" || typeof (window.external.AddService) != "undefined")
              return 8;
           }
        return ver;
        }
     }
  return 20;
}

//
function calculatePhotosSize()
{
  if (withDebug)
     {
     debugDiv =  document.getElementById('debug');
     }
  var photosDiv =  document.getElementById('photos');
  photosWidth = photosDiv.offsetWidth;

  resize();

  contDiv =  document.getElementById('conteneur');
  var w = 0;
  for(var i=0; i < contDiv.childNodes.length ; i++)
     {
     var n = contDiv.childNodes[i];
     //alert(n.nodeType + n.nodeName);
     if (n.nodeType == '1')
        {
        w = w + n.width;
        }
     }
  contDiv.style.width = (w + 100) + 'px';
  contWidth = w;
  photosDiv.style.opacity = 100;
  photosDiv.style.filter = "alpha(opacity=100)";
}

//
function resize()
{
  var winWidth = 0;
  if (document.all)
     {
     winWidth = document.documentElement.clientWidth;
     }
     else
     {
     winWidth = window.innerWidth;
     }
  var photosDiv =  document.getElementById('photos');
  var photosLeft = winWidth/2 - photosWidth/2;
  photosDiv.style.left = photosLeft + 'px';
}

//
function init()
{
  ieVer = getIEVersion();
  //alert(ieVer);
  // curseur
  curseur(menuActifId);
}

//
function curseur(menuId)
{
 if (ieVer < 8)
    {
    return;
    }
 if (document.getElementById('curseur'))
   {
   var cur =document.getElementById('curseur');
   if ( document.getElementById(menuId))
      {
      var menu = document.getElementById(menuId);
      var leftMenu = menu.offsetLeft ;
      var leftCur = cur.offsetLeft ;
      var widthMenu = menu.offsetWidth;
      widthMenu = widthMenu / 2;
      cur.style.left=(leftMenu + widthMenu - 12) + 'px';
      cur.style.display='block';
      }
      else
      {
      cur.style.display='none';
      }
   }
}

//
function curseurHome()
{
  curseur(menuActifId);
}




//
function rightMouseOver()
{
  if (!scrollOn)
     scrollAsk = true;
  if (!leftOn)
     {
     if(scrollRight)
        scrollRight = false;
     leftOn = true;
     rightOn = false;
     var img = document.getElementById('imgright');
     var src = img.src;
     src = src.replace('right.jpg','right_a.jpg');
     img.src = src;
     speed = fast;
     }
}

//
function rightMouseOut()
{
  if (leftOn)
     {
     leftOn = false;
     var img = document.getElementById('imgright');
     var src = img.src;
     src = src.replace('right_a.jpg','right.jpg');
     img.src = src;
     speed = slow;
     }
}

//
function leftMouseOver()
{
  if (!scrollOn)
     scrollAsk = true;
  if (!rightOn)
     {
     if(!scrollRight)
        scrollRight = true;
     rightOn = true;
     leftOn = false;
     var img = document.getElementById('imgleft');
     var src = img.src;
     src = src.replace('left.jpg','left_a.jpg');
     img.src = src;
     speed = fast;
     }
}

//
function leftMouseOut()
{
  if (rightOn)
     {
     rightOn = false;
     var img = document.getElementById('imgleft');
     var src = img.src;
     src = src.replace('left_a.jpg','left.jpg');
     img.src = src;
     speed = slow;
     }
}

//
function scroll()
{
  var ask = 'non';
  counter = counter + 1;
  var x = contDiv.offsetLeft;
  // butees
  var buteeLeft = contWidth - photosWidth;
  var abs = Math.abs(x);
  var buteeL = 'non'
  if (!scrollRight && abs >= buteeLeft)
     {
     scrollRight = true;
     buteeL = 'oui';
     if (Number(counter) > 10)
        {
        scrollOn = false;
        }
     }
     else if(x >= 0)
     {
     scrollRight = false;
     if (Number(counter) > 10)
        {
        scrollOn = false;
        }
     }
  if (scrollAsk && !scrollOn)
     {
     scrollOn = true;
     scrollAsk = false;
     ask = 'oui';
     }
  if (scrollOn)
     {
     var sens = 1;
     sensStr = 'right';
     if (!scrollRight)
        {
        sens = -1;
        sensStr = 'left';
        }
     var mov = sens * speed
     x = x + mov;
     contDiv.style.left = x + "px";
     }
  if (withDebug)
        {
        debugDiv.innerHTML = 'Cont:' + contWidth + ', larg:' + photosWidth + ', Sens:' + sensStr + ', x:' + x + ', ButL:' + buteeLeft + ', abs:' + abs + ', bL:' + buteeL + ', count:' + counter + ', mov:' + mov + ',ask:' + ask;
        }
  setTimeout("scroll()",100);
}
