/**
 * jqPageFlow v0.1b - jQuery plugin
 * Copyright (c) 2009 Barry Roodt (http://calisza.wordpress.com)
 *
 * Licensed under the New BSD license.
 *
 * This plugin makes scrolling pagination possible (such as that found on Google Reader and dzone.com).
 * An example can be found at http://flexidev.co.za/projects/jqpageflow
 * Please check http://code.google.com/p/flexidev/downloads/ for the latest version
 *
 * Special thanks to Christopher Mills (http://imod.co.za) for the help with naming and promoting this plugin
 *
 */

var CurrentStart = 0;

;(function(jQuery){
 	jQuery.flexiPagination = {

		defaults: {
			url: "",
   			perPage: 25,
   			container: "body",
   			pagerVar : "p",
   			loaderImgPath: "images/loader.gif",
   			debug : 0
		}

	};

	jQuery.fn.extend({
		flexiPagination:function(config) {
		
			// initialize our config object
			var config = jQuery.extend({}, jQuery.flexiPagination.defaults, config);
			var loading = false;
			
			
			// create and append our progress indicator div to the body content, then make sure our css is applied
   			jQuery("body").append("<div id='jqpageflow-block'><img src='" + config.loaderImgPath + "' /><span id='jqpageflow-text'></span></div>");
			jQuery("#jqpageflow-block").addClass("jqpageflow-loader");
			jQuery("#jqpageflow-text").addClass("jqpageflow-loadertext");
   			 
   			// set default container element as body if config var is empty
   			config.container = (config.container != "") ? config.container : "body";
   			
   			
   			
   			//if (config.debug)
   			//console.log("Current Page : %s", CurrentStart);
   			
   			// bind the window's scroll event to a custom function
			jQuery(window).scroll(function(){
			
				var thisAutoPaging = (typeof(FoldOpt)!='undefined')?FoldOpt['feedback']:'block';
				// work out whether we need to fire our ajax call or not
		       if ( CurrentStart >= 0 && (CurrentStart + config.perPage < MaxRows)  
		       && !loading && jQuery(this).scrollTop() == jQuery(document).height() - jQuery(this).height() && thisAutoPaging == 'block') {
		        
		           // this automatically prevents any further attempts to execute another ajax call until the current ajax call has returned a result
		           loading = true;
		           
		           // set a default url if none specified. 
		           // Note: this needs to be calculated just before the ajax call since our currentPage counter is updated each time the event is executed 
		           if (config.url != "") {
						config.url += (config.url.indexOf("?")!==-1 ? "&" : "?") + config.pagerVar + "=" + (CurrentStart + config.perPage);
					} else {
						// the default url is the current window location with the pageVar and currentPage values attached
						config.url = window.location + (window.location.search != '' ? "&" : "?") + config.pagerVar + "=" + ( CurrentStart + config.perPage );
					}
					
				   //if (config.debug)
				   //console.log("Generated Url : %s", config.url);
		           
					// update the loader text and display the loader.
					jQuery("#jqpageflow-text").text('Loading results ' + (config.perPage * ((CurrentStart > 0) ? CurrentStart : 1)) + ' of ' + MaxRows);
					jQuery("#jqpageflow-block").show();
		           

		           // execute our ajax call and deal with the result.
		           jQuery.ajax({
		               type: "GET",
		               dataType: "html",
		               url: config.url,
		               success: function( html ){
		               	   
		                   html = jQuery.trim( html );
							
		                   if ( html ) {
		                       jQuery(config.container).append( html );
		                       CurrentStart+=config.perPage;
		                       
							} else {
								// prevent any further attempts to execute the ajax call since the backend is not returning a useable result.
								CurrentStart-=config.perPage;
							}
							
							/* if (config.debug) {
								console.log("Current Page : %s", CurrentStart);
								console.log("Per Page : %s", config.perPage);
								console.log("Total Results : %s", config.totalResults);
							}
							*/
		               },
		               complete: function(){
		               		// allow ajax call to be executed again if necessary and hide the loader
		                   loading = false;
		                   jQuery("#jqpageflow-block").hide();
		                   if(typeof(ids)!='undefined') $(ids).attr('src','/i/light_yes.gif');
		               }
		           });
		       }
			});
			
			return this;
		}
	});
	
})(jQuery);
