//************************************************************************************
// 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 is = new Is();
var page_width=800;
var page_height = 420;

function Is() {
    agent  = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns    = ((agent.indexOf('mozilla') != -1) && 
      ((agent.indexOf('spoofer')  ==   -1) && (agent.indexOf('compatible') == -1)));
    this.ns4   = (this.ns && (this.major == 4));
    this.ns6   = (this.ns && (this.major >= 5));
    this.ie    = (agent.indexOf("msie") != -1);
    this.ie3   = (this.ie && (this.major  < 4));
    this.ie4   = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.0") == -1));
    this.ie5   = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.0") != -1));
    this.ie55  = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.5") != -1));
    this.ie6  = (this.ie && (agent.indexOf("msie 6.0")!=-1) );
    this.ie7  = (this.ie && (agent.indexOf("msie 7.0")!=-1) );
}

//
// dinamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{
	page_width = window.screen.width;
	page_height = window.screen.height;
	
	if(is.ns6) {
		page_width=innerWidth - 105;
		page_height=innerHeight;
	} else if(is.ie5 || is.ie55||is.ie6) {
		page_width=document.body.clientWidth - 80;
		page_height=document.body.clientHeight;
	} else if(is.ie7) {
		page_width=document.body.clientWidth + 6;
		page_height=document.body.clientHeight;
	}
	// Debug this on multi-screen machines! alert("x=" + window.screen.width + " y=" + window.screen.height);
    document.write('<div onclick="hideWindow()" id="dimmer" class="dimmer" style="width:'+ page_width + 'px; height:' + page_height +'px"><table height="100%" width="100%" border="0"><tr><td>&nbsp;</td></tr></table?</div>');
    
	document.getElementById("dimmer").onmousedown = MouseDown
	document.getElementById("dimmer").onmousemove = MouseMove
	document.getElementById("dimmer").onmouseup = MouseUp
}


//
//
//
function displayFloatingDiv(divId, title, width, height, left, top) 
{
	DivID = divId;
	document.getElementById('dimmer').style.visibility = "visible";
    document.getElementById(divId).style.width = width + 'px';
    if (is.ie7)
    {
    	document.getElementById(divId).style.height = (height + 4) + 'px';
    } else {
	    document.getElementById(divId).style.height = height + 'px';
	}
    document.getElementById(divId).style.left = left + 'px';
    document.getElementById(divId).style.top = top + 'px';
	
	document.getElementById(divId).className = 'dimming';
	document.getElementById('dimmer').style.zIndex="4000"
	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.zIndex="-7"
	DivID = "";
}

//
//
//
function MouseDown(e) 
{
    if (over)
    {
        if (isMozilla || e) {
            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 || e) {
            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;
}

var origOnMouseMove = null;
var origOnMouseDown = null;
var origOnMouseUp = null;

//
//
//
function initializeSearchPanel()
{
    // check browser
    isMozilla = (document.all) ? 0 : 1;

    if (isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    } 
    
    document.getElementById("windowcontent").onmousedown = MouseDown
	document.getElementById("windowcontent").onmousemove = MouseMove
	document.getElementById("windowcontent").onmouseup = MouseUp

    // add the div
    // used to dim the page
	buildDimmerDiv();
    generateDateList();
}

// call init
//init();

function displayWindow() {
    var w, h, l, t;
    w = 500;
    h = 250;
    l = (screen.width/2) - 320;
    t = (screen.height/3) - 100;
    // with title	
    //generateCategoryList();
    //generateRegionsList();
    //generatePublicationsList();
    //generateAnalystsList();
    displayFloatingDiv('windowcontent', 'Search For:', w, h, l, t);
    Page_HideVisibleComboBoxes();
}

function hideWindow() {
	hiddenFloatingDiv('windowcontent');
	Page_RestoreComboBoxes();
}

var hiddenCombos = new Array();

function Page_HideVisibleComboBoxes() {
  if ( is.ie3 || is.ie4 || is.ie4 || is.ie5 || is.ie55 || is.ie6 ) {
	  combos = document.getElementsByTagName("select");
	  if (combos != null) {
		  for ( i = 0; i < combos.length; i++ ) {
			combo = combos[i];
			if (combo.style.display != 'none') {
			   // Box is visible right now
			   if ( combo.id.indexOf("nh_", 0) != 0 ) {
				  hiddenCombos.length = hiddenCombos.length+1;
				  hiddenCombos[hiddenCombos.length - 1] = [combo, combo.style.width];   
				  combo.style.width=0;
			   }
			}
		  }
	  }
  }
}

function Page_RestoreComboBoxes() {
  if ( is.ie3 || is.ie4 || is.ie4 || is.ie5 || is.ie55 || is.ie6 ) {
	  for ( i = 0; i < hiddenCombos.length; i++ ) {
		combo = hiddenCombos[i];
		combo[0].style.width=combo[1];
	  }
	  hiddenCombos.length = 0;
  }
}
