/* ouvre les liens absolus dans une nouvelle fenêtre */
var galw = new Array();
var galh = new Array();
var gall = 0;

function initialize () {
  var anchorList = $$("a[href]");
  var pelicule = $('pelicule');
  anchorList.each(function (anchor) {
    var href = anchor.readAttribute("href");
    if (!href) {
      return;
    } else if (pelicule && anchor.descendantOf(pelicule)) {
      anchor.writeAttribute("href", "javascript:changeGaleryPhoto('" + href + "')");
      var params = href.toQueryParams();
      var cur = 1 * params.photo;
      galw[cur] = params.w;
      galh[cur] = params.h;
      if (gall < cur) {
        gall = cur;
      }
    } else if (pelicule && href.startsWith("galeri")) {
      if (anchor.descendantOf($('prePhoto')) || anchor.descendantOf($('suiPhoto'))) {
        anchor.writeAttribute("href", "javascript:changeGaleryPhoto('" + href + "')");
      }
    } else if (href.startsWith("../images") || href.startsWith("http://images")) {
      anchor.writeAttribute("href", "javascript:openNewPhoto('" + href + "')");
    } else if (href.startsWith("com-")) {
      anchor.writeAttribute("href", "javascript:opencom('" + href + "')");
    } else if (href.endsWith("statuts.html") || href.endsWith("ROI.html")) {
      anchor.writeAttribute("href", "javascript:opencom('" + href + "')");
    } else if (href.startsWith("http://")) {
      anchor.writeAttribute("target", "_blank");
    }
  });
  var areaList = $$("area[href]");
  areaList.each(function (anchor) {
    var href = anchor.readAttribute("href");
    if (!href) {
      return;
    } else if (href.startsWith("http://")) {
      anchor.writeAttribute("target", "_blank");
    }
  });
  if (location.host.startsWith("sfl.")) {
    var counterBloc = $("counter");
    if (counterBloc) {
      counterBloc.setStyle({display: "none"});
    }
  }
  var careerList = $$("span.shortCareer");
  careerList.each(function (career) {
    career.observe('mouseover', showCareer);
    career.observe('mouseout', hideCareer);
  });
  var mailList = $$("span.email");
  mailList.each(function (span) {
    var mailto = span.firstChild.data;
    span.update('<a href="mailto:' + mailto + '">' + mailto + '</a>');
  });
  // retour au début de la page en cliquant sur l'image gotoTop
  var topImg = $("gotoTop");
  if (topImg) {
    topImg.observe('mouseover', gotoTopMouseOver);
    topImg.observe('mouseout', gotoTopMouseOut);
  }
}

Event.observe(document, 'dom:loaded', initialize);

/* * * * * * * * * * * * * * * * * * * * * * *
 * script pour le retour en haut de la page. *
 * * * * * * * * * * * * * * * * * * * * * * */

var top_on = new Image(10,15);
top_on.src = '../topOn.png';
var top_off = new Image(10,15);
top_off.src = '../topOff.png';

function gotoTopMouseOver () {
  this.src = top_on.src;
}

function gotoTopMouseOut () {
  this.src = top_off.src;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * *
 * script pour afficher le parcours d'une joueuse. *
 * * * * * * * * * * * * * * * * * * * * * * * * * */

function showCareer () {
  this.firstDescendant().setStyle("visibility: visible");
}

function hideCareer () {
  this.firstDescendant().setStyle("visibility: hidden");
}

/* * * * * * * * * * * * * * * * * * * * * * * * *
 * script pour l'ouverture de nouvelle fenêtre.  *
 * * * * * * * * * * * * * * * * * * * * * * * * */

function opencom (comurl) {
  window.open(comurl, 'commentaire',
  'height=550,width=770,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
}

function openNewPhoto (href) {
  var params = href.toQueryParams();
  href = href.replace(/\?/,"&").replace(/:/,"%3A").gsub(/\//,"%2F").gsub(/ /,"%20");
  var loc = '/popup-photo.php?src=' + href;
  var scb = 'no';
  var w = params.w;
  var h = params.h;
  if (w > (screen.width - 10)) {
    scb = 'yes';
    w = screen.width - 10;
  }
  if (h > (screen.height - 40)) {
    scb = 'yes';
    h = screen.height - 40;
  }
  var param = 'width=' + w + ',height=' + h + ',top=' + (((screen.height-h)/2)-20) + ',left=' + ((screen.width-w)/2) +
          ',toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=' + scb + ',resizable=no,copyhistory=no';
  window.open(loc, '', param);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * *
 * script pour changer les photos dans les galeries. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * */

function changeGaleryPhoto (href) {
  var params = href.toQueryParams();
  href = href.substring(0, href.indexOf("?"));
  var cur = 1 * params.photo;
  var team = params.t;
  var gal = params.gal;
  var filename = "0" + cur;
  filename = filename.substring(filename.length - 2);
  var imgElem = $$('#mainPhoto img').first();
  var altAttr = imgElem.readAttribute("alt");
  var altAttr = altAttr.substring(0, altAttr.indexOf(" "));
  var imgCode = "<img src=\"../images/" + gal + team + "/" + filename + ".jpg\" alt=\"" + altAttr + " " + cur + "\" width=\"" + params.w + "\" height=\"" + params.h + "\">";
  $('mainPhoto').update(imgCode);
  imgElem = $$('#prePhoto img').first();
  altAttr = imgElem.readAttribute("alt");
  if (cur > 1) {
    var idx = cur - 1;
    imgCode = "<a href=\"javascript:changeGaleryPhoto('" + href + "?t=" + team + "&gal=" + gal + "&photo=" + idx +
      "&amp;w=" + galw[idx] + "&amp;h=" + galh[idx] + "')\"><img src=\"../preAct.png\" alt=\"" + altAttr + "\" width=\"24\" height=\"24\"></a>";
  } else {
    imgCode = '<img src="../preInact.png" alt="' + altAttr + '" width="24" height="24">';
  }
  $('prePhoto').update(imgCode);
  imgElem = $$('#suiPhoto img').first();
  altAttr = imgElem.readAttribute("alt");
  if (params.last == "1") {
    imgCode = '<img src="../suiInact.png" alt="' + altAttr + '" width="24" height="24">';
  } else {
    var idx = cur + 1;
    imgCode = "<a href=\"javascript:changeGaleryPhoto('" + href + "?t=" + team + "&gal=" + gal + "&photo=" + idx +
      "&amp;w=" + galw[idx] + "&amp;h=" + galh[idx];
    if (idx == gall) {
      imgCode += "&amp;last=1";
    }
    imgCode += "')\"><img src=\"../suiAct.png\" alt=\"" + altAttr + "\" width=\"24\" height=\"24\"></a>";
  }
  $('suiPhoto').update(imgCode);
  $('pelicule').scrollLeft = 132 * (cur - 1) - 333;
}

/* * * * * * * * * * * * * * * * * * * * * *
 * script pour les statistiques chez Xiti. *
 * * * * * * * * * * * * * * * * * * * * * */

function xiti (page) {
  var hsd = document;
  var hsi = '<a href="http://www.xiti.com/xiti.asp?s=57774"><img width="39" height="25" src="http://logv13.xiti.com/hit.xiti?s=57774&amp;p=' + page;
  var hsh = new Date();
  hsi += '&amp;hl=' + hsh.getHours() + 'x' + hsh.getMinutes() + 'x' + hsh.getSeconds();
  if (parseFloat(navigator.appVersion)>=4) {
    var hss = screen;
    hsi += '&amp;r=' + hss.width + 'x' + hss.height + 'x' + hss.pixelDepth + 'x' + hss.colorDepth;
  }
  hsi += '&amp;ref=' + hsd.referrer.replace('&', '$') + '" alt="Mesurez votre audience"></a>';
  hsd.writeln(hsi);
}

/* * * * * * * * * * * * * * * * * * * * *
 * script pour l'affichage du compteur.  *
 * * * * * * * * * * * * * * * * * * * * */

function showCounter () {
  if (location.hostname != 'www.standard-femina.be') {
    document.writeln('compteur');
  } else {
    document.writeln('<img src="http://lescgi.hebergement-discount.com/cgi-bin/Count.cgi?df=ju28952&amp;ft=0&amp;dd=B&amp;reload=N&amp;expire=3600&amp;md=6" alt="compteur">');
  }
}
