var delay = 4 * 1000;
var nImg2 = 0;
var nb_image = 3;
var img = new Array(nb_image);
var url = new Array("http://www.recyclaisne.fr", "http://www.forumforthefuture.be","http://www.univ-reims.fr");
var pn = 0;
var imgTop = new Array( );

var marqueespeed1 = 1, marqueespeed2 = 1;
var actualwidth1 = 0, actualwidth2 = 0;
var marqueewidth1 = 0, marqueewidth2 = 0;
var Marquee1_isPaused = false, Marquee2_isPaused = true;
var cross_marquee1, cross_marquee2;

var down = 0, mouseX = 0;

function initMarquee( ) {
  cross_marquee1 = document.getElementById("Marquee");
  actualwidth1   = parseInt(document.getElementById("Marquee").offsetWidth);
  marqueewidth1  = parseInt(document.getElementById("LText").offsetWidth);
  if( document.getElementById("Marquee2") ) {
    cross_marquee2 = document.getElementById("Marquee2");
    actualwidth2   = parseInt(document.getElementById("Marquee2").offsetWidth);
    marqueewidth2  = parseInt(document.getElementById("MarqueeContainer").offsetWidth);
    Marquee2_isPaused = false;
  }

  lefttime = setInterval("scrollM( )",25);
}

function scrollM( ) {
  if( !Marquee1_isPaused && down != 1 )
    scrollmarquee(cross_marquee1, actualwidth1, marqueewidth1, marqueespeed1);
  if( !Marquee2_isPaused && down != 2 )
    scrollmarquee(cross_marquee2, actualwidth2, marqueewidth2, marqueespeed2);
}

function scrollmarquee(cross_marquee, actualwidth, marqueewidth, marqueespeed) {
  var position = parseInt(cross_marquee.style.left);
  if(-actualwidth < position && position <= marqueewidth)
    cross_marquee.style.left = (position - marqueespeed) + "px";
  else if( position > marqueewidth )
    cross_marquee.style.left = (-actualwidth + 1) + "px";
  else
    cross_marquee.style.left = marqueewidth + "px";
}

function marqueeMouseDown( e, nm ) {
  down   = nm;
  mouseX = e.clientX;

  return false;
}

function marqueeMouseUp( e ) {
  down   = 0;
  mouseX = 0;
}

function marqueeMouseMove( e ) {
  var x, y;
  if( down ) {
    if( e.x != null )
      x = e.x;
    else
      x = e.clientX;
    if( down == 1 )
      scrollmarquee(cross_marquee1, actualwidth1, marqueewidth1, mouseX-x);
    else if( down == 2 )
      scrollmarquee(cross_marquee2, actualwidth2, marqueewidth2, mouseX-x);
    mouseX = x;
  }
}

document.onmouseup = marqueeMouseUp;
document.onmousemove = marqueeMouseMove;

function addNewBanner(nom_img) {
	imgTop[nom_img] = new Image( );
	imgTop[nom_img].src = "images/navigation_" + nom_img + ".png";
}

function initTabs( ) {
  // Initialisation du Tableau Fondu Enchaine
  for( var i = 0; i < nb_image; i++ ) {
    img[i] = new Image( );
    img[i].src = "images/ban_center_" + i + ".png";
  }
  
  // Initialisation du Tableau Banniere RollOver
  addNewBanner("accueil");
  addNewBanner("conseil");
  addNewBanner("creation");
  addNewBanner("communication");
  addNewBanner("coordination");
  addNewBanner("contact");
}

initTabs( );

function RefreshIMG( ) {
  nImg2 = (nImg2 + 1) % img.length;

  if( pn == 0 ) {
    document.getElementById("Url").href = url[nImg2];
    document.getElementById("LeftBan2").src = img[nImg2].src;
    initImage(document.getElementById("LeftBanner2"), document.getElementById("LeftBanner"));
  }
  else {
    document.getElementById("Url2").href = url[nImg2];
    document.getElementById("LeftBan1").src = img[nImg2].src;
    initImage(document.getElementById("LeftBanner"), document.getElementById("LeftBanner2"));
  }

  pn = ( pn + 1 ) % 2;
  setTimeout("RefreshIMG( )",delay);
}
    
function Actualiser( nomBalise )
{
  if( nomBalise ) {
    document.getElementById("TopBanner").style.backgroundImage = "url(" + imgTop[nomBalise].src + ")";
  }
  else {
    if( !pageName ) pageName = 'accueil';
    document.getElementById("TopBanner").style.backgroundImage = "url(" + imgTop[pageName].src + ")";
  }
}

function setOpacity(obj, opacity) {
  // IE et Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari < 1.2, Konqueror
  obj.style.KhtmlOpacity = (opacity / 100);
  // Ancienne Versions Mozilla et Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, Firefox et Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function initImage(image1, image2) {
  setOpacity(image1, 0);
  setOpacity(image2, 100);
  image1.style.visibility = 'visible';
  image2.style.visibility = 'visible';
  image1.style.zIndex = "20";
  image2.style.zIndex = "10";
  fadeIn(image1, image2, 0);
}

function fadeIn(obj1, obj2, opacity) {
  if (opacity <= 100) {
    setOpacity(obj1, opacity);
    setOpacity(obj2, 100 - opacity);
    opacity += 5;
    setTimeout( function( ) {
      fadeIn(obj1, obj2, opacity);
    },50);
  }
  else {
    obj2.style.visibility = 'hidden';
  }
}

