﻿/**
	Script Name: 	WiLi - Windowed Links - 
	version: 		1.1.0
	version date:	2007-04-08
	+------------------------------------------------------------------------------
	Author: 				Diego La Monica		 (http://www.diegolamonica.info
											 	   mailto:diego.lamonica@gmail.com)
	Based on idea by: 		Carlo Filippo Follis (http://www.norisberghen.it)
												   mailto:cffollis@norisberghen.it)
	+------------------------------------------------------------------------------
	First time pubblished on:	2007-04-02
	+------------------------------------------------------------------------------
	Description:	[ITALIANO]
					Questo script rende possibile l'apertura dei collegamenti in
					altre pagine solo se non sono definiti come link interni.
					Ma ciò è garantito solo se l'utente chiede di aprire i link
					in altre finestre (specificandolo esplicitamente). In alternativa
					non accadrà nulla. 
					Questo script è coerente con regole di accessibilità ed usabilità.
					
					[ENGLISH]
					This script makes the links on the page to open in other window
					only if those ones are not defined as internal links.
					But only if user want to open links in other window (defining it
					explicitelly) the system makes it. Nothing else matter.
					This script takes care of usability and accessibility rules.
	+------------------------------------------------------------------------------
	License:		[ITALIANO]
					Licenza Creative Commons
					Questo script è rilasciato sotto licenza Creative Commons ed è
					scaricabile ed utilizzabile gratuitamente. Non esistono obblighi
					economici per l'utilizzo su siti commerciali o personali.
					Esiste tuttavia l'obbligo morale di aggiungere un link alle
					pagine http://www.norisberghen.it/it/?p=430
					e http://diegolamonica.info/wili tramite uno degli esempi di
					codice presentati di seguito.
					
					Codice HTML, 1.:
					
					<p>Da un'idea di <a href="http://www.norisberghen.it" title="Carlo Filippo Follis - Norisberghen.it">Carlo Filippo Follis</a> un JavaScript firmato <a href="http://www.diegolamonica.info" title="Diego La Monica - Diegolamonica.info">Diego La Monica</a>: <strong>&lt;wi.li.� /&gt;</strong>. Questo Sito ha scelto una soluzione del Progetto &#8220;<a href="http://www.norisberghen.it/it/?page_id=438" title="Progetto D-Code"><strong>D-Code</strong></a>&#8221;!</p>
					
					oppure
					
					Codice HTML, 2.:
					
					<p>Da un'idea di <a href="http://www.norisberghen.it" title="Carlo Filippo Follis - Norisberghen.it">Carlo Filippo Follis</a> un JavaScript firmato <a href="http://www.diegolamonica.info" title="Diego La Monica - Diegolamonica.info">Diego La Monica</a>: <strong>&lt;wi.li.� /&gt;</strong>.</p>
					
					infine
					
					Codice HTML, 3.:
					
					<p>Questo sito usa <a href="http://www.norisberghen.it/it/?p=430" title="Questo sito usa wi.li."><strong>&lt;wi.li.� /&gt;</strong></a>.</p>
					
					Inoltre per garantirci di informarti in caso di aggiornamenti
					dello script, ti preghiamo di inviare una mail ai seguenti
					indirizzi:
								cffollis@norisberghen.it e me@diegolamonica.info
								
					indicando la data di installazione dello script ed il sito
					(o i siti) sul quale viene utilizzato.
					Un collegamento al/ai sito/i verrà aggiunto nelle pagine
					predisposte per questo script nei siti di Diego La Monica
					(http://www.diegolamonica.info) e Carlo Filippo Follis
					(http://www.norisberghen.it)
					Un tutorial completo sull'uso dello script è presente ai
					seguenti link:
					
									1. http://www.norisberghen.it/it/?p=430
									2. http://www.diegolamonica.info/tutorials/?id=
					
					Diego La Monica e Carlo Filippo Follis non si assumono alcuna
					responsabilità sull'uso o sul mancato uso o su eventuali bugs
					conseguiti o non conseguiti a seguito dell'installazione di
					questo script.
					Qualsiasi obiezione a quanto sopra vincola al non utilizzo di
					questo script.
					
					[ENGLISH]
					Creative Commons License
					This script is released under a Creative Commons license.
					It can be freely dowloaded and there are no fees or costs
					whatsoever for either personal or commercial use.
					You are nonetheless required to link to either
					http://www.norisberghen.it/it/?p=430 or
					http://www.diegolamonica.info/wili using one of the code
					snippets provided below.
					
					Code HTML, 1.:
					
					<p>By a <a href="http://www.norisberghen.it" title="Carlo Filippo Follis - Norisberghen.it">Carlo Filippo Follis</a> ideas, javascript made by<a href="http://www.diegolamonica.info" title="Diego La Monica - Diegolamonica.info">Diego La Monica</a>: <strong>&lt;wi.li. /&gt;</strong>. This site has choosen a &#8220;D-code&#8221; project solution.</p>
					
					or
					
					Code HTML, 2.:
					
					<p>Da un'idea di <a href="http://www.norisberghen.it" title="Carlo Filippo Follis - Norisberghen.it">Carlo Filippo Follis</a> un JavaScript firmato <a href="http://www.diegolamonica.info" title="Diego La Monica - Diegolamonica.info">Diego La Monica</a>: <strong>&lt;wi.li.� /&gt;</strong>.</p>
					
					finally
					
					Code HTML, 3.:
					
					<p>This Site is using <a href="http://www.norisberghen.it/it/?p=430" title="This Site is using wi.li."><strong>&lt;wi.li. /&gt;</strong></a>.</p>
					
					We also ask that you register by sending an e-mail
					to:
								cffollis@norisberghen.it or me@diegolamonica.info
								
					with the address of the web sites you plan to use the scripts on.
					The websites will be listed on the script's pages at
					http://www.diegolamonica.info and http://www.norisberghen.it.
					A complete tutorial is available at:
					
									1. http://www.norisberghen.it/it/?p=430
									2. http://www.diegolamonica.info/tutorials/?id=
					
					Diego La Monica and Carlo Filippo Follis are not to be held
					liable for any damage, loss or wrong caused or related to any
					use or misuse of the script. 
				*/

function wiLi(){

	/*------------------------------------------------------------------------------+ 
		Start of Property block 
	
		WARNING!!! 
	
		DO NOT CHANGE VARIABLE NAMES. IF NECESSARY CHANGE ONLY ATTRIBUTES VALUE
	
	+------------------------------------------------------------------------------*/
	
	wiLi.id = 'nwlm';
		// Original ID 'nwlm' for HTML code: <span id="nwlm">&nbsp;</span>

	wiLi.accessKey = 'w';
		// Defines the access key value required for accessibility validation

	wiLi.newWindowHTMLDescription = 'Clicca per aprire i link esterni in questa finestra';
		// Text/Image/HTML shown inside the action link when the status of the 
		// script is to open links in a new window.
		
	wiLi.sameWindowHTMLDescription = 'Clicca per aprire i link esterni in una nuova finestra';
		// Text/Image/HTML shown inside the action link when the status of the 
		// script is to open links in the same window.

	wiLi.newWindowTitleDescription = 'Apre tutti i link definiti come esterni in una nuova finestra';
		// As for newWindowHTMLDescription property, but for the title attribute of the link.
		// If this value is empty (but always must be defined) the title attribute will not be generated

	wiLi.sameWindowTitleDescription = 'Apre indiscriminatamente tutti i link nella stessa finestra';
		// The same than newWindowTitleDescription but related to property sameWindowHTMLDescription
		
	wiLi.className = 'wl-ext';
		// If defined each link identified as external will be marked with this class too.
		// It is usefull both for presentation purpose and debugging
		
	wiLi.noNewWindowsURL = new Array();
		// Network sites array that must be open in the same window always.

	/* ***************************************************
		Features available from Version 1.1.0
	*************************************************** */
	wiLi.forcedNewWindowURL = new Array(
		'/tutorials/examples/',
		'/external_dir/'
	);
		// Sites that need to open however in other window.
		
	wiLi.newWindowForceCSSClass = 'wili-forced';
		// Il links has this class specified, WiLi, will force to open those 
		// links always in a new window
	
	wiLi.popupWindowName = '';
		// If not specified wili will'open each external link in a new window

	wiLi.popupProperties = '';
		// Optional properties to apply to opened popup window
		
	/*------------------------------------------------------------------------------+
		End of Property block 
	
		WARNING!!! 
		DO NOT MAKE CHANGES AFTER HERE. THE SCRIPT COULD NOT WORK ANYMORE
		
	+------------------------------------------------------------------------------*/

	wiLi.noNewWindowsURL[wiLi.noNewWindowsURL.length] = window.location.protocol + '//' + window.location.host;


	/* Cookies Manager */

	wiLi.setCookie = function (c_name,value,expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	}



	wiLi.getCookie = function (c_name){
		if (document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){ 
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
    				return unescape(document.cookie.substring(c_start,c_end));
			} 
		}
		return "";
	}

	/* End Cookies Manager */

	/* Link Manager */	
		
	wiLi.alterLinks = function(){
		
		var links = window.document.getElementsByTagName('a');
		for(var i = 0; i<links.length; i++){
			if( links[i].href != null && links[i].onclick == null){
				var escludi = false;
				for(var j = 0; j < wiLi.noNewWindowsURL.length; j++){
					var escluso = wiLi.noNewWindowsURL[j]; 
					if(links[i].href.substr(0, escluso.length) == escluso){
						escludi = true;
						break;
					}
				}
				
				if(escludi){
					// Only if link is excluded by opening in new window
					// we make sure it's not into forcing array
					
					for(var j = 0; j < wiLi.forcedNewWindowURL.length; j++){
						var incluso = wiLi.forcedNewWindowURL[j]; 
						if (incluso.substr(0,1) == '/') incluso = window.location.protocol + '//' + window.location.host + incluso;
						if(links[i].href.substr(0, incluso.length) == incluso){
							escludi = false;
							break;
						}
						if(links[i].className.indexOf(wiLi.newWindowForceCSSClass)>-1){
							escludi = false;
							break;
						}						
					}
				}
				
				if(!escludi){
					if(links[i].onkeypress!= null){
						// Extends onkeypress event
						links[i].realkeypress = links[i].onkeypress;
					}
					links[i].onkeypress =  function (event){
						if(this.realkeypress != null) this.realkeypress(event);
						
						if(window.event){ // IE
							var keynum = event.keyCode
						}else if(event.which){ // Netscape/Firefox/Opera
							var keynum = event.which
						}
						if(keynum == 13 || keynum==32){
							wiLi.openLink(this);
							return false;
						}
					}
					links[i].onclick = function (){ 
						return wiLi.openLink(this);
					}; 
					
					if(wiLi.className!=''){
						links[i].className += (links[i].className!=''?' ':'') + wiLi.className;
					}
				}
			}
		}
	}
	
	wiLi.openLink = function(link){
		if(wiLi.openNewWindow || link.className.indexOf(wiLi.newWindowForceCSSClass)> -1){
			window.open(link.href, wiLi.popupWindowName, wiLi.popupProperties);
			return false;
		}
		return true;
	}
	
	wiLi.newWindowYesNo = function(firstTime){
		if(firstTime){
			wiLi.openNewWindow = wiLi.getCookie('wiLi')=='true'?true:false;
		}else{
			wiLi.openNewWindow = !wiLi.openNewWindow;
			wiLi.setCookie('wiLi', wiLi.openNewWindow,9000);
		}
		var l = document.getElementById(wiLi.id);
		var buffer = wiLi.sameWindowHTMLDescription;
		var title = wiLi.sameWindowTitleDescription;
		if(wiLi.openNewWindow){
			buffer = wiLi.newWindowHTMLDescription;
			title = wiLi.newWindowTitleDescription;
		}
		title = title.replace(/"/g, "&quot;");
		buffer = '<a accesskey="' + wiLi.accessKey + '"' +
					(title!=''?' title="' + title + '"':'') +
					' href="#" onclick="return wiLi.newWindowYesNo();">' + 
					buffer + '<\/a>';
		
		l.innerHTML = buffer;
		return false;

	}
	wiLi.init = function(){
		wiLi.newWindowYesNo(true);
		wiLi.alterLinks();
	}
	
	wiLi.init();
}

wiLi_autoStart = true;
	// If true, wiLi will initalize by itself when page ends to load.

/* AttachEvent */
if(wiLi_autoStart){
	if (document.addEventListener) { // Mozilla, Firefox and Opera
		window.addEventListener("load",function(){ wiLi() }, false);
	} else {
		window.attachEvent("onload", function(){ wiLi() } );
	}
}else{
	alert('wiLi not start');
}
