/* preload images */
var imgs=new Array("toto.gif");

this.preLoadImages(imgs);

function preLoadImages(imgs) {
  for (i=0; i<imgs.length; i++) {
    var image=new Image();
    //alert("./images/"+imgs[i]);
    image.src="./images/"+imgs[i];
  }
}

/* test browser */
var DHTML=(document.getElementById || document.all || document.layers);

function init() {
  // center body
  initBody();

  // preload table of content
  initToc();

  // center en_construction.gif
  try {
    imgLeft = (this.getWindowWidth()/2) - 148;
    imgTop = (this.getWindowHeight()/2) - 22;
    var enConstruction = new this.getObj('en_construction');
    enConstruction.style.left = imgLeft+'px';
    enConstruction.style.top = imgTop+'px';
  } catch (e) {}
}

/* to perform while page is loading in order to center the body block */
var bodyBlockHeight=600;
var bodyBlockWidth=1024;

function initBody() {
  if (!DHTML) return;

  /*alert('windowWidth : ' + this.getWindowWidth()
                         + ', windowHeight : '
                         + this.getWindowHeight());*/
  var top=(this.getWindowHeight()-bodyBlockHeight)/2;
  var left=(this.getWindowWidth()-bodyBlockWidth)/2;
  if (top < 0) top=0;
  if (left < 0) left=0;
  //alert('top : ' + top + ', left : ' + left);
  var bodyBlock=new this.getObj('bodyBlock')
  bodyBlock.style.left=left.toString()+'px';
  bodyBlock.style.top=top.toString()+'px';
}

function getWindowWidth() {
  // ie4: screen width : screen.width
  //   4  document.body.clientWidth
  //   4  document.body.offsetWidth
  //   6  document.documentElement.clientWidth

  if (typeof(window.innerWidth)=='number') {
    //Non-IE
    return window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  } else if (document.body && document.body.clientWidth) {
    //IE 4 compatible
    return document.body.clientWidth;
  }
}

function getWindowHeight() {
  if (typeof(window.innerHeight)=='number') {
    //Non-IE
    return window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  } else if (document.body && document.body.clientHeight) {
    //IE 4 compatible
    return document.body.clientHeight;
  }
}

/* init table of content properties and method */
var positions = new Object();
var details_accueil = new Object();
var details_contact = new Object();
var details_agenda = new Object();
var details_bio = new Object();
var details_disco = new Object();
var details_photos = new Object();
var details_page_pro = new Object();

details_accueil["x_begin"]         = 4;
details_accueil["y_begin"]         = 58;
details_accueil["x_end"]           = 30;
details_accueil["y_end"]           = 165;
details_accueil["link"]            = "./index.html";

details_contact["x_begin"]         = 71;
details_contact["y_begin"]         = -6;
details_contact["x_end"]           = 182;
details_contact["y_end"]           = 10;
details_contact["link"]            = "./contact.html";

details_agenda["x_begin"]          = 23;
details_agenda["y_begin"]          = 3;
details_agenda["x_end"]            = 130;
details_agenda["y_end"]            = 60;
details_agenda["link"]             = "./agenda.html";

details_bio["x_begin"]             = 13;
details_bio["y_begin"]             = 50;
details_bio["x_end"]               = 100;
details_bio["y_end"]               = 120;
details_bio["link"]                = "./biographie.html";

details_disco["x_begin"]           = -23;
details_disco["y_begin"]           = 27;
details_disco["x_end"]             = 55;
details_disco["y_end"]             = 130;
details_disco["link"]              = "./discographie.html";

details_photos["x_begin"]          = 12;
details_photos["y_begin"]          = 34;
details_photos["x_end"]            = 130;
details_photos["y_end"]            = 110;
details_photos["link"]             = "./photos.html";

details_page_pro["x_begin"]        = 39;
details_page_pro["y_begin"]        = 0;
details_page_pro["x_end"]          = 135;
details_page_pro["y_end"]          = 40;
details_page_pro["link"]           = "./page_pro.html";

positions["toc_accueil"]      = details_accueil;
positions["toc_contact"]      = details_contact;
positions["toc_agenda"]       = details_agenda;
positions["toc_biographie"]   = details_bio;
positions["toc_discographie"] = details_disco;
positions["toc_photos"]       = details_photos;
positions["toc_page_pro"]     = details_page_pro;

function initToc() {
  if (document.getElementById) {
    var x = document.getElementById('toc').getElementsByTagName('IMG');
  } else if (document.all) {
    var x = document.all['toc'].all.tags('IMG');
  } else {
    return;
  }
  var preloads = new Object();
  for (var i=0;i<x.length;i++) {
    //alert('nom: ' + x[i].id + '; index: ' + x[i].id.indexOf('toc'));
    if (x[i].id.indexOf('toc_') != -1) {
      preloads[x[i].id+'_out'] = new Image;
      preloads[x[i].id+'_out'].src = './images/toc/'+ x[i].id + '_out.gif';
      preloads[x[i].id+'_over'] = new Image;
      preloads[x[i].id+'_over'].src = './images/toc/'+ x[i].id + '_over.gif';
      preloads[x[i].id+'_over'].onerror = function () {this.src='./images/default.gif'}
      preloads[x[i].id+'_down'] = new Image;
      preloads[x[i].id+'_down'].src = './images/toc/'+ x[i].id + '_down.gif';
      preloads[x[i].id+'_up'] = new Image;
      preloads[x[i].id+'_up'].src = './images/toc/'+ x[i].id + '_out.gif';
      x[i].onmouseover = function () {this.src=preloads[this.id+'_over'].src;this.style.cursor='pointer';movePetal(this.id,'over')}
      x[i].onmouseout  = function () {this.src=preloads[this.id+'_out'].src;movePetal(this.id,'out')}
      x[i].onmousedown = function () {this.src=preloads[this.id+'_down'].src;}
      x[i].onmouseup   = function () {this.src=preloads[this.id+'_over'].src;}
      x[i].onclick     = function () {location.href=positions[this.id]["link"]}
    }
  }

  // define toc_petale objects positions
  try {
    var home=new this.getObj("toc_accueil");
    home.style.left=positions["toc_accueil"]["x_begin"]+'px';
    home.style.bottom=positions["toc_accueil"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var contact=new this.getObj("toc_contact");
    contact.style.left=positions["toc_contact"]["x_begin"]+'px';
    contact.style.bottom=positions["toc_contact"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var biographie=new this.getObj("toc_biographie");
    biographie.style.left=positions["toc_biographie"]["x_begin"]+'px';
    biographie.style.bottom=positions["toc_biographie"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var agenda=new this.getObj("toc_agenda");
    agenda.style.left=positions["toc_agenda"]["x_begin"]+'px';
    agenda.style.bottom=positions["toc_agenda"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var discographie=new this.getObj("toc_discographie");
    discographie.style.left=positions["toc_discographie"]["x_begin"]+'px';
    discographie.style.bottom=positions["toc_discographie"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var photos=new this.getObj("toc_photos");
    photos.style.left=positions["toc_photos"]["x_begin"]+'px';
    photos.style.bottom=positions["toc_photos"]["y_begin"]+'px';
  } catch (e) {}

  try {
    var page_pro=new this.getObj("toc_page_pro");
    page_pro.style.left=positions["toc_page_pro"]["x_begin"]+'px';
    page_pro.style.bottom=positions["toc_page_pro"]["y_begin"]+'px';
  } catch (e) {}
}

/* get section name from petalId */
function getSectionName(petalId) {
  return petalId.substring(11);
}

/* methods used to retrieve objects according to the browser */
function getObj(name) {
  if (document.getElementById) {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  } else if (document.all) {
    this.obj = document.all[name];
    this.style = document.all[name].style;
  } else if (document.layers) {
    this.obj = document.layers[name];
    this.style = document.layers[name];
  }
}

function displayObj(objId) {
  if (!DHTML) return;

  //alert('display element: ' + objId);
  var myObj = new this.getObj(objId);
  myObj.style.display="block";
}

function hideObj(objId) {
  if (!DHTML) return;

  var myObj = new this.getObj(objId);
  myObj.style.display="none";
}

function swapImage(imgId,imgSrc) {
  if (!DHTML) return;

  var myImg = new Image(this.getObj(imgId));
  //alert('myImg.src: '+myImg.src);
  myImg.src=imgSrc;
  //alert('myImg.src: '+myImg.src);
}

/* methods used to move objects */
var delay=20;
var nb_increment=10;

function moveObject(petalId, _x, _y) {
  if (!DHTML) return;

  var myObj = new this.getObj(petalId);
  myObj.style.left=_x.toString()+'px';
  myObj.style.bottom=_y.toString()+'px';
}

/* methods used to move petales */
var pas_x;
var pas_y;

function getObjX(petalId) {
  if (!DHTML) return;

  var position_x = new this.getObj(petalId).style.left;
  //alert('petal x : ' + position_x);
  if (typeof(position_x)=='number') {
    return position_x;
  } else {
    return Number(position_x.substring(0, position_x.indexOf('p')));
  }
}

function getObjY(petalId) {
  if (!DHTML) return;

  var position_y = new this.getObj(petalId).style.bottom;
  //alert('petal y : ' + position_y);
  if (typeof(position_y)=='number') {
    return position_y;
  } else {
    return Number(position_y.substring(0, position_y.indexOf('p')));
  }
}

//var positions="";

function movePetal(petalId,mouse) {
  if (mouse == 'over') {
    moveTo(petalId, null, null, positions[petalId]["x_end"], positions[petalId]["y_end"], null, null);
  } else if (mouse == 'out') {
    moveTo(petalId, null, null, positions[petalId]["x_begin"], positions[petalId]["y_begin"], null, null);
  } else {
    return;
  }
}

function moveTo(petalId, initial_x, initial_y, final_x, final_y, pas_x, pas_y) {
  if (!DHTML) return;

  /* 1. define current position */
  if (initial_x == null) initial_x = this.getObjX(petalId);
  if (initial_y == null) initial_y = this.getObjY(petalId);
  /* 2. define pas to increment */
  if (pas_x == null) pas_x = (final_x-initial_x)/nb_increment;
  if (pas_y == null) pas_y = (final_y-initial_y)/nb_increment;
  /* 3. move petal to last position if quiet near */
  if ( ( ((final_x-initial_x) >= 0) && ((initial_x+pas_x) >= final_x) )
    || ( ((final_x-initial_x) <= 0) && ((initial_x+pas_x) <= final_x) )
    || ( ((final_y-initial_y) >= 0) && ((initial_y+pas_y) >= final_y) )
    || ( ((final_y-initial_y) <= 0) && ((initial_y+pas_y) <= final_y) ) ) {
    moveObject(petalId,final_x,final_y);
    //alert('positions : ' + positions+"position_x: "+final_x+"position_y: "+final_y);
    return;
  }
  /* 4. move petal to next incremented position */
  moveObject(petalId,(initial_x+pas_x),(initial_y+pas_y));
  //positions=positions+"position_x: "+initial_x+" position_y: "+initial_y+", ";
  /* 5. call current method recursively after delayed */
  setTimeout("moveTo('"+petalId+"',"
                          +(initial_x+pas_x)+","
                          +(initial_y+pas_y)+","
                          +final_x+","
                          +final_y+","
                          +pas_x+","
                          +pas_y+")",delay);
}

