
// Cross browser event handling for IE 5+, NS6+ and Gecko
function addEvent(elm, evType, fn, useCapture){
  if (elm.addEventListener){
    // Gecko
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
     // Internet Explorer
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  } else {
    // nutscrape?
    elm['on' + evType] = fn;
  }
}
function removeEvent(elm, evType, fn, useCapture){
  if (elm.removeEventListener){
    // Gecko
    elm.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.detachEvent){
     // Internet Explorer
    var r = elm.detachEvent('on' + evType, fn);
    return r;
  } else {
    // nutscrape?
    elm['on' + evType] = null;
  }
}


var objCallbacks={};
var objCallbacksCount=0;

function objCallback(obj,method){
  objCallbacks[objCallbacksCount]=obj;
  var f=new Function("return( objCallbacks["+objCallbacksCount+"]['"+method+"'].apply(objCallbacks["+objCallbacksCount+"],arguments));");
  objCallbacksCount++;
  return(f);
}
function objCallbackStr(obj,method){
  objCallbacks[objCallbacksCount]=obj;
  var str="objCallbacks["+objCallbacksCount+"]";
  objCallbacksCount++;
  return(str);
}
function objCallbackRemove(obj){
  for (var i in objCallbacks){
    if (objCallbacks[i]==obj)
      delete objCallbacks[i];
  }
}
function objTimeout(obj,method){
  objCallbacks[objCallbacksCount]=obj;
  var f="objCallbacks["+objCallbacksCount+"]['"+method+"'].apply(objCallbacks["+objCallbacksCount+"]);";
  objCallbacksCount++;
  return(f);
}

var _windows={focusWindow:null,wins:[]};

function windowOnFocus(){
  if (typeof this.edit != 'undefined')
    this.edit.focus();
  this.className="enabled";
/*
  this.style.mozBoxShadow="4px 4px 15px #aab";
  this.style.webkitBoxShadow="4px 4px 15px #aab";
  this.style.msBoxShadow="4px 4px 15px #aab";
  this.style.boxShadow="4px 4px 15px #aab";
  this.style.border="1px solid #999";
  this.style.backgroundColor="#fff";
*/
  if (_windows.focusWindow!=null && _windows.focusWindow!=this)
    _windows.focusWindow.onLoseFocus();
  _windows.focusWindow=this;
}

function windowOnLoseFocus(){
  this.className="disabled";
/*
  this.style.mozBoxShadow="1px 1px 7px #bbb";
  this.style.webkitBoxShadow="1px 1px 7px #bbb";
  this.style.msBoxShadow="1px 1px 7px #bbb";
  this.style.boxShadow="1px 1px 7px #bbb";
  this.style.border="1px solid #bbb";
  this.style.backgroundColor="#fafafa";
*/
}

function windowRegister(w){
  _windows.wins.push(w);
  w.onLoseFocus=windowOnLoseFocus;
  w.onFocus=windowOnFocus;
  w.style.zIndex=_windows.wins.length;
  w.onFocus();
  if (typeof w.edit != 'undefined'){
    w.edit.win=w;
    w.edit.onfocus=function(){ w.onFocus(); }
  }
}

function windowBringToFront(w){
  var zindex=w.style.zIndex;
  for (var i=0; i<_windows.wins.length; ++i){
    if (_windows.wins[i]==w) continue;
    if (_windows.wins[i].style.zIndex>zindex)
      --_windows.wins[i].style.zIndex;
  }
  w.style.zIndex=_windows.wins.length;
  w.onFocus();
}


function moveObjectMMove(e){
  e=e||window.event;
  this.moved=true;
  this.style.left=(this.movStartObject.x + e.clientX - this.movStartX)+"px";
  this.style.top=(this.movStartObject.y + e.clientY - this.movStartY)+"px";
  return(false);
}
function moveObjectMDown(e)
{
  e=e||window.event;

  this.moved=false;
  this.movStartX = e.clientX; this.movStartY = e.clientY;
  this.movStartObject={x:parseInt(this.offsetLeft),y:parseInt(this.offsetTop)};

  this.onmousemove=moveObjectMMove;
  addEvent(document,'mousemove',this.onmousemoveCallback,false);
  addEvent(document,'mouseup',this.onmouseupCallback,false);
  windowBringToFront(this);
  return(true);
}
function moveObjectMUp(e)
{
  e=e||window.event;
  this.onmousemove=null;
  removeEvent(document,'mousemove',this.onmousemoveCallback,false);
  removeEvent(document,'mouseup',this.onmouseupCallback,false);
  if (typeof this.onFocus != 'undefined')
    this.onFocus();
  if (!this.moved)
    return(true); // pass click onto objects in this tag
  return(false);
}

function moveObjectTMove(e){
  e=e||window.event;
  for (var i=0; i<e.touches.length; ++i)
    if (e.touches[i].identifier==this.touchId) break;

  if (i>=e.touches.length)
    return(moveObjectTUp(e));

  var touch=e.touches[i];
  this.style.left=(this.movStartObject.x + touch.clientX - this.movStartX)+"px";
  this.style.top=(this.movStartObject.y + touch.clientY - this.movStartY)+"px";
  return(false);
}
function moveObjectTDown(e)
{
  e=e||window.event;
  if (e.targetTouches.length == 0) return(false);

  var touch=e.targetTouches[0];
  this.touchId=touch.identifier;
  this.movStartX = touch.clientX; this.movStartY = touch.clientY;
  this.movStartObject={x:parseInt(this.style.left),y:parseInt(this.style.top)};

  this.ontouchmove=moveObjectTMove;
  addEvent(document,'touchmove',this.ontouchmoveCallback,false);
  addEvent(document,'touchend',this.ontouchupCallback,false);
  windowBringToFront(this);
  return(false);
}
function moveObjectTUp(e)
{
  e=e||window.event;
  this.ontouchmove=null;
  removeEvent(document,'touchmove',this.ontouchmoveCallback,false);
  removeEvent(document,'touchend',this.ontouchupCallback,false);
  if (typeof this.onFocus != 'undefined')
    this.onFocus();
  return(false);
}




function setMovable(obj)
{
  obj.onmouseupCallback=objCallback(obj,"onmouseup");
  obj.onmousemoveCallback=objCallback(obj,"onmousemove");
  obj.ontouchupCallback=objCallback(obj,"ontouchup");
  obj.ontouchmoveCallback=objCallback(obj,"ontouchmove");

  obj.onmousedown=moveObjectMDown;
  obj.onmouseup=moveObjectMUp;

  obj.ontouchstart=moveObjectTDown;
  obj.ontouchend=moveObjectTUp;
  windowRegister(obj);
}

