//_______________________________________________________________________________________
var _global_DontDrag            = false;
var _global_ElementToDrag       = null;
var _global_MousePos            = [0, 0];
var _global_InitialOffset       = [0, 0];
var _global_InitialDifference   = [0, 0];
var _global_onMouseMove         = null;
var _global_onMouseUp           = null;
    
//_______________________________________________________________________________________
// JS funtion that gets the mouse position Broser independent.
function setMousePos(e)
{
	if(e.pageX || e.pageY)
		_global_MousePos = [e.pageX ,  e.pageY];
	else 
		_global_MousePos = [e.clientX ,e.clientY];
}

//_______________________________________________________________________________________
// This function is fired every time the mouse is moved. It keeps the mouse pos uptodate
// and is only really used when dragging a file.
function mouseMove(e)
{
	// Get the event Browser independent.
	if(e == null || e == 'undefined')
		e = event;
	
	setMousePos(e);
	
	if (_global_onMouseMove != null)
	{
	    _global_onMouseMove();
	}
	
	_MouseMovedSinceStartedScrolling = true;
}

//_______________________________________________________________________________________
function mouseUp(e)
{
    _global_onMouseMove    = null;
    
    if (_global_onMouseUp != null)
        _global_onMouseUp();
        
    _global_DontDrag       = false;
}

//_______________________________________________________________________________________
// Works out the difference between the Object that to be draged top left corner
// and the mouse position.
function CalculateInitialOffset(elementToGetPosOfTemp)
{
    if (elementToGetPosOfTemp != null)
    {
	    var left = 0,
		    top = 0;
    	
	    // Calculate its offset 
	    while (elementToGetPosOfTemp.offsetParent){
		    left += elementToGetPosOfTemp.offsetLeft;
		    top  += elementToGetPosOfTemp.offsetTop;
		    elementToGetPosOfTemp = elementToGetPosOfTemp.offsetParent;
	    }
    	
	    left += elementToGetPosOfTemp.offsetLeft;
	    top  += elementToGetPosOfTemp.offsetTop;
    	
	    // ScrollOffset is must be removed as the content within the div has a differenct pos
	    // to the mouse pos on the screen.
	    _global_InitialOffset[0] = left;
	    _global_InitialOffset[1] = top; // + document.getElementById('SiteContainer').scrollTop;
	    
	    _global_InitialDifference[0] = _global_MousePos[0] - left;
	    _global_InitialDifference[1] = _global_MousePos[1] - top;
	}
}

//_______________________________________________________________________________________
function DontDrag()
{
    _global_DontDrag = true;
}

//_______________________________________________________________________________________
function DragObject()
{
    if (_global_ElementToDrag != null  && _global_DontDrag == false)
	{
	    var left = _global_MousePos[0] - _global_InitialDifference[0];
	    var top = _global_MousePos[1] - _global_InitialDifference[1];
	
		_global_ElementToDrag.style.left = left + 'px';
		_global_ElementToDrag.style.top  = top  + 'px';
	}
}

//_______________________________________________________________________________________
function StartDrag(objectToDrag)
{
    _global_ElementToDrag = objectToDrag;

    CalculateInitialOffset(objectToDrag);
    _global_onMouseMove = DragObject;
    _onMouseMove = DoneDragging();
}

function DoneDragging()
{
    _global_DontDrag = false;
    SetEditStatus();
}


//_______________________________________________________________________________________
document.onmousemove = mouseMove;
document.onmouseup   = mouseUp;


function DateAdd(objDate, strInterval, intIncrement)
{
    if(typeof(objDate) == "string")
    {
        objDate = new Date(objDate);

        if (isNaN(objDate))
        {
            throw("DateAdd: Date is not a valid date");
        }
    }
    else if(typeof(objDate) != "object" || objDate.constructor.toString().indexOf("Date()") == -1)
    {
        throw("DateAdd: First parameter must be a date object");
    }

    if(
    strInterval != "M" && 
    strInterval != "D" && 
    strInterval != "Y" && 
    strInterval != "h" && 
    strInterval != "m" && 
    strInterval != "uM" && 
    strInterval != "uD" && 
    strInterval != "uY" && 
    strInterval != "uh" && 
    strInterval != "um" && 
    strInterval != "us"
    )
    {
        throw("DateAdd: Second parameter must be M, D, Y, h, m, uM, uD, uY, uh, um or us");
    }

    if(typeof(intIncrement) != "number")
    {
        throw("DateAdd: Third parameter must be a number");
    }

    switch(strInterval)
    {
        case "M":
        objDate.setMonth(parseInt(objDate.getMonth()) + parseInt(intIncrement));
        break;

        case "D":
        objDate.setDate(parseInt(objDate.getDate()) + parseInt(intIncrement));
        break;

        case "Y":
        objDate.setYear(parseInt(objDate.getYear()) + parseInt(intIncrement));
        break;

        case "h":
        objDate.setHours(parseInt(objDate.getHours()) + parseInt(intIncrement));
        break;

        case "m":
        objDate.setMinutes(parseInt(objDate.getMinutes()) + parseInt(intIncrement));
        break;

        case "s":
        objDate.setSeconds(parseInt(objDate.getSeconds()) + parseInt(intIncrement));
        break;

        case "uM":
        objDate.setUTCMonth(parseInt(objDate.getUTCMonth()) + parseInt(intIncrement));
        break;

        case "uD":
        objDate.setUTCDate(parseInt(objDate.getUTCDate()) + parseInt(intIncrement));
        break;

        case "uY":
        objDate.setUTCFullYear(parseInt(objDate.getUTCFullYear()) + parseInt(intIncrement));
        break;

        case "uh":
        objDate.setUTCHours(parseInt(objDate.getUTCHours()) + parseInt(intIncrement));
        break;

        case "um":
        objDate.setUTCMinutes(parseInt(objDate.getUTCMinutes()) + parseInt(intIncrement));
        break;

        case "us":
        objDate.setUTCSeconds(parseInt(objDate.getUTCSeconds()) + parseInt(intIncrement));
        break;
    }
    return objDate;
}
