/**
	Script Name: 	WiLi - Windowed Links - 
	version: 		1.2.0
	version date:	2007-05-20
	+------------------------------------------------------------------------------
	Author: 				Diego La Monica (http://diegolamonica.info)
	Based on idea by: 		Carlo Filippo Follis (http://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]
	Il dettagli sulla licenza d'uso è disponibile ai seguenti link:
		1. http://www.norisberghen.it/it/?p=430
		2. http://diegolamonica.info/wili/

	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=9

	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]
	End User License details are available at: 
		1. http://www.norisberghen.it/it/?p=430
		2. http://diegolamonica.info/wili/

	A complete tutorial is available at:
		1. http://www.norisberghen.it/it/?p=430
		2. http://www.diegolamonica.info/tutorials/?id=9
	
	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.
	The script comes with absolutely no warranty to the extent 
	permitted by applicable law.
	If you do not agree with the above, we request that you do 
	not use the script.
	*/

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

function wiLi(){

	/*------------------------------------------------------------------------------+ 
		Start of Property block 
	
		WARNING!!! 
	
		DO NOT CHANGE VARIABLE NAMES. IF NECESSARY CHANGE ONLY ATTRIBUTES VALUE
	
	+------------------------------------------------------------------------------*/
	
	wiLi.id = 'windowedLinks';

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

	wiLi.newWindowHTMLDescription = 'Apri i collegamenti esterni in una nuova 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 = 'Apri tutti i collegamenti in questa 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 */

	/* System Functions */
	
	wiLi.isAvailbale = function(){
		try{
			var tag = document.getElementById(wiLi.id);
			clearInterval(wiLi_interval);
			wiLi_interval = 0;
			wiLi.init();
			return (tag!=null);
		}catch(e){
			return false;
		}
	}

	wiLi.removeLastChar = function(textArray, lastChar){
		
		for(var i=0; i < textArray.length; i++){
			var l = textArray[i].length;
			if(textArray[i].substr(l-1,1) == lastChar) textArray[i] = textArray[i].substr(0,l-1);
		}
		return textArray;
	}

	wiLi.split = function(stringa, separatore){
		var newArray = stringa.split(separatore);
		return wiLi.removeLastChar(newArray, separatore);
	}
	
	/* End System Functions */
	
	/* 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){
			/*
			 * Check if properties are defined on class attribute
			 */
			var popupProperties = wiLi.popupProperties;
			if(classes!=''){
				var firstProperty = true;
				var classes =wiLi.split(link.className, ' ');
				for(i = 0; i < classes.length; i++){
					if(classes[i].substr(0,5) == 'wili-' && classes[i] != 'wili-forced'){
						var suffix = classes[i].substr(5,classes[i].length);
						if(firstProperty){
							popupProperties = '';
							firstProperty = false;
						}
						if(popupProperties != '') popupProperties +=',';
						var params = wiLi.split(suffix,'-');
						popupProperties += params[0] + '=' + params[1];
					}
				}
			}
			window.open(link.href, wiLi.popupWindowName, 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();
	}
	
	if (wiLi_started && wiLi_autoStart || !wiLi_autoStart){
		if(wiLi_interval != 0) clearInterval(wiLi_interval);
		wiLi_interval = 0;
		wiLi.init();
	}
}

/*  
 * !!!! WARNING !!!!
 * STARTUP CODE - DO NOT TOUCH / CODICE DI AVVIO - NON MODIFICARE
 */

var wiLi_started = false;
var wiLi_interval = 0;

if(wiLi_autoStart){
	wiLi();
	wiLi_interval = setInterval('try{ wiLi.isAvailable() }catch(e){ }', 10);

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