function utilsSetTarget(){$('a[rel="external"]').attr('target','link');}
function utilsCheckAdmin(){if(typeof adminDeletePostCallback=='function'){adminSetup();}}
function utilsClickableWithInnerA($sel){$($sel).css('cursor','pointer').click(function(e){e.preventDefault();var $l=$(this).find('a').attr('href');document.location.href=$l;});}
function utilsShowMessage(){$('#modalMessage').modal();}
function utilsShowError(){$('#errorMessage').modal();}
function utilsRaiseError($message){$('#errorMessage').remove();$('#footer').after('<div id="errorMessage"><h3>Errore</h3>'+$message+'<a class="simplemodal-close">Chiudi</a></div>');utilsShowError();}
function utilsAddLoadingMessage($id,$message){var $out='<span class="loading" id="loading-'+$id+'">'+$message+'</span>';return $out;}
function utilsShowHide($o,$t){$($o).unbind('click');$($o).click(function(e){e.preventDefault();if($($t).is(':visible')){$($t).hide();}else{$($t).show();}});}
function utilsSplitProperties($s){var $couples=new Array();var $properties=new Array();var $element=new Array();var $y=0;$couples=$s.split(',');for(var $i=0;$i<$couples.length;$i++){if($couples[$i]!=undefined&&$couples[$i]!=''){$element=$couples[$i].split(':');$properties[$y]={name:$element[0],value:$element[1]}
$y++}}
return $properties;}
function utilsGetProperties($p,$k){for(var $i=0;$i<$p.length;$i++){if($p[$i].name==$k){return $p[$i].value;}}
return-1;};var $uploadHandler;function forumSetExpandCollapse(){$('#linkForumShowhide').click(function(e){e.preventDefault();if($('#forumShortIntro').is(':visible')){$('#forumShortIntro').hide();$('#forumLongIntro').show();$('#linkForumShowhide').html('Riduci');}else{$('#forumLongIntro').hide();$('#forumShortIntro').show();$('#linkForumShowhide').html('Espandi');}});}
function forumShowNewPostLoader(){$('.newPost').hide();$('.newPost').after(utilsAddLoadingMessage('newPost','Sto salvando il tuo commento...'));}
function forumResetNewCommentForm(){$('#post').val('');$('#newPostFilePreview').html('');$('#loading-newPost').remove();$('.newPost').show();forumAddNewCommentEnableClick();}
function forumNewPostCallback($data){forumNewPostCallbackCreate($data.success,$data.error,$data.output);}
function forumNewPostCallbackCreate($success,$errror,$output){if($success){$('#posts ul li:first').before($output);$('#posts ul:hidden').remove();}else{utilsRaiseError($error);}
$('post-loading').hide();forumAddNewCommentEnableClick();forumResetNewCommentForm();forumSetAddComment();forumSetAddLike();forumShowLikeList();forumHideLikes();forumShowLikeList();userSetDetailsBaloon();utilsCheckAdmin();}
function forumSetAddNewPost(){$uploadHandler=new AjaxUpload('addNewFile',{action:'/_ajax/newPost.php',autoSubmit:false,name:'attachment',responseType:'json',onChange:function(file,ext){$('#newPostFilePreview').html('('+file+')');},onSubmit:function(file,ext){var $ok=true;var $forumid=$('#newPostForumId').val();var $post=$('#post').val();if(!(ext&&/^(jpg|png|jpeg|gif|doc|docx|xls|xlsx|ppt|pptx|pdf)$/i.test(ext))){utilsRaiseError('Stai caricando un tipo di file non valido.');$ok=false;}
if($post.length<3){utilsRaiseError('Per condividere immagini o file all\'interno di una conversazione Ã¨ necessario accompagnarli con un commento. Scrivi nell\'apposito box la tua opinione sull\'argomento discusso.');$ok=false;}
if(parseInt($forumid)<1){utilsRaiseError('Non posso salvare al momento');$ok=false;}
var $data={forumid:$forumid,post:$post}
if($ok){forumShowNewPostLoader();$uploadHandler.setData($data);}else{forumResetNewCommentForm();return false;}},onComplete:function(file,response){forumNewPostCallbackCreate(response.success,response.error,response.output);}});forumAddNewCommentEnableClick();}
function forumAddNewCommentEnableClick(){$('#addNewCommentSubmit').unbind('click');$('#addNewCommentSubmit').removeAttr('disabled').click(function(e){e.preventDefault();$('#addNewCommentSubmit').unbind('click');var $fileUpload=$('input[name="attachment"]').val();if($fileUpload==undefined||$fileUpload==''){var $forumid=$('#newPostForumId').val();var $post=$('#post').val();if(parseInt($forumid)<1){utilsRaiseError('Non posso salvare al momento');forumResetNewCommentForm();}else{if($post.length<2){utilsRaiseError('Per condividere immagini o file all\'interno di una conversazione Ã¨ necessario accompagnarli con un commento. Scrivi nell\'apposito box la tua opinione sull\'argomento discusso.');forumResetNewCommentForm();}else{var $data={forumid:$forumid,post:$post}
forumShowNewPostLoader();$.post('/_ajax/newPost.php',$data,forumNewPostCallback,'json');}}}else{$uploadHandler.submit();}});}
function forumAddCommentCallback($data){$('li#post-'+$data.postid+' li.addLike span.loading').remove();if($data.success){$('div#addNewComment-'+$data.postid).hide();$('div#addNewComment-'+$data.postid+' input[type="submit"]').unbind('click');$('a#linkAddComment-'+$data.postid).html('Commenta');$('li#post-'+$data.postid+' .meComment p strong').html(parseInt(parseInt($('li#post-'+$data.postid+' .meComment p strong').html())+1));if(parseInt($('li#post-'+$data.postid+' .meComment p strong').html())==1){$('li#post-'+$data.postid+' .meComment p span').html('o');}
$('li#post-'+$data.postid+' .meComment ul.commentsList li:last').after($data.output);$('li#post-'+$data.postid).hide();var $newest=$('li#post-'+$data.postid).clone();$('li#post-'+$data.postid).remove();$newest.insertBefore($('#posts ul li:first')).show();var $offset=$('li#post-'+$data.postid).offset();$('html,body').animate({scrollTop:$offset.top},1000);forumSetAddComment();userSetDetailsBaloon();utilsCheckAdmin();}else{utilsRaiseError($data.error);}}
function forumSetAddComment(){$('li.addComment a').unbind('click');$('li.addComment a').click(function(e){e.preventDefault();var $properties=utilsSplitProperties($(this).attr('rel'));var $postid=utilsGetProperties($properties,'postid');var $forumid=utilsGetProperties($properties,'forumid');if($('div#addNewComment-'+$postid).is(':visible')){$('div#addNewComment-'+$postid).hide();$('div#addNewComment-'+$postid+' input[type="submit"]').unbind('click');$(this).html('Commenta');}else{$(this).html('Annulla commento');$('div#addNewComment-'+$postid).show();$('div#addNewComment-'+$postid+' input[type="submit"]').click(function(e){e.preventDefault();$(this).after(utilsAddLoadingMessage('like-'+$postid,'Sto salvando...'));var $post=$('div#addNewComment-'+$postid+' textarea').val();if($post.length>2){$data={postid:$postid,forumid:$forumid,post:$post}
$.post('/_ajax/addComment.php',$data,forumAddCommentCallback,'json');}else{utilsRaiseError('Dai, un po\' piÃ¹ descrittivo, quel commento!');}});$('div#addNewComment-'+$postid+' textarea').focus();}});}
function forumAddLikeCallback($data){$data=jQuery.parseJSON($data);if($data.success){$('li#post-'+$data.postid+' li.addLike').remove();if($('li#post-'+$data.postid+' div.meLike span').length>0){$('li#post-'+$data.postid+' div.meLike span').after($data.output);}else{$('li#post-'+$data.postid+' div.meLike').html('<span class="likeLabel">Piace a </span> te.');}
$('li#post-'+$data.postid+' div.meLike').show();}else{$('li#post-'+$data.postid+' li.addLike span.loading').remove();$('li#post-'+$data.postid+' li.addLike a').show();utilsRaiseError($data.error);}}
function forumSetAddLike(){$('li.addLike a').unbind('click');$('li.addLike a').click(function(e){e.preventDefault();var $properties=utilsSplitProperties($(this).attr('rel'));var $postid=utilsGetProperties($properties,'postid');var $forumid=utilsGetProperties($properties,'forumid');$(this).hide();$(this).after(utilsAddLoadingMessage('like-'+$postid,'Sto salvando...'));var $data={postid:$postid,forumid:$forumid}
$.post('/_ajax/addLike.php',$data,forumAddLikeCallback,'JSON');});}
function forumHideLikes(){$('.meLike').each(function(){if($.trim($(this).text())=='Piace a'){$(this).hide();}});}
function forumShowLikeList(){$('.showCompleteLikeList').unbind('click');$('.showCompleteLikeList').click(function(e){e.preventDefault();var $properties=utilsSplitProperties($(this).attr('rel'));var $postid=utilsGetProperties($properties,'postid');if($('#completeLikeList-'+$postid).is(':visible')){$('#completeLikeList-'+$postid).hide();}else{$('#completeLikeList-'+$postid).show();}});}
function forumPaginationCallback($data){if($data.success){if($data.output!=''&&$data.output!=null){$('#posts > ul > li:last').after($data.output);}else{$('#forumLoadMore').hide();}}else{if($data.error!=''&&$data.error!=null){utilsRaiseError($data.error);}else{$('#forumLoadMore').hide();}}
forumSetAddComment();forumSetAddLike();userSetDetailsBaloon();utilsCheckAdmin();}
function forumSetPagination(){var $offset=0;if($('#forum #posts > ul > li:visible').length<20){$('#forumLoadMore').hide();}else{$('#forumLoadMore > p > a').click(function(e){e.preventDefault();$offset=$('#posts > ul > li:visible').length;var $properties=utilsSplitProperties($(this).attr('rel'));var $forumid=utilsGetProperties($properties,'forumid');var $data={forumid:$forumid,offset:$offset,length:20}
$.post('/_ajax/getPosts.php',$data,forumPaginationCallback,'json');});}};;function userSetCustomUpload(){$("input[type=file]").rebrushfileupload({button_text:'Sfoglia...',class_container:'fileupload-rebrush',class_field:'fileupload-rebrush-field',class_button:'fileupload-rebrush-button'});}
function userBaloonCallback($data){if($data.success){var $pos=$('a#'+$data.elementid).offset();if($('#baloon').length>0){$('#baloon').remove();}
$('#footer').after($data.output);var $top=($pos.top-($('#baloon').height()+30));var $left=($pos.left-10);$('#baloon').hide().css('top',$top).css('left',$left).show().hover(function(){},function(){$('#baloon').hide().remove();});}else{utilsRaiseError('Si è verificato un errore accedendo ai dettagli dell\'utente.\n'+$data.error);}}
function userSetDetailsBaloon(){$('.onHoverShowUserDetails').unbind('click');$('.onHoverShowUserDetails').click(function(e){e.preventDefault();var $properties=utilsSplitProperties($(this).attr('rel'));var $userid=utilsGetProperties($properties,'id');var $elemid=$(this).attr('id');if($userid){var $data={usercodedid:$userid,elementid:$elemid};$.post('/_ajax/getBaloon.php',$data,userBaloonCallback,'json');}else{utilsRaiseError('Si è verificato un errore accedendo ai dettagli dell\'utente');}});}
function usersPaginationCallback($data){if($data.success){$('#team .users > ul > li:last').after($data.output);if($data.number<20){$('#team .loadMoreUsers').hide();}}else{utilsRaiseError($data.error);}
usersSetPagination();userSetDetailsBaloon();utilsSetTarget();}
function usersSetPagination(){$('#team .loadMoreUsers a').unbind('click');var $n=$('#team .users > ul > li:visible').length;if($n<20){$('#team .loadMoreUsers').hide();}else{if($n>0){$('#team .users > ul > li:hidden').remove();}
$('#team .loadMoreUsers a').click(function(e){e.preventDefault();var $offset=$n;var $data={offset:$offset}
$.post('/_ajax/getUsers.php',$data,usersPaginationCallback,'json');});}}
function usersSetAddSon(){$('#linkAddSon').unbind('click');$('#linkAddSon').click(function(e){e.preventDefault();$(this).before('<br /><br />di <input name="sonname[]" title="Nome" value="" size="10" type="text"> di <input name="sonage[]" title="Età" value="" size="2" type="text"> anni.  ');});}
function usersDeleteMeCallback($data){window.location.href='/?m='+$data.message;}
function usersDeleteMe(){$('#deleteme').click(function(e){e.preventDefault();if(confirm('Sicuro di voler cancellare il tuo profilo?\nUna volta completata l\'operazione, non potrai piÃ¹ accedere al sito, e i tuoi dati verranno eliminati.\nPer accedere nuovamente, dovrai eseguire una nuova registrazione.\nContinuo?')){$.post('/_ajax/deleteUser.php',{},usersDeleteMeCallback,'json');}});};function blogCheckLoginCallback($data){if($data.success){$('#author').val($data.output.nickname);$('#email').val($data.output.email);$('#url').val($data.output.link);}else{$('#form-comment-data').css('width','auto').css('height','auto').css('overflow','show');}}
function blogCheckLogin(){$.post('/_ajax/getUser.php',{},blogCheckLoginCallback,'json');};$('document').ready(function(){$('document').pngFix();if($('.onHoverShowUserDetails').length>0){userSetDetailsBaloon();}
if($('a#addNewFile').length>0&&$('div#uploadNewFileElement').length>0){forumSetAddNewFileClick();}
if($('.newPost').length>0){forumSetAddNewPost();}
if($('li.addLike').length>0){forumSetAddLike();}
if($('#forumShortIntro').length>0){forumSetExpandCollapse();}
if($('li.addComment').length>0){forumSetAddComment();}
if($('#forumLoadMore').length>0){forumSetPagination();}
if($('.forums .forum > div.latestPost').length>0){utilsClickableWithInnerA('.forums .forum > div.latestPost');}
if($('.forums .forum > div.readmore').length>0){utilsClickableWithInnerA('.forums .forum > div.readmore');}
if($('.forums .forum > div.partecipa').length>0){utilsClickableWithInnerA('.forums .forum > div.partecipa');}
if($('.meLike').length>0){forumHideLikes();}
if($('.showCompleteLikeList').length>0){forumShowLikeList();}
if($('#team .users').length>0){usersSetPagination();}
if($('#linkAddSon').length>0){usersSetAddSon();}
if($('#avatar').length>0){userSetCustomUpload();}
if($('#deleteme').length>0){usersDeleteMe();}
if($('#errorMessage').length>0){utilsShowError();}
if($('#modalMessage').length>0){utilsShowMessage();}
if($('form#commentform').length>0){blogCheckLogin();}
if($('#boxWidget .embed a').length>0){utilsShowHide('#boxWidget .embed a','#boxWidget .texttocopy');}
utilsSetTarget();utilsCheckAdmin();});
