
	//
	// definizione del NS FlexJS
	//

FlexJS = window.FlexJS || {};

FlexJS.isIE = jQuery.browser.msie;
FlexJS.isGecko = jQuery.browser.mozilla;

	//
	// accesso alla configurazione
	//

FlexJS.AppConfigure = window.FlexJS.AppConfigure || {};

FlexJS.AppConfigure.Get = function (pName) {

	return gAppConfigure[pName];
}


	//
	// loader dinamico
	//

/*

	FlexJS.Loader.LoadJS('path/to/file.js', function () { use JS });

*/

FlexJS.Loader = window.FlexJS.Loader || {};

FlexJS.Loader.sCache = [];
FlexJS.Loader.sImageCache = [];

	// carica un JS esterno
	// siccome JS non è multithread, uso la callback da posizionare sull'onload
	// dell'elemento script, in modo da creare una continuazione

FlexJS.Loader.LoadJS = function(path, cb, useUniqueID) {

	path = path instanceof Array ? path : [path];
	useUniqueID = useUniqueID || false;

		// se ho già caricato l'include mi limito ad eseguire la CB

	for (var i = 0, m = path.length; i < m; i++) {

		var fName = path[i];

		if (FlexJS.Loader.sCache[ fName ]) {

				// se siamo all'ultimo elemento

			if (i == m - 1) {

				if (cb) cb();

				return;
			}
		}

		var script = document.createElement('script');

			// aggiunge al nome del file un identificativo numerico univoco
			// per forzare il caricamento (no cache) per il debug

		script.type = 'text/javascript';
		script.src = FlexJS.AppConfigure.Get('kBaseURL') + fName + (useUniqueID ? ('?v=' + FlexJS.Utils.Random(1, 9999)) : '');

			// metto la callback solamente sull'ultimo elemento

		if (i == m - 1 /* && cb */) {

			var f = function() {
				
				FlexJS.Loader.sCache[ fName ] = true;
				if (cb) cb();
			}
/*
			if (FlexJS.isIE)
				script.onreadystatechange = function () {
					if (script.readyState == 'loaded' || script.readyState == 'complete') {
						f(); //cb();
					}
				}
			else
				$(script).load(f  );
*/
		} else {

			var f = function() {
				
				FlexJS.Loader.sCache[ fName ] = true;
			}
/*
			if (FlexJS.isIE)
				script.onreadystatechange = function () {
					if (script.readyState == 'loaded' || script.readyState == 'complete') {
						f(); //cb();
					}
				}
			else
				$(script).load(f );
*/
		}
//			script.onload = cb || function () {};

			if (FlexJS.isIE)
				script.onreadystatechange = function () {
					if (! script.p && (script.readyState == 'loaded' || script.readyState == 'complete')) {
						f(); //cb();
						script.p = true;
					}
				}
			else
				$(script).load(f /* cb */ );

		document.getElementsByTagName('head')[0].appendChild(script);  

//		FlexJS.Loader.sCache[ FlexJS.Loader.sCache.length ] = fName;
//		FlexJS.Loader.sCache[ fName ] = true;
	}
}

	//
	// utility generiche
	//

FlexJS.Utils = window.FlexJS.Utils || {}

FlexJS.Utils.InArray = function (needle, haystack) {

	for (var i = 0, m = haystack.length; i < m; i++)
		if (haystack[i] === needle)
			return true;

	return false;
}

FlexJS.Utils.Random = function (min, max) {

	if (! min) min = 0;
	if (! max) max = 10e10;

	return Math.round(max * Math.random() + min)
}

	//
	// apertura in una nuova finestra dei link
	//

FlexJS.TBHack = window.FlexJS.TBHack || {}

FlexJS.TBHack.sKeyCode = [13, 32];

FlexJS.TBHack.Init = function() {

	$('a.target_blank').each(function() {

		this.title += FlexJS.AppConfigure.Get('kBlankLinkMessage');

		$(this).click(function(e) {

			FlexJS.TBHack.Open(this.href);

			return e.preventDefault();
		});

		$(this).keypress(function(e) {

			if (FlexJS.Utils.InArray(e.keyCode, FlexJS.TBHack.sKeyCode)) {

				FlexJS.TBHack.Open(this.href);

				return e.preventDefault();
			}
		});
	});
}

FlexJS.TBHack.Open = function(url) {

		// apre in una finestra nuova

	window.open(url, 'TBH' + FlexJS.Utils.Random());
}

	// funzione usata per replicare il comportamento del click nel keypress

FlexJS.TBHack.InitKeypress = function() {

	$('.InputIMG').each(function() {

		$(this).keypress(function(e) {

			if (FlexJS.Utils.InArray(e.keyCode, FlexJS.TBHack.sKeyCode)) {

				this.click();

				return e.preventDefault();
			}
		});
	});
}

$().ready(function() {

	FlexJS.TBHack.Init();
	FlexJS.TBHack.InitKeypress();
});

