/** Pour les script FireFox **/
function errorMsg(name,ext,cat)
{
  // alert("Netscape 6 or Mozilla is needed to install a sherlock plugin");
  f=document.createElement("form");
  f.setAttribute("name","installform");
  f.setAttribute("method","post");
  f.setAttribute("action","http://mycroft.mozdev.org/error.html");
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","name");
  fe.setAttribute("value",name);
  f.appendChild(fe);
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","ext");
  fe.setAttribute("value",ext);
  f.appendChild(fe);
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","cat");
  fe.setAttribute("value",cat);
  f.appendChild(fe);
  document.getElementsByTagName("body")[0].appendChild(f);
  if (document.installform) {
    document.installform.submit();
  } else {
    location.href="http://mycroft.mozdev.org/error.html"; //hack for DOM-incompatible browsers
  }
}

function addEngine(name,ext,cat)
{
  if ((typeof window.sidebar == "object") && (typeof
  window.sidebar.addSearchEngine == "function"))
  {
    //cat="Web";
    //cat=prompt("In what category should this engine be installed?","Web")
    window.sidebar.addSearchEngine(
      "http://www.megatom.info/Script/"+name+".src",
      "http://www.megatom.info/Script/"+name+"."+ext,
      name,
      cat );
  }
  else
  {
    errorMsg(name,ext,cat);
  }
}

/** gestion des cookies **/
function fctGetCookie (name) {
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i = 0;
 while (i < clen){
   var j = i + alen;
   if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
   i = document.cookie.indexOf(" ", i) + 1;
   if (i == 0) break;
 }
 return null;
 }

function getCookieVal (offset) {
 var endstr = document.cookie.indexOf (";", offset);
 if (endstr == -1)
   endstr = document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
 }

function fctSetCookie (name,value,expires,path,domain,secure) {
 document.cookie = name + "=" + escape (value) +
 ((expires) ? "; expires=" + expires.toGMTString() : "") +
 ((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}

function fctDeleteCookie (name,path,domain) {
 if (fctGetCookie(name)) {
 document.cookie = name + "=" +
 ((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
 }
}

//pointeur vers l'objet boite selectionne pour le deplacement
var selectedBoite = null;
var maxZIndex = 0;
var posX = 0;
var posY = 0;

/**
 * Objet boite
 * Prend en entree un element HTML
 * Active le Drag&Drop sur l'element
 *
 * domNode : pointeur vers l'element HTML associe
 * setPos(x,y) : repositionne l'element HTML à la position donnee
 */
var boite = function(domNode){
    this.domNode = domNode;
    var _this=this;

    this.domNode.onmousedown = function(pEvent){
        this.className += ' selected';
        this.style.zIndex = maxZIndex++;
        selectedBoite = _this;

		// donc tous sauf IE
		  if( pEvent ){
			var x = pEvent.pageX;
			var y = pEvent.pageY;
		  }
		  else{
			var x = event.clientX;
			var y = event.clientY;
		  }

		  posX = x - parseInt(this.style.left,10);
		  posY = y - parseInt(this.style.top,10);
        return false;
    }

    this.domNode.onmouseup = function(){
        this.className = this.className.split('selected')[0];
        selectedBoite = null;
        return false;
    }

    this.domNode.onselectstart = function(){return false;}
    this.domNode.ondragstart = function(){return false;}

    this.setPos = function(x,y){
        this.domNode.style.left=x+'px';
        this.domNode.style.top=y+'px';

        var lexpire = new Date();
        lexpire.setFullYear(2020);
        lexpire.setDate(31);
        lexpire.setMonth(11);

        //sauvegarde la position de la boite en cookie
        fctSetCookie(this.domNode.id,x+'|'+y,lexpire);
    }

}

// methode permettant de chercher tous les elements HTML de type 'strtype' et possedant la class 'strclass'
// exemple : getElementsByClassName('div','boite') => retourne toutes les DIV de class 'boite'
function getElementsByClassName(strtype,strclass){
    var tabElems = new Array();
    var elems = document.getElementsByTagName(strtype);
    for(var i=0;i<elems.length;i++){
        if(elems[i].className.indexOf(strclass) > -1)
            tabElems.push(elems[i]);
    }
    return tabElems;
}

// sur la onload de la page, cherche toutes les boites et instancie l'objet JS 'boite'
// repositionne chaque boite à sa dernière position dans la session
self.onload = function fctonload(){
    var boites = getElementsByClassName('div','boite');
    for(var i=0;i<boites.length;i++){
        //creation de l'objet boite
        var b = new boite(boites[i]);

        //repositionne la boite depuis les valeurs stockees en cookie
        var pos = fctGetCookie(boites[i].getAttribute('id'));
        if(pos){
            var tab = pos.split('|');
            b.setPos(tab[0],tab[1]);
        }
    }
}

//gere le Drag&Drop d'une boite si une boite est selectionnee
document.onmousemove = function fctmousemove(pEvent){
    if(!selectedBoite) return;

    // donc tous sauf IE
      if( pEvent ){                     // Dans ce cas on obtient directement la position dans la page
        var x = pEvent.pageX;
        var y = pEvent.pageY;
      }
      else{                      // Dans ce cas on obtient la position relative à la fenêtre d'affichage
        var x = event.clientX;
        var y = event.clientY;
      }

    selectedBoite.setPos(x-posX,y-posY);
}

