// JavaScript Document
$(function(){
	var sbajsuid=0;
	$.fn.sbAjaxScroll=function(settings,evt){
		var $this=$(this);
		var id=$this.attr("id");
		var fire=false;
		if(id=="undefined"){
			sbajsuid++;
			var id="sbajs" +sbajsuid;
			$this.attr("id",id);
		}
		var handler=function(evt){
			var $elem=$this;
			if($elem.data("sbAjaxScrollEnabled")!=true){return;}
			checkGet();
		};
		if(options=="disable"){
			$this.data("sbAjaxScrollEnabled",false);
			return;
		}
		if(options=="enable"){
			$this.data("sbAjaxScrollEnabled",true);
			return;
		}
		var defaults={
			'ajaxSettings':{},
			'fireEndOfDoc':false,
			'fireScrollBuffer':true,
			'fireEndOfElem':false,
			'scrollBuffer':150,
			'beforeSubmit':null,
			'onComplete':null,
			'onSuccess':null,
			'fillElement':'',
			'loadingElement':'',
			'useLoadingElement':true,
			'dbInitialOffset':0,
			'dbReturnRecords':10,
			'maxRuns':-1
		}
		var Runs=0;
		var options = $.extend(defaults, settings);
		$this.data("sbAjaxScrollEnabled",true);
		$this.data("sbAjaxScrollLoading",false);
		//fire either begining or end not both
		if(options.fireScrollBuffer==true && options.fireEndOfElem==true){options.fireEndOfElem=false;}
		$(window).bind("scroll resize",handler);
		function checkGet(){
			if(fire==true || $this.data("sbAjaxScrollLoading")==true || $this.data("sbAjaxScrollEnabled")==false){return;}
			var viewTop = $( window ).scrollTop();
			var viewBottom = (viewTop + $( window ).height());
			var containerBottom = Math.floor(
				$this.offset().top +
				$this.height()
			);
			var docBot=$(document).height();
			if(options.fireScrollBuffer===true){
				if ((containerBottom - options.scrollBuffer) <= viewBottom){
				// The bottom of the container is close enough
				// to the bottom of thew view frame window to
				// imply more item loading.
					fire=true;
				}
			}else if(options.fireEndOfElem===true){
				if (containerBottom - options.scrollBuffer <= viewBottom){
					fire=true;
					alert("fire end of elem");
				}
			}
			if(options.fireEndOfDoc===true){
				var oSet=$(document).height()-$(window).height();
				if(viewTop==oSet){
					fire=true;	
					alert("fire end of doc");
				}
			}
			if(fire!==true || !options.ajaxSettings.url){return;}
			Runs+=1;
			if(options.maxRuns>=0 && Runs>options.maxRuns){
						$this.data("sbAjaxScrollEnabled",false);
						return;
			}
			//we got this far send the ajax request
			//override ajax settings
			if(options.ajaxSettings.beforeSend){
				if(typeof(options.ajaxSettings.beforeSend)=="function"){
					options.beforeSubmit=options.ajaxSettings.beforeSend;
					options.ajaxSettings.beforeSend=null;
				}
			}
			if(options.ajaxSettings.success){
				if(typeof(options.ajaxSettings.success)=="function"){
					options.onSuccess=options.ajaxSettings.success;
					options.ajaxSettings.success=null;
				}
			}
			if(options.ajaxSettings.complete){
				if(typeof(options.ajaxSettings.complete)=="function"){
					options.onComplete=options.ajaxSettings.complete;
					options.ajaxSettings.complete=null;
				}
			}
			var d=$.param({'dbOffset':options.dbInitialOffset,'dbLimit':options.dbReturnRecords});
			var sendSettings=$.extend({}, options.ajaxSettings);
			sendSettings.data=Array(sendSettings.data,d).join("&");
			options.dbInitialOffset+=options.dbReturnRecords;
			var myAjaxSettings={
				'success':function(data, textStatus){
					if(!data || data==''){
						$this.data("sbAjaxScrollLoading",false);
						$this.data("sbAjaxScrollEnabled",false);
						var target=(options.fillElement&&options.fillElement!==''?$(options.fillElement):$this);
						$(".sbajs_load",target).remove();
						return;
					}
					if(typeof(options.onSuccess)=="function"){
						options.onSuccess.call(data, textStatus);
					}else{
						var target=(options.fillElement&&options.fillElement!==''?$(options.fillElement):$this);
						target.append(data);
					}
					//$this.data("sbAjaxScrollLoading",false);
				},
				'complete':function(jqXHR, textStatus){
					//remove loading
						var target=(options.fillElement!==''?$(options.fillElement):$this);
						$(".sbajs_load",target).remove();
						if(typeof(options.onComplete)=="function"){
							options.onComplete.call(jqXHR, textStatus);
						}
					$this.data("sbAjaxScrollLoading",false);
				},
				'beforeSend':function(jqXHR, settings){
					$this.data("sbAjaxScrollLoading",true);
					fire=false;
					//add loading
					if(options.useLoadingElement===true){
						var target=(options.fillElement!==''?$(options.fillElement):$this);
						if(options.loadingElement!=''){
							var elem1=$(options.loadingElement);
						}else{
							var elem1=$("<div></div>");
						}
						if(!$(".sbajs_load",target).length){
							elem1.css("text-align","center");
							elem1.append("<img src='/images/loading.gif' alt='Sending request to server'/>&nbsp;Loading Content...");
							target.append(elem1);
						}
						$(elem1,target).addClass("sbajs_load");
					}
						if(typeof(options.beforeSubmit)=="function"){
							options.beforeSubmit.call(jqXHR, settings);
						}
				},
				'error':function(jqXHR, textStatus, errorThrown){
					//alert the message
					alert(textStatus+"\n"+errorThrown);
				}
			}
			$.extend(sendSettings,myAjaxSettings);
			$.ajax(sendSettings);
		}
	};//end ajaxscroll
});
