/****************************************************************************
 *  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', 'pieceOptions', 'printPreview');

/*
 * 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(!draggingDiv)
		return;
		
	if(!e)
		e = window.event;
	
    var maxX = $(window).width() + $(window).scrollLeft() - $(draggingDiv).width() - 10;
    var maxY = $(window).height() + $(window).scrollTop()  - $(draggingDiv).height() - 10;
    
    var left = e.clientX + (tx - x);
    var top = e.clientY + (ty - y);
    
    left = (left < 0 ? 0 : (left > maxX ? maxX : left));
    top = (top < 0 ? 0 : (top > maxY ? maxY : top));
    
  	draggingDiv.style.left = left + 'px';
	draggingDiv.style.top = top + 'px';
	
	return false;
}

/*
 * 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');
}

function scaleThumbs(_identifier, _maxwidth, _maxheight)
{
    var maxWidth = _maxwidth; // Max width for the image
    var maxHeight = _maxheight;    // Max height for the image
    var identifier = _identifier;
    
    if($('.' + identifier).length > 0)
        identifier = '.' + identifier;
    else if($('#' + identifier).length > 0)
        identifier = '#' + identifier;
        
    $(identifier).each(function() 
    {
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
        }

        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }
    });
}
