// paginate_media
// by chris brack
// 12/29/07

// basically, we want to have a function available so that
// when we click to follow the next page, we'll make a
// call to get_results.php using the xmlhttprequest

var curr_page;
var total_pages;
var curr_results;
var curr_zip;
var curr_range;

var load_icon = "<div align='center'><img src='/missioncontrol/img/anim/load_circle.gif' width='24' height='24' alt='load icon' /><br /><br /><br />One moment, locating nearby media...</div>";

function fetch_page( target_page )
{
	if( (target_page > 0)  && (target_page <= total_pages) )
	{
		var output_area = document.getElementById("paginate_output_container");
		
		output_area.innerHTML = load_icon;
		
		
		var XHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
			
		XHR.onreadystatechange = function()
		{
			if( XHR.readyState == 4 )
			{
				curr_page = target_page;
				
				output_area.innerHTML = XHR.responseText;
				//alert( XHR.responseText );
				draw_page_options();
			}
		}
		
		var script_loc  = "/volunteerhq/lib/paginate_media/get_results.php";
		var params		= "?func=data&zip=" + curr_zip + "&range=" + curr_range + "&curr_page=" + target_page + "&num_results=" + curr_results;
		
		XHR.open("GET", script_loc+params );
		XHR.send( null );
		
		curr_page = target_page;
	}
	else
	{

		// No results found -- so tell them this!
		
		var output_area = document.getElementById("paginate_output_container");
		
		output_area.innerHTML = '';
		
		var page_bar = document.getElementById("paginate_pages");
		var page_listing = "Sorry, but there were no media outlets found within " + curr_range + " miles of zip code " + curr_zip + ".<br /><br />[ <a href=\"/volunteerhq/volunteermedia.php\">Try a Different Search</a> ]";
		
		page_bar.innerHTML = page_listing;
		
	}
	
	document.body.onscroll  = popup_scroll_compensation;
	window.onscroll			= popup_scroll_compensation;

}

function get_total_pages( result_count, z, r )
{
	document.getElementById("paginate_output_container").innerHTML = load_icon;
	
	curr_results 	= result_count;
	curr_zip 		= z;
	curr_range 		= r;
	curr_page 		= 1;
	
	var XHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
		
	XHR.onreadystatechange = function()
	{
		if( XHR.readyState == 4 )
		{
			total_pages = parseInt( XHR.responseText / result_count );
			
			total_pages = ( XHR.responseText % result_count != 0 ) ? total_pages+1 : total_pages;
			
			//alert("ok we have the response: " + total_pages + " with: " + XHR.responseText + " rows" );

			fetch_page( curr_page );
		}
	}
	
	var script_loc  = "/missioncontrol/templates/template1/lib/paginate_media/get_results.php";
	var params		= "?func=rows&zip=" + z + "&range=" + r;
	
	XHR.open("GET", script_loc+params );
	XHR.send( null );
}

function draw_page_options()
{
	var page_bar = document.getElementById("paginate_pages");
	var page_listing = "";
			
	page_listing 	+= "<a href='javascript:void();' style='margin-right: 10px;' onclick='previous_page();'>&lt;&lt; Previous</a>";
	
	for( var i = 1; i <= total_pages; i++ )
	{				
		var inner_linky = ( i == curr_page ) ? "<strong>" + i + "</strong>" : i;
		
		page_listing += "<a href='javascript:void();' style='margin: 0 3px 0 3px;' onclick='fetch_page(" + i + ");'>" + inner_linky + "</a>";
	}
	
	page_listing 	+= "<a href='javascript:void();' style='margin-left: 10px;' onclick='next_page();'>Next &gt;&gt;</a>";
	
	page_bar.innerHTML = page_listing;
}

function set_page( page_num )
{
	curr_page = page_num;
}

function next_page()
{
	if( curr_page+1 <= total_pages )
	{
		fetch_page( curr_page + 1 );
	}
}

function previous_page()
{
	if( curr_page-1 >= 0 )
	{
		fetch_page( curr_page - 1 );
	}
}
