// functions 

function LiveCycle() { 

		function onAfter(curr, next, opts) {
			var index = opts.currSlide + 1;
			$(this).parent().parent().find('.currentSlide').html(index); 
		}


	$('#content .newsImages').each(function() { 
				$(this).cycle({
				fx:'fade',
				speed:300,
				next:$(this).parent().find('.next'),
				prev:$(this).parent().find('.prev'),
				pager:$(this).parent().find('.pager'),
			    after: onAfter,
				timeout:0
		});

		var totalImages = $(this).children('img').size();
		if (totalImages < 2) { $(this).children('img').css('cursor','default'); }
	});

}	

$(document).ready(function() {

LiveCycle(); 

	$('.load').live('click', function() {

		// var thisDomain = "http://"+window.location.hostname;
		var lastRecord = $(this).attr("lastRecord");
		var perPage = $(this).attr("perPage");
		var totalRecords = $(this).attr("totalRecords");
		
		// debugging : alert('total records picked up...'+totalRecords+'\n per page picked up...'+perPage+'\n last record picked up...'+lastRecord);
		
		if ((totalRecords * 1) <= (lastRecord * 1) + (perPage * 1)) {

			// we are NOT going to need another load more button... 

			$(this).html("<img src='/ndxz-studio/site/slowmobile/elements/more-posts-loading.gif' id='loader' alt='Loading More...'/>Loading More...");
			$.ajax({type: "POST",url: "/ndxz-studio/site/plugin/_ajax_news_loader.php",data: "q="+ lastRecord+"&p="+perPage+"&c="+totalRecords+"&e=true",success: function(html){
				$("#homeNavigation").remove();
				$("#content").append(html);
				var scrollTarget = "#news-"+ ( 1 * (lastRecord) + 1); // calculate post to slide to from lastrecord
				$.scrollTo(scrollTarget, 900);
				LiveCycle(); 
			}
			});

		}
		
		else
		
		{
	
			// we WILL need another load more button... 
	
			$(this).html("<img src='/ndxz-studio/site/slowmobile/elements/more-posts-loading.gif' id='loader' alt='Loading More...'/>Loading More...");
			$.ajax({type: "POST",url: "/ndxz-studio/site/plugin/_ajax_news_loader.php",data: "q="+ lastRecord+"&p="+perPage+"&c="+totalRecords+"&e=false",success: function(html){
				$("#homeNavigation").remove();
				$("#content").append(html);
				var scrollTarget = "#news-"+ ( 1 * (lastRecord) + 1); // calculate post to slide to from lastrecord
				$.scrollTo(scrollTarget, 900);
				LiveCycle(); 
			}
			});
		}

		});
	
});
