/*
 * NScroller
 * Scroll a set of numbers.
 * Copyright (c) 2005 Mackley F. Pexton.  All rights reserved.
 * Send correspondence and feedback to: mack_pexton[at]acmebase[dot]com.
 */

/*******************************************************************************

NScroller v1.2 -- Scroll numbers over a set of elements.

Example:

<div id="search_scroll">
<table align="right" cellspacing="0" cellpadding="0" border="0">
<tr>
  <td align="left" width="16"><a href="javascript:void 'scroll left'" class="scroll-button-left" onmousedown="scroller.scroll(-1)" onmouseup="scroller.stop()" title="Scroll">&nbsp;</a></td>
  <td><div class="scroll-element" id="search_scroll_1">&nbsp;</div></td>
  <td><div class="scroll-element" id="search_scroll_2">&nbsp;</div></td>
  <td><div class="scroll-element" id="search_scroll_3">&nbsp;</div></td>
  <td><div class="scroll-element" id="search_scroll_4">&nbsp;</div></td>
  <td><div class="scroll-element" id="search_scroll_5">&nbsp;</div></td>
  <td align="right" width="16"><a href="javascript:void 'scroll right'" class="scroll-button-right" onmousedown="scroller.scroll(1)" onmouseup="scroller.stop()" title="Scroll">&nbsp;</a></td>
</tr>
</table>
</div>
</div> 
<script type="text/javascript">
var scroller_min = 1;
var scroller_max = <?php echo $max_page; ?>;
var scroller_curr = <?php echo $page; ?>;
var scroller_elements = [
	document.getElementById('search_scroll_1'),
	document.getElementById('search_scroll_2'),
	document.getElementById('search_scroll_3'),
	document.getElementById('search_scroll_4'),
	document.getElementById('search_scroll_5') ];
// Make new buttons with following template.
var scroller_template = '<a href="search.php?location=&property_type=&price_range=&page=%N">%N</a>';
// Execute following function when scroll stops. 
var scroller_action = function(n) { location.href="search.php?location=&property_type=&price_range=&page="+n; };
var scroller = new NScroller(scroller_min,scroller_max,scroller_curr,scroller_elements,scroller_template,scroller_action);
</script>

*******************************************************************************/

// Scroller configuration variables. (change as desired)
NScroller.Rate  = null;		// Rate of scroll (milliseconds between iterations, null is adjustable rate)
NScroller.Delay = 500;		// Initial delay before scrolling (milliseconds)
NScroller.ClassNameNormal = 'scroll-element';			// class name assigned to non-current elements
NScroller.ClassNameCurrent = 'scroll-element-current';		// class name appended to current page element

// Scroller working variables. (do not change)
NScroller.timer = null;		// setTimeOut() timer
NScroller.rate = 0;		// working variable - milliseconds between scroll
NScroller.count = 0;		// working variable - number of times scrolled (for adjustable rate)
NScroller.dir = 1;		// working variable - direction of scroll (-1 left, +1 right)
NScroller.scrollers = [];	// array of active scrollers being scrolled

// Constructor
function NScroller(min,max,curr,elements,template,action) {

	// Scroller parameters
	this.min = min;			// minimum scroll number (1 based)
	this.max = max;			// maximum scroll number
	this.curr = curr;		// the current scroll (page) number
	this.elements = elements;	// document elements to scroll through
	this.template = template;	// string with %N where number is to be substituted
	this.action = action;		// function called when scroll stops with argument this.curr

	// Settings
	this.Rate = NScroller.Rate;
	this.Delay = NScroller.Delay;
	this.ClassNameNormal = NScroller.ClassNameNormal;
	this.ClassNameCurrent = NScroller.ClassNameCurrent;

	// Working variables
	this.first = null;		// number in first element (computed)

	// Initialize display
	this._move(0);	
	this._display();	

	return this;
}

// Class methods (implements repeated scrolls in required global context)
NScroller.next = function() {
	var scroller, done = false;
	for (var i = 0; i < NScroller.scrollers.length; i++) {
		scroller = NScroller.scrollers[i];
		if (scroller.min > 0 && scroller.max > 0) {
			scroller._move(NScroller.dir);
			scroller._display();
		}
		if (scroller.curr == scroller.min || scroller.curr == scroller.max) done = true;
	}
	if (!done) {
		var rate = NScroller.rate ? NScroller.rate : NScroller.adjust_rate(++NScroller.count);
		NScroller.timer = setTimeout('NScroller.next()',rate);
	}
}

NScroller.stop = function(evt) {
	// document onmouseup handler
	clearTimeout(NScroller.timer);
	for (var i = 0; i < NScroller.scrollers.length; i++) {
		NScroller.scrollers[i].stop( (evt ? evt : window.event) );
	}
}
NScroller.adjust_rate = function(count) {
	if	(count > 40)	return 20;
	else if	(count > 15)	return 50;
	else if	(count > 8)	return 100;
	else if	(count > 2)	return 200;
	else			return 300;
}

// Object methods (event handlers)
NScroller.prototype.scroll = function(dir) {
	// Use as button's onmousedown event handler.
	// Scroll left (dir=-1) or right (dir=1)
        document.onmouseup = NScroller.stop;	// safety

	// Save arguments for next scroll in global context.
	NScroller.scrollers = [ this ];		// one element array
	NScroller.dir       = (dir < 0 ? -1 : 1);
	NScroller.rate      = this.Rate;
	NScroller.count     = 0;

	this._move(NScroller.dir);
	this._display();

	// Start scrolling.
	NScroller.timer = setTimeout('NScroller.next()',this.Delay);
}

NScroller.prototype.stop = function(evt) {
	// Use as button's onmouseup event handler.
	clearTimeout(NScroller.timer);

	evt = evt ? evt : window.event ? window.event : null;
	if (evt) {
		evt.cancelBubble = true;
		if(evt.stopPropagation) evt.stopPropagation();
	}

        if (document.selection && document.selection.empty) document.selection.empty();                                 //IE
        if (window.getSelection && window.getSelection().removeAllRanges) window.getSelection().removeAllRanges();      // Moz

	if (this.action) {
		if (navigator.userAgent.match(/MSIE/)) {
			// HACK: IE has a timing problem when action function assigns a
			// new url to location.href (an initial intended purpose of this).
			NScroller.next_action = this.action;
			NScroller.next_curr = this.curr;
			setTimeout('NScroller.next_action(NScroller.next_curr)',100);
		}
		else {
			this.action(this.curr);
		}
	}
}

// Private methods
NScroller.prototype._move = function(dir) {
	// Assign new values to the scroll elements.
	// dir is either 1 or -1

	var nelem = this.elements.length;

	// Compute middle element (0 based) to display current page number.
	var curr_elem = Math.floor((nelem + 1)/2) - 1;

	this.curr += dir;
	if (this.curr < this.min) this.curr = this.min;
	if (this.curr > this.max) this.curr = this.max;

	this.first = this.curr - curr_elem;
	if (this.first < this.min) {
		curr_elem -= this.min - this.first;
		this.first = this.min;
	}
	if ((this.first + nelem) > this.max) {
		curr_elem += (this.first + nelem) - this.max - 1;
		this.first = this.curr - curr_elem;
	}
}

NScroller.prototype._display = function() {
	var n = this.first;
	for (var i = 0; i < this.elements.length; i++) {
		if (n > 0 && n <= this.max)	this.elements[i].innerHTML = this.template.replace(/%N/g,n);
		else				this.elements[i].innerHTML = '&nbsp;';
		this.elements[i].className = this.elements[i].className.replace(new RegExp(this.ClassNameCurrent),"");
		if (n == this.curr) this.elements[i].className += " "+this.ClassNameCurrent;
		n++;
	}
}