$(document).ready(function() {
    var slides = $('div#lede_stories ul.slides li').length;
    console.log('Slides: ', slides);
    if(slides == 1) {
        $('div#lede_stories ul.slides li').show();
        $('div#prev').attr('id','prev_off');
        $('div#next').attr('id','next_off');
    }
    
    $('div#lede_stories ul.slides').cycle({
            fx:'scrollHorz',
            speed:300,
            timeout:0,
            next:'#next a', 
            prev:'#prev a',
            pager:'div#lede_pagination',
            pagerAnchorBuilder:function(idx,slide){
                return '<a href="#"><img src="http:\/\/media.charlotteobserver.com\/static\/design\/blank.gif" alt=""><\/a>';
            }
        });
    
    // upper comment pref confirm bubble function
    $('span#comment_pref a').click(function(){
        $('#comment_bubble_up').fadeIn();
    });
    
    $('div#comment_bubble_up img.cancel').click(function(){
        $('#comment_bubble_up').fadeOut();
    });
    
    // Comment Disclaimer
    $('a#disclaimer-link').click( function() {
        var id = this.id;
        if ($('div.'+id).is(":hidden")) {
            $('span.'+id).removeClass('icon-down');
            $('span.'+id).addClass('icon-up');
        }
        else {
            $('span.'+id).removeClass('icon-up');
            $('span.'+id).addClass('icon-down');
        }
        $('div.'+id).slideToggle("normal");
    });
    
});


function MostRecentForumPosts(forum) {
var activityDisco = new Activity('Recent');  
var contentType = new ContentType('Discussion');
var ParentKeys = new Array(new ForumKey(forum));
var requestBatch = new RequestBatch();
activityRecommended = new DiscoverContentAction('','','',activityDisco,contentType,14,4,'',ParentKeys);
requestBatch.AddToRequest(activityRecommended);
requestBatch.BeginRequest(serverUrl, getForumPost);

function getForumPost(responseBatch) {
        console.log('Response', responseBatch);
        var Discussions = responseBatch.Responses[0].DiscoverContentAction.DiscoveredContent;
	
    $('#mostRecentPosts').empty();
        
        var result = jQuery.each(Discussions, function () {
                var html = '';
                var discussKey = this.ForumDiscussionKey.Key;
                var discussTitle = this.DiscussionTitle;
		var discussUrl = this.DiscussionUrl.replace('\/forums?','\/moms_forums?');;
                forumPostPageRequest = new ForumPostsPage(new DiscussionKey(discussKey), 1, 1, 'TimeStampDescending');
        	var requestBatch = new RequestBatch();
        	requestBatch.AddToRequest(forumPostPageRequest);
        	requestBatch.BeginRequest(serverUrl, function(responseBatch) {
                        //console.log('Post Info: ', responseBatch);
			var posts = responseBatch.Responses[0].ForumPostsPage.Posts[0];
                        var postUrl =  posts.PostUrl;
                        var postUser = posts.LiteUser.DisplayName;
			var postAvatar = posts.LiteUser.ImageUrl;
                        var postPersona = posts.LiteUser.PersonaUrl;
                        var postUpdated = posts.LastUpdated;
                        var postBody = posts.PostBody;
                        postBody = postBody.replace(/\[QUOTE\]/g,'\"');
                        postBody = postBody.replace(/\[\/QUOTE\]/g,'\" -- ');
                        var postTitle = posts.PostTitle;
        
                        if (postBody.length > 100) {
                        postBody = postBody.substr(0,100) + ' ...';
                        }
                        html = '';
                        html += '<li>';
                        html += '<h6><a href="' + postUrl + '" title="' + discussTitle + '">' + postBody +'</a></h6>';
                        html += '<p class="time_stamp">' + postUpdated + ' | (' + this.PostCount + ') Posts</p>';
                        html += '</li>';
                        $('#forums ul.headline_list').append(html);
                        });
                
        });
}
}

