addLoadListener(activateSlider);
addLoadListener(activateSelections);

// Slider Contants
var currentPosition = 0;
var currentLeft = 0;
var itemWidth;
var itemCount;
var fullPages;
var itemsPerPage;

function activateSlider() {
	currentPosition = 0;
	
	// How wide in pixels is each item?
	itemWidth = 79;
	
	// How many items per page do you want to display?
	itemsPerPage = 7;
	
	
	var slideContainer = document.getElementById('selection_list');
	var slideItems = slideContainer.getElementsByTagName('li');
	itemCount = slideItems.length;
	
	if (itemCount <= itemsPerPage) {
		document.getElementById('selection_dn').style.display = 'none';
	}
	
	fullPages = -Math.floor(itemCount / itemsPerPage);
	var pageOverflow = -(itemCount % itemsPerPage);
	var slideLeft = document.getElementById('selection_up');
	var slideRight = document.getElementById('selection_dn');
	
	slideLeft.onclick = function() {
		currentPosition++;
		if (currentPosition == 0) {
			document.getElementById('selection_up').style.display = 'none';
		}
		
		if (currentPosition > (fullPages)) {
			document.getElementById('selection_dn').style.display = 'block';
		}
		
		if (currentPosition == (fullPages + 1) && pageOverflow != 0) {
			currentLeft = currentPosition * (itemWidth*itemsPerPage);
		}
		
		var animLeft = new YAHOO.util.Anim('selection_list', { top: {to: currentPosition * (itemWidth*itemsPerPage)}}, 1.5, YAHOO.util.Easing.easeOut);
		animLeft.animate();
		
		return false;
	}
	
	slideRight.onclick = function() {
		currentPosition--;
		
		if (currentPosition < 0) {
			document.getElementById('selection_up').style.display = 'block';
		}
		
		if (currentPosition == (fullPages + 1) && pageOverflow == 0) {
			currentLeft = currentPosition * (itemWidth*itemsPerPage);
			document.getElementById('selection_dn').style.display = 'none';
		} else if (currentPosition == (fullPages + 1) && pageOverflow != 0) {
			currentLeft = currentPosition * (itemWidth*itemsPerPage);
			document.getElementById('selection_dn').style.display = 'block';
		} else if (currentPosition == (fullPages) && pageOverflow != 0) {
			currentLeft = currentLeft + (itemWidth*pageOverflow);
			document.getElementById('selection_dn').style.display = 'none';
		} else {
			currentLeft = currentPosition * (itemWidth*itemsPerPage);
		}
		
		var animRight = new YAHOO.util.Anim('selection_list', { top: {to: currentLeft}}, 1.5, YAHOO.util.Easing.easeOut);
		animRight.animate();
		
		return false;
	}
}

function activateSelections() {
	var selectInfo = document.getElementById('selection_information');
	var selectionList = document.getElementById('selection_list');
	var selectionItems = selectionList.getElementsByTagName('a');
	for (i=0; i < selectionItems.length; i++) {
		selectionItems[i].onclick = function() {
			thisRel = this.getAttribute('rel');
			selectInfo.innerHTML = '<div id="selection_image_container"><img id="selection_image" src="images/stones/' + this.id + '_med.jpg" alt="' + thisRel + ' Medium" title="' + thisRel + '" /></div>';
			selectInfo.innerHTML += '<a href="#" id="overlay_control" class="selection_enlarge image_text">View Slab</a>';
			selectInfo.innerHTML += '<h2>' + thisRel + '</h2>';
			activateOverlayControl(this.id, thisRel);
			$('#selection_information').jScrollPane({showArrows:true});
			return false;
		};
	}
}