// VOIR //  : AJOUT : emplacement pour mettre une fonction en fin de l ouverture du site


/*
 SCOTTSCHILLER.COM | V4.04: NOW WITH FUNK
 ----------------------------------------
 This is some crazy animation scriptage.
 probably not very efficient, either .. 
 .. But it works. Usually. view at risk.

 Core objects:
 ----------------------------------------
  - Window
  - DropShadow
  - Queue Item
  - Queue Object
  - Event
  - Action
  - DragHandler
  - Preloader
  - ContentManager
  - SoundManager  

 Quick summary:
 ----------------------------------------

 Window objects are created which are
 tied to events. There is an event queue
 that holds event objects, which can have
 associated action objects. Windows have
 methods like moveTo, resizeBy etc. which
 "automagically" create and assign events.

 A number of actions can be queued up and
 fired - the animated into to this site
 is done exactly that way.

 eg. the following call:

  with (addAction(preload)) {
    delay = 500;
    method = restore;
    sound = 'window3';
    enqueue();
  }

 ..enqueues the "preload" window object,
 calling the "restore" method (action)
 and playing the associated "window3"
 sound effect. This action fires after
 a 500ms delay.

 A number of actions can be queued up in
 this manner and fired simultaneously or
 in sequence (ie. multiple windows moving
 at once, or one window at a time.)

 Refer to the "preInit()" function below
 to see how the intro animation is made.


 Bugs and other known annoyances:
 ----------------------------------------

 IE doesn't seem to like JS-to-ActiveX
 calls after a certain point for some odd
 reason, this can cause it to crash.
 (This method is used for sound effects.)

 Related (improved) javascript-sound API:
 schillmania.com/projects/soundmanager/

*/

var o = null;

var contentManager = null;
var soundManager = null;

var hideWhileScrolling = false;

function WindowObject(o,x,y,w,h) {
  var self = this;
  this.o = o;
  this.c = this.o.getElementsByTagName('div')[0]; // content area
  this.s = this.o.getElementsByTagName('div')[1]; // scroll content object (used to fix vertical scrollbar if applicable)
  this.index = windowObjects.length; // for reference from queue object
  this.fixedX = false;
  this.fixedY = false;
  this.fixedW = false;
  this.fixedH = false;

  // dimensions (left/top/width/height)
  this.x = x||-64;
  this.y = y||48;
  this.w = w||20;
  this.h = h||20;

  // default values
  this.defaultX = this.x;
  this.defaultY = this.y;
  this.defaultW = this.w;
  this.defaultH = this.h;

  this.i = 0;
  this.tweenDone = 0;
  this.tweenFrame = 0;
  this.tweenWidth = [];
  this.tweenHeight = [];
  this.tweenLeft = [];
  this.tweenTop = [];
  this.tweenStep = [1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1];

  this.wRel = null;
  this.hRel = null;
  this.xRel = null;
  this.yRel = null;

  this.setDimensions = function(x,y,w,h) {
    this.x = x+1?x:this.x;
    this.y = y+1?y:this.y;
    this.w = w+1?w:this.w;
    this.h = h+1?h:this.h;
  }

  this.setRelativeDimensions = function() {
    //var x,y,w,h;
    // experimental - relative (%) style values for "flexible" content
    if (!isNaN(self.w)) self.wRel = self.w/screenX;
    if (!isNaN(self.h)) self.hRel = self.h/screenY;
    if (!isNaN(self.x)) self.xRel = self.x/screenX;
    if (!isNaN(self.y)) self.yRel = self.y/screenY;
  }

  this.refresh = function() {
    this.o.style.width = this.w+'px';
    this.o.style.height = this.h+'px';
    this.o.style.left = this.x+'px';
    this.o.style.top = this.y+'px';
  }

  this.createTween = function(x,y,w,h) {
    // from current position: determine resulting coords for desired effect.

    this.tweenFrame = 0;
    this.tweenDone = 0;
    this.tweenWidth = [];
    this.tweenHeight = [];

    // avoid undefined/undeclared values
    x = x+1?x:this.x;
    y = y+1?y:this.y;
    w = w+1?w:this.w;
    h = h+1?h:this.h;

    var wDiff = w-this.w; // (w+1||this.w)-this.w;
    var hDiff = h-this.h; // (h+1||this.h)-this.h;
    var xDiff = x-this.x; // (x+1||this.x)-this.x;
    var yDiff = y-this.y; // (y+1||this.y)-this.y;

    // expand smoothly to w/h

    var tmpW = 0;
    var tmpH = 0;
    var tmpX = 0;
    var tmpY = 0;

    for (var i=0; i<this.tweenStep.length; i++) {
      tmpW += wDiff*this.tweenStep[i]*0.01;
      tmpH += hDiff*this.tweenStep[i]*0.01;
      tmpX += xDiff*this.tweenStep[i]*0.01;
      tmpY += yDiff*this.tweenStep[i]*0.01;
      this.tweenWidth[i] = this.w+tmpW;
      this.tweenHeight[i] = this.h+tmpH;
      this.tweenLeft[i] = this.x+tmpX;
      this.tweenTop[i] = this.y+tmpY;
    }

    // math not quite perfect due to rounding or something.
    // following ensures last tween step is perfect.

    // backup
    this.tweenWidth[this.tweenWidth.length-1] = w;
    this.tweenHeight[this.tweenHeight.length-1] = h;
    this.tweenLeft[this.tweenLeft.length-1] = x;
    this.tweenTop[this.tweenTop.length-1] = y;

    // EXPERIMENTAL - use percentages at end
    // Add percentage sign when setting via JS.

    this.wRel = (w/screenX*100);
    this.hRel = (h/screenY*100);
    this.xRel = (x/screenX*100);
    this.yRel = (y/screenY*100);

    this.scrollResizeHandler(w,h);
    
  }

  this.scrollResizeHandler = function(w,h) {
    w = w||this.w;
    h = h||this.h;
    if (this.s && this.s.className == 'scrollWrapper' && w>16 && h>19) {
      // adjust height from 100% to absolute value - 18px so vertical scrollbar displays properly if applicable
      this.s.style.width = (w-16)+'px';
      this.c.style.width = this.s.style.width; // prevent content overflow on right side
      this.s.style.height = (h-(this.o.id=='portfolio'&&h>63?63:19))+'px'; //43?43:19 aulieu 63?63:19 pour baisser w
    }
  }

  this.resizeBy = function(x,y,w,h,center) {
    // relative resize/move
    // catch any -1's being passed in
    if (x==-1) x = 0;
    if (y==-1) y = 0;
    if (w==-1) w = 0;
    if (h==-1) h = 0;
    if (center) {
      // expand around current x/y position
      this.createTween(this.x-(w/2),this.y-(h/2),this.w+w,this.h+h);
    } else {
      this.createTween(this.x+x,this.y+y,this.w+w,this.h+h);
    }
  }

  this.resizeTo = function(x,y,w,h,center) {
    // absolute resize
    if (center) {
      this.createTween(x,y,w,h);
      // this.createTween(x+2?this.x-(w/2):-1,y+2?this.y-(h/2):16,w,h);
    } else {
      this.createTween(x,y,w,h);
    }
  }

  this.restore = function() {
    this.createTween(this.defaultX,this.defaultY,this.defaultW,this.defaultH);
    this.o.style.border = this.defaultBorder;
  }

  this.restoreCenter = function() {
    this.createTween(screenMidX,screenMidY,this.defaultW,this.defaultH);
    this.o.style.border = this.defaultBorder;
  }

  this.center = function() {
    this.createTween(screenMidX-(parseInt(this.w/2)),screenMidY-(parseInt(this.h/2)));
  }

  this.maximize = function() {
    this.o.style.border = 'none';
    this.createTween(0,0,screenX,screenY); // 18 offset to avoid scrollbars
  }

  this.animate = function() {
    this.setDimensions(this.tweenLeft[this.tweenFrame],this.tweenTop[this.tweenFrame],this.tweenWidth[this.tweenFrame],this.tweenHeight[this.tweenFrame]);
    this.refresh();
    this.tweenFrame++;
    if (this.tweenFrame >= this.tweenStep.length) {
      // finishing tween
      this.setRelativeDimensions();
      this.tweenDone = 1;
    }
  }

  this.disableScroll = function() {
    var t = self||this;
    if (t.s && t.s.className == 'scrollWrapper') {
      t.s.style.overflow = 'hidden';
    }
    if (hideWhileScrolling) t.s.style.visibility = 'hidden';    
  }
  
  this.enableScroll = function() {
    var t = self||this;
    if (t.s && t.s.className == 'scrollWrapper') {
      t.s.style.overflow = 'auto'; // general scope weirdness here (called from action)
      // duplication from scrollResizeHandler
      t.s.style.width = (parseInt(t.o.style.width)-16)+'px';
    }
    if (hideWhileScrolling) t.s.style.visibility = 'visible';
  }

  this.resizeHandler = function() {
    // reflow size relative to window if not at default w/h
    if (this.h && this.w && (!this.fixedX || !this.fixedY)) {
      this.setDimensions((!this.fixedX?this.xRel*screenX:this.x),(!this.fixedY?this.yRel*screenY:this.y),(!this.fixedW?this.wRel*screenX:this.w),(!this.fixedH?this.hRel*screenY:this.h));
      this.refresh();
      this.scrollResizeHandler();
    }
  }

  this.setDimensions(this.x,this.y,this.w,this.h);
  this.refresh();
  this.defaultBorder = '1px solid #666666';

  // create drop shadow if necessary
  if (this.o.className.indexOf('dropShadow')+1) {
    this.dropShadow = new DropShadow(this.o);
    this.defaultBorder = 'none';
  }

  this.o.style.display = 'block';

}

function DropShadow(o) {
  var self = this;
  this.o = o; // window container div
  this.c = o.getElementsByTagName('div')[0]; // first child DIV: content container
  // this.o.style.backgroundColor = '#fdfdfd';
  this.o.style.border = 'none';
  this.type = (this.o.className.indexOf('preload')+1?'preload/':'');

  this.corners = []; // static images
  this.edges = []; // repeating backgrounds

  this.Img = function(src,w,h) {
    this.o = document.createElement('img');
    if (w && h) {
      this.o.style.width = w+'px';
      this.o.style.height = h+'px';
    }
    this.o.src = 'intro/image/'+self.type+'bg_'+src+'.gif';
    this.c = src.split('_').join(' '); // determine class from src
    this.o.className = 'pos '+this.c+' png';
  }

  this.Div = function(className) {
    this.o = document.createElement('div');
    this.o.className = 'pos edge '+className+' png';
    // this.o.appendChild(document.createTextNode('0'));
  }

  this.edges = [new this.Div('top'),new this.Div('right'),new this.Div('bottom'),new this.Div('left')];
  this.corners = [new this.Img('top_right',11,9),new this.Img('top_left',9,9),new this.Img('bottom_right',12,11),new this.Img('bottom_left',9,10)];

  this.corners[0].o.style.marginRight = '-1px';
  this.corners[2].o.style.marginRight = '-1px';
  this.corners[2].o.style.marginBottom = '-1px';
  this.corners[3].o.style.marginBottom = '-1px';

  for (var i=0; i<4; i++) {
    this.o.appendChild(this.edges[i].o);
  }
  for (i=0; i<4; i++) {
    this.o.appendChild(this.corners[i].o);
  }
}

function Queue() {
  var self = this;
  this.active = 0;
  this.index = -1;
  this.items = []; // QueueObject array
  this.onComplete = null;
  this.addItem = function(queueObject) {
    this.items[this.items.length] = queueObject;
  }
  this.fire = function() {
    if (self.index >= 0) { // fire handler after first event done // DEBUG - used to be > 0
      self.items[self.index].onComplete(); // was onCompleteHandler();
    }
    if (self.index >= self.items.length-1) {
      // queue finished
      self.active = 0;
      if (queue.onComplete) {
        queue.onComplete();
        queue.onComplete = null;
      }
    } else {
      self.active = 1;
      self.items[++self.index].go();
    }
  }
}

var queue = new Queue();
var queueCount = 0;

function QueueObject(oParent,oAction) {
  var self = this;
  var oParent = oParent;

  this.oA = oAction; // oAction (Action Object)
  this.oParent = oParent;
  this.index = queue.length;
  this.method = oAction.method; // oParent.actions[oAction.method];

  this.methods = []; // array for simultaneous "synchronous" actions (if needed)
  this.methods[this.methods.length] = this.method;

  this.delay = oAction.delay;

  this.onComplete = oAction.onComplete;
  this.onCompleteHandlers = [];
  this.onCompleteHandlers[this.onCompleteHandlers.length] = this.onComplete;

  this.addMethod = function(oSyncAction) {
    // alert('addMethod() - sync');
    // for adding simultaneous "synchronous" calls
    //    alert(this.oA.method);
    //    alert(oMethod);
    //    alert('this.oA.method == oSyncAction.method: '+(this.oA.method == oSyncAction.method));
    this.methods[this.methods.length] = oSyncAction.method;
    // if (self.method) alert('self.method: '+self.method);
    if (this.method != this.methodSyncHandler) {
      // alert('setting .method() to .methodSyncHandler()');
      this.method = this.methodSyncHandler;
    }
  }

  this.methodSyncHandler = function() { // call all "hooked" (added) synchronous methods
    // alert('calling synch methods');
    for (var i=0; i<this.methods.length; i++) {
      // alert('calling methods['+i+']\n'+this.methods[i]);
      this.methods[i]();
    }
  }

  this.addOnComplete = function(oSyncAction) { // oSyncAction: Action object or raw function()
    // alert('Adding synchronous onComplete()');
    this.onCompleteHandlers[this.onCompleteHandlers.length] = oSyncAction.onComplete?oSyncAction.onComplete:oSyncAction; // assign object.method or function pointer accordingly
    if (this.onComplete != this.onCompleteSyncHandler) {
      // alert('setting onComplete handler for queue object.');
      this.onComplete = this.onCompleteSyncHandler;
    }
  }
 
  this.onCompleteSyncHandler = function() { // call all "hooked" (added) synchronous onComplete events
    // alert('firing onCompleteSyncHandler');
    for (var i=0; i<this.onCompleteHandlers.length; i++) {
      this.onCompleteHandlers[i]();
    }
  }

  this.go = function() {
    // window.status = 'QueueObject['+self.index+'] firing.';
    if (self.delay) {
      // window.status = 'Waiting '+self.delay+' ms..';
      setTimeout("queue.items["+queue.index+"].oParent.disableScroll();queue.items["+queue.index+"].oA.playSound();queue.items["+queue.index+"].method();animateStart()",self.delay);
    } else {
      self.oParent.disableScroll();
      self.oA.playSound();
      self.method();
      animateStart();
    }
  }

  // experimental
  // alert(oParent.setRelativeDimensions)
  // this.addOnComplete(function(){alert('QueueObject: done')});
  // this.addOnComplete(oParent.setRelativeDimensions);

}

function Action(parent) {
  var oParent = parent; // reference to animation object (for method pointers)
  var self = this;

  this.method = null;
  this.x = -1;
  this.y = -1;
  this.w = -1;
  this.h = -1;
  this.center = null;
  this.delay = null;
  this.sound = null;
  this.onComplete = function() {} // empty function reference
  this.emptyFunction = this.onComplete // for comparison later

  this.restore = function() { oParent.restore(); }
  this.restoreCenter = function() { oParent.restoreCenter(); }
  this.resizeBy = function() { oParent.resizeBy(self.x,self.y,self.w,self.h,self.center); }
  this.resizeTo = function() { oParent.resizeTo(self.x,self.y,self.w,self.h,self.center); }
  this.center = function() { oParent.center(); }
  this.maximize = function() { oParent.maximize(); }

  this.playSound = function() { if (self.sound!=null) soundManager.play(null,self.sound); }

  this.enqueue = function() {
    queue.addItem(new QueueObject(oParent,this));
  }

  this.enqueueSimultaneous = function() {
    var q = queue.items[queue.items.length-1];
    //    alert('adding simultaneous action\n'+this.method+'\n(old:)\n'+q.method);
    q.addMethod(this);
    if (this.onComplete && this.onComplete != this.emptyFunction) {
      q.addOnComplete(this);
    }
  }

}

var timers = null;
var isWin9X = (navigator.appVersion.toLowerCase().indexOf('windows 98')+1);
var isOpera = (navigator.userAgent.toLowerCase().indexOf('opera')+1?1:0);
var isIE = (navigator.appName.toLowerCase().indexOf('internet explorer')+1?1:0);
if (isOpera) isIE = false;
var isSafari = (navigator.appVersion.toLowerCase().indexOf('safari')+1?1:0);

function animateStart() {
  if (!timers) timers = (!isWin9X?window.setInterval("animateLoop()",20):[window.setInterval("animateLoop()",75),window.setInterval("animateLoop()",25)]);
}

function animateStop() {
  if (!isWin9X) {
    if (timers) {
      window.clearInterval(timers);
      if (timers) timers = null;
    }
  } else if (timers) {
    window.clearInterval(timers[0]);
    window.clearInterval(timers[1]);
    timers = null;
  }
}

function animateLoop() {
  var done = 1;
  for (var i=0; i<windowObjects.length; i++) {
    if (!windowObjects[i].tweenDone) {
      windowObjects[i].animate();
      done = 0;
    }
  }
  if (!done) {
    // window.setTimeout("animateLoop()",20);
  } else {
    // window.status = 'Animation done.';
    animateStop();
    queue.fire();
  }
}

function whatIs(o) {
  var stuff = '';
  for (var tmp in o) {
    stuff += tmp + ' ';
  }
  alert('whatIs():\n'+stuff);
}

var windowObjects = [];

var actions = [];
var action = null;
var actionCount = -1;

function addAction(winObject) {
  return actions[++actionCount] = new Action(winObject);
}

var pWidth = 200; // largeur colonne 1 passee de 188 à 200

function initMain() {
  // getCoords();

  // initialise manager objects
  contentManager = new ContentManager();

  // simple window references
  var main = windowObjects[0];
  var news = windowObjects[1];
  var portfolio = windowObjects[2];
  var featured = windowObjects[3];
  var portfolioContent = windowObjects[4];
  var preload = windowObjects[5];

  portfolio.setDimensions(-255,0,pWidth,58);

  // configure a few things

  with (portfolio) {
    fixedW = true;
    fixedX = true;
  }
  with (news) {
    fixedW = true;
    fixedX = true;
  }
  with (featured) {
    fixedW = true;
    fixedX = true;
  }
  with (portfolioContent) {
    fixedX = true;
  }

  // create an animation sequence

  with (addAction(preload)) {
    delay = 500;
    method = restore;
    sound = 'window3';
    enqueue();
  }

  with (addAction(main)) {
    method = center;
    sound = 'window1';
    enqueue();
  }

  with (addAction(main)) {
    delay = 100;
    center = true;
    method = maximize;
    sound = 'maximize';
    enqueue();
  }

  with (addAction(news)) {
    x = 8 + pWidth + 10;
    y = 18; //18 au lieu de 32= top col-2
    method = resizeTo;
    sound = 'window0';
    enqueue();
  }

  with (addAction(portfolio)) {
    x = 8;
    y = 18; //18 au lieu de 32= top col-1
    method = resizeTo;
    enqueueSimultaneous(); // simultaneous: move at the same time as previous action
  }

  with (addAction(featured)) {
    x = 8 + pWidth + 10;
    y = 236; //32-18=14 =>  au lieu de 250= top col-2 bas (puis 32=>23)
    // center = true;
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(portfolioContent)) {
    x = 8 + pWidth + 10 + 215 + 10;
    y = 18; //18 au lieu de 32= top col-3
    center = true;
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(portfolioContent)) {
    w = screenX-(8 + pWidth + 10 + 215 + 10)-8; // -60; (logo)
    method = resizeTo;
    // need to disable portfolioContent scroll manually (because of simultaneous enqueue)
    onComplete = function(){windowObjects[1].disableScroll();}
    enqueue();
  }

  with (addAction(news)) {
    w = 215; // largeur colonne 2 - haut (news)
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(portfolio)) {
    h = (screenY-32-60);
    method = resizeTo;
    sound = 'window2';
    enqueue(); //32 au lieu de 72 60= haut col-1
  }

  with (addAction(featured)) {
    w = 215; // largeur colonne 2 - bas (featured)
    // h = 145;
    center = false;
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(featured)) {
    h = (screenY-32-60)-110-58-10-40; // 145
    center = false;
    method = resizeTo;
    sound = 'window2';
    enqueue(); //32 au lieu de 72 60= haut col-2
  }

  with (addAction(news)) {
    delay = 125;
    h = (308-100);  // hauteur colonne 2 - haut (news) : 208
    center = false;
    method = resizeTo;
    // enqueue();
    enqueueSimultaneous();
  }

  with (addAction(portfolioContent)) {
    h = (screenY-32-60);
    method = resizeTo;
    enqueue();  //32 au lieu de 72 60= haut col-3
  }

  queue.onComplete = function() {
    for (var i=0; i<windowObjects.length; i++) {
      windowObjects[i].enableScroll(); parent.immobilier.so.write('flashcontent8x');//function au_choix();  //  AJOUT  AJOUT  AJOUT
      // flash de départ sur pages comme - programme_sound.html -
    }
  }

  queue.fire(); // start the animation sequence

}

var screenX = 0;
var screenY = 0;

function getElementsByClassName(className,oParent) {
  var doc = (oParent||document);
  var matches = [];
  var nodes = doc.all||doc.getElementsByTagName('*');
  for (var i=0; i<nodes.length; i++) {
    if (nodes[i].className == className || nodes[i].className.indexOf(className)+1 || nodes[i].className.indexOf(className+' ')+1 || nodes[i].className.indexOf(' '+className)+1) {
      matches[matches.length] = nodes[i];
    }
  }
  return matches; // kids, don't play with fire. ;)
}

function toggleDiv(o) {
  o.blur();
  o = o.parentNode.getElementsByTagName('div')[0];
  if (!o) return false;
  o.style.display = o.style.display != 'block'?'block':'none';
  return false;
}

function getCoords() {
  if (!isSafari && !isOpera) {
    screenX = document.documentElement.clientWidth||document.body.clientWidth||document.body.scrollWidth; // document.documentElement.clientWidth||document.body.clientWidth||window.innerWidth;
    screenY = document.documentElement.clientHeight||document.body.clientHeight||document.body.scrollHeight; // document.documentElement.clientHeight||document.body.clientHeight||window.innerHeight;
  } else {
    // safari doesn't pick up document dimensions via the usual. (?)
    screenX = window.innerWidth;
    screenY = window.innerHeight;
  }

  screenMidX = parseInt(screenX/2);
  screenMidY = parseInt(screenY/2);

  if (windowObjects) {
    for (var i=0; i<windowObjects.length; i++) {
      windowObjects[i].resizeHandler();
    }
    if (windowObjects[4]) { // Portfolio content hack
      windowObjects[4].setDimensions(-1,-1,screenX-windowObjects[4].x-8,-1); // windowObjects[4].x-58,
      windowObjects[4].scrollResizeHandler();
      windowObjects[4].refresh();
    }
  }

}

var dragHandler = null;

function DragHandler() {
  var self = this;
  this.disabled = 0;
  this.disableDrag = function() {
    this.disabled = 1;
    this.mouseup();
    document.onmouseup = this.enableDrag;
  }
  this.enableDrag = function() {
    self.disabled = 0;
    self.mouseup();
  }
  this.startDrag = function(o,e) {
    if (this.disabled) {
      return false;
    }
    if (o) this.setTarget(o);
    this.offX = this.mouse.clientX - this.o.offsetLeft;
    this.offY = this.mouse.clientY - this.o.offsetTop;
    document.onmousemove = this.mousemove;
    document.onmouseup = this.mouseup;
  }
  this.mousemove = function(e) {
    self.o.style.left = self.mouse.clientX - self.offX;
    self.o.style.top = self.mouse.clientY - self.offY;
    self.stopBubbleHandler(e);
    return false;
  }
  this.mouseup = function() {
    document.onmousemove = null;
    document.onmouseup = null;
  }
  this.setTarget = function(o) {
    if (this.disabled) {
      return false;
    }
    this.o = o;
    this.o.onmouseover = this.stopBubbleHandler;
    this.o.onmouseout = this.o.onmouseover;
  }
  this.setMouse = function(mouse) {
    // called from global mouse handler
    this.mouse = mouse;
  }
  this.cancelBubble = function() {
    event.cancelBubble = true;
  }
  this.stopPropagation = function(e) {
    e.stopPropagation();
  }
  this.stopBubbleHandler = (navigator.appName.indexOf('Microsoft')?this.stopPropagation:this.cancelBubble);
  // this.mouse = mouse?mouse:null;
  this.offX = 0;
  this.offY = 0;
  this.o = null;
}

function toggleFont() {
  var o = document.getElementsByTagName('link');
  for (var i=0; i<o.length; i++) {
    if (o[i].title == 'altFonts') {
      o[i].disabled = (o[i].disabled?false:true);
      break;
    }
  }
}
toggleFont(); // modifie : ajout

dragHandler = new DragHandler();

document.onmousemove = function(e) {
  dragHandler.setMouse(e||event);
  document.onmousemove = null;
}

window.onresize = getCoords;

var portfolioState = null;

function focusPortfolio() {
  if (portfolioState) return false;
  portfolioState = 'focused';

  // simple window references
  var main = windowObjects[0];
  var news = windowObjects[1];
  var portfolio = windowObjects[2];
  var featured = windowObjects[3];
  var portfolioContent = windowObjects[4];

  with (addAction(news)) {
    h = 20;
    center = false;
    method = resizeTo;
    enqueue();
  }

  with (addAction(featured)) {
    h = 20;
    method = resizeTo;
    // need to disable portfolioContent scroll manually (because of simultaneous enqueue)
    onComplete = function(){windowObjects[4].disableScroll();}
    enqueueSimultaneous();
  }

  with (addAction(news)) {
    delay = 125;
    w = 20;
    center = false;
    method = resizeTo;
    // manual scroll enable (see disable above)
    onComplete = function(){windowObjects[4].enableScroll();}
    enqueue();
  }

  with (addAction(featured)) {
    w = 20;
    center = false;
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(portfolioContent)) {
    x = portfolio.x + portfolio.w + 40;
    w = screenX - 8 - pWidth - 10 - 24 - 16; // - 60 - 24;
    // w += news.w + 16;
    center = true;
    method = resizeTo;
    enqueueSimultaneous();
  }
  queue.fire();
}

function Preloader(o) {
  var self = this;
  this.o = o;
  this.objects = [];
  this.objectTotal = 0;
  this.objectLoaded = 0;
  this.objectBytesLoaded = 0;
  this.objectBytesTotal = 123240; // variable
  this.objectBytesTotalTrue = 0;
  this.objectIndex = -1;
  this.path = 'intro/image/';
  this.useTrueTotal = 0; // Flag: use true byte count if applicable rather than average
  this.progress = this.o.getElementsByTagName('div')[4];
  this.preloadWidth = parseInt(this.o.getElementsByTagName('div')[2].getElementsByTagName('img')[0].offsetWidth)-(isIE?3:1); // compensate for offset + border
  // debug('Preloader(): Got preload width: '+this.preloadWidth);

  this.objectOnLoadHandler = function() {
    // within scope of object (ie. image)
    // debug('Preloader.objectOnLoadHandler(): '+this.src + ' loaded.');
    this.onload = null;
    self.objectLoaded++;
    self.objectBytesLoaded += !isNaN(this.fileSize)&&self.useTrueTotal?parseInt(this.fileSize):(self.objectBytesTotal*1/self.objectTotal);
    if (!isNaN(this.fileSize)) {
      self.objectBytesTotalTrue += parseInt(this.fileSize);
    }
    // self.loadNextObject();
    soundManager.play(null,'mouseover0');
    setTimeout('preloader.loadNextObject()',20);
    self.update(!isNaN(this.fileSize)&&self.useTrueTotal?this.fileSize:null);
  }

  this.PreloadImage = function(oSrc) {
    this.src = oSrc;
    this.o = new Image(); // document.createElement('img');
    this.init = function() {
      this.o.onload = self.objectOnLoadHandler;
      this.o.src = self.path+this.src; // +'?rnd='+parseInt(Math.random()*1048576);
    }
  }

  this.addImages = function() {
    for (var i=0; i<arguments.length; i++) {
      this.objects[this.objects.length] = new this.PreloadImage(arguments[i]);
      this.objectTotal++;
    }
  }

  this.update = function() {
    // called after each object load
    // window.status = 'Preloader.update() | loaded: '+this.objectLoaded+'/'+this.objectTotal+' | bytes: '+this.objectBytesLoaded+'/'+this.objectBytesTotal+' ('+this.objectBytesTotalTrue+')';
    self.updateProgress();
    if (self.objectLoaded >= self.objectTotal) {
      // self.progress.style.width = self.preloadWidth+'px';
      // self.preloadComplete();
      setTimeout('preloader.preloadComplete()',20);
    }
  }

  this.updateProgress = function(p) {
    t = self||this;
    t.progress.style.width = p?parseInt(p*t.preloadWidth/100):(parseInt(t.objectBytesLoaded/t.objectBytesTotal*t.preloadWidth))+'px';
  }

  this.preloadComplete = function() {
    this.preloadCompleteHandler();
  }

  this.init = function() {
    // begin preloading content
    // debug('Preloader.init()');
    // self.o.getElementsByTagName('div')[3].style.visibility = 'visible'; // show preload placeholder
    self.progress.style.visibility = 'visible';
    for (var i=0; i<3; i++) {
      self.loadNextObject();
    }
  }

  this.loadNextObject = function() {
    if (self.objectIndex < self.objectTotal-1) {
      // debug('loadNextObject()');
      self.objects[++self.objectIndex].init();
    } else {
      // debug('loadNextObject(): no more objects to load.');
    }
  }
}

var preloader = null;

function ekkert() {
	var fontSize = "12";
	if ((getCookie("fontSize") != null) &&  (getCookie("fontSize") != '')) {
		fontSize = getCookie("fontSize");
		setCookie("fontSize",'');
	}
	if (fontSize != 12) {
		document.body.style.fontSize = percyfy(fontSize) + "%";
	}
	setCookie("fontSize",fontSize);

	var cookie = getCookie("stylesheet");
	var title = (cookie) ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
			
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
    		&& a.getAttribute("title")
    		&& a.getAttribute("rel").indexOf("stylesheet") != -1 ) {
      a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
  }
  setCookie("stylesheet",title);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
    		&& a.getAttribute("title")
    		&& !a.disabled
    		&& a.getAttribute("rel").indexOf("stylesheet") != -1 )
    	return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
			 && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       && a.getAttribute("rel").indexOf("stylesheet") != -1
       ) return a.getAttribute("title");
  }
  return null;
}

function enlargeFont() {
	var size = getCookie("fontSize");
	if (size == null) {
		size = 12;
	}
	size++;
	if (size > 25) {size = 25}
	document.body.style.fontSize = percyfy(size)+'%';
	setCookie("fontSize",size);
}

function shrinkFont() {
	var size = getCookie("fontSize");
	if (size == null) {
		size = 12;
	}
	size--;
	if (size < 12) {size = 12}
	document.body.style.fontSize = percyfy(size)+'%';
	setCookie("fontSize",size);
}

function setSize(size) {
	if (size < 12) {size = 12}
	document.body.style.fontSize = percyfy(size)+'%';
	setCookie("fontSize",size);
}

function setScheme (sc) {
	if (sc = 'inverse') {

	} else {
	}
}

function restoreSize() {
	size = "12";
	document.body.style.fontSize = percyfy(size)+'%';
	setCookie("fontSize",'');
}


function percyfy(size) {
	return (size/16)*100;
}


function setCookie(cookieName,cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*1000);
	document.cookie = cookieName + "="+escape(cookieValue)+";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
	var cookie = document.cookie;
	var index = cookie.indexOf(cookieName + "=");
	if (index == -1) return null;
	index = cookie.indexOf("=", index) + 1;
	var endstr = cookie.indexOf(";", index);
	if (endstr == -1) endstr = cookie.length;
	return unescape(cookie.substring(index, endstr));
}


window.onload = function() {
  getCoords();
  preloader = new Preloader(document.getElementById('preload'));
  preloader.preloadCompleteHandler = function() {
    // window.status = 'Préchargement terminé.';
    initMain();
    ekkert();
    setActiveStyleSheet('grey');
    parent.immobilier.setActiveStyleSheet('greyfmes');
    parent.defiscalisation.setActiveStyleSheet('greyph');
    parent.catalogue.setActiveStyleSheet('greyph');
    afficheDescURL(base);
  }


  preloader.addImages('bg_left.gif','bg_right.gif','bg_test.gif','bg_top.gif','bg_bottom_left.gif','bg_top_left.gif','bg_top_right.gif','bg_bottom_right.gif','bg_bottom.gif','t_0.gif','t_1.gif','immo.gif','plus.gif','none.gif','photo_bg.gif','square.gif','placeholder.gif','shadow_bottom.gif','shadow.gif','intro.jpg','fiche-technique.gif');                      
  var winContainers = getElementsByClassName('windowObject');
  for (var i=0; i<winContainers.length; i++) {
    windowObjects[windowObjects.length] = new WindowObject(winContainers[i],null,null,!i?8:0,!i?8:0); // ,null,null,winObjXY[i],winObjXY[i]
  }

  var preload = windowObjects[windowObjects.length-1];

  preload.defaultX = screenMidX-60;
  preload.defaultY = -27;
  preload.defaultY = -60;
  preload.defaultW = 115;
  preload.defaultH = 54;

  preload.setDimensions(preload.defaultX,preload.defaultY,preload.defaultW,preload.defaultH);

  with (addAction(preload)) {
    method = center;
    sound = 'window2';
    onComplete = preloader.init;
    enqueue();
  }  

  queue.fire();

}

function deconstructor() {
  windowObjects = null;
  contentManager = null;
  preloader = null;
  soundManager.deconstructor();
}

function debug(msg) {
/*
  var o = document.createElement('div');
  o.innerHTML = msg;
  document.getElementById('wDebug').appendChild(o);
*/
}

function ContentManager() {
  var self = this;
  this.xmlhttp = null;
  this.sections = [];

  if (window.ActiveXObject) {
    try {
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      this.xmlhttp = null;
    }
  } else if (window.XMLHttpRequest) {
    try {
      this.xmlhttp = new XMLHttpRequest();
      // this.xmlhttp.overrideMimeType("text/xml"); // So mozilla doesn't crash (unsupported in Safari.)
    } catch(e) {
      this.xmlhttp = null;
    }
  }

  this.readystatechangeHandler = function() {
    if (self.xmlhttp.readyState == 4) {
      if (self.onloadHandler) {
        self.onloadHandler();
      }
    }
  }

  this.load = function(url,className) {
    if (this.xmlhttp) {
      try {
        document.getElementById('portfoliocontent').getElementsByTagName('div')[1].innerHTML = '';
        url = url.toString();
        if (url.indexOf('.html')+1) {
          blurPortfolio();
        } else {
          focusPortfolio(); // shift focus
        }
        // URL hack (so domain is the same if browsing from http://scottschiller.com)
        if (window.location.toString().indexOf('www.')<0) {
          url = url.replace('www.','');
        }
        this.xmlhttp.open('GET',url,true); // "true" - don't hold up browser
        this.xmlhttp.onreadystatechange = this.readystatechangeHandler;
        this.xmlhttp.setRequestHeader('Content-Type', 'text/html');
        this.xmlhttp.send(null);
      } catch(e) {
        // something blew up - d'oh!
        return true;
      }
    } else {
      // no XMLHTTP support.
      return true;
    }
    return false;
  }
  

  this.onloadHandler = function() {
    c = document.getElementById('portfoliocontent').getElementsByTagName('div')[1];
    c.innerHTML = self.xmlhttp.responseText;
  }

  this.assignHandlers = function(id) {
    // intercept onClick and load via XMLHTTP where supported
    var links = document.getElementById(id).getElementsByTagName('a');
    for (i=0; i<links.length; i++) {
      links[i].onmouseover = this.linkMouseoverHandler; 
      links[i].onclick = links[i].href.toString().indexOf('#')<0?this.linkClickHandler:this.toggleClickHandler;
    }
  }

  this.linkClickHandler = function() {
    soundManager.play(null,'select');
    this.blur();
    return self.load(this,this.className);
  }

  this.linkMouseoverHandler = function() {
    soundManager.play(null,'mouseover0');
  }

  this.toggleClickHandler = function() {
    toggleDiv(this);
    soundManager.play(null,'click0');
    return false;
  }

  this.assignHandlers('portfolio');
  this.assignHandlers('featured');

}

function SoundManager(container) {
  // DHTML-controlled sound via Flash
  var self = this;
  this.o = []; // movie references
  this.container = container;
  this.unsupported = 0; // assumed to be supported

  this.FlashObject = function(url) {
    this.movie = null;
    this.movieContainer = document.createElement('div');
    this.movieContainer.className = 'movieContainer';
    var htmlIE = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"><param name="movie" value="'+url+'"><param name="quality" value="high"></object>';
    var htmlMoz = '<embed src="'+url+'" width="1" height="1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>';
    if (navigator.appName.toLowerCase().indexOf('microsoft')+1) {
      this.movieContainer.innerHTML = htmlIE;
      this.movie = this.movieContainer.getElementsByTagName('object')[0];
    } else {
      this.movieContainer.innerHTML = htmlMoz;
      this.movie = this.movieContainer.getElementsByTagName('embed')[0];
    }
    var tmp = document.getElementsByTagName('div')[0];
    tmp.style.position = 'absolute';
    tmp.style.left = '-255px';
    tmp.appendChild(this.movieContainer);
  }

  this.addMovie = function(name,url) {
    this.o[name] = new this.FlashObject(url);
  }

  this.play = function(flashName,label,volume) {
    if (this.unsupported) return false;
    if (!flashName) flashName = this.defaultName;
    oTmp = this.o[flashName].movie;
    if (!oTmp) {
      window.status = 'SoundManager.play(): could not find movie named '+flashName+'.';
      return false;
    }
    volume = (volume>=0?volume:50);
    // window.status = 'play(): '+flashName+','+label+','+volume;
    try {
      if (oTmp && (oTmp.ReadyState == 4 || oTmp.PercentLoaded() == 100)) {
        oTmp.TGotoLabel("/"+label,"start");
        oTmp.TPlay("/"+label);
        this.playFrame(flashName,label,'v'+volume); // ,'v'+volume);
      } else {
        // movie not fully loaded
      }
    } catch(e) {
      // Something blew up. Not supported?
      window.status = 'SoundManager.play(): Exception. Sound will be disabled.';
      setTimeout("window.status=''",3000);
      this.unsupported = 1;
      return false;
    }

  }

  this.playFrame = function(flashName,label,volume) {
    this.o[flashName].movie.TGotoLabel('/'+label,volume);
  }

  this.deconstructor = function() {
    self.o = null;
    self.movie = null;
  }

  // constructor - create flash objects

  if (navigator.appName.indexOf('msie')+1 && navigator.platform.indexOf('mac')+1) {
    this.unsupported = 1;
  }

  this.defaultName = 'default';

  if (!this.unsupported) {
    this.addMovie(this.defaultName,'intro/audio.swf');
  }

}

function SoundManagerNull(container) {
  // Null object for unsupported case
  var self = this;
  this.o = [];
  this.container = null;
  this.unsupported = 1;
  this.FlashObject = function(url) {}
  this.addMovie = function(name,url) {}
  this.play = function(flashName,label,volume) {}
  this.playFrame = function(flashName,label,volume) {}
  this.deconstructor = function() {}
  this.defaultName = 'default';
}

function preInit() {
  // Add CSS for script-enabled browsers - no script = unstyled content (fair enough in this case IMO.)
  var h = document.getElementsByTagName('head')[0];
  var css = document.createElement('link');
  css.rel = 'stylesheet';
  css.type = 'text/css';
  css.href = 'intro/css/base.css';
  h.appendChild(css);
  soundManager = new SoundManager();
  if (!soundManager) {
    soundManager = new SoundManagerNull(); // gotcha for IE:mac
  } else {
    window.onbeforeunload = deconstructor; // IE only (flash crash fix?)
  }
}

// basic ie:mac detection (originally referenced by WebPad)
var naV = navigator.appVersion.toLowerCase();
var isIE = naV.indexOf('msie')+1;
var isMac = naV.indexOf('mac')+1;

if (isIE && isMac) {
  // ie:mac fix case - DHTML->Flash API unsupported
  // reassign sound manager object
  SoundManager = SoundManagerNull;
}

function blurPortfolio() {
  if (!portfolioState) return false;
  portfolioState = null;
  // simple window references
  var main = windowObjects[0];
  var news = windowObjects[1];
  var portfolio = windowObjects[2];
  var featured = windowObjects[3];
  var portfolioContent = windowObjects[4];
  var preload = windowObjects[5];

  with (addAction(news)) {
    // empty action for onComplete
    method = resizeTo;
    // need to disable portfolioContent scroll manually (because of simultaneous enqueue)
    onComplete = function() {windowObjects[4].disableScroll();}
    enqueue();
  }

  with (addAction(news)) {
    w = 215;
    method = resizeTo;
    sound = 'window0';
    enqueue();
  }

  with (addAction(portfolioContent)) {
    w = screenX-(8 + pWidth + 10 + 215 + 10)-8; // -60; (logo)
    x = 8 + pWidth + 10 + 215 + 10;
    y = 18; //18 au lieu de 32 = top col-?
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(featured)) {
    w = 215;
    // h = 145;
    center = false;
    method = resizeTo;
    enqueueSimultaneous();
  }

  with (addAction(featured)) {
    h = (screenY-32-60)-110-58-10-40; // 145
    center = false;
    method = resizeTo;
    sound = 'window2';
    enqueue(); //32 au lieu de 72 60= haut col-2 APRES HOME
  }

  with (addAction(news)) {
    h = (58+250-100);
    center = false;
    method = resizeTo;
    enqueueSimultaneous();
  }

  queue.onComplete = function() {
    for (var i=0; i<windowObjects.length; i++) {
      windowObjects[i].enableScroll();
    }
    portfolioState = null;
  }

  queue.fire();

}