// Juan Matias de la Camara Beovide
// TJF - Tiger JavaScrip Framework
// Abril 2008

// Este archivo contiene al objeto TJF_Drag

var TJF_Drag = new Object;

TJF_Drag.DEBUG = false;

TJF_Drag.is_dragging  = false;
TJF_Drag.drag_object  = null;
TJF_Drag.offsetX	  = 0;
TJF_Drag.offsetY	  = 0;
TJF_Drag.previous_cursor = 'default';
TJF_Drag.on_x;
TJF_Drag.on_y;
TJF_Drag.orig_offset= null;

TJF_Drag.Handlers = { onmousedown: null, onselectstart: null, onmouseover: null};

TJF_Drag.DEFAULTS = {
  on_x: true,
  on_y: true,
  min_x: undefined,
  min_y: undefined,
  max_x: undefined,
  max_y: undefined,
  onmove: null,
  relative_offset: false,
  offset_left: 0,
  offset_top: 0
};

TJF_Drag.options = {};


TJF_Drag.start = function(obj, e, options) {

  e = events.getEvent(e);
  
  if ( options )
    TJF_Drag.options = Extend(options, TJF_Drag.DEFAULTS);
  else
    TJF_Drag.options = Extend({}, TJF_Drag.DEFAULTS);


  // Deshabilitamos la seleccion sobre el document, para ello primero guardamos los handlers viejos y luego seteamos los nuevos
  TJF_Drag.Handlers.onmousedown   = document.onmousedown;
  TJF_Drag.Handlers.onmouseover   = document.onmouseover;
  TJF_Drag.Handlers.onselectstart = document.onselectstart;

  document.onmousedown	  = function() { return false; }
  document.onmouseover	  = function() { return false; }
  document.onselectstart  = function() { return false; }
  

  
  

  // hay un error en el codigo...
  if ( TJF_Drag.is_dragging ) {
    if ( TJF_Drag.DEBUG && console ) { console.warning('opps.. intentando comenzar a draguear un objeto sin haber finalizado uno antes'); }
    TJF_Drag.stop();
  }

  TJF_Drag.offsetX = ( e.offsetX ) ? e.offsetX : e.layerX;
  TJF_Drag.offsetY = ( e.offsetY ) ? e.offsetY : e.layerY;


  //console.log('offsetX=' + TJF_Drag.offsetX + ' offsetY=' + TJF_Drag.offsetY);

  TJF_Drag.is_dragging  = true;
  TJF_Drag.drag_object  = obj;

  //TJF_Drag.previous_cursor = obj.style.cursor;

  obj.style.cursor = 'move';

  if ( TJF_Drag.DEBUG && console ) { console.log("TJF_Drag.is_dragging=" + TJF_Drag.is_dragging + " TJF_Drag.drag_object=" + TJF_Drag.drag_object.aaa + " cursor=" + TJF_Drag.previous_cursor ); }
}

TJF_Drag.stop = function() {

  // hay un error en el codigo...
  if ( ! TJF_Drag.is_dragging ) {
    if ( TJF_Drag.DEBUG && console ) { console.error('error en el codigo.. intentando finalizar a draguear un objeto sin haber comenzado uno antes'); }
    return;
  }

  // Habilitamos la seleccion sobre el document, para ello recuperamos los handlers viejos y luego los seteamos
  document.onmousedown	  = TJF_Drag.Handlers.onmousedown;
  document.onmouseover	  = TJF_Drag.Handlers.onmouseover;
  document.onselectstart  = TJF_Drag.Handlers.onselectstart;

  TJF_Drag.drag_object.style.cursor = TJF_Drag.previous_cursor;

  TJF_Drag.is_dragging  = false;
  TJF_Drag.drag_object  = null;

}

// ------------------------------------------------------------ 
// onmousemove
TJF_Drag.onmousemove = function(e) {

  if ( TJF_Drag.is_dragging ) {

    var dragged_object = TJF_Drag.drag_object;

    var new_y;
    var new_x;

/*
    new_y = parseInt(e.clientY) - parseInt(TJF_Drag.offsetY);
    new_x = parseInt(e.clientX) - parseInt(TJF_Drag.offsetX);
*/

    if ( e.pageX ) {
      new_y = parseInt(e.pageY) - parseInt(TJF_Drag.offsetY);
      new_x = parseInt(e.pageX) - parseInt(TJF_Drag.offsetX);
    }
    else {
      new_y = parseInt(e.clientY) + document.body.scrollTop - parseInt(TJF_Drag.offsetY);
      new_x = parseInt(e.clientX) + document.body.scrollLeft - parseInt(TJF_Drag.offsetX);
    }

    
    // si tiene offsets relativos realizamos el ajuste necesario
    if ( TJF_Drag.options.relative_offset ) {
      new_y -= TJF_Drag.options.offset_top;
      new_x -= TJF_Drag.options.offset_left;
      //console.log('new_x=' + new_x + ' new_y=' + new_y  + 'min_x:' + TJF_Drag.options.min_x);
    }


    // veamos is estamos dentro de la zona permitada de dragging
    if ( (!isNaN(TJF_Drag.options.min_x) && new_x<TJF_Drag.options.min_x) ||
	 (!isNaN(TJF_Drag.options.max_x) && new_x>TJF_Drag.options.max_x) ||
         (!isNaN(TJF_Drag.options.min_y) && new_y<TJF_Drag.options.min_y) ||
	 (!isNaN(TJF_Drag.options.max_y) && new_y>TJF_Drag.options.max_y )    )
      return;

    // primero vemos si el objeto implementa el método move(x,y)
    if ( dragged_object.draggeable_move ) {
      dragged_object.draggeable_move(new_x, new_y);
    }
    else {
      if ( TJF_Drag.options.on_y )
	dragged_object.style.top   = new_y + 'px';

      if ( TJF_Drag.options.on_x )
	dragged_object.style.left  = new_x + 'px';
    }

    // avisamos por medio de la función de callback
    if ( TJF_Drag.options.onmove )
      TJF_Drag.options.onmove();

  }

}
// agregamos el listener
document.onmousemove.addListener( TJF_Drag.onmousemove );
// ------------------------------------------------------------ 



// ------------------------------------------------------------ 
// onmouseup
TJF_Drag.onmouseup = function(e) {

  if ( TJF_Drag.is_dragging ) {
    TJF_Drag.stop();
  }
}
// agregamos el listener
document.onmouseup.addListener( TJF_Drag.onmouseup );
// ------------------------------------------------------------ 



// ------------------------------------------------------------ 
// onclick
TJF_Drag.onclick = function(e) {
  //console.log("e.clientX=" + e.clientX + " e.clientY=" + e.clientY);
}
// agregamos el listener
document.onclick.addListener( TJF_Drag.onclick);
// ------------------------------------------------------------ 


TJF_Drag.register = function(object) {
  object.onmousedown = function(e) {
    TJF_Drag.start(object, e);
  }
}

TJF_Drag.unregister = function(object) {
  object.onmousedown = null;
}

/* end of drag code */


