/**
*
*  Crossbrowser Drag Handler
*  http://www.webtoolkit.info/
*
**/

stopEvent = function() {
  return false;
}

var DragHandler = {


	// private property.
	_oElem : null,


	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;

		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();

		return oElem;
	},


	// private method. Begin drag process.
	_dragBegin : function(e) {
		document.onmousewheel = stopEvent;
		var oElem = dragged = DragHandler._oElem = this;

		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

//    if(oElem.mouseX > (parseInt(oElem.style.left)+25) &&
//      oElem.mouseX < (parseInt(oElem.style.left)+parseInt(oElem.style.width)-25) &&
//      oElem.mouseY > (parseInt(oElem.style.top)+25) &&
//      oElem.mouseY < (parseInt(oElem.style.top)+parseInt(oElem.style.height)-25)) {

        oElem.dragBegin(oElem, x, y);

        var link = parseInt(oElem.getAttribute("link"));

        if(link > 0) {


//          oElem.elements = document.getElementsByName(link);
          f = 0;
          oElem.elements = Array();
          $("div[link='"+$(oElem).attr("link")+"']").each(function(){
            oElem.elements[f] = this;
            f++;
          });


        } else {
          oElem.elements = false;
        }

        document.onmousemove = DragHandler._drag;
        document.onmouseup = DragHandler._dragEnd;
//    }
		return false;
	},


	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;
    var stickElem = document.getElementById("pic_"+oElem.getAttribute("y")+"_"+oElem.getAttribute("x"));

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
    stickElem.style.left = ( x + (e.clientX - oElem.mouseX) - 0 )+ 'px';
		stickElem.style.top = ( y + (e.clientY - oElem.mouseY) - 0 )+ 'px';

    for (i=0; i < oElem.elements.length; i++ ){
    var el = oElem.elements[i];
      if(el.id != oElem.id) {
        var xAlt = parseInt(el.style.left) + (e.clientX - oElem.mouseX) + 'px';
        var yAlt = parseInt(el.style.top) + (e.clientY - oElem.mouseY) + 'px';
        var pic = document.getElementById("pic_"+el.getAttribute("y")+"_"+el.getAttribute("x"));
        pic.style.left = xAlt;
        pic.style.top = yAlt;
        el.style.left = xAlt;
        el.style.top = yAlt;
      }
    }

		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.drag(oElem, x, y);

		return false;
	},


	// private method. Stop drag process.
	_dragEnd : function() {
		document.onmousewheel = null;
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		oElem.dragEnd(oElem, x, y);

		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
		dragged = null;
	}

}

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
  var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
  var oCurrent;
  var oAttribute;
  for(var i=0; i<arrElements.length; i++){
    oCurrent = arrElements[i];
    oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
    if(typeof oAttribute == "string" && oAttribute.length > 0){
      if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
      arrReturnElements.push(oCurrent);
    }
    }
  }
  return arrReturnElements;
}