/*
	These are the variables available in the javascript that are set in activityItemGateway method renderActivityFeed
	
	var merchantID = #arguments.merchantID#;
	var numberOfItemsGet = #arguments.numberOfItemsGet#;
	var userID = #arguments.userID#;
	var renderFriendsOnly = #arguments.renderFriendsOnly#
	
	var delay = #arguments.refreshIncrement#;
	var count = #local.activityItems.recordcount#;
	var showing = #arguments.numberOfItemsDisplay#;
*/


var seeMoreCount = 0;

function move(i) {
    return function() {
      $('#item-'+i).remove().css('display', 'none').prependTo('#activityFeed');
    }
}

function rollActivity() {
	var toShow = (i + showing) % count;
	$('#item-'+ toShow).slideDown(1000, move(i));
	$('#item-'+ i).slideUp(1000, move(i));
		i = (i + 1) % count;
}

function seeFriendsOnly() {
	
	seeMoreCount = 0

	$('#activityFeed').html('<img src="/assets/images/loading.gif" />');
	
	$.get
	(
		'/xhr/renderActivityFeedProxy.cfm?v=' + Math.random(), 
		{
			refreshIncrement:delay, 
			numberOfItemsDisplay:friendsOnlyNumber, 
			numberOfItemsGet:numberOfItemsGet, 
			merchantID:merchantID, 
			userID:userID, 
			renderFriendsOnly:true,
			makeUnique:true
		}, 
		function(response)
		{
			$('#activityFriendsOnly').unbind('click')

			if($(response).length == 1)
			{
				$('#activityFeed').replaceWith(response);
			} else {
				$('#activityFeed').html('No recent activity');
				$('#activityFeed').css('height', '50px');
			}
			$('#activityFriendsOnly').removeClass('cursor').removeClass('siteBlue')
			$('#activitySeeAll').addClass('siteBlue').addClass('cursor').bind('click', seeAll)
			
		}
	)
}

function seeAll() {
	$('#activityFeed').html('<img src="/assets/images/loading.gif" />');
	$.get
	(
		'/xhr/renderActivityFeedProxy.cfm?v=' + Math.random(), 
		{
			refreshIncrement:delay, 
			numberOfItemsDisplay:friendsOnlyNumber, 
			numberOfItemsGet:numberOfItemsGet, 
			merchantID:merchantID, 
			userID:userID, 
			renderFriendsOnly:false,
			makeUnique:true
		}, 
		function(response)
		{
			$('#activitySeeAll').unbind('click')	
			$('#activityFeed').replaceWith(response);
			$('#activitySeeAll').removeClass('cursor').removeClass('siteBlue')
			$('#activityFriendsOnly').addClass('siteBlue').addClass('cursor').bind('click', seeFriendsOnly)
		}
	)
}

function seeMore(){
	var currentHeight = $('#activityFeed').css('height');
	
	seeMoreCount++
	
	$('#seeMore').html('<img src="/assets/images/loading.gif" style="margin-top:-10px;" />');
	
	$.get
	(
		'/xhr/renderActivityFeedProxy.cfm?v=' + Math.random(), 
		{
			refreshIncrement:delay, 
			numberOfItemsDisplay:showing, 
			numberOfItemsGet:seeMoreCount * showing  + 12, 
			merchantID:merchantID, 
			userID:userID, 
			renderFriendsOnly:true,
			startRow:seeMoreCount * showing + 1
		}, 
		function(response)
		{
			
			
			
			$('#seeMore').parent().remove();
		
			var newHeight = parseFloat($(response).children().length * 71) + parseFloat(currentHeight)
			

			if($(response).find('#seeMore').length == 1)
			{
				newHeight = newHeight - 71;
			} else {
				newHeight = newHeight - 30;
			}
			
			$('#activityFeed').height(newHeight);
			$('#activityFeed').append($(response).children().fadeIn(800));
			$('#footer').prepend('&nbsp;');
		}
	)
}

$(document).ready(function(){
	setInterval('rollActivity()', delay);
	$('#activityFriendsOnly').bind('click', seeFriendsOnly)	
	$('#seeMore').live('click', seeMore)
});



