/**
 * Prototype Based Tooltip, version 0.1.0
 * Copyright (c) 2006, Nio Xiao <krazynio AT hotmail.com>
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * For details, see: http://nio.infor96.com/
 *
 * Requirements:
 * 	Prototype 1.5.0_rc1. You can download it from the Prototype web site: http://prototype.conio.net/
 *
 * Usage:
 * 	1. Insert the following lines into <head> section in your html file:
 * 		<script type="text/javascript" src="prototype.js"></script>
 * 		<script type="text/javascript" src="tooltip.js"></script>
 *  2. Define a cool css for tooltip, for example:
 *		<style type="text/css">
 *		#tooltip {
 *			background-color: #ffffcc;
 *			color: #000000; 
 *			padding: 4px; 
 *			border: 1px solid gray; 
 *			text-align: left; 
 *			cursor: default;
 *			opacity: 0.85;
 *			z-index: 10000;
 *		}
 *		</style>
 *	3. Create a new Tooltip instance for your tooltip source html element:
 *		<div id="mytip">Move mouse over here!</div>
 *		<script type="text/javascript">new Tooltip({element:"mytip", text:"Tooltip message.<br/>You can use HTML tags here."})</script>
 */

var Tooltip = Class.create();
Object.extend(Tooltip.prototype, {
	initialize: function(options) {
		Tooltip.Container.init();
		this.setOptions(options);
		Element.observe(this.element, 'mouseover', this.onMouseOver.bind(this));
		Element.observe(this.element, 'mouseout' , this.onMouseOut.bind(this));
	},
	
	setOptions: function(options) {
		var defaultOptions = {
			element: null,
			text: '',
			width: 300,
			left: 0
		};
		Object.extend(Object.extend(this, defaultOptions), options);
	},
	
	onMouseOver: function() {
		Tooltip.Container.show(this);
	},
	
	onMouseOut: function() {
		Tooltip.Container.hide(this);
	}
});

Tooltip.Container = {
	id: 'tooltip',
	
	className: '',
	
	init: function() {
		if (!$(this.id)) {
			var e = document.createElement('div');
			e.id = this.id;
			if (this.className)
				e.className = this.className;
			e.style.display  = 'none';
			e.style.position = 'absolute';
			document.body.appendChild(e);
		}
	},
	
	isShowing: function() {
		return Element.visible(this.id);
	},
	
	show: function(tip) {
		var bodyDim = Element.getDimensions(document.body);
		var pos = Position.cumulativeOffset($(tip.element));
		var x = pos[0], y = pos[1]+2;
		// décalle la Tooltip dans le calendrier mensuel sous FF 2
		if (navigator.userAgent.indexOf("Firefox/2")>-1 && $(tip.element).nodeName=="TD") {
			x+=20; y+=20;
		}
		var l = tip.left;
		if (!l) {
			l = (x + tip.width > bodyDim.width - 20) ? (x - tip.width) : x;
		}
		Element.setStyle(this.id, {'width': tip.width+'px', 'left': l+'px', "top": (y+20)+'px'});
		Element.update(this.id, tip.text);
		Element.show(this.id);
	},
	
	hide: function() {
		Element.update(this.id, '');
		Element.hide(this.id);
	}
}

// pour pouvoir utiliser proprement des fonctions qui définissent
// des tooltips, on utilise cette fonction qui permet d'appeler
// plusieurs fonctions sur window.onload
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}
function switchCal(dtstartParam, todaylinkParam, showWeekParam, showTooltipParam, rubriqueHistorique, filtreParam) {
	var url = "/servlet/com.jsbsoft.jtf.core.SG";
	var param = new Object();
	param = {
		PROC : 'RECHERCHE_AGENDA',
		ACTION : 'AFFCAL',
		todaylink : todaylinkParam,
		showWeek : showWeekParam,
		showTooltip : showTooltipParam,
		DTSTART : dtstartParam,
		id : '42',
		RH : rubriqueHistorique,
		FILTRE :filtreParam
	}
	var o_options = new Object();
	o_options = {
		method : 'get',
		parameters : param,
		onComplete : afficherReponse
	};
	var laRequete = new Ajax.Request(url,o_options);
	function afficherReponse(requete){
		var cdiv=document.getElementById('contenu_agenda');
		if (requete.responseText.lastIndexOf('<script') < requete.responseText
				.lastIndexOf('new Tooltip')) {
			cdiv.innerHTML = requete.responseText.substring(0,
					requete.responseText.lastIndexOf('<script'));
			var fct = requete.responseText.substring(requete.responseText
					.lastIndexOf('function') + 12, requete.responseText
					.lastIndexOf('}'));
			eval(fct);
		} else {
			cdiv.innerHTML = requete.responseText
		}
	}
}
