/**
 * funzioni js per area blog
 * @name		blog.js
 * @encoding	UTF-8
 * @project		Socialnet - trilla.it
 * @package		trilla.it
 * @author		Giorgio tonelli <g.tonelli@staff.libero.it>, <giorgio.tonelli@email.it>,www.thantalas.net
 * @creation	04/jun/09
 * @version		1.0
 */
var commentclass=Array();
var liveComment;
var deletecommentclass;
var deletepostclass;
var postOverItem=false;

/**
 * mostra il pulsante per la rimozione di un post 
 * @param {String} prefisso id del div del messaggio
 * @param {int} id del post
 */
 function overPostItem(ele, id){
 	el = $(ele + id);
 	if (postOverItem) {
 		if (id != postOverItem) {
 			removeClass($(ele + postOverItem), 'post-item-del-hover');
 		}
 	}
 	addClass(el,'post-item-del-hover');
	postOverItem=id;
 }
 
function outPostItem(ele,id){
 	removeClass($(ele + id), 'post-item-del-hover');
}
/**
 * controllo tramite ajax se  un immagine è stata encodata
 * e in caso risposta positiva sostituzione
 * @param {Integer} idAttachment id allegato
 * @param {Integer} idPost id del post
 * @param {String} imgType quale immagine devo aggiornare 
 * @param {String} nfs path post
 * @param {Object} oggetto dom immagine
 */
var checkerImage=[];
var checkerXhr=[];
function checkImageReady(idAttachment,idPost,imgType,nfs,obj){
	var att=idAttachment;
	var post=idPost;
	var img=imgType;
	var obj=obj;
	var nfs=nfs;
	checkerImage[idAttachment]=setInterval(function(){
		checkerXhr[idAttachment]=new Libero.Request({
			url: 'checkerEncoder.php',
			method: 'get',
				secure : true, 
				requestType: 'json',
			data : {'postId':post,'attId':att,'imgt':img}
		});
		checkerXhr[idAttachment].onSuccess = function(){
			checkerXhr[idAttachment]=null;
			if(typeof(json)== 'undefined'){
				clearInterval(checkerImage[att]);
				return;
			}
			if (json.status == 1) {
				clearInterval(checkerImage[att]);
				newsrc=nfs + '/' + json.params.namefile+img+'.jpg';
				var newsrcb=nfs + '/' + json.params.namefile+'_med.jpg';
				obj.src=newsrc;
				obj.setAttribute('width',json.params.w);
				obj.setAttribute('height',json.params.h);
				obj.setAttribute('class','imagezoom');
				obj.LiberoOrgWidth=parseInt(json.params.lw);
				obj.LiberoOrgHeight=parseInt(json.params.lh);
				Libero.Utils.prototype.startListening(obj,'click',handleClickZoomImage);
				obj.onload=null;

/*
				var wi=json.params.w;
				var he=json.params.h;
				obj.onclick=function(){
					zoomImage(newsrcb,wi,he);
				};
*/
			}else if(json.status!=2){// nn pronta
				clearInterval(checkerImage[att]);
			}
			if (json.status==3) { // fallimento upload / encoding foto
				var anch=obj.parentNode;
				anch.removeChild(obj);
				anch.parentNode.removeChild(anch);
			}
			
		}
		checkerXhr[idAttachment].onFailure = function(){
			hxr=null;
			clearInterval(checkerImage[att]);
		};
		checkerXhr[idAttachment].send();
	}, 5000 );
}
/**
 * funzione per l'urlencode di parametri
 * @param {String} url
 */
function encodeUrl(url)  {  
    if (url.indexOf("?")>0)   {  
        encodedParams = "?";  
        parts = url.split("?");  
        params = parts[1].split("&");  
        for(i = 0; i < params.length; i++){  
            if (i > 0){  
                encodedParams += "&";  
            }  
            if (params[i].indexOf("=")>0){   
                p = params[i].split("=");  
                encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));  
            }else{  
                encodedParams += params[i];  
            }  
        }  
        url = parts[0] + encodedParams;  
    }  
    return url;  
} 
/**
 * apre una finestra pop up per visualizzare la dimensione maggiore di un immagine
 * @param {String} image
 * @param {Int} w width immagine 
 * @param {Int} h height immagine 
 */
function zoomImage(image,w,h){
	if (!w) w=410;
	if (!h) h=410;
	url=encodeUrl('imagezoom.php?image='+image);
	url=stripHTML(url);
	posx=parseInt((800-w)/2);
	posy=parseInt((500-h)/2);
	windDlg=window.open(url,'imagezoom','width='+w+',height='+h+',toolbar=0,left='+posx+',top='+posy+',location=0,directories=0,status=0, toolbar=no, menubar=no,,scrollbars=1,resizable=yes');
	  // if (!windDlg.opener) windDlg.opener = self;  
	if (window.focus) {windDlg.focus()}  
}


/**
 * Ripristina il valore di default di un campo in un form
 * se il suo contenuto è vuoto
 * @param {Object} field campo del form
 */
function restoreDefaultText(field){
	val=field.value.length;
	rel=field.getAttribute('rel');
	if(val<=0){
		field.value=rel;
		$s(field).color='#999999';
		return;
		if(field.id){
			if(field.getAttribute('id')== 'comment_text'){
				//$s('comment_text').color='#999999';
				$s(field).color='#999999';
			}
		}
	}
}
/**
 * rimuove al focus il valore di default in un campo di un form
 * @param {Object} field campo del form
 */
function removeDefaultText(field){
		val=field.value.toLowerCase();
		rel=field.getAttribute('rel').toLowerCase();
		if(val==rel){
			field.value='';
		}
		$s(field).color='#000000';
	}
/**
 * invia il form per l'inserimento dei commenti
 * @param {Object} form form da spedire
 * @param {Int} postId id del post
 */
function sendNewComment(form,postId){
	return commentclass[postId].newComment(form,postId);
}
/**
 * visualizza commenti live di un post
 * @param {Int} postId id del post
 * @param {Int} blogId  id del blog
 */
function getLiveComments(postId,blogId){
	if(!liveComment){
		liveComment=new Libero.commentLive();
	}
	return liveComment.getLiveComments(postId,blogId);
}
/**
 * funzione che ricarica tramite ajax il box last comment
 */
function reloadLastComment(){
		var _this = this;
		if ($('box-lastcomment-container') != 'undefined') {

	 		var hxr=new Libero.Request({
				url:'ajax_getlast_comment.php',
				method: 'get',
				requestType: 'html',
				update : $('box-lastcomment-container'), 
				data : {}
			});
			hxr.onSuccess = function(){
				if($('box-lastcomment-container'))
					$s('box-lastcomment-container').display='';
			}
			hxr.onFailure = function(){
				
			};
			hxr.send();
		}
	
}
/**
 * classe per i commenti live
 */
Libero.commentLive=Class.create();
Libero.commentLive.prototype = {
	initialize: function(){
		this.boxes=[];// array - oggetto elementi aperti: uno per ogni post
		this.containerPrefix='liveComment'; //prefisso id container div contenitore commenti live
		this.spanLinkContainerPrefix='linktoComment'; // prefisso id span contenitore del link "commanta/commenti"
		this.LiveUrl='ajax_getlive_comment.php';
	},
	/**
	 * questa funzione prende e momorizza il contenuto originale dello span che contiene il link "Commenta- commenti"
	 * lo sostituisce con "Nascondi" o quello che dovrà essere e assegna la funzione di kiusura del div contenitore dei commenti
	 * @param {Int} postId id del post	 */
	setSpanCommand: function(postId){
		this.boxes[postId].spanLinkContainer=$(this.spanLinkContainerPrefix+postId);
		this.boxes[postId].spanLinkContainerParent=this.boxes[postId].spanLinkContainer.parentNode;
		var hideA = document.createElement('a');
		hideA.setAttribute("id",'hideLiveCommentC'+postId);
		hideA.setAttribute("href",'javascript:;');
		hideA.innerHTML = 'Nascondi';
		hideA.onclick=function(){
			this.hideLiveComment(postId)
		}.bind(this);
		this.boxes[postId].spanLinkContainerParent.appendChild(hideA);
		this.boxes[postId].linkHIde=hideA;
  		this.boxes[postId].spanLinkContainer.style.display='none';
		this.boxes[postId].spanNumComm=$('numcomm'+postId);// contenitore numero commenti
		this.boxes[postId].numComm=$(this.spanLinkContainerPrefix+postId).getAttribute('rel');
	},
	/**
	 * nasconde il div in questione, e ripristina l'html di "commenta/commenti"
	 * @param {int} postId id del post
	 */
	hideLiveComment: function(postId){
		this.boxes[postId].spanLinkContainerParent.removeChild(this.boxes[postId].linkHIde);
		this.boxes[postId].spanLinkContainer.style.display='';
		this.setNumCommentLabel(postId);
		this.boxes[postId].obj.style.display='none';
		this.boxes[postId].open=false;
		this.boxes[postId].obj.style.marginBottom='0';
	},
	/**
	 * imposta il numero di commenti tra[] nel link Commenta
	 * e setta correttamente la label del link "Commenti/Commenta
	 * @param {int} postId id del post
	 */
	setNumCommentLabel : function(postId){
		numComm=$(this.spanLinkContainerPrefix+postId).getAttribute('rel');
		anchorElements=$(this.spanLinkContainerPrefix+postId).getElementsByTagName('a');// setto l'etichetta giusta per l'ancora "commenti"
		if(numComm>0){
			this.boxes[postId].spanNumComm.innerHTML=' ['+numComm+']';
			if(typeof(anchorElements[0])=='object'){
				anchorElements[0].innerHTML='Commenti';
			}
		}else{
			if(typeof(anchorElements[0])=='object'){
				anchorElements[0].innerHTML='Commenta';
			}
			this.boxes[postId].spanNumComm.innerHTML='';
		}
	},

		/**
	 * funzione fare il reload del div contenitor come nel caso di un nuovo commento
	 * @param {Int} postId id del post
	 * @param {Int} blogId  id del blog
	 * @param {Int} numCom  numero di commenti del post
	 */
	reloadContent: function(postId,blogId){
			var _this = this;
			if ($(this.containerPrefix + postId) != 'undefined') {
				obj = $(this.containerPrefix + postId);
				oldh=obj.offsetHeight;
				if(typeof(oldh)!='undefined'){//fix orrore in ie
					obj.style.height=oldh+'px';
				}
		 		var hxr=new Libero.Request({
					url: _this.LiveUrl,
					method: 'get',
					requestType: 'html',
					update : obj, 
					data : {'msgId':postId,'mId':blogId,'ajaxLoad':1}
				});
				
				//obj.style.paddingTop='30px';
				//_this._showPreload(obj);
				hxr.onSuccess = function(){
					obj.style.height='';
					//obj.style.paddingTop='0';
					//_this._hidePreload(obj);
					if($('newcommentFromLive'+postId)){// form new comment
						$('newcommentFromLive'+postId).value=1;
					}
					_this.boxes[postId].numComm=parseInt($(_this.spanLinkContainerPrefix+postId).getAttribute('rel'));
					reloadLastComment();
				}
				hxr.onFailure = function(){
					//obj.style.paddingTop='0';
					//_this._hidePreload(obj);
					_this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Prego riprovare più tardi.</p>');
				}.bind(this);
				hxr.send();
			}
	},
	/**
	 * funzione per visualizzare i commenti live
	 * @param {Int} postId id del post
	 * @param {Int} blogId  id del blog
	 */
	getLiveComments: function(postId,blogId,numCom){
		if(this.boxes[postId]!=undefined){
			if(this.boxes[postId].open){
				return false;
			}else{
				this.setSpanCommand(postId);
				this.boxes[postId].obj.style.display='block';
				this.boxes[postId].open=true;
				this.boxes[postId].numCom=numCom;
			}
		}else{
			if($(this.containerPrefix+postId)!='undefined'){
				obj=$(this.containerPrefix+postId);
				var _this=this;
		 		var hxr=new Libero.Request({
					url: _this.LiveUrl,
					method: 'get',
					requestType: 'html',
					update : obj, 
					data : {'msgId':postId,'mId':blogId,'ajaxLoad':1,',numCom':numCom}
				});
				_this._showPreload(obj);
				hxr.onSuccess = function(){
					_this._hidePreload(obj);
					this.boxes[postId]={};
					this.boxes[postId].id=postId;
					this.boxes[postId].open=true;
					this.boxes[postId].obj=obj;
					this.boxes[postId].numCom=numCom;
					this.setSpanCommand(postId);
					if($('newcommentFromLive'+postId)){// form new comment
						$('newcommentFromLive'+postId).value=1;
					}
				}.bind(this);
				hxr.onFailure = function(){
					_this._hidePreload(obj);
					_this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Prego riprovare più tardi.</p>');
				}.bind(this);
				hxr.send();
			}else{
				return true;
			}	
		}
		return false;
	},
	/**
	 * mostra eventuali errori
	 * @param {string} message
	 */
	_showError:function(message){
		alert(stripHTML(message));
	},

	/**
	 * assegna al div che si sta aprendo la classe preload
	 * @param {Object} obj oggetto dom che in cui stiamo caricando i dati
	 */
	_showPreload :function(obj){
		createPageVeil(0);
		addClass(obj,'preload');
		obj.style.marginBottom='10px';
	},
	/**
	 * rimuove al div che si sta aprendo la classe preload
	 * @param {Object} obj
	 */
	_hidePreload :function(obj){
		removePageVeil();
		removeClass(obj,'preload');
	}
}
/**
 * classe per l'inserimento di nuovi commenti
 */
Libero.newComment=Class.create();
Libero.newComment.prototype = {
	options: {
		useAjax: true // se true in modalità ajax
	},
	/**
	 * inizializza la classe
	 * @param {Object} options opzioni
	  * @param {int} postId id del post
	 */
	initialize: function(options,postId){
		this._setoptions(options);
		this.divErrorObj=$('comment-error'+postId);
		this.postId=postId;
	},
	/**
	 * Invia il form dei commenti
	 * @param {Object} form form da validare e spedire 
	  * @param {int} postId id del post
	 */
	newComment: function(form,postId){
		error=false;
		errMesg=new Array(); 
		commField=form.commentText;//$('comment_text'+postId);
		commValue=getFieldValue(commField).text;
		if(rel=commField.getAttribute('rel')){
			if(rel.toLowerCase() == commValue.toLowerCase()){
				error=true;
				errMesg.push('<p>Inserire correttamente il testo del commento.</p>');
			}
		}
		if(typeof(form.screenName) != 'undefined'){// commenti da nn loggati
			if(rel=form.screenName.getAttribute('rel')){
				nameValue=getFieldValue(form.screenName).text;
				if((rel.toLowerCase() == nameValue.toLowerCase()) || (nameValue.trim() == '')){
					error=true;
					errMesg.push('<p>Inserisci correttamente il tuo nome.</p>');
				}
			}
			if(rel=form.linkUrl.getAttribute('rel')){
				urlValue=getFieldValue(form.linkUrl).text;
				if((rel.toLowerCase()== urlValue.toLowerCase())){
					form.linkUrl.value='';
				}
			}
			if(form.linkUrl.value!=''){
				if(!validateUrl(form.linkUrl.value)){
					errMesg.push('<p>Inserisci correttamente l\' indirizzo del  tuo sito</p>');
				}
			}
			
			if(rel=form.userMail.getAttribute('rel')){
				mailValue=getFieldValue(form.userMail).text;
				if((rel.toLowerCase() == mailValue.toLowerCase()) || (mailValue.trim() == '') ){
					error=true;
					errMesg.push('<p>Inserisci correttamente la tua e-mail.</p>');
				}
			}
		}
		if(error){
			Msg='';
			for(i=0;i<errMesg.length;i++)  Msg+=errMesg[i];
			this._showError(Msg);
			return false
		}
		this._showPreload(form);
		if(this.options.useAjax==true){
			form.newcommentuseAjax.value=1;//$('newcomment-useAjax').value=1;
			this._sendRequest(form);
			return false;
		}else{
			return true;
		}
	},
	/**
	 * visualizza il div del preload
	 * @param {Object} form form che si sta spedendo
	 */
	_showPreload: function(form){
		createPageVeil();
		form.commentplsSubmit.disabled=true;//$('commentplsSubmit').disabled=true;
		if($('comment-area-ajaxpreload'+this.postId)){
			preload=$s('comment-area-ajaxpreload'+this.postId);
			preload.display='block';
		}
	},
		/**
	 * nasconde il div del preload
	 * @param {Object} form form che si sta spedendo
	 */
	_hidePreload: function(form){
		removePageVeil();
		form.commentplsSubmit.disabled=false;//$('commentplsSubmit').disabled=false;
		if($('comment-area-ajaxpreload'+this.postId)){
			preload=$s('comment-area-ajaxpreload'+this.postId);
			preload.display='none';
		}
	},
	/**
	 * Invia la ricnhiesta ajax
	 * @param {Object} form form che si sta spedendo
	 */
	_sendRequest:function(form){
		var _this=this;
 		var hxr=new Libero.Request({
			url: 'newcom.php',
			method: 'post',
				secure : true, 
				requestType: 'json',
			data : {}
		});
		hxr.onSuccess = function(){
			_this._hidePreload(form);
			if(json.status==true){
				reload=false;
				if(typeof(json.reloadLiveComment)!='undefined'){
					if (json.reloadLiveComment) {
						if(!liveComment){
							liveComment=new Libero.commentLive();
						}
						liveComment.reloadContent(json.postId,json.blogId);
					}else{
						reload=true;
					}
				}else{
					reload=true;
				}
				if(typeof(json.reload) !='undefined' && reload){
					target=json.reload;
						window.location.href=target;
				}
			}else{

				if(typeof(captchaUrl) != 'undefined' && typeof(captchaFieldName) != 'undefined'){// devo rinfrescare il captcha
					$('captcha'+json.postId).src=captchaUrl+'?'+Math.random();
					$(captchaFieldName+json.postId).focus();
				}
				_this._showError(json.errormessage);
			}
		}
		hxr.onFailure = function(){
			_this._hidePreload(form);
			_this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Prego riprovare più tardi.</p>');
		}.bind(this);
		hxr.send(form);
	},
	/**
	 * mostra gli errori all'utente
	 * @param {String} message messaggio
	 */
	_showError:function(message){
		
		this.divErrorObj.style.display='block';
		if (typeof message!='undefined') {
			this.divErrorObj.innerHTML=message;
		}
		var _this=this;
		window.setTimeout(function(){
			_this._hideError();
		}, 4000);
		//alert(stripHTML(message));
	},	
	/**
	 * nasconde e "svuota il div di errore"
	 * @param {Object} form form che si sta spedendo
	 */
	_hideError: function(){
		this.divErrorObj.style.display='none';
		this.divErrorObj.innerHTML='';
	},	
	/**
	 * override delle opzioni di default
	 * @param {Object} opzioni passate come oggetto
	 */
	_setoptions : function (options){
		if (typeof(options)=='object') {
			var result={};
			for (var i in this.options) {
				if(typeof(options[i])!='undefined'){
					result[i]=options[i];
				}else{
					result[i]=this.options[i];	
				}
			}
		}else{
			result=this.options;
		}
		this.options=result;
	}
}

/** 
 * funzioni per eliminare un commento 
 * @param {Object} id
 */

function deleteComment(id,postId){
	return deletecommentclass.deleteComment(id,postId);
}

/** 
 * funzioni per eliminare un post 
 * @param {integer} id
 */
function deleteMessage(id){
	return deletepostclass.deletePost(id);
}
/**
 * classe per l'eliminazione live di un post
 */
Libero.deletePost=Class.create();
Libero.deletePost.prototype = {
	options: {
		formId: 'formdelpost',
		postDivItemPrefix:'post-item-container',
		useAjax: true // se true in modalità ajax
	},
	initialize: function(options){
		this._setoptions(options);
		this.form=$(this.options.formId);
		this.postIdField=$('delpost-postid');
		this.id=0;
		this.element=false;
	},
	/**
	 * Inizio eliminazione post 
	 * mostra la richiesta di conferma
	 * @param {Integer} id
	 */
	deletePost: function(id){
		if(this.element=$(this.options.postDivItemPrefix +id)){
		}else{
			return false;
		}
		if(!this._showConfirm()){
			return false;
		}
		if(typeof(id)!='indefined'){
			this.postIdField.value=id;
		}else{
			return false;
		}
		this.id=id;
		if(this.options.useAjax){
			if(this.element)
				Libero.Utils.prototype.setOpacity(this.element,0.5);
			$('delpost-useAjax').value=1;
			this._sendRequest(this.form);
		}else{
			this.form.submit();
		}
	},
	/**
	 * Invia la ricnhiesta ajax
	 * @param {Object} form
	 */
	_sendRequest:function(form){
		var _this=this;
 		var hxr=new Libero.Request({
			url: 'eliminapost.php',
			method: 'post',
				secure : true, 
				requestType: 'json',
			data : {}
		});
		
		hxr.onSuccess = function(){
			//_this._hidePreload();
			if(json.status==true){
				_this.deleteElement();
			}else{
				if(_this.element)
					Libero.Utils.prototype.setOpacity(_this.element,1);
				_this._showError(json.errormessage);
			}
		}
		hxr.onFailure = function(){
			//_this._hidePreload();
			if(_this.element)
				Libero.Utils.prototype.setOpacity(_this.element,1);
				_this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Prego riprovare più tardi.</p>');
		}.bind(this);
		hxr.send(form);
	},
	/**
	 * elimina l'elemento dal dom
	 */
	deleteElement: function(){
		postItems=document.getElementsByClassName('post-item-container', 'div');
		if(postItems.length==1){
			window.location.href='index.php';
			return;
		}
		if(elToremove=$(this.options.postDivItemPrefix +this.id)){
			/*topline=$('post-item-topline'+this.id);
			topline.parentNode.removeChild(topline);*/
			elToremove.parentNode.removeChild(elToremove);
			liveCommentContainer=$('liveComment'+this.id);
			liveCommentContainer.parentNode.removeChild(liveCommentContainer);

			torem=[];
			if (boxcl = $('box-lastcomment')) {
				if (boxcl.childNodes.length > 0) {
					var nodes = boxcl.childNodes;
					for (w = 0; w < nodes.length; w++) {
						node = nodes[w];
						if(typeof(node.innerHTML)!='undefined'){
							rel = node.getAttribute('rel')
							if (rel = node.getAttribute('rel')) {
								rel=rel.split('-');
								if(rel[0]==this.id){// id blog
									reloadLastComment();
									break;
									torem.push(node);
								}
							}
						}
					}
				}
			}
/*
			if(torem.length>0){
				for (w = 0; w < torem.length; w++) {
					torem[w].parentNode.removeChild(torem[w]);
				}	
			}
*/
			// FINE elimino eventuale commento box dx
			 
			 this.id=0;
		}else{
			// error;
		}
		$('delpost-useAjax').value=0;
		this.postIdField.value=0;
	},
	_showConfirm: function(){
		return confirm('Eliminare definitivamente questo messaggio?');
	},
	/**
	 * mostra gli errori all'utente
	 * @param {String} message messaggio
	 */
	_showError:function(message){
		alert(stripHTML(message));
	},		
	/**
	 * override delle opzioni di default
	 * @param {Object} opzioni passate come oggetto
	 */
	_setoptions : function (options){
		if (typeof(options)=='object') {
			var result={};
			for (var i in this.options) {
				if(typeof(options[i])!='undefined'){
					result[i]=options[i];
				}else{
					result[i]=this.options[i];	
				}
			}
		}else{
			result=this.options;
		}
		this.options=result;
	}
}

/**
 * classe per l'eliminazione live di un pcommento
 */
Libero.deleteComment=Class.create();
Libero.deleteComment.prototype = {
	options: {
		formId: 'formdelcomment',
		commentDivItemPrefix:'post-comment-item',
		useAjax: true, // se true in modalità ajax
		postId:0
	},
	initialize: function(options){
		this._setoptions(options);
		this.form=$(this.options.formId);
		this.commentIdField=$('delcomment-commentid');
		this.postIdField=$('delcomment-postid');
		this.id=0;
		this.element=false;
	
	},
	/**
	 * Inizio eliminazione post 
	 * mostra la richiesta di conferma
	 * @param {Integer} id
	 */
	deleteComment: function(id,postId){
		if(this.element=$(this.options.commentDivItemPrefix +id)){
		}else{
			return false;
		}
		if(!this._showConfirm()){
			return false;
		}
		if(typeof(id)!='undefined'){
			this.commentIdField.value=id;
			this.postIdField.value=postId;
		}else{
			return false;
		}
		this.id=id;
		if(this.options.useAjax){
			if(this.element)
				Libero.Utils.prototype.setOpacity(this.element,0.5);
			$('delcomment-useAjax').value=1;
			this._sendRequest(this.form);
		}else{
			this.form.submit();
		}
	},
	/**
	 * Invia la ricnhiesta ajax
	 * @param {Object} form
	 */
	_sendRequest:function(form){
		var _this=this;
 		var hxr=new Libero.Request({
			url: 'eliminacommento.php',
			method: 'post',
				secure : true, 
				requestType: 'json',
			data : {}
		});
		hxr.onSuccess = function(){
			//_this._hidePreload();
			if(json.status==true){
				if(('linktoComment'+_this.postIdField.value)){
					numcomm=parseInt($('linktoComment'+_this.postIdField.value).getAttribute('rel'));
					$('linktoComment'+_this.postIdField.value).setAttribute('rel',(numcomm-1));
				}

				_this.deleteElement();
				// prendo il numero di commenti da displaiare nel link 
				
			}else{
				if(_this.element)
					Libero.Utils.prototype.setOpacity(_this.element,1);
					
				_this._showError(json.errormessage);
			}
		}
		hxr.onFailure = function(){
			if(_this.element)
				Libero.Utils.prototype.setOpacity(_this.element,1);
			//_this._hidePreload();
			_this._showError('<h2>Servizio momentaneamente non raggiungibile!</h2> <p>Prego riprovare più tardi.</p>');

		}.bind(this);
		hxr.send(form);
	},
	/**
	 * elimina l'elemento dal dom
	 */
	deleteElement: function(){
		this.element.parentNode.removeChild(this.element);
		// elimino eventuale commento box dx
		if (boxcl = $('box-lastcomment')) {
			if (boxcl.childNodes.length > 0) {
				var nodes = boxcl.childNodes;
				for (w = 0; w < nodes.length; w++) {
					node = nodes[w];
					if(typeof(node.innerHTML)!='undefined'){
						if (rel = node.getAttribute('rel')) {
							rel=rel.split('-');
							if(rel[0]==this.options.postId){// id blog
								if(rel[1]==this.id){
									reloadLastComment();
									break;
									//node.parentNode.removeChild(node);
								}
							}
						}
					}
				}
			}
		}// FINE elimino eventuale commento box dx
		
		this.id=0;

		$('delcomment-useAjax').value=0;
		this.commentIdField.value=0;
	},
	_showConfirm: function(){
		return confirm('Eliminare definitivamente questo commento?');
	},
	/**
	 * mostra gli errori all'utente
	 * @param {String} message messaggio
	 */
	_showError:function(message){
		alert(stripHTML(message));
	},		/**
	 * override delle opzioni di default
	 * @param {Object} opzioni passate come oggetto
	 */
	_setoptions : function (options){
		if (typeof(options)=='object') {
			var result={};
			for (var i in this.options) {
				if(typeof(options[i])!='undefined'){
					result[i]=options[i];
				}else{
					result[i]=this.options[i];	
				}
			}
		}else{
			result=this.options;
		}
		this.options=result;
	}
}
/**
 * Verifica le dimensioni dell'immagine, e se la larghezza
 * supera la dimensione orizzontale massima della colonna contenuto
 * del blog, la ridimensiona.
 * 
 * @param {Object} image		Elemento img del dom contenente l'immagine da ridimensionare
 */
function adjustImageProportions(image) {
	var z,mw=400,w=image.width,h=image.height,p=w/h;
	if (w>mw) {
		z=mw/w;
		image.LiberoOrgWidth=w;
		image.LiberoOrgHeight=h;
		image.width=mw;
		image.height=z*h;
		if (image.className!=''){
			image.className+=' zoomable';
		}else{
			image.className='zoomable';
		}
		Libero.Utils.prototype.startListening(image,'click',handleClickZoomImage);
	}
}
function handleClickZoomImage(e) {
	var evt=Libero.Utils.prototype.getEvent(e);
	var tgt=Libero.Utils.prototype.getEventTarget(evt);
	if (evt && tgt && tgt.tagName=='IMG'){
		openZoomedImage(parseInt(tgt.LiberoOrgWidth),parseInt(tgt.LiberoOrgHeight),'http://static.post.it',tgt.src);
	}
	Libero.Utils.prototype.stopEvent(evt);
	return false;
}
function openZoomedImage(lw,lh,bu,u) {
	var max_width=(document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth)-20,
		max_height=(document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight)-40,
		dlg_width,
		dlg_height,
		zoom_factor=lw/lh;
	if(zoom_factor>1){
		if((max_width >= lw) && (max_height >= lh)){
			dlg_width=lw;
			dlg_height=lh;
		}else{
			zoom_factor=max_width/lw;
			dlg_width=max_width;
			dlg_height=zoom_factor*lh;
		}
	}else{
		if((max_height >= lh) && (max_width >= lw)){
			dlg_width=lw;
			dlg_height=lh;
		}else{
			zoom_factor=max_height/lh;
			dlg_height=max_height;
			dlg_width=zoom_factor*lw;
		}
	}
	if (dlg_height>=max_height) {
		zoom_factor=max_height/dlg_height;
		dlg_width=zoom_factor*dlg_width;
		dlg_height=max_height;
	}
	if (dlg_width>=max_width) {
		zoom_factor=max_width/dlg_width;
		dlg_height=zoom_factor*dlg_height;
		dlg_width=max_width;
	}
	Libero.Utils.prototype.openModalDialog(window,{
		mdWidth:dlg_width+10
		,mdHeight:dlg_height+10
		,mdBaseUrl:bu
		,mdContent:'<'+'img width='+(dlg_width-10)+' height='+(dlg_height-10)+' style=\'width:'+(dlg_width-10)+'px;height:'+(dlg_height-10)+'px;margin: 10px;padding: 0;border:1px solid #666;\' src=\''+u+'\' /'+'>'
	});
	return false;
}

