/****************************************************************************
 *  Mixed Media Utilities 
 *  can manually set dragHandlesArray to include additional items
 *  can manually set dragIDsArray to include additional items
 *  MUST INCLUDE widgets_array.js with this file
 ****************************************************************************/

/*
 * Set event handlers
 */
document.onmousedown = startDrag;
document.onmousemove = doDrag;
document.onmouseup = function() { draggingDiv = null;isDrag = false; };

/*
 * Global vars for dragging
 */
var isDrag = false;
var x, y, tx, ty;
var draggingDiv;
var dragHandlesArray = new Array('title', 'titleNoPrintPreview');
var dragIDsArray = new Array('help', 'piecePreview', 'thumbnailPreview', 'addContactsPreCheck');

/*
 * Start drag delegate
 */
function startDrag(e)
{
	if(!e)
		e = window.event;

	var dragTitle = e.target ? e.target : e.srcElement;
	if(dragHandlesArray.inArray(dragTitle.parentNode.className) || dragHandlesArray.inArray(dragTitle.className))
	{
		isDrag = true;
		x = e.clientX;
		y = e.clientY;

        //figure out the draggingDiv here, either help or piecePreview
        findParent(dragTitle);
		tx = parseInt(draggingDiv.style.left);
		ty = parseInt(draggingDiv.style.top);
	}
}

/*
 * Dragging delegate
 */
function doDrag(e)
{
	if(!isDrag)
		return;
		
	if(!e)
		e = window.event;

  	draggingDiv.style.left = e.clientX + (tx - x) + 'px';
	draggingDiv.style.top = e.clientY + (ty - y) + 'px';
}

/*
 * find parent recurses up the tree to find if a parent node hsa the correct id to be dragged
 */
function findParent(obj)
{
    if(dragIDsArray.inArray(obj.id))
        draggingDiv = obj;
    else
        findParent(obj.parentNode);

    return obj;
}

/*
 * returns array (left, top) with positions of obj passed in
 */
function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return [curleft,curtop];
}

/*
 * returns iframe height based on users screen resolution
 */
function getDocHeight() 
{
    var iframe = parent.document.getElementById('mainiframe');
 
    return document.all ? iframe.document.body.offsetHeight : iframe.offsetHeight;

    /*
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
    */
}

/*
 * highlights an image that has the appropriate naming convention [-off, -on].
 */
function doHighlight(image)
{
    image.src = image.src.replace('-off', '-on');
}

/*
 * removes a highlight on an image that has the appropriate naming convention [-off, -on].
 */
function undoHighlight(image)
{
    image.src = image.src.replace('-on', '-off');
}
