/**
 * Forms Framework Javascript Handling Object
 */
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
		    if(this[i]==obj){
		        return i;
		    }
		}
		return -1;
	}
}

if(!String.trim){
	String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
}

var ff = (function () {
// private vars
var loaded_css		= undefined;

var plugins			= undefined;
var plugins_loads	= undefined;
var plugins_inits	= undefined;

function pluginInit(id) {
	var inits ;

	if ((inits = plugins_inits.get(id)) !== undefined) {
		for (var i = 0; i < inits.length; i++) {
			inits[i]();
		}
		plugins_inits.set(id, []);
	}
}

function initEvents(key) {
	var parts = key.split(".");
	if (parts.length > 1 && parts[0] == "ff") {
		var path = "ff";
		for (var i = 1; i < parts.length; i++) {
			path += "." + parts[i];
			if (eval(path + ' === undefined'))
				eval(path + " = {};");

			if (eval('typeof(' + path + ')') === "object" && eval(path + ".events === undefined"))
				eval('jQuery.extend(true, ' + path + ', ff.ffEvents());');
		}
	}
}

var that = { // publics

"site_path"		: undefined,
"theme"			: undefined,
"language" 		: undefined, 
"layer" 		: undefined, 
"phpsession"	: {
	"name"			: undefined,
	"value"			: undefined
},
"struct"		: undefined,

"initFF" : function (params) {
	that.site_path				= params.site_path;
	that.theme					= params.theme;
	
	if(params.site_path.length > 0 && params.site_path != '/') {
		that.page_path 			= window.location.pathname.substr(params.site_path.length);
	} else {
		that.page_path 			= window.location.pathname;
	}
	that.domain 				= window.location.hostname;
	that.language 				= params.language;
	that.layer 					= params.layer;
	that.phpsession.name		= params.phpsession.name;
	that.phpsession.value		= params.phpsession.value;
	that.struct					= params.struct;
	that.js_path = that.site_path + '/themes/' + that.theme + '/javascript';

	// ------------------------------------------------------------------------------------------------------
	// le estensioni sugli eventi devono essere caricate per prime
	that.pluginLoad("ff.ffEvent",		that.site_path + "/themes/library/ff/ffEvent.js",	undefined, false);
	that.pluginLoad("ff.ffEvents",		that.site_path + "/themes/library/ff/ffEvents.js",	undefined, false);
	// ------------------------------------------------------------------------------------------------------

	//that.pluginLoad("ff.ffField",		that.site_path + "/themes/library/ff/ffField.js",	undefined, false);
	//that.pluginLoad("ff.ffPage",		that.site_path + "/themes/library/ff/ffPage.js",	undefined, false);
	//that.pluginLoad("ff.ffGrid",		that.site_path + "/themes/library/ff/ffGrid.js",	undefined, false);
	//that.pluginLoad("ff.ffRecord",		that.site_path + "/themes/library/ff/ffRecord.js",	undefined, false);
	//that.pluginLoad("ff.ffDetails",		that.site_path + "/themes/library/ff/ffDetails.js", undefined, false);

	if (params.libs !== undefined)
		params.libs.each(function (key, value) {
			that.pluginLoad(key, that.site_path + value, undefined, false);
		});
},

"extend" : function (object) {
	jQuery.extend(true, ff, object);
},

"pluginLoad" : function (id, source, callback, async) {
	if (callback !== undefined)
		that.pluginAddInitLoad(id, callback);

	if (plugins.isset(id) === undefined) {
		plugins.set(id, false);
		//jQuery.getScript(source, function (data, textStatus) {

		var objFF = id.split(".");
		var strFF = '';
		var objFF_is_loaded = true;

		for (var x=0; x<objFF.length; x++)
		{
			if(strFF == '') {
				strFF = objFF[x];
				if(strFF == 'jquery') {
					strFF = 'jQuery';
				}

				strFF = 'window' + '["' + strFF  + '"]';
			} else {
				strFF = strFF + '["' + objFF[x] + '"]';
			}

			try {
				if(eval(strFF) === undefined) {
					objFF_is_loaded = false;
					break;
				}
			} catch(error){
				objFF_is_loaded = false;
				break;
			}
		}		

		if(objFF_is_loaded) {
			if (id === "ff.ffEvents")
				ff.extend(that.ffEvents());

			ff.pluginInitLoad(id);
		} else {
			if(ff.site_path !== undefined && ff.site_path.length > 0 && source.indexOf("/") == 0 && source.indexOf(ff.site_path) != 0)
				source = ff.site_path + source;

			jQuery.ajax({
				"async": async,
				"url": source,
				"dataType": "script",
	//			"cache": false,
				"success": function (data) {
					if (id === "ff.ffEvents")
						ff.extend(that.ffEvents());

					//ff.pluginInitLoad(id);
					jQuery(document).append('<script type="text/javascript">ff.pluginInitLoad("' + id + '");</script>');
				}
			});
		}
	}
},

"pluginAddInitLoad" : function (id, callback) {
	if (plugins.get(id) !== true) {
		if (plugins_loads.isset(id) === undefined)
			plugins_loads.set(id, []);
		
		plugins_loads.get(id).push(callback);
	} else
		callback();
},

"pluginInitLoad" : function (id) {
	if (id !== "ff.ffEvent" && id !== "ff.ffEvents")
		initEvents(id);

	var loads;
	if ((loads = plugins_loads.get(id)) !== undefined) {
		for (var i = 0; i < loads.length; i++) {
			loads[i]();
		}
		plugins_loads.set(id, []);
	}
	pluginInit(id);
	plugins.set(id, true);
},

"pluginAddInit" : function (id, callback) {
	if (plugins.get(id) !== true) {
		if (plugins_inits.isset(id) === undefined)
			plugins_inits.set(id, []);

		plugins_inits.get(id).push(callback);
	} else
		callback();
},

"CSSload" : function(link, callback) {
    var cssLoaded = false;
    try{
        if ( link.sheet && link.sheet.cssRules.length > 0 ){
            cssLoaded = true;
        }else if ( link.styleSheet && link.styleSheet.cssText.length > 0 ){
            cssLoaded = true;
        }else if ( link.innerHTML && link.innerHTML.length > 0 ){
            cssLoaded = true;
        }
    }
    catch(ex){ }
    if ( cssLoaded) {
    	if(callback !== undefined)
        	callback();
    } else {
        setTimeout(function(){
            ff.CSSload(link, callback);
        }, 10);
    }
},


"injectCSS" : function (id, source, callback, media) {
	if (loaded_css.get(id) !== true) {
		loaded_css.set(id, true);

		if(ff.site_path !== undefined && ff.site_path.length > 0 && source.indexOf("/") == 0 && source.indexOf(ff.site_path) != 0)
			source = ff.site_path + source;
		
		if ('\v' == 'v') /* ie only */ {
			var css = document.createStyleSheet(source);
			
			if(media !== undefined) 
				css.media = media;
				
            if(callback !== undefined)
        		callback();
		} else {
			if(media !== undefined) {
				var attrMedia = 'media="' + media + '"';
			}

			var css = jQuery('<link rel="stylesheet" href="' + source + '" ' + attrMedia + ' />');
			jQuery('head').append(css);
			
			ff.CSSload(css.get(0), callback);
		}
    	//if(callback !== undefined)
        //	callback();

		/*
		var async = true;
		var css = document.createElement('link');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('href', source);
		css.addEventListener('load', function() { alert('foo'); }, false);
		document.getElementsByTagName('head')[0].appendChild(css);
		
		jQuery.ajax({
				"async": async,
				"url": source,
				"dataType": "text",
				"success": function (data) {
//					jQuery("head").append('<style type="text/css">' + data + '</style>');
					if (callback !== undefined)
						callback();
				}
			});*/
	} else {
		if (callback !== undefined)
			callback();
	}
},

"addLoadEvent" : function (func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
},

"submitProcessKey" : function (e, button) {
    if (null == e)
        e = window.event;
    if (e.keyCode == 13)  {
        document.getElementById(button).click();
        return false;
    }
},

"clearComponent" : function (component) {
	if (ff.struct.get(component) !== undefined) {
		if (ff.struct.get(component).type == "ffGrid")
			try {
				ff.ffPageNavigator.deleteNavigator(component);
			} catch (e) {};

		ff.struct.get(component).fields.each( function (key, field) {
			ff.doEvent({
				"event_name"	: "onClearField",
				"event_params"	: [component, key, field]
			});
		});
		ff.struct.unset(component);
	}
},

"utf8" : {
	"encode" : function(s) {
	  return unescape(encodeURIComponent(s));
	},

	"decode" : function(s) {
	  return decodeURIComponent(escape(s));
	}
},

// *******************************
//  Hash Table - version 1

"hash" : function () {
	// privates

	var that = {
	// publics

	"keys"		: [],
	"values"	: [],
	"length"	: 0,

	//functions
	"set" : function (key, value) {
		var index = that.keys.indexOf(key);
		if (index > -1) {
			that.values[index] = value;
			return index;
		} else {
			that.keys.push(key);
			that.values.push(value);
			that.length++;
			return that.length - 1;
		}
	},

	"get" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1)
			return that.values[index];
		else
			return undefined;
	},

	"indexget" : function (index) {
		if (index > 0 && index < that.length)
			return that.values[index];
		else
			return undefined;
	},

	"isset" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1)
			return index;
		else
			return undefined;
	},

	"find" : function (value, offset) {
		var index = that.values.indexOf.apply(that.values, arguments);
		if (index > -1) {
			return {"index" : index, "key" : that.keys[index]};
		} else {
			return undefined;
		}
	},

	"keyfind" : function (value, key) {
		var index;

		if (key === undefined)
			index = that.values.indexOf(value);
		else {
			index = that.keys.indexOf(key);
			if (index > -1)
				index = that.values.indexOf(value, index);
			else
				return undefined;
		}

		if (index > -1) {
			return {"index" : index, "key" : that.keys[index]};
		} else {
			return undefined;
		}
	},

	"unset" : function (key) {
		var index = that.keys.indexOf(key);
		if (index > -1) {
			that.keys.splice(index, 1);
			that.values.splice(index, 1);
			that.length--;
			return that.length;
		} else {
			return undefined;
		}
	},

	"indexunset" : function (index) {
		if (index > 0 && index < that.length)
			return undefined;
		else {
			that.keys.splice(index, 1);
			that.values.splice(index, 1);
			that.length--;
			return that.length;
		}
	},

	"each" : function (func) {
		var tmp_keys	= that.keys.slice();
		var tmp_values	= that.values.slice();
		var l = tmp_keys.length;

		for (var i = 0; i < l; i++) {
			func(tmp_keys[i], tmp_values[i], i);
		}

		return l;
	},

	"clear" : function () {
		that.keys.length = 0;
		that.values.length = 0;
		that.length = 0;
	}
};

return that;
}
// ********* Hash Table *********

}; // publics' end

loaded_css		= that.hash();
plugins			= that.hash();
plugins_loads	= that.hash();
plugins_inits	= that.hash();

return that;

// code' end.
})();

