var window_width = 950;
var loaded_pages = new Array(1,2); // Array of page numbers that we have already loaded


if(navigator.appName == 'Microsoft Internet Explorer') {
	if(((navigator.appVersion + '').indexOf('MSIE 6.0;')) !== -1) {
		window_width = 960;
	}
}



function scroll_to(old_page, new_page, do_ajax){
	
	if ((new_page - old_page)>0) var go_forward = true;
	
	if(go_forward){
	
		// First, move yo' div - we can do this when going forward before inserting the new div
		do_scroll(-window_width);
		
		// Do we need to preload in another div?
		if((loaded_pages.in_array(new_page+1)==false) && (do_ajax!=false)) {
			
			// Put the content of the ajax into a new div
			new Ajax.Updater('page_' + new_page, '/search/ajaxresults/' + (new_page+1), {
				method: 'get',
				
				insertion: 'after',
			
				onSuccess: function(){
						
					loaded_pages.push(new_page+1);
					// Allow for the new div in the CSS
					$('containing_results_box').setStyle({
						width: (window_width * (loaded_pages.size()+1)) + 'px'
					});
				},
			
				onFailure: function(request){
					handle_error(request, "/");
				}
		
			});
			
		}
	}
	
	else{ 	// Going back a page, so we need to load in the ajax before scrollage (to stop the screen jumping around as the AJAX page would be bunged in during scroll). 
			// This is slightly annoying but only happens after a jump_to followed by a "Previous".
		
		// Do we need to preload in another div?
		if((loaded_pages.in_array(new_page-1)==false) && (do_ajax!=false)) {
			
			new Ajax.Updater('page_' + new_page, '/search/ajaxresults/' + (new_page-1), {
				
				method: 'get',
				
				insertion: 'before',
			
				onSuccess: function(transport) {
					loaded_pages.push(new_page-1);
					loaded_pages.sort();
					$('containing_results_box').setStyle({
						width: (window_width * loaded_pages.size()) + 'px',
						left:  (-window_width * (loaded_pages.indexOf(new_page+1))) + 'px' // This is v important as it very quickly resets the viewpoint to the correct position following the insert.
					});
					do_scroll(window_width);
				},
				
				onFailure: function(request){
					handle_error(request, "/");
				}
		
			});
		}
		else{
			do_scroll(window_width);
		}

	}
	set_hovers(); roundthecorners();
	return true;
}


function jump_to(page, last_page){
	
	page = parseInt(page);
	var needed = 0;
	var completed = 0;
	var	stop_pe = false;
	var to_be_added = [];
	var lower;
	var upper;
	var one_below_min_tba;
		
	// First hide the results div...
	$('containing_results_box').hide();
	// ..and show a "loading" spinner
	$('loading').show();
		
	//Do we actually need to grab any pages? (look either side - within reason! - as well as the page itself)
	lower = ((page-1)>1) ? (page-1) : 2; // 2 is the first guaranteed div as that's on the first generated page HTML
	upper = ((page+1)<=last_page) ? (page+1) : last_page;
	
	for(j=lower; j<=upper; j++){
		if(loaded_pages.in_array(j)==false) to_be_added.push(j);
	}
	
	// Anything to add in?
	if(to_be_added.size()>0){
		//alert(to_be_added);
			
		// Find the less closest page that is already present so we know the 'insert after' point ("hook_div").
		var one_below_min_tba = to_be_added.min()-1;
		if(loaded_pages.in_array(one_below_min_tba)){
			var hook_div = 	'page_' + one_below_min_tba;
		}
		else{ // Loops down but should always find at least page_2...
			for(i = (one_below_min_tba-1); i>0; i--){
				if(loaded_pages.in_array(i)){
					var hook_div = 	'page_' + i;
					break;
				}					
			}
		}	
	
		// Do it!
		new Ajax.Request('/search/ajaxresults', {  // Request used rather than update to make it prettier if it fails (e.g. 404 contents would get put in otherwise)
			
			parameters: {
				pages: to_be_added.toJSON()
			},
			
			onSuccess: function(transport){
				loaded_pages = loaded_pages.concat(to_be_added);
				loaded_pages.sort();	// Ensure the array order matches the screen order, so we can use indexOf.
				$(hook_div).insert({after: transport.responseText});
				
				$('containing_results_box').setStyle({
					width: ( window_width * (loaded_pages.size()+1) ) + 'px',
					left:  (-window_width * (loaded_pages.indexOf(page))) + 'px'
				});
				$('containing_results_box').show();
				$('loading').hide();
				set_hovers();
			},
			
			onComplete: function(){
				setTimeout('roundthecorners()', 1); // The delay is there as it wasn't rounding without it... makes sense.
			},
			
			onFailure: function(request){
				handle_error(request, "/");
			}
		
		});
	}
	else{
		// No page load needed, so just move to the position of the page div
		// Do a nice transition here?
		$('containing_results_box').setStyle({
			left:  (-window_width * (loaded_pages.indexOf(page))) + 'px'
		});
		$('containing_results_box').show();
		$('loading').hide();
	}
	
	return true;
	// Or, IF YA WANNA SEE IT SCROLL: $('loading').hide(); $('containing_results_box').show(); new Effect.Move('containing_results_box', { x:-(window_width*(loaded_pages.indexOf(page))), mode:'absolute'});
}



function do_scroll(x){ // TODO: use effect.Move() instead
	new Effect.MoveBy('containing_results_box', 0, x, {duration: 0.8,  transition: Effect.Transitions.sinoidal, queue: 'end'});
}



// counter
var i = 0;

// create object
imageObj = new Image();

// set image list
images = new Array();
images[0]="/images/layout/"+theme+"/results-box-hover.png";
images[1]="/images/layout/"+theme+"/results-showcase-box-hover.png";
/*
images[2]="image3.jpg"
images[3]="image4.jpg"*/

// start preloading
for(i=0; i<=images.size(); i++){
  imageObj.src=images[i];
}


Event.observe(window, 'load', roundthecorners, false);

