/**
  * @file SlideEffect.js
  * @desc Management of the whole slide effect
  * @author Jeannette Gaetan / Netwyse
  * @copyright Netwyse
  * @version 1.0
  */

/* Global */
var carListLength = 0;
var ceilHeight = 404;
var ceilWidth = 483;
var downTo = 0;
var index = 0;
var move = false;
var timer = 0;
var topValue = 0;

/* Global for carList Slide */
var carIndex = 0;
var currentLeft = 0;
var minValue = 0;
var nbDiv = 0;

/* Cars per Frame for announce_box */
var CPF = 4;
var CPF2 = 3;

/* Function for announce_box */
function slideDownButton()
{
	var maxList = Math.floor(carListLength / CPF);

	if (maxList > index && !move)
	{	
		topValue = -index * ceilHeight;
		downTo = topValue - ceilHeight;
		timer = setInterval( "slideDownEffect(timer)", 10);
		index++;
	}
};

/* Functions for announce_box */
function slideUpButton()
{
	var maxList = Math.floor(carListLength / CPF);

	if (0 < index && !move)
	{
		topValue = -index * ceilHeight;
		downTo = topValue + ceilHeight;
		timer = setInterval( "slideUpEffect(timer)", 10);
		index--;
	}
};

/* Functions for announce_box */
function slideDownEffect(id)
{
	if (!move)
		move = true;
	Ext.get('slide_div').setStyle('top', topValue+'px');
	if (topValue === downTo)
	{
		if (id)
		{
			move = false;
			clearInterval(id);
		}
		return ; 
	}

	topValue -= 4;
};

/* Functions for announce_box */
function slideUpEffect(id)
{
	if (!move)
		move = true;
	Ext.get('slide_div').setStyle('top', topValue+'px');
	if (topValue === downTo)
	{
		if (0 != id)
		{
			move = false;
			clearInterval(id);
		}
		return ; 
	}

	topValue += 4;
};

/* Functions */
function slideRightButton()
{
	var divWidth = Ext.get('slide_ceil').dom.clientWidth;
	var maxList = Math.floor(carListLength / CPF2);

	if (divWidth != (carListLength * 161))
		Ext.get('slide_ceil').setStyle('width', (carListLength * 161)+'px');
	if (maxList > index && !move)
	{	
		topValue = -index * ceilWidth;
		downTo = topValue - ceilWidth;
		timer = setInterval( "slideRightEffect(timer)", 90);
		index++;
	}
};

/*
 * Left, Right Effect for the cars announces list
 */
function slideRightEffect(id)
{
	if (!move)
		move = true;
	Ext.get('slide_ceil').setStyle('left', topValue+'px');
	if (topValue === downTo)
	{
		if (id)
		{
			move = false;
			clearInterval(id);
		}
		return ; 
	}

	topValue -= 23;
};

function slideLeftButton()
{
	var maxList = Math.floor(carListLength / CPF2);

	if (0 < index && !move)
	{
		topValue = -index * ceilWidth;
		downTo = topValue + ceilWidth;
		timer = setInterval( "slideLeftEffect(timer)", 90);
		index--;
	}
};

function slideLeftEffect(id)
{
	if (!move)
		move = true;
	Ext.get('slide_ceil').setStyle('left', topValue+'px');
	if (topValue === downTo)
	{
		if (0 != id)
		{
			move = false;
			clearInterval(id);
		}
		return ; 
	}
	topValue += 23;
};

/* Slide of the cars models list */
function changeModel(id)
{
	var aux = "";
	var shift = 0;
	var maxIndex = 0;
	var maxWidth = 0;

	aux = Ext.get('car_selection').getStyle('left');
	aux = aux.split('px');
	currentLeft = Math.floor(aux[0]);
	if (0 >= currentLeft && !move)
	{
		aux = Ext.get('car_selection').getStyle('width');
		aux = aux.split('px');
		maxWidth = aux[0];
		maxIndex = Math.floor((maxWidth / widthCeil) / nbDiv);

		/* right slide */
		if (0 == id)
		{
			/* If it 's the last slide */
			if ((carIndex + 1) == maxIndex)
				shift = maxWidth - (maxIndex * (widthCeil * nbDiv));
			else
				shift = widthCeil * nbDiv;
			minValue = currentLeft - shift;
			if (carIndex < maxIndex)
			{
				/* first slide to the right */
				if (0 == carIndex)
					$j("#arrow_prev").removeClass();

				/* Slide action */
				timer = setInterval("slideCarEffect(0, timer);", 15);
				
			}
		}
		/* left slide */
		else
		{
			if ((carIndex) == maxIndex)
				shift = maxWidth - (maxIndex * (widthCeil * nbDiv));
			else
				shift = widthCeil * nbDiv;
			minValue = currentLeft + shift;
			if (carIndex > 0)
			{
				if (maxIndex == carIndex)
					$j("#arrow_next").removeClass();
				timer = setInterval("slideCarEffect(1, timer);", 15);
			}
		}
	}
};

function slideCarEffect(id, timerId)
{
	if (!move)
		move = true;
	if (currentLeft === minValue)
	{
		move = false;
		clearInterval(timerId);
		if (id == 0)
		{
			carIndex++;
			
			aux = Ext.get('car_selection').getStyle('width');
			aux = aux.split('px');
			maxWidth = aux[0];
			maxIndex = Math.floor((maxWidth / widthCeil) / nbDiv);
			if (maxIndex == carIndex)
				$j("#arrow_next").addClass("idle");
		}
		else
		{
			carIndex--;
			if (0 == carIndex)
				$j("#arrow_prev").addClass("idle");
		}

		return ; 
	}
	
	if (id == 0)
		currentLeft -= nbDiv;
	else
		currentLeft += nbDiv;

	Ext.get('car_selection').setStyle('left', currentLeft+'px');
};

