/* *	IWE CALENDAR *	V. 1.3 *	Javascript Engine *	By Alessandro Perrone *	19th November 2009 *	© 2009 - iweconsulting.net * *	@changelog: only one tooltip is shown at a time */  /*  *	RICORDA DI CAMBIARE IL PERCORSO DEL FILE calendar.php IN BASE A DOVE QUEST'ULTIMO RISIEDERA' RISPETTO ALLA PAGINA CHE ESEGUE QUESTO SCRIPT  *  *	RICORDA INOLTRE DI INCLUDERE ANCHE IL FILE xmlparse.js  */tipIsShown = false;function AJAX(){	var xmlObj;	try{		xmlObj = new XMLHttpRequest();	}	catch(error){		try{			xmlObj = new ActiveXObject("Microsoft.XMLHTTP");		}		catch(error){			xmlObj = null;		}	}	return xmlObj;}	function showCalendar(file){	var xmlObj = AJAX();	// calendar data are html data, so we don't need to use the XMLParse object, but we use innerHTML instead	//xmlParser = new XMLParse();	if(xmlObj != null){		xmlObj.onreadystatechange = function(){		if(xmlObj.readyState == 4){			if(xmlObj.status == 200 || xmlObj.status == 304){				var newCont = xmlObj.responseText;				// use innerHTML property as formatted HTML code is returned instead of XML or text data				document.getElementById('calendar').innerHTML = newCont;				initTooltips();			}else				alert('Could not generate Calendar.');		    }		};		// anti-caching triks		//xmlObj.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');		//xmlObj.open ('GET', file + '?timestamp=' + new Date().getTime(), true);		xmlObj.open ('GET', file, true);		xmlObj.send (null);	}else		alert('AJAX requests cannot be generated on your browser');}	function parseXML(xml, target){	//alert('xml: ' + xml + ', target: ' + target);	var resArray = xml.getElementsByTagName('events');	//alert(resArray.length);	xml = resArray[0];	//alert('returning: ' + xml);	return XMLParse.xml2ObjArray(xml, target);}function retrieveEvents(file, date, target){	var xmlObj = AJAX();	if(xmlObj != null){		var loadingNotice = document.createElement('h1');		loadingNotice.setAttribute('id', 'loadingNotice');		target.loadingNotice = target.appendChild(loadingNotice.appendChild(document.createTextNode('loading events list...')));		xmlObj.onreadystatechange = function(){			if(xmlObj.readyState == 4){				if(xmlObj.status == 200 || xmlObj.status == 304){					var updatedCont = parseXML(xmlObj.responseXML, 'event');					var content = '';					if(updatedCont.length > 0){						// clean loading events notice...						var loadingNotice = document.getElementById('loadingNotice');						var header_wrapper = document.createElement('div');						var wrapper = document.createElement('div');						header_wrapper.className = 'tooltip_head_wrap';						wrapper.className = 'tooltip_wrap';						target.removeChild(target.loadingNotice);						// create data structure						for(var a in updatedCont){							var box = document.createElement('div');							var obj = updatedCont[a];							for(var x in obj){								var element = obj[x];								// limit tip content to eventName and eventLocation								//if(x == 'eventName' || x == 'eventLocation'){								if(x == 'eventURL'){									var docLink = 'undefined';									if(obj[x] != 'undefined'){										var docLink = document.createElement('a');										docLink.setAttribute("href", obj[x]);										//docLink.setAttribute("target", '_blank');									}								}								if(x == 'eventName'){									var h = document.createElement('h1');									if(docLink != 'undefined'){										var htmlContent = element.replace(/\n\r/, "<br />");										//docLink.appendChild(document.createTextNode(element));										docLink.innerHTML = htmlContent;										h.appendChild(docLink);									}else{										//p.appendChild(document.createTextNode(element));										//p.innerHTML = element;										h.innerHTML = element.replace(/\n/, "<br />");									}									var mail = document.createElement('a');									mail.appendChild(document.createTextNode('m.berbotto@selmi-group.it'));									mail.setAttribute('href', 'mailto:m.berbotto@selmi-group.it');									var p = document.createElement('p');									p.className = 'mailLink';									p.appendChild(document.createTextNode('Per info: '));									p.appendChild(mail);									content = h;									box.appendChild(content);									box.appendChild(p);								}								/*								if(x == 'eventDocument'){									var docLink = document.createElement('a');									docLink.setAttribute("href", obj[x]);									docLink.setAttribute("target", '_blank');								}								*/								// To make this work you must ensure the eventDocument above has been setup or no docLink will be defined								// and the append process will fail								/*								if(x == 'eventDescription'){									var p = document.createElement('p');									if(docLink != 'undefined'){										var htmlContent = element.replace(/\n\r/, "<br />");										//docLink.appendChild(document.createTextNode(element));										docLink.innerHTML = htmlContent;										p.appendChild(docLink);									}else{										//p.appendChild(document.createTextNode(element));										//p.innerHTML = element;										p.innerHTML = element.replace(/\n/, "<br />");									}									content = p;									if(!target.hasContent)										target.hasContent = true;									//target.appendChild(content);									//box.appendChild(content);								}								*/							}														wrapper.appendChild(box);						}						target.appendChild(header_wrapper);						target.appendChild(wrapper);						/*						var footer_img = document.createElement('img');						footer_img.src = 'images/footer_tooltip.png';						target.appendChild(footer_img);						*/					}else{						content = 'Error: invalid XML content!';						target.appendChild(document.createTextNode(content));					}				}else{					alert('Could not retrieve Events.');				}			}		};		xmlObj.open ('GET', file + '?date=' + date, true);		//xmlObj.open ('GET', file + '&date=' + date, true);		xmlObj.send (null);	}else		alert('AJAX requests cannot be generated on your browser');}	function showDetail(event){	var target = getEventTarget(event);	// hide tip when an element is selected	hideTip(event);	var data = target.dataObj;	if(data != undefined){		var date = document.createElement('h2');		var title = document.createElement('h1');		var location = document.createElement('h3');		var description = document.createElement('p');		var teacher = document.createElement('p');		var teacherLbl = document.createElement('span');		teacherLbl.style.fontWeight = 'bold';		teacherLbl.appendChild(document.createTextNode('Docente: '));		teacher.appendChild(teacherLbl);		var popupLink = document.createElement('a');		var infoLink = document.createElement('a');		var closeBtn = document.createElement('a');		closeBtn.setAttribute("href", "javascript:void(0);");		closeBtn.className = 'closeBtn';		popupLink.setAttribute("href", "javascript:void(0);");		infoLink.setAttribute("href", "javascript:void(0);");		attachEventListener(infoLink, "click", openInfoPopup, false);		for(var a in data){			var curVar = curData = null;			switch(a){				case 'eventDate':					curVar = eval('date');					curData = data[a];					break;				case 'eventName':					curVar = eval('title');					curData = data[a];					break;				case 'eventLocation':					curVar = eval('location');					curData = data[a];					break;				case 'eventDescription':					curVar = eval('description');					curData = data[a];					break;				case 'eventTeacher':					curVar = eval('teacher');					curData = data[a];					break;				case 'eventURL':					curData = '';					if(data[a] != '' && data[a] != 'null'){						popupLink.popupURL = data[a];						attachEventListener(popupLink, "click", openDetailPopup, false);						popupLink.appendChild(document.createTextNode('Visualizza programma'));					}					break;			}			if(a != 'eventURL')				curVar.appendChild(document.createTextNode(curData));		}		var info = document.getElementById('calendar_info');		while(info.childNodes.length > 0){			info.removeChild(info.childNodes[0]);		}		var infoContent = document.createElement('div');		infoLink.appendChild(document.createTextNode('Richiedi informazioni'));		closeBtn.appendChild(document.createTextNode('Chiudi'));		infoContent.appendChild(title);		infoContent.appendChild(date);		infoContent.appendChild(location);		infoContent.appendChild(description);		infoContent.appendChild(teacher);		infoContent.appendChild(popupLink);		infoContent.appendChild(infoLink);		info.appendChild(infoContent);		info.appendChild(closeBtn);		// remember to include the 'hide' class to your CSS to make this work correctly		// commented lines are for use without mootools		/*		$("div#calendar_info").addClass('hide').show("slow");		$("div#calendar_info a").click(function(){		   $("div#calendar_info.hide").hide("slow");		 });		 */		jQuery("div#calendar_info").addClass('hide').show("slow");		// with the commented code below it hides the div also when selecting popupLink button		//jQuery("div#calendar_info a").click(function(){		jQuery("div#calendar_info a.closeBtn").click(function(){			jQuery("div#calendar_info.hide").hide("slow");		});			}else		alert('ERROR: invalid data!');	}function openDetailPopup(event){	var target = getEventTarget(event);	var url = target.popupURL;	//window.open(url, '', 'width=640,height=480');	NewWindow(url, 'approccio', '550', '450', 'yes', 'no');}function openInfoPopup(event){	NewWindow('infocorsi.htm', 'approccio', '550', '450', 'yes', 'no');}   function appendElement(_parent, _child){	var parent = document.getElementById(_parent);	if(parent == null){		parent = document.createElement('div');		parent.id = _parent;	}	parent.appendChild(_child);	document.getElementById('calendar').appendChild(parent);}   /*TOOLTIPS management*/	   function addLoadListener(fn){	if(typeof window.addEventListener != 'undefined'){		window.addEventListener('load', fn, false);	}else if(typeof document.addEventListener != 'undefined'){		document.addEventListener('load', fn, false);	}else if(typeof window.attachEvent != 'undefined'){		window.attachEvent('onload', fn);	}else{		var oldfn = window.onload;		if(typeof window.onload != 'function')			window.onload = fn;		else{			window.onload = function(){				oldfn();				fn();			};		}	}}	function attachEventListener(target, eventType, functionRef, capture){	if(typeof target.addEventListener != 'undefined')		target.addEventListener(eventType, functionRef, capture);	else if(typeof target.attachEvent != 'undefined')		target.attachEvent('on' + eventType, functionRef);	else{		eventType = 'on' + eventType;		if(typeof target[eventType] == 'function'){			var oldListener = target[eventType];			target[eventType] = function(){				oldListener();				return functionRef();			};		}else			target[eventType] = functionRef;	}}	function getEventTarget(event){	var targetElement = null;	if(typeof event.target != 'undefined')		targetElement = event.target;	else		targetElement = event.srcElement;	while(targetElement.nodeType == 3 && targetElement.parentNode != null){		targetElement = targetElement.parentNode;	}	return targetElement;}	function getScrollingPosition(){	var position = [0, 0];	if(typeof window.pageYOffset != 'undefined'){		position = [			window.pageXOffset,			window.pageYOffset		];	}else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0 || document.documentElement.scrollLeft > 0){		position = [			document.documentElement.scrollLeft,			document.documentElement.scrollTop		];	}else if(typeof document.body.scrollTop != 'undefined'){		position = [			document.body.scrollLeft,			document.body.scrollTop		];	}	return position;}function initTooltips(){	var target = document.getElementById('calendar');	var tips = target.getElementsByTagName('a');	for(var i = 0; i < tips.length; i++){		// tips are the <a> tags that will open a tooltip		if(tips[i].className == "hastooltip"){			/*			 * setting 'capture' argument to true will avoid firing javascript error (as for some reason also links in the tooltip div			 * (dynamically created in 'showTip()') will fire the showTip() method on the 'click' event (they seem to inherit it, or maybe it's fired clicking on the tip that is an			 * 'extension' of the 'a' element that fires it			 */			attachEventListener(tips[i], "click", showTip, true);		}	}	return true;}	function showTip(event){	if(!tipIsShown){		if(typeof event == "undefined")			event = window.event;		// get the target that called this function and to which is attached the generated tooltip layer				var target = getEventTarget(event);		while(target.className == null || !/(^| )hastooltip( |$)/.test(target.className)){			target = target.parentNode;		}				//console.debug(target);				if(target.tooltip == undefined){			var tip = target.tooltip == null ? document.createElement("div") : target.tooltip;			var date = target.getAttribute("title");			// create close link to close tipbox			var closeLink = document.createElement("a");			var closeLinkText = document.createTextNode("chiudi");			closeLink.appendChild(closeLinkText);			closeLink.setAttribute("href", "javascript:void(0);");			closeLink.className = 'closeBtn';			closeLink.onclick = function(){				hideTip(event);			};			tip.appendChild(closeLink);					target.date = date;			target.tooltip = tip;			target.setAttribute("title", " ");							if(target.getAttribute("id") != " "){				if(target.getAttribute("id") == null)					var replaceID = 'robot_';				else					var replaceID = target.getAttribute("id");								//tip.setAttribute("id", target.getAttribute("id") + "tooltip");				tip.setAttribute("id", replaceID + "tooltip");			}							tip.className = "tooltip";			if(!tip.hasContent)				retrieveEvents('../calendar.php', date, tip);			var scrollingPosition = getScrollingPosition();			var cursorPosition = [0, 0];						if(typeof event.pageX != "undefined" && typeof event.x != "undefined"){				cursorPosition[0] = event.pageX;				cursorPosition[1] = event.pageY;			}else{				cursorPosition[0] = event.clientX + scrollingPosition[0];				cursorPosition[1] = event.clientY + scrollingPosition[1];			}						/*			OLD STYLE						tip.style.background = "#FFF url(js/bg_tooltip.gif) no-repeat";			tip.style.border = "none";			tip.style.borderBottom = "1px solid #666";			tip.style.color = "#000";			tip.style.display = 'block';			tip.style.width = "200px";			tip.style.position = "absolute";			*/			tip.style.left = cursorPosition[0] - 200 + "px";			tip.style.top = cursorPosition[1] - 15 + "px";			//attachEventListener(tip, "mouseout", hideTip, false);			target.appendChild(tip);			tipIsShown = true;			return true;		}else			return false;			//return true;		}}function hideTip(event){	if(typeof event == 'undefined')		event = window.event;	var target = getEventTarget(event);	while(target.className == null || !/(^| )hastooltip( |$)/.test(target.className)){		target = target.parentNode;	}		//console.debug(target + " - " + target.tooltip);		if(target.tooltip != null){		target.tooltip.status = undefined;		target.setAttribute('title', target.date);		target.tooltip.parentNode.removeChild(target.tooltip);		target.tooltip = null;		tipIsShown = false;	}}