//************************************************************************************
// 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;



//
// dinamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{

   /*
    var className="";
	if(isMozilla){
      className='dimmer_ff';
	 }
    else{
	   className='dimmer_ie';
    }
	document.write('<div id="dimmer" class="'+className+'" style="width:100%; height:100%"></div>');
    
   */ 
   document.write('<div id="modalPage" style="width:100%; height:100%"><div class="modalBackground" id="modalPage2"><!--[if lte IE 6.5]><iframe id="specialIframe"></iframe><![endif]--></div></div>');
}


//
//
//
var zIndex=1000; //this will used for z-index of pop-upped divs
var clientHeight="";//calculates the client Height
var clientWidth="";//calculates the client width

function displayFloatingDiv(divId, title, width, height, left, top) 
{
	DivID = divId;

	  var pageWidth = 0, pageHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		pageWidth = window.innerWidth;
		pageHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	  }

	  left = (pageWidth - width)/2;
	  top = (pageHeight - height)/2;


	//calculates the client Height and if blanck then assign value
	if(clientHeight =="" && clientWidth ==""){
     clientHeight=document.body.clientHeight;
	 clientWidth=document.body.scrollWidth;
	}
    clientHeight=document.body.scrollHeight;
    clientWidth=document.body.scrollWidth;
 
	//document.getElementById('dimmer').style.visibility = "visible";
    document.getElementById('modalPage').style.display = "block";
    document.getElementById('modalPage').style.height=clientHeight+'px';
    document.getElementById('modalPage').style.width=clientWidth+'px';
    document.getElementById('modalPage2').style.display = "block";
    document.getElementById('modalPage2').style.height=clientHeight+'px';
    document.getElementById('modalPage2').style.width=clientWidth+'px';
    
    try{
       //to fix IE6's z-index problem
       document.getElementById('specialIframe').style.width = '100%';
       document.getElementById('specialIframe').style.height = height + 'px';
       document.getElementById('specialIframe').style.left = left + 'px';    
    }
    catch(e){
    }
                                                                
//    document.getElementById('modalPage2').style.height='849px';
    
    

 //   window.status=document.getElementById('modalPage').style.height;

    
    //it is the id of the menu(although can not be seen in html,generated dynamically)
   //makeMenuDisable('qm0',true);
    

    document.getElementById(divId).style.width = width + 'px';
    //document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.left = left + 'px';

	//var w=document.getElementById(divId).style.width.split("px");
    //document.getElementById(divId).style.left =((window.screen.width- w[0])/2) + 'px';
    
	document.getElementById(divId).style.top = top + 'px';
	
	
	document.getElementById(divId).className = 'dimming';
    
    document.getElementById(divId).style.zIndex=++zIndex; //z-index incremented by one
    
	document.getElementById(divId).style.visibility = "visible";
	document.getElementById(divId).style.display = 'block';


}


/*****************/
//Purpose:To disable/enable all elements of the menu when popup div open/close
//Author:Dipanjan Bhattacharjee
//Date:28.08.2008
/******************/
function makeMenuDisable(id,disable){
 var nodesToDisable = {button :'', input :'', optgroup :'',
    option :'', select :'', textarea :'',span : '', a: ''};

    var node, nodes;
    var div = document.getElementById(id);
    if (!div) return;

    nodes = div.getElementsByTagName('*');
    if (!nodes) return;

    var i = nodes.length;
    while (i--){
     node = nodes[i];
     if ( node.nodeName && node.nodeName.toLowerCase() in nodesToDisable ){
         node.disabled = disable;
         node.style.cursor=(disable ? "default":"pointer");
        }
     }
}


//
//
//
function hiddenFloatingDiv(divId) 
{
	//document.getElementById(divId).innerHTML = originalDivHTML;
	document.getElementById(divId).style.visibility='hidden';
	//document.getElementById('dimmer').style.visibility = 'hidden';
    document.getElementById('modalPage').style.display = "none";
    makeMenuDisable('qm0',false);
	
	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();

}

//Purpose: escape key handler[closes pop up divs when escape key(27) is pressed]
//Author:Dipanjan Bhattacharjee
//Date:30.08.2008

  document.onkeypress=escapeHandler;


  function escapeHandler(e){
   var evt = ( (!document.all) ? e : window.event);
   if(evt.keyCode==27){
	   if(DivID!=""){
        hiddenFloatingDiv(DivID); 
       }
   }
 }


// call init
init();


 