//************************************************************************************
// 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;
var div_counter = 1000;
var div_array = new Array();


//
// dinamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{
    document.write('<div id="dimmer" class="dimmer" style="width:'+ 0+ 'px; height:' + 0 +'px"></div>');
    
}

//
//
//
function displayFloatingDiv(divId, title, width, height, left, top, form_name) 
{   
  
  
     document.onmousedown = MouseDown;
      document.onmousemove = MouseMove;
      document.onmouseup = MouseUp;

    DivID = divId;
    var tmp_array = new Array();
    if(!form_name)   {
        form_name = '';
    }
    tmp_array[0] = divId;
    tmp_array[1] = form_name;
    div_array[div_array.length] = tmp_array;
    
    document.getElementById('dimmer').style.zIndex = div_counter;
    
    document.getElementById('dimmer').style.visibility = "visible";
    document.getElementById('dimmer').style.width = window.screen.width + 'px'; 
    document.getElementById('dimmer').style.height = (window.screen.height + 200) + 'px'; 
    document.getElementById(divId).style.left = left + 'px';
    document.getElementById(divId).style.top = top + 'px';
    
    document.getElementById(divId).style.zIndex = div_counter;
    document.getElementById(divId).style.display = "block";
    div_counter++;
}


//
//
//
function setDivId(divid)
{
DivID = divid;
}
function hiddenFloatingDiv(divId) 
{
    var tmp_array = div_array;
    div_array = new Array();
    for(i=0;i<tmp_array.length - 1;i++) {
        div_array[i] = tmp_array[i];
    }
   document.getElementById(divId).style.display='none';
    
    document.getElementById('dimmer').style.zIndex = div_counter - 5;
    
    /*if(div_counter <= 1000)    {
        document.getElementById('dimmer').style.visibility = 'hidden';
        document.getElementById('dimmer').style.width = '0px'; 
        document.getElementById('dimmer').style.height = '0px'; 
        alert(div_counter);
    }
    else    {
        alert('==' +div_counter);
        document.getElementById('dimmer').style.visibility = 'hidden';
        document.getElementById('dimmer').style.width = '0px'; 
        document.getElementById('dimmer').style.height = '0px'; 
        document.getElementById('dimmer').style.visibility = "visible";
        document.getElementById('dimmer').style.width = window.screen.width + 'px'; 
        document.getElementById('dimmer').style.height = (window.screen.height + 200) + 'px'; 
        
    }*/
    div_counter--;
    var div_length = div_array.length;
    if(div_length == 0) {
        DivID = "";
        document.getElementById('dimmer').style.visibility = 'hidden';
        document.getElementById('dimmer').style.width = '0px'; 
        document.getElementById('dimmer').style.height = '0px'; 
        showSelectBoxes();
    }
    else    {
        DivID = div_array[div_length - 1][0];
        document.getElementById('dimmer').style.visibility = "visible";
        document.getElementById('dimmer').style.width = window.screen.width + 'px'; 
        document.getElementById('dimmer').style.height = (window.screen.height + 200) + 'px'; 
        showSelectBoxes(div_array[div_length - 1][1]);
    }
    
}

//
//
//
function MouseDown(e) 
{
    
    if (over)
    {
        if (isMozilla) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById(DivID);
            try {
            objDiv = objDiv.style;
            }catch(e){}
            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();


/** 
*  hideSelectBoxes  : This function is used to hide the select boxes
*  Parameters       : 
*       form        : This is the current form. It is passed to skip the hiding of select boxes from the current form.
*  Return Value     : null
*  Compatibility    : Internet Explorer(6.0)&Mozilla Firefox(5.0)
**/

function hideSelectBoxes(form_name)  {
    var all_forms = document.forms;
    for (i=0; i<all_forms.length; i++)  {
        var curr_form = all_forms[i];
        if(form_name != curr_form.name) {
            var all_elements = curr_form.elements;
            for(j=0; j<all_elements.length; j++)    {
                if(all_elements[j].type == 'select-one')    {
                    all_elements[j].style.display = 'none';
                }
            }
        }
    }
}

/** 
*  showSelectBoxes  : This function is used to show the hidden select boxes
*  Parameters       : 
*       form        : This is the current form. It is passed to skip the select boxes from the current form.
*  Return Value     : null
*  Compatibility    : Internet Explorer(6.0)&Mozilla Firefox(5.0)
**/

function showSelectBoxes(form_name)  {
    var all_forms = document.forms;
    for (i=0; i<all_forms.length; i++)  {
        var curr_form = all_forms[i];
        //alert(form_name == curr_form.name);
        if((form_name == curr_form.name) || (!form_name)) {
            var all_elements = curr_form.elements;
            for(j=0; j<all_elements.length; j++)    {
                if(all_elements[j].type == 'select-one')    {
                    all_elements[j].style.display = 'block';
                }
            }
        }
    }
}
