// FUNCIONES PARA CONFIGURAR EL MOVIMIENTO DEL CAROUSEL
picsPerPage = 0; // IMAGENES POR PAGINA DEL CAROUSEL
picsWidth = 0; // ANCHO DE LAS IMAGENES
carouHeight = 0;
function imagesPerPage(qty)	{
	picsPerPage = qty;
}
function imageCellWidth(width)	{
	picsWidth = width;
}
function carouselHeight(height)	{
	carouHeight = height;
}

// LOS ALERTS CORRESPONDIENTES PARA CADA VARIABLE
picsPerPage_alert = "You must set the number of images per page.\nUse imagesPerPage( quantity ) to set the number of images per page";
picsWidth_alert = "You must set a width for the image cell.\nUse imageCellWidth( width ) to set the width of the image cell. This width doesnt include borders, margins, etc.";
carouselHeight_alert = "You must set a height for the carousel.\nUse carouselHeight( height ) to set the height of the carousel";
noImages_alert = "The carousel must have at least 1 image.\nUse addImage( imageUrl , [imageLinkUrl] ) to add images to the carousel";
movementSpeed_alert = "Movement speed must be set between 1 and 5 included";

// FUNCIONES PARA CONFIGURAR EL HTML DEL CAROUSEL
idDivCarousel = "jslb_carousel"; // LA ID DEL DIV CONTENEDOR DEL CAROUSEL
classDivCarousel = ""; // LA CLASSNAME DEL DIV CONTENEDOR DEL CAROUSEL
function carouselId(par)	{
	idDivCarousel = par;
}
function carouselClass(par)	{
	classDivCarousel = par;
}

// ARMAMOS UN ARRAY CON LA URL DE LAS IMAGENES
images = new Array();
imageNumber = 0;
function addImage(url, href, alt)	{
	if(url != "")	{
		images[imageNumber] = new Array();
		images[imageNumber][0] = url;
		if(typeof(href) != "undefined")	{
			images[imageNumber][1] = href;
		}
		else	{
			images[imageNumber][1] = "javascript:void(0)";
		}
		if(typeof(alt) != "undefined")	{
			images[imageNumber][2] = alt;
		}
		else	{
			images[imageNumber][2] = "";
		}
		imageNumber++;
	}
}

// CONSTRUIMOS EL CAROUSEL 
carousel_html = "";
pageWidth = 0;
movementSpeed = 30;
function buildCarousel(movementSpeedParam)	{
	// NOS ASEGURAMOS Q ESTEN TODAS LAS CONFIGURACIONES NECESARIAS Y PEDIMOS LAS Q NO ESTEN
	if(picsPerPage == 0)	{
		//alert(picsPerPage_alert);
		return false;
	}
	if(picsWidth == 0)	{
		//alert(picsWidth_alert);
		return false;
	}
	if(carouHeight == 0)	{
		//alert(carouselHeight_alert);
		return false;
	}
	if(images.length == 0)	{
		//alert(noImages_alert);
		return false;
	}
	if(typeof(movementSpeedParam) != "undefined")	{
		if(typeof(movementSpeedParam) == "number")	{
			if(movementSpeedParam < 1 || movementSpeedParam > 5)	{
				//alert(movementSpeed_alert);
				return false;
			}
			else	{
				movementSpeed = movementSpeedParam;
			}
		}
	}
	// EMPEZAMOS A CONSTRUIR
	pageWidth = picsWidth*picsPerPage;
	calculateAllValues();
	
	carousel_html += '<style type="text/css">';
	carousel_html += '#jslb_carousel_frame{position:relative;overflow:hidden !important;width:'+pageWidth+'px !important;height:'+carouHeight+'px !important;}'+"\n";
	carousel_html += '#jslb_carousel_moves{position:absolute;left:0;top:0;width:'+((numberOfPages*pageWidth)+pageWidth)+'px;}'+"\n";
	carousel_html += '.jslb_carousel_item{position:relative;overflow:hidden !important;float:left !important;width:'+picsWidth+'px !important;height:'+carouHeight+'px !important;}'+"\n";
	carousel_html += '.jslb_carousel_item img{border:0;}'+"\n";
	carousel_html += '</style>';
	carousel_html += '<div id="'+idDivCarousel+'" ';
	if(classDivCarousel != "")	{
		carousel_html += 'class="'+classDivCarousel+'"';
	}
	carousel_html += '>';
	carousel_html += 	'<a href="javascript:seePrevious()" id="jslb_carousel_prev"></a>';
	carousel_html += 	'<div id="jslb_carousel_frame">';
	carousel_html += 		'<div id="jslb_carousel_moves">';
	
	for(a=0;a<images.length;a++)	{
		carousel_html += '<a class="jslb_carousel_item" href="'+images[a][1]+'"><img src="'+images[a][0]+'" ';
		if(images[a][2] != "")	{
			carousel_html += 'alt="'+images[a][2]+'"';
		}
		carousel_html += '/></a>';
	}
	
	carousel_html += 		'</div>';
	carousel_html += 	'</div>';
	carousel_html += 	'<a href="javascript:seeNext()" id="jslb_carousel_next"></a>';
	carousel_html += '</div>';
	carousel_html += '<p class="click_carro">Click the arrows to view more images</p>';
	
	document.write(carousel_html);
	movementDiv = document.getElementById("jslb_carousel_moves");
	carousel_html = "";
}

///////////////////////////////////////////
///////////////////////////////////////////
///////////////////////////////////////////
//// EL CAROUSEL ESTA ARAMADO AHORA ///////
//// AHORA LAS FUNCIONES PARA MOVERLO /////
///////////////////////////////////////////
///////////////////////////////////////////
///////////////////////////////////////////

// VARIABLES NECESARIAS PARA EL MOVIMIENTO
numberOfPages = 0;
currentLeft = 0;
minLeft = 0;
growthValue = 0;
numberOfMovements = 10;

// FUNCIONES PARA CALCULAR EL VALOR DE LAS ANTERIORES VARIABLES
function calculateNumberOfPages()	{
	numberOfPages = images.length/picsPerPage;
	numberOfPages = Math.ceil(numberOfPages);
}
function calcultateMinLeft()	{
	minLeft = (numberOfPages-1)*pageWidth;
}
function calculateGrowthValue()	{
	growthValue = pageWidth/numberOfMovements;
}
function calculateNumberOfMovements()	{
	switch(movementSpeed)	{
		case 1: movementSpeed = 50;break;
		case 2: movementSpeed = 40;break;
		case 3: movementSpeed = 30;break;
		case 4: movementSpeed = 20;break;
		case 5: movementSpeed = 10;break;
	}
}
function calculateAllValues()	{
	calculateNumberOfPages();
	calcultateMinLeft();
	calculateGrowthValue();
	calculateNumberOfMovements();
}

// FUNCIONES PARA MOVER
movementNumber = 0;
movementDiv = "";
function seeNext()	{
	if(currentLeft != minLeft)	{
		document.getElementById("jslb_carousel_prev").href = "javascript:void(0)";
		document.getElementById("jslb_carousel_next").href = "javascript:void(0)";
		goNext = setInterval("goNextInterval()", movementSpeed);
	}
	else	{
	}
}
function goNextInterval()	{
	if(movementNumber != numberOfMovements)	{
		movementNumber++;
		currentLeft += growthValue;
		movementDiv.style.left = "-"+currentLeft+"px";
	}
	else	{
		clearInterval(goNext);
		movementNumber = 0;
		document.getElementById("jslb_carousel_prev").href = "javascript:seePrevious()";
		document.getElementById("jslb_carousel_next").href = "javascript:seeNext()";
	}
}

function seePrevious()	{
	if(currentLeft != 0)	{
		document.getElementById("jslb_carousel_prev").href = "javascript:void(0)";
		document.getElementById("jslb_carousel_next").href = "javascript:void(0)";
		goPrev = setInterval("goPrevInterval()", movementSpeed);
	}
	else	{	
	}
}
function goPrevInterval()	{
	if(movementNumber != numberOfMovements)	{
		movementNumber++;
		currentLeft -= growthValue;
		movementDiv.style.left = "-"+currentLeft+"px";
	}
	else	{
		clearInterval(goPrev);
		movementNumber = 0;
		document.getElementById("jslb_carousel_prev").href = "javascript:seePrevious()";
		document.getElementById("jslb_carousel_next").href = "javascript:seeNext()";
	}
}
