
// handles section to section scrolling of the content //
function slideContent(boton,id,prefix,timer) {
  var div = document.getElementById(id);
  var slider = div.parentNode;
  clearInterval(slider.timer);
  slider.section = parseInt(id.replace(/\D/g,''));
  slider.target = div.offsetTop;
  slider.style.top = slider.style.top || '0px';
  slider.current = slider.style.top.replace('px','');
  slider.direction = (Math.abs(slider.current) > slider.target) ? 1 : -1;
  slider.style.opacity = STARTINGOPACITY * .01;
  slider.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  slider.timer = setInterval( function() { slideAnimate(boton,slider,prefix,timer) }, SLIDETIMER);
}

function slideAnimate(boton,slider,prefix,timer) {
  var curr = Math.abs(slider.current);
  var tar = Math.abs(slider.target);
  var dir = slider.direction;
  if((tar - curr <= SLIDESPEED && dir == -1) || (curr - tar <= SLIDESPEED && dir == 1)) {
    slider.style.top = (slider.target * -1) + 'px';
	slider.style.opacity = 1;
	slider.style.filter = 'alpha(opacity=100)';
    clearInterval(slider.timer);
	if(slider.autoscroll) {
	  setTimeout( function() { autoScroll(boton,slider.id,prefix,timer) }, timer * 1000);
	}
  } else {
	var pos = (dir == 1) ? parseInt(slider.current) + SLIDESPEED : slider.current - SLIDESPEED;
    slider.current = pos;
    slider.style.top = pos + 'px';
  }
}

// handles manual scrolling of the content //
function scrollContent(id,dir) {
  var div = document.getElementById(id);
  clearInterval(div.timer);
  var sections = div.getElementsByTagName('div');
  var length = sections.length;
  var limit;
  if(dir == -1) {
    limit = 0;
  } else {
    if(length > 1) {
      limit = sections[length-1].offsetTop;
    } else {
      limit = sections[length-1].offsetHeight - div.parentNode.offsetHeight + 20;
    }
  }
  div.style.opacity = STARTINGOPACITY * .01;
  div.style.filter = 'alpha(opacity=' + STARTINGOPACITY + ')';
  div.timer = setInterval( function() { scrollAnimate(div,dir,limit) }, SCROLLTIMER);
}

function scrollAnimate(div,dir,limit) {
  div.style.top = div.style.top || '0px';
  var top = div.style.top.replace('px','');
  if(dir == 1) {
	if(limit - Math.abs(top) <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = '-' + limit + 'px';
	} else {
	  div.style.top = top - SCROLLSPEED + 'px';
	}
  } else {
	if(Math.abs(top) - limit <= SCROLLSPEED) {
	  cancelScroll(div.id);
	  div.style.top = limit + 'px';
	} else {
	  div.style.top = parseInt(top) + SCROLLSPEED + 'px';
	}
  }
}

// cancel the scrolling on mouseout //
function cancelScroll(id) {
  var div = document.getElementById(id);
  div.style.opacity = 1;
  div.style.filter = 'alpha(opacity=100)';
  clearTimeout(div.timer);
}

// initiate auto scrolling //
function autoScroll(boton,id,prefix,timer,restart) {
  var div = document.getElementById(id);
  div.autoscroll = (!div.autoscroll && !restart) ? false : true;
  if(div.autoscroll) {
    var sections = div.getElementsByTagName('div');
    var length = sections.length;
	if (boton > 0)
		{
		div.section = boton;
		}
	else
		{
		div.section = (div.section && div.section < length) ? div.section + 1 : 1;
		}

  window.addEvent('domready', function()
	{
	var fx1 = new Array();
	fx1[1] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 1, {duration:1000, wait:false});
	fx1[2] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 2, {duration:1000, wait:false});
	fx1[3] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 3, {duration:1000, wait:false});
	fx1[4] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 4, {duration:1000, wait:false});
	fx1[5] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 5, {duration:1000, wait:false});
	fx1[6] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 6, {duration:1000, wait:false});
	fx1[7] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 7, {duration:1000, wait:false});
	fx1[8] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 8, {duration:1000, wait:false});
	fx1[9] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 9, {duration:1000, wait:false});
	fx1[10] = new Fx.Styles('slide-show-portada-peatom-wrapper-info-botonera-boton-section-' + 10, {duration:1000, wait:false});

	for (x = 1; x <= length; x++)
 		{
 		if (x == eval(div.section))
			{
			fx1[x].start({
				'background-color': '#0b0',
				color: '#fff'
				});
			}
		else
			{
			fx1[x].start({
				'background-color': '#fff',
				color: '#bbb'
				});
			}
		}
	});

    slideContent(0,prefix + '-' + div.section,prefix,timer);
  }
}

// cancel automatic scrolling //
function cancelAutoScroll(id) {
  var div = document.getElementById(id);
  div.autoscroll = false;
}