//================================================
// Classe Slider
//================================================
// LEGENDA
// c1 - Contenitore 1 Esterno
// c2 - Contenitore 2 Interno
// doInitFade - true/false - Fade iniziale
// itemSize - larghezza/altezza item
// direction - h/v direzione di spostamento

function Slider(c1, c2, doInitFade, direction, selezionato)
{
	
	this.selezionato = selezionato
	this.contenitore_1_id = c1;
	this.contenitore_2_id = c2;
	this.doInitFade = doInitFade;
	this.direction = direction;
	this.indiceAttuale = 0;
	this.positionPx = '0px';
	this.newPosition = 0;
	this.newPositionPx = '0px';
	this.itemSize = 0;
	this.list_item = $(this.contenitore_2_id).getElementsByTagName('li');
	this.numItems = this.list_item.length;
	var semaforo = 1; // 1 OK  - 0 BLOCCATO
	
	//alert("->: " + this.list_item[0].id);
	//alert("->: " + $(this.list_item[0].id));
	
	this.coordinates_item = $(this.list_item[0].id).getCoordinates();
	
	//alert("FATTO COORDINATE");
	
	if(this.direction.toLowerCase()=="h")
	{
		this.itemSize = this.coordinates_item['width'];
		//alert("ITEM SIZE -> " +  this.itemSize);
		//alert("NUM ITEMS -> " +  this.numItems);
		//alert("LARGHEZZA -> " +  (this.itemSize * this.numItems));
		// SETTO GRANDEZZA CONTENITORE INTERNO
		$(this.contenitore_2_id).setStyle('width', (this.itemSize * this.numItems));
	}
	else
	{
		this.itemSize = this.coordinates_item['height'];
		//alert("ITEM SIZE -> " +  this.itemSize);
		//alert("NUM ITEMS -> " +  this.numItems);
		//alert("LARGHEZZA -> " +  (this.itemSize * this.numItems));
		// SETTO GRANDEZZA CONTENITORE INTERNO
		$(this.contenitore_2_id).setStyle('height', (this.itemSize * this.numItems));
	}
	
	
	// METODO ON RESIZE
	this.onResize = function(){}

	// METODO FADE
	this.doFade=function()
	{
		$(this.contenitore_2_id).set('tween', 
		{
			duration: 1500,
			transition: Fx.Transitions.Sine.easeOut
		});
		$(this.contenitore_2_id).tween('opacity', '0', '1');
	}
	
	// SPOSTAMENTO A DESTRA
	this.goRight = function(step)
	{
		//alert("SEMAFORO: " + semaforo);
		if(semaforo == 1)
		{
			var coordinates_c1 = $(this.contenitore_1_id).getCoordinates();
			var right_c1 = coordinates_c1['right'];
			
			var coordinates_c2 = $(this.contenitore_2_id).getCoordinates();
			var right_c2 = coordinates_c2['right'];
			
			if(right_c1 <= right_c2)
			{
				//metto il semaforo a 0
				semaforo = 0;
				
				//this.indiceAttuale++;
				this.indiceAttuale = this.indiceAttuale + step;
				
				this.positionPx = $(this.contenitore_2_id).getStyle('left');
				this.newPosition = -this.itemSize * this.indiceAttuale;
				this.newPositionPx = this.newPosition + 'px';
				//$(this.contenitore_2_id).tween('left', this.positionPx, this.newPositionPx);
				
				var myFx = new Fx.Tween($(this.contenitore_2_id));
				myFx.start('left', this.positionPx, this.newPositionPx);
				myFx.addEvent('complete', function() {
					//metto il semaforo a 1							   	
					semaforo = 1;
				});

				
				this.onResize();
			}
		}
	}
	
	// SPOSTAMENTO IN BASSO
	this.goDown = function(step)
	{
		if(semaforo == 1)
		{
			var coordinates_c1 = $(this.contenitore_1_id).getCoordinates();
			var top_c1 = coordinates_c1['top'];
			
			var coordinates_c2 = $(this.contenitore_2_id).getCoordinates();
			var top_c2 = coordinates_c2['top'];
			
			if(top_c1 <= top_c2)
			{
				//metto il semaforo a 0
				semaforo = 0;
			
				//this.indiceAttuale++;
				this.indiceAttuale = this.indiceAttuale + step;
				
				this.positionPx = $(this.contenitore_2_id).getStyle('top');
				this.newPosition = -this.itemSize * this.indiceAttuale;
				this.newPositionPx = this.newPosition + 'px';
				//$(this.contenitore_2_id).tween('top', this.positionPx, this.newPositionPx);
				
				var myFx = new Fx.Tween($(this.contenitore_2_id));
				myFx.start('left', this.positionPx, this.newPositionPx);
				myFx.addEvent('complete', function() {
					//metto il semaforo a 1							   	
					semaforo = 1;
				});
				
				this.onResize();
			}
		}
	}
	
	
	
	// SPOSTAMENTO A SINISTRA
	this.goLeft = function(step)
	{
		if(semaforo == 1)
		{
			//this.indiceAttuale--;
			this.indiceAttuale = this.indiceAttuale - step;
			
			this.positionPx = $(this.contenitore_2_id).getStyle('left');
			this.newPosition = -this.itemSize * this.indiceAttuale;
			this.newPositionPx = this.newPosition + 'px';
			
			if (this.newPosition <= 0) 
			{
				//metto il semaforo a 0
				semaforo = 0;
			
				//$(this.contenitore_2_id).tween('left', this.positionPx, this.newPositionPx);
				
				var myFx = new Fx.Tween($(this.contenitore_2_id));
				myFx.start('left', this.positionPx, this.newPositionPx);
				myFx.addEvent('complete', function() {
					//metto il semaforo a 1															   	
					semaforo = 1;
				});
				
				this.onResize();
			}
			else 
			{
				this.indiceAttuale = 0;
			}
		}
	}
	
	// VAI SU
	this.goUp = function(step)
	{
		if(semaforo == 1)
		{
			//this.indiceAttuale--;
			this.indiceAttuale = this.indiceAttuale - step;
			
			this.positionPx = $(this.contenitore_2_id).getStyle('top');
			this.newPosition = -this.itemSize * this.indiceAttuale;
			this.newPositionPx = this.newPosition + 'px';
			
			if (this.newPosition <= 0) 
			{
				//metto il semaforo a 0
				semaforo = 0;
			
				//$(this.contenitore_2_id).tween('top', this.positionPx, this.newPositionPx);
				
				var myFx = new Fx.Tween($(this.contenitore_2_id));
				myFx.start('left', this.positionPx, this.newPositionPx);
				myFx.addEvent('complete', function() {
					//metto il semaforo a 1												   	
					semaforo = 1;
				});
				
				
				this.onResize();
			}
			else 
			{
				this.indiceAttuale = 0;
			}
		}
	}
	
	// PROSSIMO
	this.goNext = function()
	{
		if(this.direction.toLowerCase()=="h")
		{
			this.goRight(1);
		}
		if(this.direction.toLowerCase()=="v")
		{
			this.goDown(1);
		}
	}
	
	// PRECEDENTE
	this.goPrevious = function()
	{
		if(this.direction.toLowerCase()=="h")
		{
			this.goLeft(1);
		}
		if(this.direction.toLowerCase()=="v")
		{
			this.goUp(1);
		}
	}
	
	// METODO DA CHIAMARE PER FA INIZIARE TUTTO
	this.doInit = function()
	{
		if(this.doInitFade)
		{
			this.doFade();
		}
		
		// CHIAMO ONRESIZE
		this.onResize();
		
		// POSIZIONAMENTO INIZIALE
		if(this.direction.toLowerCase()=="h")
		{
			this.goRight((this.selezionato-1));
		}
		if(this.direction.toLowerCase()=="v")
		{
			this.goDown((this.selezionato-1));
		}
	}
}


//================================================
// UTILITY
//================================================
function getInsideWindowWidth()
{
	if (window.innerWidth) 
	{
		return window.innerWidth;
	}
	else 
		if (document.images && document.compatMode && document.compatMode.indexOf("CSS1") >= 0) 
		{
			// measure the html element's clientWidth
			return document.body.parentElement.clientWidth;
		}
		else 
			if (document.body && document.body.clientWidth) 
			{
				return document.body.clientWidth;
			}
	return 0;
}

// Return the available content height space in browser window
function getInsideWindowHeight()
{
	if (window.innerHeight) 
	{
		return window.innerHeight;
	}
	else 
		if (document.images && document.compatMode && document.compatMode.indexOf("CSS1") >= 0) 
		{
			// measure the html element's clientHeight
			return document.body.parentElement.clientHeight;
		}
		else 
			if (document.body && document.body.clientHeight) 
			{
				return document.body.clientHeight;
			}
	return 0;
}

//===========================================
// CALCOLO DELLA POSIZIONE
//===========================================
function leftPosition(target) 
{
  var left = 0;
  if(target.offsetParent) 
  {
    while(1) 
	{
      left += target.offsetLeft;
      if(!target.offsetParent) 
	  {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

function topPosition(target) 
{
  var top = 0;
  if(target.offsetParent) 
  {
    while(1) 
	{
      top += target.offsetTop;
      if(!target.offsetParent) 
	  {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

