// **********************************************************************
//
// Javascript St-fanémule 2009 at www.stfanemule.fr
//
// contact me at sir_bline@hotmail.com
//
// **********************************************************************
//
// if you want to learn a Prototype lib is freely distributable under the
// terms of an MIT-style license. see the next link for more informations
//
// http://script.aculo.us/
// search dojo or scriptaculous to learn
//
// *************************************
//
// NOTICE: You may use this code for any purpose, commercial or 
// private, without any further permission from me. You may 
// remove this notice from your final code if you wish, however we 
// would appreciate it if at least the web site address is kept. 
// 
// You may *NOT* re-distribute this code in any way except through its 
// use. That means, you can include it in your product, or your web 
// site, or any other form where the code is actually being used. You 
// may not put the plain javascript up on your site for download or 
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them 
// to the URL instead. 
// 
// Please DO NOT link directly to this .js files from your site. Copy 
// the files to your server and use them there. Thank you.
//
// thx to Denis Howlett for this notice i have been searching for
// ten eras for how to explicite it.. :/ - tired huh -
//
// *********************************************************************

// -----------------------------
// just replace getElementById
// by a $ - dojo function!
//------------------------------

function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

// and use this for the debugs ->
// $("debug").innerHTML;

// ----------------------
// return the user infos
// ----------------------

var langAr = new Array();
var welcomeTitles = new Array();
var lgTitles = new Array();

var userAgent = navigator.userAgent; var uAName = navigator.appName;
var uAVendor = window.navigator.vendor; var uAVersion = navigator.appVersion;
var lang = navigator.language;

function navType() {
 if(uAName=="Microsoft Internet Explorer") { uAVersion = uAVersion.substring(uAVersion.indexOf("MSIE ",5),uAVersion.indexOf(";",uAVersion.indexOf("MSIE ",5))); uAVersionN = uAVersion.substring(5,9); lang = navigator.systemLanguage != "fr" ? "En" : "fr"; }
 if(uAName=="Netscape") { uAName = "Mozzilla"; uAVersion = (uAVersion.substring(0,uAVersion.indexOf("(",0))); }
 if(uAVendor=="Apple Computer, Inc.") { uAName = "Safari"; lang = lang != "fr-FR" ? "En" : "fr"; }
 if(uAName=="Opera") { uAVersion = uAVersion.substring(0,uAVersion.indexOf("(",0)); }
 if(navigator.vendor=="Google Inc.") { uAName = navigator.vendor+" Chrome"; }
}

function translate() {

 langAr['0'] = "Fr";
 langAr['1'] = "En";

 welcomeTitles['0'] = "St-fanémule un site Atariste";
 welcomeTitles['1'] = "St-fanémule an Atarist website";

 lgTitles['0'] = "::: Atari St-fanémule ::: Entrez Sunnyvale où entrez Replicant mais vous n'en ressortirez pas! Bienvenue sur un site Atariste...";
 lgTitles['1'] = "::: Atari St-fanémule ::: Enter Sunnyvale or enter Replicant but you will never return! Welcome on an Atarist website...";

 lang = lang != "fr" ? "En" : "Fr";

}

// -----------------------
// display the divs
// -----------------------

function displayGamesList(parentLayer,childLayer) {
  for(loopParentsLayers=1;loopParentsLayers<=26;loopParentsLayers++)
   returnDspl("parentGamesLayer_",loopParentsLayers,parentLayer);
  for(loopChildLayers=1;loopChildLayers<=43;loopChildLayers++)
   returnDspl("childGamesLayer_",loopChildLayers,childLayer);
}

function dispSimpleDvs(DvId,linkId,maxIds) {
  for(loopLinks=1;loopLinks<=maxIds;loopLinks++)
   returnDspl(DvId,loopLinks,linkId);
}

function displayNamedDivs(div,cache) {
 if(!false) {
  if(div=="settingsDiv") {
   if(uAName=="Safari"||uAName=="Google Inc. Chrome") {
    $(div).style.position = "absolute"; $(div).style.marginLeft = "28%"; }
   $("settingsResult").innerHTML = "";
  } else if(div=="votesDiv") {
   $("voteResult").innerHTML = voteForm;
   $("nResult").innerHTML = "";
  } else if(div=="dlDiv") {
   url = "http://www.stfanemule.fr/modules/googleAdd.aj.php?size=200x200"; result = file(url);
   $("googleAdd").innerHTML=result; }
  if(div=="trashDiv") {
   bgSnd("stDiv");
  } else {
   if($(cache).style.display == "block")
    bgSnd("closeDiv");
   else
    bgSnd("openDiv");
  }
  return(returnDspl(cache,""),returnDspl(div,""));
 }
}

function openTopicsList() {
  for(loopTr="1";loopTr<="10";loopTr++)
   if(loopTr!="1"&&loopTr!="2"&&loopTr!="6"&&loopTr!="7")
    returnDspl("topicListDisplayed_",loopTr);
  return false;
}

function openTitlesMen(close) {
 for(i=1;i<=3;i++)
  if(i!=close)
   $("top_men_"+i).style.display = "none";
 returnDspl("top_men_",close);
 return false;
}

function returnDspl(Dv,nm,forNm) {
  if(!forNm) {
   $(Dv+nm).style.display = $(Dv+nm).style.display == "block" ? "none" : "block";
   return false;
  } else {
   return(false,$(Dv+nm).style.display = nm == forNm ? "block" : "none"); }
}

// -----------------------------------------------
// display the ftp alerts like trash, files dirs
// or ftp forms
// -----------------------------------------------
// class are necessary for the child nodes or
// attributes and style directly inside  the html
// will not be returned a fps can be added but
// not very usefull... just cut the "//" in the
// 2nd function
// -------------------

// now 1rst set vars and load the element to display
// an auto value will be set for the next function
// and the timerId is used to stop the setInterval

//$("debug").innerHTML = $("debug").innerHTML+xInc+ " - "+Yh+" - ";

var myFps = 10;
var timerID;

function ftpToDirs(eleDv,X,Y) {

 if(eleDv && X && Y){ xInc = "100";

  $("cache").style.display = "block";
  node = $(eleDv);
  node.style.display = "block";
  node.firstChild.style.display = "none";

  XSize = node.offsetWidth; YSize = node.offsetHeight;
  Xa = X; Ya = Y;

  Yi = parseInt(Ya)+(parseInt(YSize)/parseInt(2));
  Xi = parseInt(Xa)+(parseInt(XSize)/parseInt(2));

  auto = true;

 }

 if(auto==true) {
  if(uAName=="Safari"||uAName=="Microsoft Internet Explorer") {
   node.firstChild.style.display = "block";
   bgSnd("stDiv");
   auto = false;
   return;
  } else { return launchFtpAlert(); }
 } else { return; }
}

// next calculate the properties and stop the functions or
// apply the element its new values - i didn't find usefull
// than X and Y maths vars for the vars sorry...
// so take care of the type of the X and Y values or they
// will be concateneted as a string chain

function launchFtpAlert() {

 if(auto==true) {

  Yr = parseInt(Yi)-parseInt(Ya);
  Yn = parseInt(Ya)+((parseInt(xInc)*parseInt(Yr))/parseInt(100));
  Yh = (parseInt(Yi)-parseInt(Yn))*parseInt(2);

  Xr = parseInt(Xi)-parseInt(Xa);
  Xn = parseInt(Xa)+((parseInt(xInc)*parseInt(Xr))/parseInt(100));
  Xw = (parseInt(Xi)-parseInt(Xn))*parseInt(2);

// if(xInc==100) { myFps = XSize < "1500" ? Math.ceil(parseInt(500)/parseInt(Yr)) : "1"; }

   if(xInc=="0") { node.style.width = parseInt(Xw)+parseInt(2)+"px";
                   node.firstChild.style.display = "block";
                   auto = false;
                   clearInterval(timerID);
                   bgSnd("stDiv"); return;
   } else { return returnSize(); }
 } else { return; }
}

// now we can set the properties break
// the function if the Inc is ended

function returnSize() {

 if(auto==true) {

  node.style.width = Xw+"px"; node.style.height = Yh+"px";
  node.style.marginLeft = Xn+"px"; node.style.marginTop = Yn+"px";

  if(xInc>"0") { xInc--; return timerID = setTimeout("ftpToDirs()",10);
  } else { return; }
 }
}


// -------------------
// write the bbcode
// -------------------

function bbcode(code) {
  if(uAName=="Mozzilla") { var chaine = document.selection.createRange().text;
    if(chaine) { var sel = document.selection.createRange();
      sel.text = "[" + code + "]" + chaine + "[/" + code + "]";
    } else { $("article").value += "[" + code + "]Votre adresse ici[/" + code + "]"; }
  } else {
    champ = $("article");
    val = $("article").value;
    valDeb = val.substring(0, champ.selectionStart);
    valFin = val.substring(champ.selectionEnd, champ.textLength);
    valSel = val.substring(champ.selectionStart, champ.selectionEnd);
    champ.value = valDeb + "[" + code + "]" + valSel + "[/" + code + "]" + valFin;
  }
}

function addSmile(smiley) {
  if(uAName=="Mozzilla") { var chaine = document.selection.createRange().text;
    if(chaine) { var sel = document.selection.createRange(); sel.text = "[" + smiley + "]";
    } else { $("article").value += "[" + smiley + "]"; }
  } else {
    champ = $("article");
    val = $("article").value;
    valDeb = val.substring(0, champ.selectionStart);
    valFin = val.substring(champ.selectionEnd, champ.textLength);
    valSel = val.substring(champ.selectionStart, champ.selectionEnd);
    champ.value = valDeb + "[" + smiley + "]" + valFin;
  }
}

// -------------------------
// open a file
// -------------------------

function file(fichier) {
 if(window.XMLHttpRequest) // firefox
  xhr_object = new XMLHttpRequest();
 else if(window.ActiveXObject) // ie
  xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
 else
  return(window.alert(simpleErno));
 xhr_object.open("GET", fichier, false);  xhr_object.send(null);
 if(xhr_object.readyState == 4)
  return(xhr_object.responseText);
 else
  return(window.alert(simpleErno));
}

// -------------------------
// change the vote value
// and display the result
// -------------------------

function voteGame() {

 if($("vote_2").checked) { message = voteBon; note = "bon";
 } else if($("vote_1").checked) { message = voteMoyen; note = "moyen";
 } else if($("vote_0").checked) { message = voteMauvais; note = "mauvais";
 } else { return(window.alert(simpleErno)); }

 divWaitAlert = "voteResult"; isMarginLeft = "100"; isMarginTop = "63";
 initWaitAlert("1");

 url = "http://www.stfanemule.fr/modules/index.aj.php?titre=" + gameId + "&note=" + note; result = file(url);
 if(result != "") {
  initWaitAlert("0");
  if(result=="previously_voted") {
   $("voteResult").innerHTML = cantVote;
  } else {
   initWaitAlert("0");
   $("voteResult").innerHTML = message; $("nResult").innerHTML = "<center>"+isNew+result+"</center>"; $("resultatVote").innerHTML = result; }
  return(bgSnd("isSubmited"));
 } else {
  initWaitAlert("0");
  return(window.alert(simpleErno)); }
}

// -------------------------
// download a game
// -------------------------

function dlGame(dl,view,id) {
 view++; url = "http://www.stfanemule.fr/modules/index.aj.php?download="+dl+"&titre="+id; result = file(url);
 if(result != "") {
  window.open(result);
  $("dlButton").disabled = "disabled";
  return($("nbrDl").innerHTML = view+" fois");
 } else {
  return(window.alert(simpleErno)); }
}

// -------------------------
// turn the music on/off
// -------------------------

function turnMusic() {
 url = "http://www.stfanemule.fr/modules/index.aj.php?turn_music=1"; result = file(url);
 if(result != "") {
  return($("musicPlayer").innerHTML=result);
 } else {
  return(window.alert(simpleErno)); }
}

// -------------------------
// change the template
// (anywhere on the website)
// -------------------------

function turnTemplate() {
 url = "http://www.stfanemule.fr/modules/index.aj.php?template=invert"; result = file(url);
 if(result != "") {
  isClass = isClass == "1" ? "0" : "1";
  $("body").className = isClass == "1" ? "" : "body_1";
  return($("musicPlayer").innerHTML = result);
  if(ieInf7 != "_ie_inf_7")
   $("corps").className = isClass == "1" ? "corps" : "corps_1";
 } else {
  return(window.alert(simpleErno)); }
}

// ------------------------
// display a waiting alert
// ------------------------

function initWaitAlert(onOff) {
 if(onOff==true) {
  autoWait = true;
  waitingAlert();
 } else {
  countActions = "0";
  return(autoWait=false); }
}

var waitAlPts = "0";

function waitingAlert() {
 if(autoWait==true) {
  if(waitAlPts=="3") {
   waitAlPts = "0";
  } else {
   waitAlPts++; }
  begTxt = "<div style=\"margin-left:" + isMarginLeft + "px;margin-top:" + isMarginTop + "px;\">" + txt; endTxt = "</div>";
  pts = ".";
  for(isPt="0";isPt<waitAlPts;isPt++) {
   pts = pts+"."; }
  document.getElementById(divWaitAlert).innerHTML = begTxt+pts+endTxt;
  return(isWaiting = setTimeout("waitingAlert()",400));
 } else {
  return false; }
}

// ----------------------
// count down the time
// before they can make
// any action
// ----------------------

var t = 8;

function dlTimer() { t = t-1;
 if(t > 0) { $("dlTimer").innerHTML = t;
 } else { $("dlTxt").innerHTML = "vous pouvez maintenant télécharger";
          $(cacheDl).style.display = "none"; }
 if(t > 0) { setTimeout("dlTimer()",1000); return false; }
}


// ----------------------
// display server time
// ----------------------

function showHeurre() {
 var d = new Date(); var minutes = d.getMinutes(); var secondes = d.getSeconds();
 if(d.getMinutes() < 10)
  var minutes = "0" + d.getMinutes();
 if(d.getSeconds() < 10)
  var secondes = "0" + d.getSeconds();
 $("hol").innerHTML = d.getHours() + ":" + minutes + ":" + secondes + " Gmt";
 return(setTimeout("showHeurre()",1000));
}


// ----------------------
// change music for the
// juke box and scroll
// the music title
// ----------------------

function loadJuke(idMus,titleMus,LenMus,NumMus) {
 scroolTextLen = LenMus; musNum = NumMus; scroolNews = titleMus; scroolNews = musNum + ". " + scroolNews + " - Atari St games musics - " + musNum + ". " + scroolNews;
 return($("player").innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"http://www.stfanemule.fr/design/player_mp3_maxi.swf\" width=\"188\" height=\"20\"><param name=\"movie\" value=\"http://www.stfanemule.fr/design/player_mp3_maxi.swf\" /><param name=\"FlashVars\" value=\"mp3=http://www.stfanemule.fr/design/musiques/"+ idMus +"&amp;autoplay=1&amp;slidercolor2=007100&amp;slidercolor1=00f200&amp;sliderovercolor=009900&amp;loadingcolor=00ff00&amp;buttoncolor=00ff00&amp;buttonovercolor=00aa00\" /></object>");
}

function setJuke() {
 document.getElementById("scroll_banner").value = scroolNews.substring(scroolPos,scroolPos); scroolPos++;
 if (scroolPos==scroolTextLen)
  scroolPos = 0;
  return(setTimeout("setOut()",500));
}

// ----------------------
// display a scrolling
// text in an input
// ----------------------

var scroolPos = 0;

function setOut() {
 document.getElementById("scroll_banner").value = scroolNews.substring(scroolPos,scroolPos+160); scroolPos++;
 if (scroolPos==scroolTextLen)
  scroolPos = 0;
  return(setTimeout("setOut()",360));
}

// ------------------------
// set the links bgsounds
// ------------------------

function bgSnd(action) {
 url = "http://www.stfanemule.fr/modules/bgSounds.aj.php?bgSound="+action; resultSnd = file(url);
 if(resultSnd != "")
  return($("hvrSnds").innerHTML = resultSnd);
 else
  return(window.alert(simpleErno));
}


// -----------------------
// scroll banner for ie
// users -> to firefox
// all is % do not try to
// add px it bugs -
// but try it to know! ^^
// and ie display the scroll
// much slowly than others...
// -----------------------

var Y = 1;
var Yn = "false";
var testIt;
var nbRebonds = "false";
var InitRbd = false;

// ie6 et 7 -> document.documentElement.clientHeight
// ie 4 et 5 -> document.body.clientHeight

function testFirefox() {

// first calculate the heights
// and width -> %!!!
// and an init pos

 if(Yn=="false") {
  eleHeight = $("test_firefox").offsetHeight;
  eleWidth = $("test_firefox").offsetWidth;
  clientHeight = document.documentElement.clientHeight;
  clientWidth = document.documentElement.clientWidth;
  eleHeight = IaN(((eleHeight*100)/clientHeight)+((clientHeight*4)/100));
  eleWidth = IaN((eleWidth*100)/clientWidth);
  Yn = IaN(0-eleHeight);
  Yi = IaN(50-(eleHeight/2));
  Xi = IaN(50-(eleWidth/2));
  $("test_firefox").style.marginLeft = Xi+"%";
  nY = IaN(Yi/Y); lastPosInit = IaN(nY*Y);
  auto = true; }

// all vars are set so display
// the box

 if(auto==true) {

  if(Yn<Yi&&InitRbd==false) {
   if(Yn==IaN(lastPosInit-1)) {
    Ya = Yi;
    lastPos = lastPosInit;
    i = 0;
    nbRebonds = 5; }
   Yn+=IaN(Y);
   return(returnPos("test_firefox",Yn));

// all else is to go up
// and down to the final
// Y pos - we have our last
// Y stocked so just build
// a Yn that can move to the
// lastPos...

  } else {
   if(nbRebonds>1) {
    if(Yn==lastPos) {
       if(InitRbd==false) {
        upDwn = 2;
        InitRbd = true; }
        floatUpDwn = parseInt(upDwn)/parseInt(2);
       if(floatUpDwn == Math.round(IaN(upDwn/2))) {
        nbRebonds--;
        lastPos = IaN(Ya-(nbRebonds*3));
        i+=IaN(3);
        upDwn = 3;
        Yn-=IaN(Y);
       } else {
        lastPos = lastPosInit;
        upDwn = 2;
        Yn+=IaN(Y); }
       return(returnPos("test_firefox",Yn));
    } else {
     if(floatUpDwn == Math.round(IaN(upDwn/2))) {
      Yn-=IaN(Y);
     } else {
      Yn+=IaN(Y); }
     return(returnPos("test_firefox",Yn)); }
   } else {
    clearInterval(testIt);
    return(Yn="false",auto=false); }
  }
 }
}

// and display the box...

function returnPos(Dv,Pos) {
 $(Dv).style.marginTop = Pos+"%";
 clearInterval(testIt);
 return(testIt=setInterval("testFirefox()",1));
}

// ---------------------------
// open or close the firefox
// alert
// ---------------------------

function ieToFirefoxContinue(openClose,isSiteIndex,whLang) {
 whL = whLang.toLowerCase();
 if(openClose=="open") {
  url = "http://www.stfanemule.fr/modules/index.aj.php?lang="+whL+"&firefox_alert=display";
  result = file(url);
  if(result!="") {
   document.body = document.body+result;
   return(testFirefox());
  } else {
   $("firefoxAlertDiv").style.display = "none";
   $("corps").style.display = "visible"; }
 } else {
  if($("checkFirefox").checked) {
   url = "http://www.stfanemule.fr/modules/index.aj.php?no_more_firefox_alert=no_display";
   result = file(url); }
   $("firefoxAlertDiv").innerHTML = "";
   $("firefoxAlertDiv").style.display = "none";
   if(isSiteIndex=="Index") {
    $("corps").style.visibility = "visible"; }
   return(result);
 }
}

// ----------------------
// set the index values
// ----------------------

function homeSettings() {
 navType();
 translate();
 langHome(lang);
 setOut();
 $("settingsResult").innerHTML = "";
 if(uAName=="Microsoft Internet Explorer") {
  if(uAVersionN<7) {
   $("settingsDiv").style.marginTop = "-400px";
  }
  $("corps").style.visibility = "visible";
  $("settingsDiv").style.marginLeft = "-18%";
 } else {
 $("corps").style.visibility = "visible"; }
 return false;
}

// -------------------------
// switch template
// at the index
// -------------------------

function turnSettings(isToTurn,varVal) {
 divWaitAlert = "settingsResult"; isMarginLeft = "90"; isMarginTop = "60";
 initWaitAlert("1");
 url = "http://www.stfanemule.fr/modules/index.aj.php?"+ isToTurn +"=" + varVal; result = file(url);
 if(result != "") {
  initWaitAlert("0");
  return($("settingsResult").innerHTML = result);
 } else {
  initWaitAlert("0");
  return(window.alert(simpleErno)); }
}

function langHome(isNlang) {

 divWaitAlert = "settingsResult"; isMarginLeft = "90"; isMarginTop = "60";
 initWaitAlert("1");

 for(i=0;i<1;i++) { varVal = langAr[i] == isNlang ? i : "1"; }

 url = "http://www.stfanemule.fr/modules/index.aj.php?lang_home="+varVal; result = file(url);

 lang = isNlang;

 if(result != "") {
  initWaitAlert("0");
  $("settingsResult").innerHTML = result;
 } else {
  initWaitAlert("0");
  return(window.alert(simpleErno)); }

 for(i=0;i<=1;i++) {
  document.title = langAr[i] == isNlang ? lgTitles[i] : document.title;
  $("welcomeTxt"+langAr[i]).style.display = langAr[i] == isNlang ? "block" : "none";
  $("linkRep"+langAr[i]).style.display = langAr[i] == isNlang ? "block" : "none";
  $("linkRick"+langAr[i]).style.display = langAr[i] == isNlang ? "block" : "none";
  $("choiceTitle"+langAr[i]).style.display = langAr[i] == isNlang ? "block" : "none";
  $("titleWelcome").innerHTML = langAr[i] == isNlang ? welcomeTitles[i] : $("titleWelcome").innerHTML;
 }

}

// ----------------------
// set the ftp values
// ----------------------

function FTPsettings() {
 navType();
 translate();
}

// ----------------------
// send functions
// ----------------------

function globals() {
 navType();
 translate();
 setOut();
}


