
<!-- aggiungo content type, altrimente la trasporta come text/html -->

<!-- 
//comments.js
/*******************************************************************************
//						Modulo Commenti e Voti
//
// dipendenze:
//
//		--jqueryJP.js
//			--jquery
//		--ui.stars.js (già incluso, ma dipendente da:)
//			--jquery
//			--ui.core	
//
*******************************************************************************/







/*
function include(file)
{
	document.write('<script type="text/javascript" src="'+ file + '"></script>');
}

function includeCSS(file)
{
	document.write('<link rel="stylesheet" type="text/css" href="'+ file + '" media="all">');
}

//includo: TODO:testare l'inclusione della versione minimizzata
include('jsp/jpcomment/js/ui.stars.js');
includeCSS('jsp/jpcomment/css/ui.stars.css');
*/



//-------------------------------- VOTI --------------------------------------//
function makeStarGadget(contentId) {
	$("#stars-wrapper_"+contentId).stars({
		split: 2,
    	oneVoteOnly: true,
    	cancelShow: false,
    	inputType: "radio",
    	callback: function(ui, type, value){
    		saveVote(contentId,value);
    	}
 	});
} 
 

function saveVote(contentId,vote) {
/*
	var url = 'comment.InsertVote.pub_do';
	var data = 'docId='+contentId+'&voteValue='+vote;
	doAjaxPost(url,data,voteLoaded);
*/
	/*per compatibilità:*/
	var url = 'comment.InsertVote.pub_do?docId='+contentId+'&voteValue='+vote;
	jpGetRemoteHTMLAsync(url,voteLoaded);
}


function voteLoaded(dataComments) {
	if(dataComments && dataComments.length > 0) {
	
	try {
			//recupero docId
			var docId = dataComments[0].docId;
			//numero votanti
			var nVot = dataComments[0].voters;
			
			//formatto la media ( due cifre decimali )
			var avg = formatDecimal(dataComments[0].average,2,'');
			//formatto la media ( per disegnare le stelline )
			avgStar = rawParseAvg(dataComments[0].average);
			
			//disegno stelline e scrivo la media
			$("#stars-avg_"+docId).text(avg + " (" + nVot + ")");
			$("#stars-wrapper_"+docId).stars("select", avgStar);
		}
		catch (e) {}

	}//else alert("Nessun voto inserito");
}


function loadVote(contentId) {
/*
	var url = 'comment.GetVote.pub_do';
	var data = 'docId='+contentId;
	doAjaxGet(url,data,voteLoaded);	
*/
	/*per compatibilità:*/
	var url = 'comment.GetVote.pub_do?docId='+contentId;
	jpGetRemoteHTMLAsync(url,voteLoaded);
}
//-------------------------------- VOTI --------------------------------------//



//------------------------------ COMMENTI ------------------------------------//
//visualizzazione
function toggleFloatComments(contentId) {
	$('#documentCommentDiv_'+contentId).slideToggle('slow');
}

function loadComment(contentId) {
/*
	var url = 'comment.GetComments.pub_do';
	var data = 'docId='+contentId;
	doAjaxGet(url,data,function(data){commentLoaded(data,contentId)});
*/	
	/*per compatibilità:*/
	var url = 'comment.GetComments.pub_do?docId='+contentId;
	jpGetRemoteHTMLAsync(url,function(data){commentLoaded(data,contentId)});	
}

function commentLoaded(dataComments,docId) {
	var newHtmlContent = '';
		
	if(dataComments && dataComments.length > 0) {
		var nRows = dataComments.length;
		//me lo passo da fuori
		//var docId = dataComments[0].docId;
				
		for(var i = 0; i < nRows; i++){
			var body = dataComments[i].body;
			var name = dataComments[i].name;
			var date = rawParseDate(dataComments[i].createDate);
						
			newHtmlContent += '<div class="singleCommentDivRound">';
			newHtmlContent += '<b>';
			newHtmlContent += 'Inserito da "'+ name + '" (' + date + ')';
			newHtmlContent += '</b>';
			newHtmlContent += '<br /><br />';
			newHtmlContent += body;
			newHtmlContent += '<br /></div>';
		}
		$('#commentNum_'+docId).html( '(' + nRows + ')' );
	}
	else newHtmlContent = '<div class="singleCommentDivRound"><br />Nessun commento inserito<br /><br /></div>';
		
	$('#documentCommentsDiv_'+docId).html(newHtmlContent);
}
//inserimento

function loadCaptcha(contentId,infoId) {
	$('#captchaDiv_'+contentId).html('');
	
	var rand = String(Math.random());	
	$('#captchaDiv_'+contentId).load('redirect.do?page=/jsp/jpcomment/captchaImg.jsp&myContentInfoId='+infoId+'&myFakeCaptcha='+rand);
}

function doCommentSubmit(commentForm, callback) {
	var url = 'comment.InsertComment.pub_do';
	
	jpXmlRpcPostAsync(url, document.forms[commentForm], callback);
}



//------------------------------ COMMENTI ------------------------------------//



//-------------------------------- UTILS -------------------------------------//

//utils: non ci stanno tanto bene in questo posto..

function rawParseDate(date) {
	//alert(date+"\n\n"+date.substr(0,16));
	
	return date.substr(0,16);
}

function rawParseAvg(num) {
	if(num){
		num = num.toString().replace(',','.');
		if(isNaN(num)){
			num = '0.0';
		}
	} else {
		num = '0.0';
	}
	
	var where_is_comma = num.indexOf('.');
	
	if(where_is_comma > -1) 
		return num.substring(0, where_is_comma );
	else return num;
}


function formatDecimal(num, dec, val) {
	// Tolgo la virgola e metto il punto per verificare se si tratta di un numero
	if(num){
		num = num.toString().replace(',','.');
		if(isNaN(num)){
			num = '0.0';
		}
	} else {
		num = '0.0';
	}
	
	// Fisso il default per il dec
	if(dec == 0)
		dec = 1;
	
	var where_is_comma = num.indexOf('.');
	var result = num;
	if(where_is_comma > -1) {
		result = num.substring(0, where_is_comma );
		result = result + ',';
		result = result + num.substring(where_is_comma + 1, where_is_comma + dec + 1 );
	} else {
		result = result + ',';
		for(i = 0; i < dec; i++)
			result = result + '0';
	}
	if(val && val != '')
		result = result + ' ' + val;
	
	return result;
}


function rawParseString(str) {
	alert(str);
	//converto i \n in <br />
	str = str.replace(/\n/gi, "<br />");
	//trim
	str = jQuery.trim( str );
	//torno la stringa
	alert(str);
	return str;
}


//-->
