//************************************************************************************
// Copyright (C) 2006, Massimo Beatini
//
// This software is provided "as-is", without any express or implied warranty. In 
// no event will the authors be held liable for any damages arising from the use 
// of this software.
//
// Permission is granted to anyone to use this software for any purpose, including 
// commercial applications, and to alter it and redistribute it freely, subject to 
// the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim 
//    that you wrote the original software. If you use this software in a product, 
//    an acknowledgment in the product documentation would be appreciated but is 
//    not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be 
//    misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//************************************************************************************

//
// global variables
//
var isMozilla;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;


function buildDimmerDiv()
{
    document.write('<div id="dimmer" class="dimmer"></div>');	
}


function displayFloatingDiv(divId, title, width, height, left, top) 
{
	DivID = divId;

	var dimmer			= document.getElementById('dimmer');	
	var popup			= document.getElementById(divId)
	var left			= document.body.scrollWidth/2 - width/2;
	var arrayPageSize	= getPageSize();
	
	for (f = 0; f < document.forms.length; f++)
    {		
		if (document.forms[f].id != "MakeAppointment")
		{		
			var elements = document.forms[f].elements;
			// looping through all elements on certain form
			for (e = 0; e < elements.length; e++)
			{				
				if (elements[e].type == "select-one")
				{
					elements[e].style.visibility = 'hidden';
				}
			}
		}
    }

	dimmer.style.width = document.body.scrollWidth;
	dimmer.style.height = arrayPageSize[1];
	dimmer.style.display = "block";

    popup.style.width = width + 'px';
    popup.style.height = height + 'px';
    popup.style.left = left + 'px';
    popup.style.top = top + 'px';

	document.getElementById(divId + '_form').style.display = 'inline';

	var addHeader;
	
	if (originalDivHTML == "")
	    originalDivHTML = popup.innerHTML;
	
	addHeader = '<table style="width:' + width + 'px">' +
	            '<tr><td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px;text-align:center;"></td></tr></table>';
	

    // add to your div an header	
	popup.innerHTML = addHeader + originalDivHTML;

	popup.className = 'dimming';
	popup.style.display = 'block';
}

function hiddenFloatingDiv(divId)
{
	document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.display='none';
	document.getElementById('dimmer').style.display = 'none';	

	for (f = 0; f < document.forms.length; f++)
    {
        var elements = document.forms[f].elements;
        for (e = 0; e < elements.length; e++)
        {
            if (elements[e].type == "select-one")
            {
                elements[e].style.visibility = 'visible';
            }
        }
    }

	document.getElementById(divId + '_result').style.display = 'none';
	document.getElementById(divId + '_form').style.display = 'none';
	
	DivID = "";
	originalDivHTML = "";
}


function MouseDown(e) 
{		
    if (over)
    {
        if (isMozilla) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById(DivID);
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

function MouseMove(e) 
{	
    if (objDiv) {
        if (isMozilla) {
            objDiv.style.top = (e.pageY-Y) + 'px';
            objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
            objDiv.pixelLeft = event.clientX-X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY-Y + document.body.scrollTop;
            return false;
        }
    }
}

function MouseUp() 
{	
    objDiv = null;
}

function init()
{
    // check browser
    isMozilla = (document.all) ? 0 : 1;


    if (isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }

    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;

    // add the div
    // used to dim the page
	buildDimmerDiv();

}

// call init
init();