
	// definizione del NS FlexJS

FlexJS = window.FlexJS || {};

	// inclusioni richieste

$().ready(function () {

//	FlexJS.Loader.LoadJS('js/jq/jDimensions.js');
});

	// widget hide/show
	
FlexJS.HideShow = function(cnt, hdlo, hdlc) {

	this.mContent = $('#' + cnt)[0];
	this.mHandleOpen = $('#' + hdlo)[0];
	this.mHandleClose = $('#' + hdlc)[0];

	var me = this;

	$(this.mHandleOpen).click(function() { me.Open(); });
	$(this.mHandleClose).click(function() { me.Close(); });
	
	this.Close();
};

FlexJS.HideShow.prototype.Open = function() {

	$(this.mContent).show();

	$(this.mHandleOpen).hide();
	$(this.mHandleClose).show();
};

FlexJS.HideShow.prototype.Close = function() {

	$(this.mContent).hide();	

	$(this.mHandleOpen).show();
	$(this.mHandleClose).hide();
};

	//
	// stylesheet handling
	//

FlexJS.CSSUtils = window.FlexJS.CSSUtils || {};

FlexJS.CSSUtils.GetCSS = function(document, name) {

	var ss = null;
	for (var i = 0; i < document.styleSheets.length; i++) {

			// nel caso di stili inline, .href non è definito

		if (document.styleSheets[i].href && document.styleSheets[i].href.indexOf(name) !== -1) {

			ss = document.styleSheets[i];
			break;
		}
	}

	return ss;
};

FlexJS.CSSUtils.AddCSS = function(document, title) {

	var node = document.createElement('style');

	node.type = 'text/css';
	node.rel = 'stylesheet';
	node.media = 'screen';
	node.title = title;

		// inserisce l'elemento nella sezione 'head'
		// creandola se necessario
		
	var head = document.getElementsByTagName('head');
	if (head.length == 0) {
		
		head = document.createElement('head');
		document.getElementsByTagName('body')[0].parentNode.appendChild(head);
	} else
		head = head[0];

	head.appendChild(node);
	
	return node;
};

FlexJS.CSSUtils.GetCSSRule = function(document, ss, ruleName) {

	ruleName = ruleName.toLowerCase();

	var
		j = 0,
		cssRule = false
	;

	do {

		cssRule = ss.cssRules[j];

		if (cssRule && cssRule.selectorText.toLowerCase() === ruleName)
			return cssRule;

		j++;

	} while (cssRule);

	return false;
};

FlexJS.CSSUtils.AddCSSRule = function(document, ss, rule) {

	document.styleSheets[0].insertRule(rule.cssText, 0);
};

FlexJS.CSSUtils.ClassNames = function(el) {

	return el.className.split(' ');
};

FlexJS.CSSUtils.HasClassName = function(el, sClassName) {

	var p = FlexJS.CSSUtils.ClassNames(el);

	for (var i = 0, l = p.length; i < l; i++)
		if (p[i] === sClassName)
			return true;
	
	return false;
};

	//
	// gestione del clipboadr per Mozilla
	//

FlexJS.Clipboard = window.FlexJS.Clipboard || function () {

		// quando ho il DOM creato, aggiungo l'iframe

	// $(document).ready(function () { FlexJS.Clipboard.Init(); });

	return {};
}();

FlexJS.Clipboard.Init = function () {

	if (typeof FlexJS.Clipboard.sFrameObj === 'undefined') {

		var iframe = document.createElement('iframe');

		iframe.setAttribute('id', 'FlexPasteFrame');
 		iframe.style.border = iframe.style.width = iframe.style.height
			= iframe.style.width = iframe.style.height = '0px';

			// in questo modo quando si fa il paste non ottengo lo scroll
			// nella posizione corrente dell'iframe

		iframe.style.position = 'fixed';

		iframe.onload = FlexJS.Clipboard._InitCont;

		FlexJS.Clipboard.sFrameObj = document.body.appendChild(iframe);
	}
};

FlexJS.Clipboard._InitCont = function () {

	if (typeof FlexJS.Clipboard.sFrameObj !== 'undefined') {

			// abilita la modalità di editing

		try {

			FlexJS.Clipboard.sFrameObj.contentDocument.designMode = 'on';
			
		} catch (err) {}
	}
};

FlexJS.Clipboard.Put = function (text) {

	alert('FlexJS.Clipboard.Put: TBD');

	return;
};

FlexJS.Clipboard.Get = function () {

	var elCD = FlexJS.Clipboard.sFrameObj.contentDocument;

	try {

			// prima di fare il paste, seleziona tutto: in questo modo
			// il nuovo contenuto sostituisce il vecchio

		elCD.execCommand('selectall', false, null);
		elCD.execCommand('paste', false, null);

		FlexJS.HTMLUtils.CleanMozDOM(elCD.body);

		var t = FlexJS.HTMLUtils.GetCleanHTML(elCD.body);

		return t;

	} catch (e) {

			// non ho i permessi per cut/paste
	
		return false;
	}
};

	//
	// pulizia di codice HTML
	//

FlexJS.HTMLUtils = window.FlexJS.HTMLUtils || {};

	// 'font' viene messo in fase di editing e trasformato in 'span'
	// 'b' ed 'i' vengono trasformati rispettivamente in 'strong' ed 'em'

FlexJS.HTMLUtils.sAllowedTags = ['br', 'b', 'strong', 'em', 'i', 'font', 'span', 'a', 'p'];

FlexJS.HTMLUtils.GetCleanHTML = function (el) {

	if (el.nodeType === 3)
		return el.nodeValue;
	else if (el.nodeType === 1) {

		var cnt = '';
		for (var i = 0, m = el.childNodes.length; i < m; i++)
			cnt += FlexJS.HTMLUtils.GetCleanHTML(el.childNodes[i]);

		var out = '';
		var tag = el.tagName.toLowerCase();
		if (FlexJS.Utils.InArray(tag, FlexJS.HTMLUtils.sAllowedTags)) {

				// risolve alcuni problemi in alcune versioni di FF che
				// effettuano l'editing non inserendo i tag corretti
				// ma stilizzando il testo attraverso span

			if (el.style.cssText == 'font-weight: bold;')
				tag = 'strong';
			else if (el.style.cssText == 'font-style: italic;')
				tag = 'em';

				// converte alcuni tag

			switch (tag) {

				case 'b':		tag = 'strong'; break;
				case 'i':		tag = 'em'; break;
				case 'font':	tag = 'span'; break;
			}

				// se il tag ha contenuto lo emetto,
				// se invece non ne ha mi limito al singolo tag
				// a meno che non sia un <p> che Word inserisce senza motivo

			if (cnt != '') {

				var cl = '';
				if (el.className != '')
					cl = " class='" + el.className + "'";

				var href = '';
				if (el.href && el.href != '') {

						// compatibilità IE6/7:
						// se viene aggiunta una componente esterna al link flex
						// la elimina

					var h = el.href.replace(/.*\[\[(.*?)\]\]$/g, "[[$1]]");

					href = " href='" + h + "'";
				}

					// nel caso di 'span' senza classe, lo elimina
					// mentre nel caso di <p> aggiungo un'interruzione di riga

				if (tag == 'span' && cl == '')
					out = cnt;
				else if (tag == 'p')
					out = cnt + '<br />';
				else
					out = '<' + tag + cl + href + '>' + cnt + '</' + tag + '>';

			} else if (tag == 'p')
				out = '';
			else
				out = '<' + tag + ' />';

		} else
			out = cnt;

		return out;
	}

	return '';
};

	// CleanMozHTML modifica il DOM in modo da eliminare
	// alcuni elementi

FlexJS.HTMLUtils.CleanMozDOM = function (el) {

		// se è un <p> al termine del quale ci sono <br> vuoti, li elimina

	if (el.nodeType === 1 && el.tagName.toLowerCase() === 'p' && el.childNodes.length > 1) {

		if (el.lastChild !== null && el.lastChild.nodeName.toLowerCase() === 'br')
			el.removeChild(el.lastChild);
	}

	for (var i = 0, m = el.childNodes.length; i < m; i++)
		FlexJS.HTMLUtils.CleanMozDOM(el.childNodes.item(i));
};

	// quoting di un valore XML
	// usa virgolette semplici o doppie se all'interno del
	// valore non ne compaiono, altrimenti procede all'escapeing
	// del contenuto

FlexJS.Utils.QuoteXMLAttr = function (v) {

		// ottiene una rappresentazione stringa
		// del valore dell'oggetto

	v = '' + v;

	var apos_pos = v.indexOf("'");
	if (apos_pos === -1)
		return "'" + v + "'";

	var quot_pos = v.indexOf('"');
	if (quot_pos === -1)
		return '"' + v + '"';

	var re = new RegExp("'", 'g');

	return "'" + v.replace(re, '&apos;') + "'";
};

	//
	//
	//

FlexJS.Loader = window.FlexJS.Loader || {};

FlexJS.Loader.LoadImage = function (path) {

	if (FlexJS.Loader.sImageCache[path])
		return FlexJS.Loader.sImageCache[path].src;

	var img = FlexJS.Loader.sImageCache[path] = document.createElement('img');

	img.src = FlexJS.AppConfigure.Get('kBaseURL') + path;

	document.getElementsByTagName('head')[0].appendChild(img);

	return img.src;
};

