/*

SCROLL LISTINGS - handles clicking arrows on each side of horizontal list of properties on listing pages

divToScroll - element - The div to slide back and forth
initially visible - integer - The number of objects of width scrollAmt that will be visible at any time inside the sliding div
scrollAmt - integer pixels - The pixel width to scroll each time a scroll button is clicked
direction - the direction to slide

*/

//log("file loaded",1,"listings.js");
var pos = new Array();
var currentScroll = new Array();

function scrollListings (divToScroll, initiallyVisible, scrollAmt,direction) {
	
	//use the id of the passed element that will is the scrolling div to create a named array item to store its scroll state
	var slider = divToScroll;
	var index = slider.id;
	//initialize the scroll position and the number of scroll clicks
	if(!currentScroll[index]) {
		currentScroll[index]=0;
		pos[index]=0;
	}
		
	//log(slider,1,"scrollListings", "slider");
	//figure out how many image elements are in the list
	//we have to count only the element nodes, because childNodes gives us 
	//all child elements PLUS their damn text nodes
	sliderChildNodes = slider.childNodes;
	var numEntries = 0;
	for(i=0; i < sliderChildNodes.length; i++) {
		if(sliderChildNodes[i].nodeType == 1) {
		numEntries++;
		}
	}
	if(numEntries <= initiallyVisible) {
		//this means no scrolling is needed. the visible number is equal to or less than the total entries
		return;
	}
	//determine how far we can scroll

	//number of scroll "clicks" we can have
	var scrollmax = numEntries-initiallyVisible;
	if(currentScroll[index] == 0 && direction=='left') {
		return;
	}
	if(currentScroll[index] >= scrollmax && direction=='right') {
		return;
	}
	//get number of property listings in this horizontal list
	//so we know when to stop scrolling
	//alert(slider.style.left);
	
	if(direction=='left') {
		pos[index] += scrollAmt;
		currentScroll[index] -= 1;
	} else {
		pos[index] -= scrollAmt;
		currentScroll[index] += 1;
	}
	//convert numeric value into string value for CSS
	slider.style.left = pos[index] + 'px';
}




