// Copyright (c) 2006 Robarov (http://www.robarov.be)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var pointer = 0;
var visibles = 4; // 3 elements visible
var step = 148;	// slide 100px at a time (will be changed by init_scrollers)
var free = true;

function init_scrollers() 
{
	var scrollers = document.getElementsByClassName('scroller');
	if(scrollers) {
		for(var i = 0; i < scrollers.length; i++) {
		
			// Calculate nr of li's, width of 1 li and the number of pixels to shift when clicked
			var lis = scrollers[i].getElementsByTagName('li');
			var li_count = lis.length;	// number of li's
			var li_width = Element.getStyle(lis[0], 'width'); // width of a li
			li_width = li_width.replace(/px/, "");
			var step = li_width * visibles; // pixels to shift when clicked
		
			// Prev button
			var elem = document.createElement('a');
			elem.className = 'prev';
			elem.href = '#';
			elem.title = 'atras';
			elem.innerHTML = '&lt; atras';			
			if(scrollers[i].hasChildNodes()) {
				scrollers[i].insertBefore(elem,scrollers[i].firstChild);
			} else {
				scrollers[i].appendChild(elem);
			}
			elem.onclick = function() {
				goto_prev(step, visibles, li_count, this);
				return false;
			}
			
			// Next button
			var elem = document.createElement('a');
			elem.className = 'next';
			elem.href = '#';
			elem.title = 'adelante';
			elem.innerHTML = 'adelante &gt;';
			scrollers[i].appendChild(elem);
			elem.onclick = function() {
				goto_next(step, visibles, li_count, this);
				return false;
			}
			
			var elem = document.createElement('div');
			elem.className = 'scroller-shadow-r';
			scrollers[i].appendChild(elem);
			var elem = document.createElement('div');
			elem.className = 'scroller-shadow-l';
			scrollers[i].appendChild(elem);
		}
	}
}

function goto_next(step, visibles, li_count, elem) 
{
	if(free == true) {
		// Voodoo magic
		if(pointer + visibles < li_count) {
			var parentElem = elem.parentNode;
			var scroller = parentElem.getElementsByTagName('ul')[0];
			
			if((pointer + visibles) > (li_count-visibles)) {
				var old_pointer = pointer; // 6 = 6
				pointer = li_count-visibles; // 7 = 10 - 3
				var foo =  step / visibles; // 240 = 960 / 3			
				step = (pointer - old_pointer) * foo; // 240 = (7 - 6) * 240
			} else {
				pointer += visibles;
			}
			free = false;
			new Effect.Move(scroller, {y:0, x:-step, duration: 1.2, afterFinish: function() { free = true; }});
		}
	}
}
function goto_prev(step, visibles, li_count, elem) 
{
	if(free == true) {
		// Voodoo magic in reverse
		if(pointer > 0) {
			var parentElem = elem.parentNode;
			var scroller = parentElem.getElementsByTagName('ul')[0];
			
			if((pointer - visibles) < 0) {
			
				var foo =  step / visibles;
				step = (pointer * foo);
				
				pointer = 0;
			} else {
				pointer -= visibles;
			}
			free = false;
			new Effect.Move(scroller, {y:0, x:step, duration: 1.2, afterFinish: function() { free = true; }});
		}
	}
}

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