var ImageBoxDragIndex = Array();

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) {
		var oElem = 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;

		oElem.dragBegin(oElem, x, y);

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


	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;

		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';

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

		oElem.drag(oElem, x, y);

		return false;
	},


	// private method. Stop drag process.
	_dragEnd : function() {
		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;
	}

}

// Add this code to use the indexOf function
// It works in all browsers
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function loadImage(myID, filename, w, h) {
	
	if (ImageBoxDragIndex.indexOf(myID) == -1) {
		
		document.getElementById("imageBox").innerHTML += '<div id="'+myID+'" onmouseover="moveMe(\''+myID+'\')" onClick="moveMe(\''+myID+'\')" style="width:'+(w)+'px; height:'+(h+37)+'px; background-color:#EEE; padding:5px; position:absolute; top:'+(getYOffset() + 200)+'px; left:200px; border:1px solid #006; cursor:move"><table width="100%" border="0" cellspacing="0" cellpadding="0" > <tr><td style="width:'+w+'px; height:'+(h+15)+'px; background-image:url('+filename+'); background-repeat:no-repeat;"></td></tr><tr><td style="padding:3px" align="center"><div style="width:100%; height:20px; cursor:pointer"" onClick="closeMe(\''+myID+'\')"><img src="/_flexishell/_images/buttons/ico_close_exp.png" /></div></td></tr></table></div>';
		document.getElementById(myID).style.zIndex = ImageBoxDragIndex.length + 1;

		
		
		
		ImageBoxDragIndex.push(myID);
		//moveMe(document.getElementById(myID));
	} 
	//alert (ImageBoxDragIndex.length);
}

function moveMe(myID) {
	var moveElement = DragHandler.attach(document.getElementById(myID));
	
	//alert (ImageBoxDragIndex.length);
	var ind = 1;
	var ele;
	for (var i = 0; i < ImageBoxDragIndex.length; i++) {
		if (ImageBoxDragIndex[i] != myID) {
			ele = document.getElementById(ImageBoxDragIndex[i]).style;
			ele.zIndex = ind;
			ele.backgroundColor = "#EEE";
			
			//alert(document.getElementById(ImageBoxDragIndex[i]));
			ind++;
		}
	}
	document.getElementById(myID).style.zIndex = ind;
	document.getElementById(myID).style.backgroundColor = "#d4d4d4";
	
	
}

function closeMe(myID) {
	var removeIndex =  ImageBoxDragIndex.indexOf(myID);
	ImageBoxDragIndex.splice(removeIndex,1);
	document.getElementById("imageBox").removeChild(document.getElementById(myID));
}

function getYOffset() {
    var pageY;
    if(typeof(window.pageYOffset)=='number') {
       pageY=window.pageYOffset;
    }
    else {
       pageY=document.documentElement.scrollTop;
    }
    return pageY;
}






















