$(document).ready(function() {
$('a#add-comment').click(function() { $('#comment-form').show(); $('#comment-text').focus();});
$('form#signup-choose-pic div.pic-item').click(function() { $(this).siblings().addClass('grey-out'); $(this).siblings().removeClass('chosen'); $(this).addClass('chosen'); var chosenpic = $(this).find('img').attr('src'); $('input#pic-choose').attr('value', chosenpic);});
$('#menu-button a').click(function() { 
$('#menu').toggle();});$('#menu a.close').click(function() { $('#menu').hide();});

/* AJAX FETCHING OF MORE ARTICLES AND COMMENTS. TODO: REDUCE TO 2 FUNCTIONS */
$('#more-comments').click(function() { var ajax_load = "<div id='loading-text'>fetching more comments...</div>"; var loadUrl = "comments-example.html"; var data = $("#more-form").serialize(); var startComment = parseInt($('input#lastComment').val()) + 20; var articleID = $('input#articleID').val(); var userID = $('input#userID').val(); $(this).hide(); $("#more-form").remove(); $("#more-box").append(ajax_load); $.get( loadUrl, data, function(responseText){ $("#comments-box").append(responseText); }, "html" ); $('#loading-text').ajaxComplete(function() { $("#more-box").append("<form id='more-form'><input type='hidden' name='userID' id='userID' value='" + userID + "' /><input type='hidden' name='articleID' id='articleID' value='" + articleID + "' /><input type='hidden' id='lastComment' name='lastComment' value='" + startComment + "' /></form>"); $(this).remove(); $('#more-comments').show(); }); }); 
$('#more-articles').click(function() { var ajax_load = "<div id='loading-text'>fetching more news articles...</div>"; var loadUrl = "article-example.html"; var data = $("#more-form").serialize(); var articleID = $('input#articleID').val(); $(this).hide(); $("#more-form").remove(); $("#more-box").append(ajax_load); $.get( loadUrl, data, function(responseText){ $("#article-box").append(responseText); }, "html" ); $('#loading-text').ajaxComplete(function() { $("#more-box").append("<form id='more-form'><input type='hidden' name='articleID' id='articleID' value='" + articleID + "' /></form>"); $(this).remove(); $('#more-articles').show(); }); });
$('#more-events').click(function() { var ajax_load = "<div id='loading-text'>fetching more events...</div>"; var loadUrl = "event-example.html"; var data = $("#more-form").serialize(); var articleID = $('input#articleID').val(); $(this).hide(); $("#more-form").remove(); $("#more-box").append(ajax_load); $.get( loadUrl, data, function(responseText){ $("#article-box").append(responseText); }, "html" ); $('#loading-text').ajaxComplete(function() { $("#more-box").append("<form id='more-form'><input type='hidden' name='articleID' id='articleID' value='" + articleID + "' /></form>"); $(this).remove(); $('#more-events').show(); }); });
$('#more-pics').click(function() { var ajax_load = "<div id='loading-text'>fetching more pics...</div>"; var loadUrl = "pics-example.html"; var data = $("#more-form").serialize(); var articleID = $('input#articleID').val(); $(this).hide(); $("#more-form").remove(); $("#more-box").append(ajax_load); $.get( loadUrl, data, function(responseText){ $("#article-box").append(responseText); }, "html" ); $('#loading-text').ajaxComplete(function() { $("#more-box").append("<form id='more-form'><input type='hidden' name='articleID' id='articleID' value='" + articleID + "' /></form>"); $(this).remove(); $('#more-pics').show(); }); });
$('#more-videos').click(function() { var ajax_load = "<div id='loading-text'>fetching more videos...</div>"; var loadUrl = "video-example.html"; var data = $("#more-form").serialize(); var articleID = $('input#articleID').val(); $(this).hide(); $("#more-form").remove(); $("#more-box").append(ajax_load); $.get( loadUrl, data, function(responseText){ $("#article-box").append(responseText); }, "html" ); $('#loading-text').ajaxComplete(function() { $("#more-box").append("<form id='more-form'><input type='hidden' name='articleID' id='articleID' value='" + articleID + "' /></form>"); $(this).remove(); $('#more-videos').show(); }); });

/* AJAXY LIKES */
$("a.like").live("click", function(){ var loadUrl = $(this).attr("href"); var curr_elem = $(this); $.get(loadUrl, function(responseText) { $(curr_elem).parents("div.item-tools").children('span.likes').html(responseText).show(); $(curr_elem).parents("div.item-tools").append("<span class='tag'>Liked</span>").find('a.like').parent("span").remove(); }); return false; });

/* NICE COMMENT REPLIES, TO BE IMPROVED LATER */
$('a.reply').live("click", function() { var curr_elem = $(this); $('#comment-form').show(); $('#comment-text').focus(); $('#comment-text').html("@" + $(curr_elem).parents("div.item").children("div.date").children("a").children("strong").html() + " ");});
});

/* COMMON VALIDATION */
$(function() { $('#comment-form').validate(); });