
jQuery.fn.defuscate = function() { 
   return this.each(function(){ 
     var email = String($(this).html()).replace(/\s*\(.+\)\s*/, "@");
	 email = email.replace(/(\s+)/g, '');
	if (is_email(email))
     	$(this).before(' <a href="mailto:' + email + '">' + email + "</a>").remove(); 
   }); 
}; 

function is_email(email)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		return false;
	}
	return true;
}

function nunaRedirect (url)
{
	top.location = url;
}

function setNunaCookie (cookieName, cookieValue)
{
	$.cookie(cookieName, cookieValue, { expires: 365, path: '/' });
}

$().ready(function() {
	// manage cookie when change language
	$("a[id='link_it'], a[id='link_es'], a[id='link_en']").click(
		function () {
			var linkId = $(this).attr('id');
			var newLang = linkId.substring(linkId.indexOf('_')+1,linkId.length);
			$.cookie('lingua', newLang, { expires: 365, path: '/' });
			return true;
		});
  // add pdf class to all pdf link	
	$('a[href$=".pdf"]:not(a.no_pdf_class)').addClass('pdf');
	$('a[href$=".pdf"]').click(
	    function () {
			url = this.href;
			if (url.indexOf('download.php') >= 0)
			{
				s=screen;
				sw=s.width;
				sh=s.height;
				sd=s.colorDepth;
				url +="&screen="+sw+"x"+sh+"&color="+sd;
				document.location=url;
				return false;
			}
		}
	);
  
	// add email links
	$(".email").defuscate();
  // add external class to all external link	
  var hostname = window.location.hostname;
  var hreflocal = "[href*="+hostname+"]";
  var asimage = ':has("img")';
  $("#content a[href^='http']")
      .not(hreflocal)
  	  .attr('target',"_blank")
  	  .not(asimage)
  	  .addClass("external")
  	  ;

  // fade in header image if any
  $('#contentHeader #header_image').hide()
    
  // if it has an anchor in url hilite div:
  var hh = document.location.hash;
  if (hh && hh != '#page') {
    setTimeout(function(){
      hiliteElement(hh)},400);
  }

});



$(window).load(function () { 
  $('#contentHeader #header_image').fadeIn(1000);
});

function hiliteElement(e)
{
  $(e).css( "backgroundColor",'#FFFF99' );
  setTimeout(function(){
    $(e).animate( { backgroundColor: '#FFFFFF' }, 1000)
    },500);
}

// log visitor
function logVisitor()
{
	s=screen;
	sw=s.width;
	sh=s.height;
	sd=s.colorDepth;
	ref = escape(document.referrer);
	url = escape(document.location);
	num=Math.round(Math.random()*0x7fffffff);
	var img=new Image(1,1);
	img.src="/utils/log.php?js=1"+"&ref="+ref+"&url="+url+"&screen="+sw+"x"+sh+"&color="+sd+"&num="+num;
	img.onload=function() { return; }
}
logVisitor();

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/**
 * End Cookie plugin
 */

