// **********************************************************************

// var $ = function(sID) {return document.getElementById(sID);}

function SCHILLMANIA() {

var nAV = navigator.appVersion.toLowerCase();
var urlSouce = urlRoot+'lights_mix/';

function doInit() {

  if (typeof XLSF2007 != 'undefined' && ((window.location.href.indexOf('maxilampe=1')!=-1) || (window.location.href.indexOf('maxilampe=2')!=-1) ) ) {
    self.xlsf = new XLSF2007(document.getElementById('tour'));
  }
}

setTimeout(doInit,500);

  /* *************** **
  ** -- XLSF 2007 -- **
  ** *************** */

function XLSF2007(oTarget) {
  var self = this;
  var xlsf = self;
  var animDuration = 1;
  this.oFrag = document.createDocumentFragment();
  this.oTarget = (oTarget||document.documentElement);
  this.lights = [];
  this.lightGroups = {
    left: [],
    haut: [],
    right: [],
    bottom: []
  }
  this.lightSmashCounter = 0;
  this.lightIndex = 0;
  this.lightInterval = 250;
  this.timer = null;
  this.bgBaseX = 0;
  this.bgBaseY = 0;
  
  this.cover = document.createElement('div');
  this.cover.className = 'xlsf-cover';
  document.documentElement.appendChild(this.cover);
  
  this.appendLights = function() {
    self.oTarget.appendChild(self.oFrag);
    self.oFrag = document.createDocumentFragment();
  }

  function Light(sClass,nType,x,y) {
    var self = this;
    this.o = document.createElement('img');
    this.o.src = urlSouce+'image/empty.gif';
    this.sClass = sClass;
    this.nType = (nType||0);
    this.useY = (sClass == 'left' || sClass == 'right');
    this.state = null;
    this.broken = 0;
    this.x = x;
    this.y = y;
    if (taille == 'gm') {this.w = 50; this.h = 50;} else {this.w = 32; this.h = 32;};
    this.bgBaseX = (self.useY?-self.w*this.nType:0);
    this.bgBaseY = (!self.useY?-self.h*this.nType:0);
    
    this.oExplosion = null;
    
    this.setBGPos = function(x,y) {
      self.o.style.backgroundPosition = ((self.bgBaseX+x)+'px '+(self.bgBaseY+y)+'px');
    }

    this.setLight = function(bOn) {
      if (self.broken || self.state == bOn) return false;
      if (!self.w || !self.h) self.getDimensions();
      self.state = bOn;
      if (self.useY) {
        self.setBGPos(0,-this.h*(bOn?0:1));
      } else {
        self.setBGPos(-this.w*(bOn?0:1),0);
      }
    }

    this.getDimensions = function() {
      self.w = self.o.offsetWidth;
      self.h = self.o.offsetHeight;
      self.bgBaseX = (self.useY?-self.w*self.nType:0);
      self.bgBaseY = (!self.useY?-self.h*self.nType:0);
    }

    this.on = function() {
      self.setLight(1);}

    this.off = function() {
      self.setLight(0);}
    
    this.flickr = function() {
//      self.setLight(Math.random()>=0.5?1:0);}
      self.setLight(Math.random()>=0.4?1:0);} // plus d allumees

    this.toggle = function() {
      self.setLight(!self.state?1:0);}


    this.init = function() {
      self.o.className = 'xlsf-light '+this.sClass;
      self.o.style.left = x+'px';
      self.o.style.top = y+'px';
       self.o.onmouseover = self.toggle;
       self.o.onmouseout = self.toggle;
      self.flickr();
      xlsf.oFrag.appendChild(self.o);
    }

    this.init();
    
  }

  this.createLight = function(sClass,nType,x,y) {
    var oLight = new Light(sClass,nType,x,y);
    self.lightGroups[sClass].push(oLight);
    self.lights.push(oLight);
    return oLight;
  }

  this.rotateLights = function() {
    self.lights[self.lightIndex==self.lights.length?self.lights.length-1:self.lightIndex].off();
    self.lightIndex++;
    if (self.lightIndex == self.lights.length) {
      self.lightIndex = 0;
    }
    self.lights[self.lightIndex].on();
  }

  this.randomLights = function() {
    self.lights[parseInt(Math.random()*self.lights.length)].toggle();
  }

  this.startSequence = function(fSequence,nInterval) {
    if (self.timer) self.stopSequence();
    self.timer = window.setInterval(fSequence,(typeof nInterval != 'undefined'?nInterval:self.lightInterval));
  }

  this.stopSequence = function() {
    if (self.timer) {
      window.clearInterval(self.timer);
      self.timer = null;
    }
  }

  var i=0;


		//var largeur = Math.round(theWidth/36);//36
		//var hauteur = Math.round(theHeight/37);//37
//var largeur = theWidth/37;//36
//var hauteur = (theHeight-85)/35;// (theHeight-100)/35;

var largeur;
var hauteur;

// if (taille == 'gm') {largeur = theWidth/54; hauteur = (theHeight-85)/36;}
if (taille == 'gm') {largeur = theWidth/54; hauteur = (theHeight-85)/50;}
 else {
largeur = theWidth/37; hauteur = (theHeight-85)/35}



  for (i=0; i<hauteur; i++) {
  if (taille == 'gm') {
    this.createLight('left',parseInt(Math.random()*4),0,30+i*50);
    this.createLight('right',parseInt(Math.random()*4),theWidth-50-urlScroll,30+i*50)}
    else {
    this.createLight('left',parseInt(Math.random()*4),0,30+i*35);
    this.createLight('right',parseInt(Math.random()*4),theWidth-33-urlScroll,30+i*35);
    }
  }

  for (i=0; i<largeur; i++) {
    if (taille == 'gm') {
    this.createLight('haut',parseInt(Math.random()*4),25+i*50,0);
    this.createLight('bottom',parseInt(Math.random()*4),25+i*50,theHeight-50)}
    else {
    this.createLight('haut',parseInt(Math.random()*4),25+i*35,0);
    this.createLight('bottom',parseInt(Math.random()*4),25+i*35,theHeight-33);
    }
  }



  this.appendLights();

  this.startSequence(self.randomLights);

} // --- XLSF2007()

} // ----- SCHILLMANIA()

var s = null;

function preInit() {
  s = new SCHILLMANIA();
}
