// The following code is used to support the small popups that
// give the full description of an event when the user move the
// mouse over it.
// Thanks to Klaus Knopper (www.knoppix.com) for this script.
// It has been modified to work with the existing WebCalendar
// architecture on 02/25/2005
//
// 03/05/2005 Prevent popup from going off screen by setting
// maximum width, which is cnfigurable
//
// Bubblehelp infoboxes, (C) 2002 Klaus Knopper <infobox@knopper.net>
// You can copy/modify and distribute this code under the conditions
// of the GNU GENERAL PUBLIC LICENSE Version 2.
//
var ns4            // Are we using Netscape4?
var ie4            // Are we using Internet Explorer Version 4?
var ie5            // Are we using Internet Explorer Version 5 and up?
var kon            // Are we using KDE Konqueror?
var x,y,winW,winH  // Current help position and main window size
var px="px"        // position suffix with "px" in some cases
var popupW         // width of popup
var popupH         // height of popup
var xoffset = 30    // popup distance from cursor x coordinate
var yoffset = -50   // popup distance from cursor y coordinate
//var followMe = 1   // allow popup to follow cursor...turn off for better performance
var followMe = 0   // allow popup to follow cursor...turn off for better performance
var maxwidth = 300 // maximum width of popup window
var curr_name = ""; //name of popup currently being shown.

// added to support multiple fading windows
BGpopupWindow = new Array();  //stores the state of all our popup windows

function nsfix(){setTimeout("window.onresize = rebrowse", 2000);}

function rebrowse(){window.location.reload();}

function infoinit(){
  ns4=(document.layers)?true:false, ie4=(document.all)?true:false;
  ie5=((ie4)&&((navigator.userAgent.indexOf('MSIE 5')>0)||(navigator.userAgent.indexOf('MSIE 6')>0)))?true:false;
  kon=(navigator.userAgent.indexOf('konqueror')>0)?true:false;
  x=0;y=0;winW=800;winH=600;
  if (followMe) {
    document.onmousemove = mousemove;
    if(ns4&&document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
  }
  // Workaround for just another netscape bug: Fix browser confusion on resize
  // obviously conqueror has a similar problem :-(
  if(ns4||kon){ nsfix() }
  if(ns4) { px=""; }
}

function gettip(name){
  return (document.layers&&document.layers[name])?document.layers[name]:(document.all&&document.all[name]&&document.all[name].style)?document.all[name].style:document[name]?document[name]:(document.getElementById(name)?document.getElementById(name).style:0);
}

function setOpacity(obj, opacity) {
//	opacity = (opacity == 100)?99.999:opacity;
	opacity = (opacity > 100)?100:opacity;
	opacity = (opacity < 0)?0:opacity;
	// IE/Win
	obj.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.opacity = opacity/100;
}

function fadeIn(name, opacity) {
  if (!BGpopupWindow[name].show) return;

  idiv = BGpopupWindow[name].obj;
	if (opacity <= 100) {
		opacity += 10;
		setOpacity(idiv, opacity);
  	opacity = (opacity > 100)?101:opacity;
		BGpopupWindow[name].opacity = opacity;
		window.setTimeout("fadeIn(\""+name+"\", "+opacity+")", 20);
	}
}

function fadeOut(name, opacity) {
  if (BGpopupWindow[name].show) return;
		
  idiv = BGpopupWindow[name].obj;
	if (opacity >= 0) {
		opacity -= 10;
		setOpacity(idiv, opacity);
  	opacity = (opacity < 0)?-1:opacity;
		BGpopupWindow[name].opacity = opacity;
		window.setTimeout("fadeOut(\""+name+"\", "+opacity+")", 20);
	}
	else {
		idiv.visibility=ns4?"hide":"hidden";
	}
}

function pop_hide(name) {
  if (BGpopupWindow[name]) {
	  BGpopupWindow[name].show = false;
		fadeOut(name, BGpopupWindow[name].opacity);
	}
}

function pop_show(evt, name){
	if (!BGpopupWindow[name]) { // create the new popup window state object
		BGpopupWindow[name] = new Object();
//		document.getElementById(name).innerHTML += " new ";  //debug
	}

  if (BGpopupWindow[curr_name]) {
	  pop_hide(curr_name); 
	}
  
  idiv = gettip(name); // store the infodiv object pointer.
  if (idiv) {
    BGpopupWindow[name].obj = idiv;
	  BGpopupWindow[name].show = true;
	  BGpopupWindow[name].opacity = 0;
		scrollX =0; scrollY=0;
		winW=(window.innerWidth)? window.innerWidth+window.pageXOffset-16:document.body.offsetWidth-20;
		winH=(window.innerHeight)?window.innerHeight+window.pageYOffset  :document.body.offsetHeight;
		scrollX=(typeof window.pageXOffset == "number")? window.pageXOffset:(document.documentElement && document.documentElement.scrollLeft)?document.documentElement.scrollLeft:(document.body && document.body.scrollLeft)?document.body.scrollLeft:window.scrollX;
		scrollY=(typeof window.pageYOffset == "number")? window.pageYOffset:(document.documentElement && document.documentElement.scrollTop)?document.documentElement.scrollTop:(document.body && document.body.scrollTop)?document.body.scrollTop:window.scrollY;
		popupW = document.getElementById(name).offsetWidth;
		popupH = document.getElementById(name).offsetHeight;   
		curr_name = name;  //store global id for mousemove function below.
		showtip(evt, name);
  }
}

function showtip(e, name){
  e = e? e: window.event;
  idiv = (BGpopupWindow[name] && BGpopupWindow[name].obj) ? BGpopupWindow[name].obj : null;
  if(idiv) {
    if(e)   {
      x=e.pageX?e.pageX:e.clientX?e.clientX + scrollX:0; 
      y=e.pageY?e.pageY:e.clientY?e.clientY + scrollY:0;
    }
    else {
      x=0; y=0;
    }
    // Make sure we don't go off screen
    if ( popupW > maxwidth ) { 
      popupW = maxwidth;
      idiv.width = maxwidth + px;
    }  
    idiv.left=(((x + popupW + xoffset)>winW)?x - popupW - xoffset:x + xoffset)+px;
    if ((popupH + yoffset)>winH) {
      idiv.top= yoffset + px;
    } else {
      idiv.top=(((y + popupH + yoffset)>winH)?winH - popupH - yoffset:y + yoffset)+px;
    }
    idiv.visibility=ns4?"show":"visible";
    fadeIn(name, BGpopupWindow[name].opacity);
  }
}

function mousemove(e){
  showtip(e, curr_name);  //permits popup to follow cursor
}
// Initialize after loading the page
window.onload=infoinit;
