window.onbeforeunload = function ( e ) { if(jQuery('#posttext').val() || jQuery('#comment').val() == true) { var e = e || window.event; if ( e ) { // For IE and Firefox e.returnValue = p2txt.unsaved_changes; } return p2txt.unsaved_changes; // For Safari } }; jQuery(function($) { /* * Insert new comment inline */ function insertCommentInline(postParent, comment_parent, commentHtml, showNotification) { postParent = "#"+postParent; if (0 == comment_parent) { if (0 == $(postParent).children('ul.commentlist').length) { $(postParent).append(''); commentsLists = $("ul.commentlist"); } $(postParent).children('ul.commentlist').append('
'); var newComment = $(postParent).children('ul.commentlist').children('div.temp_newComments_cnt'); } else { comment_parent = '#comment-' + comment_parent; if (0 == $(comment_parent).children('ul.children').length) { $(comment_parent).append(''); } $(comment_parent).children('ul.children').append('
'); var newComment = $(comment_parent).children('ul.children').children('div.temp_newComments_cnt'); } newComment.html(commentHtml); var newCommentsLi = newComment.children('li'); newCommentsLi.addClass("newcomment"); newCommentsLi.fadeIn(); var cnt = newComment.contents(); newComment.children('li.newcomment').each(function() { if (isElementVisible(this) && !showNotification) { $(this).animate({backgroundColor:'transparent'}, {duration: 1500}, function(){ $(this).removeClass('newcomment'); }); } bindActions(this, 'comment'); }); newComment.replaceWith(cnt); } /* * Insert and animate new comments into recent comments widget */ function insertCommentWidget(widgetHtml) { $("#recentcommentstable tbody").prepend(widgetHtml); var newCommentsElement = $("#recentcommentstable tbody tr:first"); newCommentsElement.fadeIn("slow"); $("#recentcommentstable tbody tr:last").fadeOut("slow").remove(); tooltip($("#recentcommentstable tbody tr:first td a.tooltip")); if (isElementVisible(newCommentsElement)) { $(newCommentsElement).removeClass('newcomment'); } //remove newcomment } /* * Check for new posts and loads them inline */ function getPosts(showNotification){ if (showNotification == null) { showNotification = true; } toggleUpdates('unewposts'); var queryString = ajaxUrl +'?action=prologue_latest_posts&load_time=' + pageLoadTime + '&frontpage=' + isFirstFrontPage; ajaxCheckPosts = $.getJSON(queryString, function(newPosts){ if (typeof newPosts.numberofnewposts != "undefined") { pageLoadTime = newPosts.lastposttime; if (!isFirstFrontPage || (typeof newPosts.html == "undefined") ) { newUnseenUpdates = newUnseenUpdates+newPosts.numberofnewposts; message = p2txt.n_new_updates.replace('%d', newUnseenUpdates) + " "+p2txt.goto_homepage+""; newNotification(message); } else { $("#main > ul > li:first").before(newPosts.html); var newUpdatesLi = $("#main > ul > li.newupdates"); newUpdatesLi.slideDown(); var counter = 0; $('#posttext_error, #commenttext_error').hide(); newUpdatesLi.each(function() { // Add post to postsOnPageQS list var thisId = $(this).attr("id"); vpostId = thisId.substring(thisId.indexOf('-')+1); postsOnPageQS+= "&vp[]=" + vpostId; if (!(thisId in postsOnPage)) postsOnPage.unshift(thisId); // Bind actions to new elements bindActions(this, 'post'); if (isElementVisible(this) && !showNotification) { $(this).animate({backgroundColor:'transparent'}, 2500, function(){ $(this).removeClass('newupdates'); titleCount(); }); } counter++; }); if (counter >= newPosts.numberofnewposts && showNotification) { var updatemsg = isElementVisible('#main > ul >li:first') ? "" : ""+p2txt.jump_to_top+"" ; newNotification(p2txt.n_new_updates.replace('%d', counter) + " " + updatemsg); titleCount(); } } $('.newupdates > h4, .newupdates > div').hover( removeYellow, removeYellow ); } }); //Turn updates back on toggleUpdates('unewposts'); } /* * Check for new comments and loads them inline and into the recent-comments widgets */ function getComments(showNotification){ if (showNotification == null) { showNotification = true; } toggleUpdates('unewcomments'); var queryString = ajaxUrl +'?action=prologue_latest_comments&load_time=' + pageLoadTime + '&lcwidget=' + lcwidget; queryString += postsOnPageQS; ajaxCheckComments = $.getJSON(queryString, function(newComments) { if (newComments != 0) { $.each(newComments.comments, function(i,comment) { pageLoadTime = newComments.lastcommenttime; if (comment.widgetHtml) { insertCommentWidget(comment.widgetHtml); } if (comment.html != '') { var thisParentId = 'prologue-'+comment.postID; insertCommentInline(thisParentId, comment.commentParent, comment.html, showNotification); } }); if (showNotification) { newNotification(p2txt.n_new_comments.replace('%d', newComments.numberofnewcomments)); } } }); toggleUpdates('unewcomments'); } /* * Submits a new post via ajax */ function newPost(trigger) { var thisForm = $(trigger.target); var thisFormElements = $('#posttext, #tags, :input',thisForm).not('input[type=hidden]'); var submitProgress = thisForm.find('span.progress'); var posttext = $.trim($('#posttext').val()); if(jQuery('.no-posts')) jQuery('.no-posts').hide(); if ("" == posttext) { $("label#posttext_error").text('This field is required').show().focus(); return false; } toggleUpdates('unewposts'); if (typeof ajaxCheckPosts != "undefined") ajaxCheckPosts.abort(); $("label#posttext_error").hide(); thisFormElements.attr('disabled', true); thisFormElements.addClass('disabled'); submitProgress.show(); var tags = $('#tags').val(); if (tags == p2txt.tagit) tags = ''; var args = {action: 'prologue_new_post', _ajax_post:nonce, posttext: posttext, tags: tags}; var errorMessage = ''; $.ajax({ type: "POST", url: ajaxUrl, data: args, success: function(result) { if ("0" == result) errorMessage = p2txt.not_posted_error; $('#posttext').val(''); $('#tags').val(p2txt.tagit); if(errorMessage != '') newNotification(errorMessage); if ($.suggest) $('ul.ac_results').css('display', 'none'); // Hide tag suggestion box if displayed if (isFirstFrontPage && result != "0") { getPosts(false); } else if (!isFirstFrontPage && result != "0") { newNotification(p2txt.update_posted); } submitProgress.fadeOut(); thisFormElements.attr('disabled', false); thisFormElements.removeClass('disabled'); } }); thisFormElements.blur(); toggleUpdates('unewposts'); } function newNotification(message) { $("#notify").stop(true).prepend(message + '
') .fadeIn() .animate({opacity: 0.7}, 3000) .fadeOut('3000', function() { $("#notify").html(''); }).click(function() { $(this).stop(true).fadeOut('fast').html(''); }); } /* * Submits a new comment via ajax */ function newComment(trigger) { var thisForm = $(trigger.target); var thisFormElements = $('#comment, #comment-submit, :input', thisForm).not('input[type=hidden]'); var submitProgress = thisForm.find('span.progress'); var commenttext = $.trim($('#comment').val()); if ("" == commenttext) { $("label#commenttext_error").text('This field is required').show().focus(); return false; } toggleUpdates('unewcomments'); if (typeof ajaxCheckComments != "undefined") ajaxCheckComments.abort(); $("label#commenttext_error").hide(); thisFormElements.attr('disabled', true); thisFormElements.addClass('disabled'); submitProgress.show(); var comment_post_ID = $('#comment_post_ID').val(); var comment_parent = $('#comment_parent').val(); var dataString = {action: 'prologue_new_comment' , _ajax_post: nonce, comment: commenttext, comment_parent: comment_parent, comment_post_ID: comment_post_ID}; var errorMessage = ''; $.ajax({ type: "POST", url: ajaxUrl, data: dataString, success: function(result) { var lastComment = $("#respond").prev("li"); if (isNaN(result) || 0 == result || 1 == result) errorMessage =result; $('#comment').val(''); if (errorMessage != "") newNotification(errorMessage); getComments(false); submitProgress.fadeOut(); $("#respond").slideUp('fast'); toggleUpdates('unewcomments'); thisFormElements.attr('disabled', false); thisFormElements.removeClass('disabled'); } }); } function newNotification(message) { $("#notify").stop(true).prepend(message + '
') .fadeIn() .animate({opacity: 0.7}, 2000) .fadeOut('1000', function() { $("#notify").html(''); }).click(function() { $(this).stop(true).fadeOut('fast').html(''); }); } /* * Handles tooltips for the recent-comment widget * param: anchor link */ function tooltip(alink){ xOffset = 10; yOffset = 20; alink.hover(function(e){ this.t = this.title; this.title = ""; $("body").append("
"+ this.t +"
"); $("#tooltip") .css("top",(e.pageY - yOffset) + "px") .css("left",(e.pageX + xOffset) + "px") .fadeIn("fast"); }, function(){ this.title = this.t; $("#tooltip").remove(); }); alink.mousemove(function(e){ $("#tooltip") .css("top",(e.pageY - yOffset) + "px") .css("left",(e.pageX + xOffset) + "px"); }); }; function isElementVisible(elem) { elem = $(elem); if (!elem.length) { return false; } var docViewTop = $(window).scrollTop(); var docViewBottom = docViewTop + $(window).height(); var elemTop = elem.offset().top; var elemBottom = elemTop + elem.height(); var isVisible = ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <= docViewBottom) && (elemTop >= docViewTop) ); return isVisible; } function toggleUpdates(updater){ switch (updater) { case "unewposts": if (0 == getPostsUpdate) { getPostsUpdate = setInterval(getPosts, updateRate); } else { clearInterval(getPostsUpdate); getPostsUpdate = '0'; } break; case "unewcomments": if (0 == getCommentsUpdate) { getCommentsUpdate = setInterval(getComments, updateRate); } else { clearInterval(getCommentsUpdate); getCommentsUpdate = '0'; } break; } } function titleCount() { if (isFirstFrontPage) { var n = $('li.newupdates').length; } else { var n = newUnseenUpdates; } if ( n <= 0 ) { if (document.title.match(/\([\d+]\)/)) { document.title = document.title.replace(/(.*)\([\d]+\)(.*)/, "$1$2"); } } else { if (document.title.match(/\((\d+)\)/)) { document.title = document.title.replace(/\((\d+)\)/ , "(" + n + ")" ); } else { document.title = '(1) ' + document.title; } } } function autgrow(textarea, min) { var linebreaks = textarea.value.match(/\n/g); if (linebreaks != null && linebreaks.length+1 >= min) { textarea.rows = (linebreaks.length+1); } else { textarea.rows = min; } } function jumpToTop() { $.scrollTo('#main', 150); } function bindActions(element, type) { $(element).find('a.comment-reply-link').click(function() { $('#main li').removeClass('replying'); $(this).parents("li").eq(0).addClass('replying'); $("#respond").show(); $("#respond").addClass('replying').show(); $("#comment").focus(); }); switch (type) { case "comment" : var thisCommentEditArea; $(element).hover( removeYellow, removeYellow ); if (inlineEditComments != 0 && isUserLoggedIn) { thisCommentEditArea = $(element).find('div.comment-edit'); thisCommentEditArea.editable(ajaxUrl, {event: 'edit',loadurl: ajaxUrl + '?action=prologue_load_comment&_inline_edit=' + nonce, id: 'comment_ID', name: 'comment_content', type : 'autogrow', cssclass: 'textedit', rows: '3', indicator : '', loadtext: p2txt.loading, cancel: p2txt.cancel, submit : p2txt.save, tooltip : '', width: '90%', onblur: 'ignore', submitdata: {action:'prologue_inline_comment_save',_inline_edit: nonce}}); $(element).find('a.comment-edit-link').click(function() { thisCommentEditArea.trigger("edit"); return false; }); } $(".single #postlist li > div.postcontent, .single #postlist li > h4, li[id^='prologue'] > div.postcontent, li[id^='comment'] > div.commentcontent, li[id^='prologue'] > h4, li[id^='comment'] > h4").hover(function() { $(this).parents("li").eq(0).addClass('selected'); }, function() { $(this).parents("li").eq(0).removeClass('selected'); }); break; case "post" : var thisPostEditArea; if (inlineEditPosts != 0 && isUserLoggedIn) { thisPostEditArea = $(element).children('div.editarea'); thisPostEditArea.editable(ajaxUrl, {event: 'edit',loadurl: ajaxUrl + '?action=prologue_load_post&_inline_edit=' + nonce, id: 'post_ID', name: 'content', type: 'autogrow', cssclass: 'textedit',rows: '3', indicator : '', loadtext: p2txt.loading, cancel: p2txt.cancel, submit: p2txt.save, tooltip : '', width: '90%', onblur: 'ignore', submitdata: {action:'prologue_inline_save', _inline_edit: nonce}}); $(element).find('a.post-edit-link').click(function() { thisPostEditArea.trigger("edit"); return false; }); } $(".single #postlist li > div.postcontent, .single #postlist li > h4, li[id^='prologue'] > div.postcontent, li[id^='comment'] > div.commentcontent, li[id^='prologue'] > h4, li[id^='comment'] > h4").hover(function() { $(this).parents("li").eq(0).addClass('selected'); }, function() { $(this).parents("li").eq(0).removeClass('selected'); }); break } } /* On-load */ commentsLists = $("ul.commentlist"); $('#posttext').focus(); $(".single #postlist li > div.postcontent, .single #postlist li > h4, li[id^='prologue'] > div.postcontent, li[id^='comment'] > div.commentcontent, li[id^='prologue'] > h4, li[id^='comment'] > h4").hover(function() { $(this).parents("li").eq(0).addClass('selected'); }, function() { $(this).parents("li").eq(0).removeClass('selected'); }); $.ajaxSetup({ timeout: updateRate - 2000, cache: false }); $("#directions-keyboard").click(function(){ $('#help').toggle(); return false; }); $("#help").click(function() { $(this).toggle(); }); $("#togglecomments").click(function(){ hidecomments = !hidecomments; var hideTxt = p2txt.hide_threads; var showTxt = p2txt.show_threads; if (hidecomments) { commentLoop = false; commentsLists.hide(); $(this).text(showTxt); } else { commentsLists.show(); $(this).text(hideTxt); } return false; }); if (!isFrontPage) $("#togglecomments").click(); // Activate inline editing plugin if ((inlineEditPosts || inlineEditComments ) && isUserLoggedIn) { $.editable.addInputType('autogrow', { element : function(settings, original) { var textarea = $('