step_ajax_start=0;
step_ajax_max= 0;
step_ajax_active = true;
step_ajax_loading_html = "<div id=\"step_ajax_loading\"><center><p>Loading</p></center></div>";

function steps_ajax_init(data_i)
{	
	
	if (data['step_ajax_init_with_anchor']=="y")
	{
		document.location.href="#"+data['step_ajax_anchor'];
	}
	
	$(window).scroll(function()
	{
		
		if  ( ($(window).scrollTop() == $(document).height() - $(window).height()) && (step_ajax_active) )
		{
				
				steps_ajax_adv(data_i);
				//alert(step_ajax_active);
				 
		}
		else
		{
			if (!ch_scr_b()) { steps_ajax_adv(data_i);}
			
		}		
				
	}); 
	
	
}


function steps_ajax_adv(data_i)
{

	step_ajax_max = data['steps_ajax_max'];
	
	//alert("start:"+step_ajax_start+ "step:"+data_i['step_ajax_step']+" type:"+data_i['step_ajax_type']);
	$.ajax({
			type: "GET",
			url: data_i['step_ajax_url'],
			cache: false,
beforeSend: function(){ step_ajax_active=false; $("#step_ajax_loading_html").append(step_ajax_loading_html); },
			data:{
				"start":step_ajax_start,
				"step":data_i['step_ajax_step'],
				"type":data_i['step_ajax_type'],
				"step_ajax_be":data_i['step_ajax_be'],
				"step_ajax_info":data_i['step_ajax_info']
				},
			complete: function(t)
			{
				//alert(t.responseText);
				$("#step_ajax_loading_html").remove();
				var ar = t.responseText.split("___SEP___");
				//alert(t.responseText+" "+ar[1]+" "+ar[2]);
				$("#"+data_i['step_ajax_container']).append(ar[0]);
								
				step_ajax_start = parseInt(ar[1]);
				//alert(step_ajax_start+"<"+step_ajax_max);
				
				if(step_ajax_start<step_ajax_max)
				{
					step_ajax_active=true;					
				}
				
				steps_ajax_callback(data_i);
				
			
			}
	});
}
function steps_ajax_adv_init(data_i)
{	
	step_ajax_max = data_i['steps_ajax_max'];	
		
	var step = data_i['step_ajax_init_step'];
	
	if (step=="0"){ step = "5"; }
	//alert("ao"+step);
	
	$.ajax({
			type: "GET",
			url: data_i['step_ajax_url'],
			cache: false,
			beforeSend: function(){ step_ajax_active=false; $("#step_ajax_loading_html").append(step_ajax_loading_html); },
			data:{
				"start":0,
				"step":step,
				"init":true,
				"type":data_i['step_ajax_type'],
				"step_ajax_be":data_i['step_ajax_be'],
				"step_ajax_info":data_i['step_ajax_info']
				},
			complete: function(t)
			{
				$("#step_ajax_loading_html").remove();
				//alert(t.responseText);
				var ar = t.responseText.split("___SEP___");
				
				$("#"+data_i['step_ajax_container']).html(ar[0]);
								
				step_ajax_start = parseInt(ar[1]);
				
				//alert(t.responseText+" "+ar[1]+" "+ar[2]);
				
				if(step_ajax_start<step_ajax_max)
				{
					step_ajax_active=true;
				}
				steps_ajax_init(data_i);
				
				//document.location.href="#"+data['step_ajax_anchor'];
				steps_ajax_callback(data_i);
				
				
			}
	});
}

function steps_ajax_callback(data_i)
{
	if (data_i['step_ajax_callback']) { eval(data_i['step_ajax_callback'])(); }
}

function ch_scr_b()
{
	var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
		    
		    if (jQuery.browser.msie)
		    {	     
	            viewportHeight += 10;
	    	}
	    
	    if(parseInt(viewportHeight)<parseInt($(document).height()))
	    {
	       return true;
	    } 
	    else 
	    {
	       return false;
	    }
}
