var current_z_index = 0; //Position des objets sur l'axe des Z (x, y, Z)
var mouse_x = 0; //position horisontal de la souri
var mouse_y = 0; //position verticale de la souris
var drag_spred_x = 0; //Ecart entre la position de la souris et le bord de l'objet (x)
var drag_spred_y = 0; //Ecart entre la position de la souris et le bord de l'objet (y)
var mouse_statut = 0;
var id_div = '';
var last_conteneur;
var conteneur_prefix = "conteneur_";

var last_pos_x;
var last_pos_y;


var y_flash = 50;

var test = 0;


function position(evt) { 
if(!evt) evt = window.event;	
 mouse_x = evt.clientX+document.body.scrollLeft; //x_mouse + scrooll
 mouse_y = evt.clientY+document.body.scrollTop;  //y_mouse + scroll


  if(mouse_statut)
  {
  //alert(mouse_statut);
  document.getElementById(id_div).style.position = 'absolute';
     

  
     

     
  document.getElementById(id_div).style.top = mouse_y - drag_spred_y;
  document.getElementById(id_div).style.left = mouse_x - drag_spred_x;
  if(id_div) {
  get_new_position(id_div, 0);
  }
}
  
} 

document.onmousemove = position; 



function set_dragging(id)
{
  document.onselectstart = function() { return false; };
  pos_div = FindPos(document.getElementById(id))

  drag_spred_x = mouse_x  - pos_div['X'];
  drag_spred_y = mouse_y  - pos_div['Y']
  
    last_pos_x = pos_div['X'];
    last_pos_y = pos_div['Y']



 set_to_top(id);

  
 
  mouse_statut = 1;
  id_div = id;
 
  
}

function save_pos(id)
{
   //var pos_box = FindPos(document.getElementById('temp_box'));

pos = get_new_position(id, 1);


  mouse_statut = 0;
  id_div = null;
  document.onselectstart = function() { return true; };

}

function save_pos_file(id)
{
   var pos_box = FindPos(document.getElementById('temp_box'));
   if((mouse_x >= pos_box['X'] && mouse_x <= pos_box['X'] + 100) && (mouse_y >=pos_box['Y'] && mouse_y <= pos_box['Y'] + 170))
   {
    add_file_in_box_list(id);
   
   }
   else { 
    document.getElementById(id).style.top = last_pos_y;
    document.getElementById(id).style.left = last_pos_x;
   
   }
  mouse_statut = 0;
  id_div = null;
  document.onselectstart = function() { return true; };
}


function set_to_top (id)
{
  current_z_index++;
  document.getElementById(id).style.zIndex = current_z_index;  
}

function FindPos(AObject)

{
    var posX = 0, posY = 0;
    do
    {
        posX += AObject.offsetLeft;
        posY += AObject.offsetTop;
        AObject = AObject.offsetParent;
    }

    while( AObject != null );

    var pos = [];
    pos['X'] = posX;
    pos['Y'] = posY;
  
    return pos;
}

function range_div()
{
var nb_div = 5;
var i = 1;
var spred_div;
var flash_menu = FindPos(document.getElementById('flash_menu'));

spred_div = flash_menu['Y'] + y_flash;

while(document.getElementById('d_' + i + ''))
{
  
  document.getElementById('d_' + i + '').style.position = 'absolute';
  document.getElementById('d_' + i + '').style.top = 170*i-170+spred_div;
  document.getElementById('d_' + i + '').style.left = screen.width-270;
  i++;
  

}
}

function add_file_in_box_list(id_file)
{
    document.getElementById(id_file).style.position = 'absolute';
  document.getElementById(id_file).style.left = last_pos_x;
  document.getElementById(id_file).style.top = last_pos_y;
  var exp=new RegExp("&","g");
//  var exp2=new RegExp("\+","g");
  id_file = id_file.replace(exp, ";amp;");
//  id_file = id_file.replace(exp2, ";plus;");
  
  document.getElementById('temp_box').innerHTML = send_req_ajax("ajax/add_file_in_box_list.php?id_file=" + URLEncode(id_file) +"");


}

function get_new_position(id, statut)
{
  var id_i = 1;
  var id_str;
  var id_str_to_save;
  id_str = "conteneur_" + id_i + "";
  var surligned = 0;
  var is_placed = 0;
  
  pos_of_dragging = FindPos(document.getElementById(id));
  
  
  while(document.getElementById(id_str))
  {

    pos = FindPos(document.getElementById(id_str));
    if(pos_of_dragging['X'] >= pos['X'] && pos_of_dragging['X'] <= pos['X'] + 300 && pos_of_dragging['Y'] >= pos['Y'] && pos_of_dragging['Y'] <= pos['Y'] + 100)
    {
      
      if(last_conteneur) {
        unselect(last_conteneur);
      }
      surligned = 1;
     surligner(id_str);
    
      last_conteneur = id_str;
      if(statut)
      {
    //  find_element_in_space(pos['X'], pos['Y'], id);
      set_position(id, pos['X'], pos['Y']);
      id_str_to_save = id_str;
      
       resize(id_str, id);
      is_placed = 1;
      
      if(last_conteneur) {
      unselect(id_str);
      }
    
      }

    }
    
    if(!surligned && last_conteneur) {
    
      unselect(last_conteneur);
    }
   
    id_i++;
    id_str = "conteneur_" + id_i + "";
     
  }
  if(!is_placed && statut)
  {
    set_position(id, last_pos_x, last_pos_y);
    unselect(last_conteneur);
  
  }
  if(statut) { 
  
  save_pos_box(id, id_str_to_save);
  
  return true; }
}


function set_position(id, posx, posy)
{
document.getElementById(id).style.position = 'absolute';
      document.getElementById(id).style.top = posy
      document.getElementById(id).style.left = posx;
      

}

function surligner(id)
{

  document.getElementById(id).style.border = '1px dashed #C40000';

}

function unselect(id)
{
  document.getElementById(id).style.border = 'none';
  document.getElementById(id).className = 'position_box_empty';
}

function  resize(resize_him, id_source)
{
  var new_x = document.getElementById(id_source).offsetWidth;
  var new_y = document.getElementById(id_source).offsetHeight;
  
  document.getElementById(resize_him).style.width = new_x;
  document.getElementById(resize_him).style.height = new_y;
  return 1;
    
  
}

function save_pos_box(id, id_str)
{

  send_req_ajax("/ajax/save_pos_box.php?id_box=" + id + "&position=" + id_str + "");
  return false;

}

function find_element_in_space(x, y, id_div)
{
  var box_id = 1;
  var pos_box;
  var r_box;
  
  while(r_box = document.getElementById(box_id))
  {
    pos_box = FindPos(r_box);
    
    if(is_in_interval(x, pos_box['X'], y, pos_box['Y']))
    {
      document.getElementById('test').value = box_id;
      
    }
    
    else {
     
    }
    box_id++;
  }
  
return 0;

}

function is_in_interval(xa,xb, ya, yb)
{
  if(xa >= xb - 20  &&  xa <= xb + 20)
  {
  
  
    if(ya >= yb  &&  ya <= yb + 20)
    {
      
    return true;
    }
  
  }
  
  document.getElementById('test').value= ""+xa+"-"+xb+"-"+ya+"-"+yb+"";
  
  return false;

}
