/*
  dojo.require("dojo.dnd.Mover");
  dojo.require("dojo.dnd.Moveable");
  dojo.require("dojo.dnd.move");
  dojo.require("dojox.image.Lightbox");
  dojo.require("dojo.parser");
*/
//  dojo.require("dojo.fx");

function rmPX(text) {
    return parseInt(text.substr(0, text.length-2));

}

function dirtyRelaxation() {
    relaxParams.damper = 0.5;
    relaxParams.doMotion = true;
    ri.length = 0;
}

function relaxItem(id) {
    this.id = id;
    obj = dojo.byId(id);
    this.obj = obj;
    this.width2 = obj.clientWidth/2;
    this.height2 = obj.clientHeight/2;
    this.x = rmPX(obj.style.left) + this.width2;
    this.y = rmPX(obj.style.top) + this.height2;
    this.vx = 0;
    this.vy = 0;
    this.dragged = false;
    this.parent = -1;
    
}

function sign(x) {
    if (x<0) return -1;
    else return 1;
}

  function relax() {
      // build array
     if (ri.length==0) {
	 //	 alert("first");
	 ri = Array();
	 for (var i in pages) if (pages[i].active) ri.push(new relaxItem(i));
	 for (i=0; i<ri.length; i++) {
	     if (pages[ri[i].id].parent!="") {
		 //console.debug(ri[i].id + " <- " + pages[ri[i].id].parent);
		 for (k=0; k<ri.length; k++) {
		     if (ri[k].id == pages[ri[i].id].parent) ri[i].parent = k;
		 }
	      }
	 }
     }
     if (ri.length==0) return;


     for (i=0; i<ri.length; i++) {
	 ri[i].vx *= relaxParams.damper;
	 ri[i].vy *= relaxParams.damper;
	 if (ri[i].dragged) {
	     ri[i].x = rmPX(ri[i].obj.style.left) + ri[i].width2;
	     ri[i].y = rmPX(ri[i].obj.style.top) + ri[i].height2;
	 }
     }

     // EDGE CONTRACTION (parent nodes only)
     for (i=0; i<ri.length-1; i++) {     
	 if (ri[i].parent>=0) {
	     //console.debug(ri[i].id + " <-- " + ri[ri[i].parent].id);
	     parent = ri[ri[i].parent];
	     
	     dx = ri[i].x - parent.x;
	     dy = ri[i].y - parent.y;
	     dx /= ri[i].width2 + parent.width2;
	     dy /= ri[i].height2 + parent.height2;

	     dist = Math.sqrt(dx*dx+dy*dy);
	     dist *= 1.5;
	     
	     ri[i].vx -= dx/dist;
	     ri[i].vy -= dy/dist;
	     parent.vx += dx/dist;
	     parent.vy += dy/dist;
	 }
     }

     
     // BOX REPULSION
     for (i=0; i<ri.length-1; i++) {
	 for (k=i+1; k<ri.length; k++) {
	     dx = ri[i].x - ri[k].x;
	     dy = ri[i].y - ri[k].y;

	     dx /= ri[i].width2 + ri[k].width2;
	     dy /= ri[i].height2 + ri[k].height2;


	     dist = dx*dx+dy*dy;
	     if (dist>10000) continue;
	     if (dist<0.01) {
		 ri[i].vx = ri[k].vy+1;
		 ri[i].vy = -ri[k].vx+1;
		 continue;
	     }
	     dist /= 1;
	     ri[i].vx += dx/dist;
	     ri[i].vy += dy/dist;
	     ri[k].vx -= dx/dist;
	     ri[k].vy -= dy/dist;
	 }
     }

     pane = dojo.byId('pane');
     screenW = pane.clientWidth;
     screenH = pane.clientHeight;
     
     var maxSpeed = 0;

     for (i=0; i<ri.length; i++) {
	 // CENTER ATTRACTION
	 ri[i].vx -= (ri[i].x-screenW/2)/screenW*5;  //*0.005;
	 ri[i].vy -= (ri[i].y-screenH/2)/screenH*5;  // *0.005;
	 // BOUNDARY
	 if (ri[i].x-ri[i].width2<=0) ri[i].vx = Math.max(0, ri[i].vx);
	 if (ri[i].x+ri[i].width2>=screenW) ri[i].vx = Math.min(0, ri[i].vx);
	 if (ri[i].y-ri[i].height2<=0) ri[i].vy = Math.max(0, ri[i].vy);
	 if (ri[i].y+ri[i].height2>=screenH) ri[i].vy = Math.min(0, ri[i].vy);
	 
	 speed = ri[i].vx*ri[i].vx + ri[i].vy*ri[i].vy;
	 if (speed>maxSpeed) maxSpeed = speed;
     }

     if (maxSpeed>0) motionRatio = relaxParams.lastMaxSpeed/maxSpeed-1;
     else motionRatio = 0;

     if (maxSpeed<2 && motionRatio<0) relaxParams.damper -= 0.01;
     if (maxSpeed<0.5 && motionRatio<0) relaxParams.damper -= 0.1;
     if (maxSpeed<0.001) relaxParams.damper = 0;
     if (relaxParams.damper<0) relaxParams.damper = 0;
     if (relaxParams.damper<0.0001) relaxParams.doMotion = false;

     relaxParams.lastMaxSpeed = maxSpeed;

     if (!relaxParams.doMotion) { return; }

      for (i=0; i<ri.length; i++) {
	  if (ri[i].dragged) continue;
	  ri[i].x = ri[i].x + ri[i].vx;
	  ri[i].y = ri[i].y + ri[i].vy;
	 
	  ri[i].obj.style.left = Math.round(ri[i].x - ri[i].width2) + "px";
	  ri[i].obj.style.top  = Math.round(ri[i].y - ri[i].height2) + "px";

      }
      //alert(ri[0].x + ", " + ri[0].obj.style.left);
  }



  function Page( id, x, y, autoload, hue, sat, parent ) { 
      this.id = id; 
      this.x = x; 
      this.y = y; 
      this.active = false;
      this.autoload = autoload;
      this.hue = hue;
      this.sat = sat;
      this.parent = parent;
  } 

  function openLink(e, id, lang) {
      document.location.hash=id;
      if(!e) e=window.event; 
      return loadBoxXY(id, e.clientX, e.clientY, lang);
  }


  function removeNode() {
     this.parentNode.removeChild(this);
  }

  function closebox(id) {
     fadeOut = dojo.fadeOut({node: id,duration: 700});
     dojo.connect(fadeOut,"onEnd",dojo.byId(id), removeNode);
     fadeOut.play();
     pages[id].active = false;
     dirtyRelaxation();
  }

  function showBox(id) {
      fadeIn = dojo.fadeIn({node: id,duration: 500});
      // wipeIn = dojo.fx.wipeIn({node: id,duration: 500});
      // dojo.fx.combine([fadeIn, wipeIn]).play();
      fadeIn.play();
      pages[id].active = true;
      dirtyRelaxation();
  }

  function loadBoxXY(id, xPos, yPos, lang) {

     if(dojo.byId(id)) return false;
     var newDiv = document.createElement("div");
     newDiv.setAttribute("position", "absolute");
     newDiv.setAttribute("id", id);
	  newDiv.style.top = yPos+"px";
	  newDiv.style.left = xPos+"px";
	  newDiv.style.position = "absolute";
	  newDiv.style.opacity = 0;	
	  newDiv.style.filter = "alpha(opacity=0)";

     dojo.xhrGet({
        url: "php/"+id+".php?id="+id+"&lang="+lang,
        load: function(response, ioArgs){
           newDiv.innerHTML = response;
	   dojo.byId('pane').appendChild(newDiv);
	   new dojo.dnd.Moveable(id, {handle: id+'_handle'});
           showBox(id);

           return response;
        },
        error: function(response, ioArgs){
           newDiv.innerHTML = "Error: " + response;
	   dojo.byId('pane').appendChild(newDiv);
	   new dojo.dnd.Moveable(id, {handle: id+'_handle'});
           showBox(id);
           return response;
        },
        handleAs: "text"
     });
     return false;
  }



   // document.getElementById('id').style.left = value+"px";
