﻿var _usingFireFox = false;
var _dialogMouseOver = false;
var _currentDraginDialog = null;
var _currentDialogId = null;

//This var is used to store the original HTML markup before showing the dialog
//and when its closed set the orignal HTML value to it so that way
//reset all its changed values
var _originalDialogHTML = null;

/*
* Shows the Dialog to the user
*/
function ModalDialog_ShowDialog( divId )
{
    var div = document.getElementById( divId );
    _currentDialogId = divId;
	_originalDialogHTML = div.innerHTML;
    
    var left = ModalDialog_PosLeft() + ( (ModalDialog_PageWidth() - div.offsetWidth) / 2 ) - 12;
    if ( left < 0 )
        left = 0;
    var top = ModalDialog_PosTop() + ( (ModalDialog_PageHeight() - div.offsetHeight) / 2 ) - 12;
    if ( top < 0 )
        top = 0;
    
    div.style.left = left + 'px';
    div.style.top = top + 'px';
    
    div.style.visibility = 'visible';
} // ModalDialog_ShowDialog

/*
* Hide the Dialog
*/
function ModalDialog_HideDialog( divId )
{
    var div = document.getElementById( divId );
    _currentDialogId = null;
	div.innerHTML = _originalDialogHTML;
	_originalDialogHTML = null;
    
    div.style.visibility = 'hidden';
} // ModalDialog_HideDialog

/*
* Handle the mouse down event
*/
function ModalDialog_MouseDown( e ) 
{
    if ( _dialogMouseOver )
    {
        _currentDraginDialog = document.getElementById( _currentDialogId );

        if ( _usingFireFox) 
        {
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else 
        {
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
} // ModalDialog_MouseDown

/*
* Handle the mouse move event
*/
function ModalDialog_MouseMove(e) 
{
    if ( _currentDraginDialog ) 
    {
        if ( _usingFireFox ) 
        {
            _currentDraginDialog.style.top = ( e.pageY - Y ) + 'px';
            _currentDraginDialog.style.left = ( e.pageX - X ) + 'px';
            return false;
        }
        else 
        {
            var left = ModalDialog_PosLeft() + event.clientX - X + document.body.scrollLeft;
            if ( left < 0 )
                left = 0;
            var top = ModalDialog_PosTop() + event.clientY - Y + document.body.scrollTop;
            if ( top < 0 )
                top = 0;
            
            _currentDraginDialog.style.pixelLeft = left;
            _currentDraginDialog.style.pixelTop = top;
            
            return false;
        }
    }
} // ModalDialog_MouseMove

/*
* Handle the mouse up event
*/
function ModalDialog_MouseUp() 
{
    _currentDraginDialog = null;
} // ModalDialog_MouseUp

/*
* Handles the mouse events
*/
function ModalDialog_HandleMouseEvents()
{
    // Check for the correct browser
    if ( document.all )
        _usingFireFox = false;
    else
        _usingFireFox = true;

    //Wire up the mouse events
    if ( _usingFireFox ) 
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);

    //Attach handlers
    document.onmousedown = ModalDialog_MouseDown;
    document.onmousemove = ModalDialog_MouseMove;
    document.onmouseup = ModalDialog_MouseUp;
} // ModalDialog_HandleMouseEvents

/*
* Returns the page width
*/
function ModalDialog_PageWidth() 
{
    return window.innerWidth != null ? window.innerWidth: 
        document.documentElement && document.documentElement.clientWidth ? 
        document.documentElement.clientWidth:document.body != null? document.body.clientWidth: null;
} // ModalDialog_PageWidth

/*
* Returns the page height
*/
function ModalDialog_PageHeight() 
{
    return window.innerHeight != null? window.innerHeight: 
        document.documentElement && document.documentElement.clientHeight ? 
        document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
} // ModalDialog_PageHeight

/*
* Returns the current left position
*/
function ModalDialog_PosLeft() 
{   
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && 
        document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? 
        document.body.scrollLeft:0;
} // ModalDialog_PosLeft

/*
* Returns the current top position
*/
function ModalDialog_PosTop() 
{
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && 
        document.documentElement.scrollTop? document.documentElement.scrollTop: 
        document.body.scrollTop?document.body.scrollTop:0;
} // ModalDialog_PosTop

//Handle the mouse events
ModalDialog_HandleMouseEvents();