
function attivaCommenti(idObjContenuto){
	$('#'+idObjContenuto).bind("mousedown", function(){
		items = getElementsByClassName("h1tmp");
		for(var i=0; i<items.length; i++) {
			classeTmp=items[i].className;
			classeTmp=classeTmp.replace("h1tmp","");
			items[i].className=classeTmp;
			items[i].setAttribute('idCommento','');
		}
		var apri=closeCommenta();
	});

	$('#'+idObjContenuto).bind("mouseup", function(){

		var data = unixTime();
		if((data-dataPreComm)<1000){
			return(false);
		}
		dataPreComm=data;

		var ris=getSelText();

		if(ris['testo']!=""){
			url="background.php";
			$.post(url,{
					testo: ris['testo'],
					htmlSelezione: ris['htmlSelezione'],
					act: "popAddCommento",
					idContenutoAtt: idContenutoAtt,
					time: unixTime()
				}, function(data){
					if(data!="ko" && data!="login" && data!="numchars" && data!=""){
						var apri=closeCommenta();
						var apri=popCommenta(data);
					} else {
						if(data!="login"){
							var apri=closeCommenta();
						}
					}
			});
		} else {
			var apri=closeCommenta();
		}
	});
}

function popCommenta(objCommento){

	/* ora objCommento è passato come stringa serializzata */
	/*
	if(scrOfX==-1 || scrOfY==-1){
		getScrollXY();
	}
	window.scrollTo(scrOfX,scrOfY);
	funzioniResize();
	*/
	oggetto=prendiElementoDaId("draggableComm");
	if(oggetto){
		url="inc_pop_commenta.php";
		$.post(url,{
			   	arrPost: objCommento,
				act: "formAddCommento",
				time: unixTime()
			}, function(data){
				insHTML("draggableComm", data);
				var risC=aggCommento($("#idCommento").val());
				// $('#draggableComm').draggable({ handle:'#draggerHandle', cursor: 'crosshair' });
				setlivelli("draggableComm",1);
		});
	}
}

function attivaClassCommenti(){
	items = getElementsByClassName("h1");
	for(var i=0; i<items.length; i++) {
		$(items[i]).bind("click", function(){
			var apri=popCommento(parseInt(this.getAttribute('idCommento')));
		});
	}
	return(false);
}

function attivaCommento(idCommento){
	items = getElementsByClassName("h1tmp");
	for(var i=0; i<items.length; i++) {
		if(parseInt(items[i].getAttribute('idCommento'))==idCommento){
			classeTmp=items[i].className;
			classeTmp=classeTmp.replace("h1tmp","h1");
			items[i].className=classeTmp;
			$(items[i]).bind("click", function(){
				var apri=popCommento(idCommento);
			});
		} else {
			classeTmp=items[i].className;
			classeTmp=classeTmp.replace("h1tmp","");
			items[i].className=classeTmp;
		}
	}
	return(false);
}

function disattivaCommento(idCommento){
	items = getElementsByClassName("h1");
	for(var i=0; i<items.length; i++) {
		if(parseInt(items[i].getAttribute('idCommento'))==idCommento){
			try{
				classeTmp=items[i].className;
				classeTmp=classeTmp.replace("h1","");
				items[i].className=classeTmp;
				items[i].setAttribute('idCommento','');
				$(items[i]).bind("click", function(){
					var apri=popCommento(idCommento);
				});
				items[i].setAttribute('onclick','var apri=0;');
			} catch(err){
				alert(err);
			}
		}
	}
	return(false);
}

function aggCommento(idComm){

	var spazioSel=new Array();
	spazioSel['x1']=0;
	spazioSel['y1']=0;
	spazioSel['x2']=0;
	spazioSel['y2']=0;

	var ris=new Array();

	var ris=getSelText();
	// alert(ris['htmlSelezione']);

	ris['range']="";

	myRange = $("#documento").getRangeAt();
	try{ ris['testo']=myRange['selection'].toString() }catch(err){
		ris['testo']=window.getSelection().toString();
	}

	if(ris['testo']!=""){
		var apri=$("#documento").wrapSelection({
			fitToWord: false,
			wrapRange: myRange
		}).addClass('h1tmp');
		items = getElementsByClassName("h1tmp");
		for(var i=0; i<items.length; i++) {
			if(!(parseInt(items[i].getAttribute('idCommento'))>0)){
				classeTmp=items[i].setAttribute('idCommento',idComm);

				spazioSel['x1']=($(items[i]).position().left < spazioSel['x1'] || spazioSel['x1']==0)? parseInt($(items[i]).position().left) : spazioSel['x1'];
				spazioSel['y1']=($(items[i]).position().top < spazioSel['y1'] || spazioSel['y1']==0)? parseInt($(items[i]).position().top) : spazioSel['y1'];
				spazioSel['x2']=(($(items[i]).width() + spazioSel['x1']) > spazioSel['x2'] || spazioSel['x2']==0)? parseInt(($(items[i]).width() + spazioSel['x1'])) : spazioSel['x2'];
				spazioSel['y2']=(($(items[i]).height() + spazioSel['y1']) > spazioSel['y2'] || spazioSel['y2']==0)? parseInt(($(items[i]).height() + spazioSel['y1'])) : spazioSel['y2'];

			}
		}

		var spazioDisp=getViewportSize();
		if(getIEVersion()>6){
			var posYtmp=(spazioSel['y1']-120)-162;
		} else { // IE6
			var posYtmp=(spazioSel['y1']-120)-182;
		}

		var posXtmpL=(spazioSel['x1']-63)-382;
		var posXtmpR=(spazioSel['x2']-63)+450;

		// ris=insHTML("boxHidden", posXtmpL+"#"+posXtmpR);

		if(posXtmpL<=0 && posXtmpR>=spazioDisp[0]){
			posXtmp=parseInt(((spazioDisp[0])/2)-(382/2));
		} else if(posXtmpR>=spazioDisp[0]){
			posXtmp=(spazioSel['x1']-63-382);
		} else {
			posXtmp=(spazioSel['x2']-63);
		}

		var posiziona=posizionaDiv("draggableComm",posXtmp,posYtmp);
		// $('#draggableComm').draggable({ handle:'#draggerHandle', cursor: 'crosshair' });

		return(ris);
	}
}



/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */

var getSelectionHTML = function () {
	var userSelection;
	
	if (window.getSelection && !(getIEVersion()<=6)) { // NOT IE
		// W3C Ranges
		userSelection = window.getSelection ();
		// Get the range:
		if (userSelection.getRangeAt){
			var range = userSelection.getRangeAt (0);
		} else {
			var range = document.createRange ();
			range.setStart (userSelection.anchorNode, userSelection.anchorOffset);
			range.setEnd (userSelection.focusNode, userSelection.focusOffset);
		}
		// And the HTML:
		var clonedSelection = range.cloneContents (); // NON VA SU IE6
		var div = document.createElement ('div');
		div.appendChild (clonedSelection);
		return div.innerHTML;
	} else if (document.selection || getIEVersion()<=6) { // IE
		// alert("IE");
		userSelection = document.selection.createRange ();
		return userSelection.htmlText;
	} else {
		return '';
	}
};

var getSelText=function(){

	var ris=new Array();

	myRange = $("#documento").getRangeAt();
	try{ ris['testo']=myRange['selection'].toString() }catch(err){
		ris['testo']=window.getSelection().toString();
	}

	ris['htmlSelezione']=getSelectionHTML();

	return(ris);
	
	/* -------------------------- FINE QUI -------------------------- */

    var txt="";
	var txtComm="";
	var txtFind="";

	if (window.getSelection) {
		txt=window.getSelection();
		txt=txt.toString();
		txtComm=txt;
		txtFind=txtComm;
	}
	else if (document.selection) { // should come last; Opera!
		txt=document.selection.createRange();
		txt=txt.toString();
		txtComm=txt;
		txtFind=txtComm;
	}

	if (document.selection){ // ---------------------------- IE
		range = document.selection.createRange();

		range.moveStart("character", -offsetSel);
		range.moveEnd("character", offsetSel);
		// range.select();
		txtFind=range.text.toString(); // testo in cui cercare il commento

	} else if (window.getSelection || document.getSelection){ // ------------------ Firefox

		var rangeBk = window.getSelection();

		myRange = $("#documento").getRangeAt();
		txt = myRange['selection'].toString();

        // txt = (window.getSelection)? window.getSelection() : document.getSelection();
		var txtComm=txt.toString();
		var txtFind="";
		var nodeLength=txt.anchorNode.length;
		var txtOffset=txt.anchorOffset;
		var txtLength=txt.toString().length;
		txtLength=((txtOffset+txtLength)>=nodeLength)? (nodeLength-txtOffset) : (txtLength);
		
		txtComm=(txtComm.length>txtLength)? txtComm.substr(0,txtLength) : txtComm;
		
		var offsetPre=((txtOffset-offsetSel)>0)? (txtOffset-offsetSel) : 0;
		var offsetPost=((txtOffset+txtLength+offsetSel)<txt.anchorNode.length)? (txtOffset+txtLength+offsetSel) : nodeLength;
		// alert("[txtOffset:"+txtOffset+"]"+"[offsetPre:"+offsetPre+"]"+"[offsetPost:"+offsetPost+"]"+"[nodeLength:"+nodeLength+"]"+"[txtLength:"+txtLength+"]");

		var txtPost="";
		var txtPre="";

		try{
			txt.extend(txt.anchorNode, offsetPost); // pezzo dopo la selezione
			txtPost=txt.toString();
		}catch(err){
			// alert("errore in post:"+err.description);
		}
		txtPost=(txtPost=="")? txtComm : txtPost;

		try{
			txt.extend(txt.anchorNode, offsetPre); // pezzo prima della selezione
			txtPre=txt.toString();
		}catch(err){
			// alert("errore in pre:"+err.description);
		}

		txtFind=txtPre+txtPost;

		// txt.extend(txt.anchorNode, txtOffset+txtLength); // ripristina selezione iniziale

		var sel = window.getSelection();
		sel.removeAllRanges();
		sel.addRange(rangeBk);

    } else {
		alert("non compatibile");
		return;
	}

	document.getElementById("objJSSx").innerHTML="["+txtComm+"]<br /><br />["+txtFind+"]";

	if(txtComm!=""){
		txtComm=txtComm.toString();
		txtComm=txtComm.replace(/\n/g," ");
		txtComm=txtComm.replace(/\p/g," ");
		txtComm=txtComm.replace(/\t/g," ");
		txtComm=txtComm.replace(/  /g," ");
		txtComm=(txtComm.charAt(0)==" ")? txtComm.substr(1,(txtComm.length-1)) : txtComm;
		txtComm=(txtComm.charAt((txtComm.length-1))==" ")? txtComm.substr(0,(txtComm.length-1)) : txtComm;
		oggetto=prendiElementoDaId("documento");
		if(oggetto){
			numColonna=1*((oggetto.scrollLeft+tempX)/larghezzaTabella(1));
		}
		document.getElementById("objJSDx").innerHTML="["+txtComm+"]<br /><br />Colonna:"+numColonna;
	} else {
		return;
	}
}

/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */

var myRange = null;


