﻿//************************************************************************************
// 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 displayWindow(divID, w, h)
{
    var l, t, winW, winH;
    
    if (parseInt(navigator.appVersion)>3)
    {
         if (navigator.appName=="Netscape") {
              winW = window.innerWidth;
              winH = window.innerHeight;
         }
         if (navigator.appName.indexOf("Microsoft")!=-1) {
              winW = document.body.offsetWidth;
              winH = document.body.offsetHeight;
         }
    }

    l = (winW / 2) - (w / 2);
    t = 200;     
    displayFloatingDiv(divID, '', w, h, l, t);
}
		    
//
// dinamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{
    document.write('<div id="dimmer" class="dimmer" style="left:-2000px; top:-2000px; visibility: visible; filter: alpha(opacity=60); width: '+ window.screen.width + 'px; height: '+ window.screen.height + 'px;">&nbsp;</div>');
}
function displayFloatingDiv(divId, title, width, height, left, top) 
{    
	DivID = divId;
	
	document.getElementById('dimmer').style.visibility = "visible";
	document.getElementById('dimmer').style.left = "0px";
	document.getElementById('dimmer').style.top = "0px";

    //document.getElementById(divId).style.width = width + 'px';
    //document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.left = (left+getScrollXY()[0]) + 'px';
    document.getElementById(divId).style.top = (top+getScrollXY()[1]) + 'px';

	//originalDivHTML = document.getElementById(divId).innerHTML;
    
	//document.getElementById(divId).innerHTML = originalDivHTML;	
	document.getElementById(divId).className = 'dimming';
	document.getElementById(divId).style.visibility = "visible";
}


//
//
//
function hiddenFloatingDiv(divId) 
{
	//document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.visibility = "hidden";

	document.getElementById('dimmer').style.visibility = "hidden";
	document.getElementById('dimmer').style.left = "-2000px";
	document.getElementById('dimmer').style.top = "-2000px";

	DivID = "";
}

//
//
//
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();

}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

// call init
init();

