// ************************ NEROAVORIO JAVASCRIPT FUNCTION LIBRARY ******************
// ************************ v. 1.5                                 ******************

// ************************ 1) pulse_elements v 1.1
// ************************ 2) blink_elements v 1.0 (OBSOLETE - kept for possible future uses
// ************************ 3) delay jquery plugin v 1.0
// ************************ 4) simple image preloader jquery plugin v 2.0

// pulse_elements (homemade using jquery) (REQUIRES jQuery library)
// v 1.1 aggiunto parametro pausetime, che aggiunge un timeout parametrizzato prima di CIASCUN fade (sia In che Out) ovvero introduce una PAUSA
//       a piacere tra i pulse. il parametro speed continua ad essere applicato 1) al fadeIn iniziale 2) al fadeTo(0) 3) al fadeTo (1)

		function pulse_elements(speed,pausetime) {
			$(".pulse").fadeIn(speed);
			function pulse_element() {
				$(this).delay(pausetime,function(){
					$(".pulse").fadeTo(speed, 0,function(){
						$(this).delay(pausetime,function(){
							$(".pulse").fadeTo(speed, 1, pulse_element);
						});
					});
				});
			}
			pulse_element();
		}
		
// Delay Plugin 1.0 for jQuery
// - http://www.evanbot.com
// - © 2008 Evan Byrne
// USAGE:   $(this).delay(1000,function(){
//				$.......;
//			);
jQuery.fn.delay = function(time,func){
	return this.each(function(){
		setTimeout(func,time);
	});
};

// Simple Image Preloader Plugin 2.0 for jQuery (1 dec. 2009)
// - http://www.mattfarina.com
// - © 2009 Matt Farina
// USAGE: $.preLoadImages("image1.gif", "/path/to/image2.png");
//     or $.preloadImages(<?php print na_mfpreload($folders_array)?>);  // $folders_array generato da na_mfpreload (mf sta per matt farina :D)
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preloadImages = function() {
    var args_len = arguments.length;
	var loaded   = 0;  	
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function($) {
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        var baseHref = '';
        if (jQuery('base').attr('href') != undefined)
            baseHref = jQuery('base').attr('href');
        jQuery('a').each(function() {
            var href = jQuery(this).attr('href');
            if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                jQuery(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
                    if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = href; }, 200);
                        return false;
                    }
                });
            }
            else if (href && href.match(/^mailto\:/i)) {
                jQuery(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
                });
            }
            else if (href && href.match(filetypes)) {
                jQuery(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    var filePath = href;
                    _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                    if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = baseHref + href; }, 200);
                        return false;
                    }
                });
            }
        });
    });
}
