
/**
@public
@brief Cerca un nodo in base ad un attributo del nodo stesso
@param tagFather nodo padre da cui partire
@param nomeTag nome del tag da cercare
@param nomeAttributo nome dell'attributo del nodo da cercare
@param valoreAttributo valore della proprietà nomeAttributo del nodo da cercare
@return il nodo oppure @c false se non lo trova

Funzione ricursiva che cerca in tutti i figli di tutti i nodi figli del padre passato come parametro. E' possibile non passare nomeAttributo e valoreAttributo: in questo caso non vengono fatti i confronti con l'attributo
*/
function searchChild (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection) {
	var chiaveDebug = nomeTag + "-" + nomeAttributo + "-" + valoreAttributo;
	var dataCorrente1 = new Date();
	var beginTime = dataCorrente1.getTime();
	var r = performSearchChilds (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection);
	var dataCorrente2 = new Date();
	var endTime = dataCorrente2.getTime();

	return r;
}
function performSearchChilds (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection) {
	var collection = Array();
	nomeTag = nomeTag.toLowerCase();
	for (var i=0; i < tagFather.childNodes.length; i++) {
		var elem = tagFather.childNodes[i];

		if (elem.tagName == undefined) {
			continue;
		};

		if (elem.tagName.toLowerCase() == nomeTag.toLowerCase() || nomeTag == '*') {
			if (nomeAttributo == undefined && valoreAttributo == undefined) {
				a = elem;
			} else {
				if (elem.tagName.toLowerCase() == 'label' && nomeAttributo.toLowerCase() == 'for' && (valoreAttributo == undefined || (elem.getAttribute('htmlFor') == valoreAttributo ^ elem.getAttribute('for') == valoreAttributo))) { // in InternetExplorer non esiste elem.getAttribute('for')
						var a = elem;
				} else if (valoreAttributo == undefined || nomeAttributo.toLowerCase() == 'class') {
					var re = '(^|\\s)'+valoreAttributo+'(\\s|$)';
					if (elem.className.match(new RegExp(re)))
						var a = elem;

				} else if (valoreAttributo == undefined || (nomeAttributo.toLowerCase() == 'checked' && elem.checked != undefined && elem.checked == valoreAttributo)) {
					var a = elem;
				} else if (valoreAttributo == undefined || (elem.getAttribute(nomeAttributo) == valoreAttributo ) ) {
					var a = elem;
				} else {
					if (elem.getAttribute(nomeAttributo) != undefined && isRegExpString(valoreAttributo)) {
						var re = valoreAttributo.replace(/\/$/, '').replace(/^\//,'');
						if (elem.getAttribute(nomeAttributo) != undefined && elem.getAttribute(nomeAttributo).search(new RegExp(re)) != -1) {
							var a = elem;
						}
					};
				};
			}
			if (retrieveCollection != true) {
				if (a != undefined) return a;
			} else {
				if (a != undefined) collection.push(a);
			};
		};

		if (elem.childNodes != undefined && elem.childNodes.length > 0)
			if (elem.childNodes.length > 0) {
				var b = performSearchChilds(elem, nomeTag, nomeAttributo, valoreAttributo, true);
				if (b != false)
					if (retrieveCollection != true)
						return b.pop();
					else if (b.length > 0)
						for (var q1 in b)
							if (typeof b[q1] == 'object')
								collection.push(b[q1]);
			};
		//
		a = undefined;
	}
	if (collection.length > 0) {
		var ww = Array();
		for (var q in collection)
			ww[q] = collection[q];
		return ww;
	}
	return false;
}

/**
@public
@brief restituisce una collection di nodi che hanno i parametri specificati
@see searchChild
@return un array con la collection dei nodi oppure @c false se non viene trovato nessun nodo
*/
function searchChildsCollection (tagFather, nomeTag, nomeAttributo, valoreAttributo) {
	var ee = searchChild(tagFather, nomeTag, nomeAttributo, valoreAttributo, true);
	return ee;
}

/**
@public
@brief Riconosce se la stringa passata come parametro è un'espressione regolare
@param str La stringa da verificare

La stringa deve iniziare con / e finire con / seguita opzionalente da g,i,m
*/
function isRegExpString (str) {
	if (str.search(/^\/.*\/[gim]*/) == -1)
		return false;
	return true;
}

/* se la si vuole usare bisogna chiamarla dopo la chiamata al file di jquery!!!
thanks to http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
uso: jQuery.preloadImage('path/img1.png', 'path/img2.jpg', '....');
*/
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preloadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)





/* thanks to: http://jacwright.com/projects/javascript/date_format

var myDate = new Date();
alert(myDate.format('M jS, Y')); // May 11th, 2006 

*/
Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};
Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Yet Supported"; },
	// Week
	W: function() { return "Not Yet Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Yet Supported"; },
	// Year
	L: function() { return (((this.getFullYear()%4==0)&&(this.getFullYear()%100 != 0)) || (this.getFullYear()%400==0)) ? '1' : '0'; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Yet Supported"; },
	g: function() { return this.getHours() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Yet Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':' + (Math.abs(this.getTimezoneOffset() % 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() % 60)); },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return this.format("Y-m-d") + "T" + this.format("H:i:sP"); },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
};


function urlSegment(numSegment) {
	var url = document.location.href;
	url = url.replace(/http:\/\//, '');
	url = url.split('/');
	
	if (numSegment == undefined)
		return url;
		
	if (url[numSegment] == undefined)
		return false;
	
	return url[numSegment];

}
function url_normalize (str) {
	if (typeof str != 'string')
		return false;
	str = str.replace(/à/, 'a');
	str = str.replace(/è/, 'e');
	str = str.replace(/é/, 'e');
	str = str.replace(/ì/, 'i');
	str = str.replace(/ò/, 'o');
	str = str.replace(/ù/, 'u');
	str = str.replace(/ç/, 'c');
	
	str = str.replace(/[_\W]+/, '-').toLowerCase();
	return str;
}
