[jQuery] Need help fine tuning
Hi all, new to the list, new to jquery, getting my feet wet.
I've got a script that I could use some help fine-tuning and optimizing, and I'm hoping you all can help. The script is used to collapse forum topics and can be seen in place at
<a href="http://gretschpages.com/forum/modern-gretsch-guitars/">http://gretschpages.com/forum/modern-gretsch-guitars/</a>
ShowLatest() is a helper function that moves the first item up in the tree so it's still visible after the collapse.
If the user has set a preference to collapse threads, a bit of on-page js will call collapse() and set collapsed=true, otherwise, it's a link-activated toggle. Profiling it in firebug, it looks like this thing is working pretty hard, though.
Here's the script. Any suggestions to clean it, lean it, or otherwise improve are welcomed.
var collapsed = "false";
function showLatest() {
$(".recentposts").find("a:eq(0)").each(function(i){
lastpost = $(this).clone();
$(lastpost).html(" »");
$(lastpost).addClass("lastpost");
theParent = $(this).parent().parent().parent().prev();
$(theParent).append($(lastpost));
});
}
// called if user preference is for collapsed:
function collapse() {
$(".recentposts").hide();
$("#collapser").html("Expand All topics");
showLatest();
collapsed = "true";
}
//document.ready:
$(function() {
if (collapsed == "false") {
$("#collapser").toggle(function(e) {
e.preventDefault
();
$(".recentposts").animate({
height: 'hide',
opacity: 'hide'
}, 'slow');
$(this).html("Expand All topics");
showLatest();
}, function() {
$(".recentposts").animate({
height: 'show',
opacity: 'show'
}, 'slow');
$(this).html("Collapse All topics");
$(".lastpost").remove();
});
} else {
$("#collapser").toggle(function(e) {
e.preventDefault();
$(".recentposts").animate({
height: 'show',
opacity: 'show'
}, 'slow');
$(this).html("Collapse All topics");
$(".lastpost").remove();
}, function() {
$(".recentposts").animate({
height: 'hide',
opacity: 'hide'
}, 'slow');
$(this).html("Expand All topics");
showLatest();
});
}
});
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/