// JavaScript Document
// © 2008 Pietro Saccardi
// HORIZONTAL BOX SLIDE JS
// requires linear_animation.js
	var placeHolders=null;
	var container=null;
	var table=null;
	var currentBox=0;
	var boxes=0;
	
	function setupSlide() {
		//Obtain the container and determine size
		container=document.getElementById("boxslide_container");
		if(container==null) return 0;
		container.style.overflow="hidden";
		container.style.position="relative";  //IE FIX
		slideBoxH-=navigatorH;
		//Obtain the number of boxes
		table=document.getElementById("boxslide_slider");
		var row=getFirstGoodChild(getFirstGoodChild(table));
		boxes=row.cells.length;
		//Add other cells
		row.insertCell(boxes);
		row.insertCell(0);
		//Apply size to all the cells
		for(var i=0; i<row.cells.length; i++) {
			row.cells[i].width=slideBoxW;
			row.cells[i].height=slideBoxH;
		}
		//Apply also to the table all the needed properties
		table.width=row.cells.length*slideBoxW;
		table.height=slideBoxH;
		table.style.position="relative";
		//Now copy last to first and viceversa
		row.cells[0].innerHTML=row.cells[boxes].innerHTML;
		row.cells[boxes+1].innerHTML=row.cells[1].innerHTML;
		//Copy attributes:
		var attributes=new Array("align", "valign", "style", "class", "bordercolor", "bordercolordark", "bordercolorlight", "background", "bgcolor");
		for(var i=0; i<attributes.length; i++) {
			//Do it
			if(row.cells[boxes].getAttribute(attributes[i])!=null&&row.cells[boxes].getAttribute(attributes[i])!="") {
				row.cells[0].setAttribute(attributes[i], row.cells[boxes].getAttribute(attributes[i]));
			}
			if(row.cells[1].getAttribute(attributes[i])!=null) {
				row.cells[boxes+1].setAttribute(attributes[i], row.cells[1].getAttribute(attributes[i]));
			}
		}
		//That's for IE.. xD
		if(row.cells[boxes].style.backgroundImage!=""&&row.cells[boxes].style.backgroundImage!=null) {
				row.cells[0].style.backgroundImage=row.cells[boxes].style.backgroundImage;
		}
		if(row.cells[1].style.backgroundImage!=""&&row.cells[1].style.backgroundImage!=null) {
				row.cells[boxes+1].style.backgroundImage=row.cells[1].style.backgroundImage;
		}
		//Move to the first position
		table.style.left="-"+slideBoxW+"px";
		//Create the placeholders
		var placeHolder=document.createElement("A");
		placeHolder.appendChild(document.createElement("IMG"));
		placeHolder.firstChild.src=images[0];
		placeHolder.firstChild.border=0;
		//Now create the array
		placeHolders=new Array();
		for(var i=0; i<boxes; i++)	{ //Add also the event
			placeHolders[placeHolders.push(placeHolder.cloneNode(true))-1].href="javascript:switchTo("+i+");";
		}
		placeHolders[0].firstChild.src=images[3];
		//Prepare also next and previous and save to a variable
		placeHolder.href="javascript:previousBox();";
		placeHolder.firstChild.src=images[2];
		var previousButton=placeHolder.cloneNode(true);
		placeHolder.href="javascript:nextBox();";
		placeHolder.firstChild.src=images[1];
		var nextButton=placeHolder.cloneNode(true);
		//Create navigator
		var boxNavigator=document.createElement("table");
		boxNavigator.width=slideBoxW;
		boxNavigator.height=navigatorH;
		boxNavigator.cellspacing=0;
		boxNavigator.cellpadding=0;
		boxNavigator.border=0;
		boxNavigator.align="center";
		row=boxNavigator.insertRow(0);
		//Insert prev
		row.insertCell(0).appendChild(previousButton);
		for(var i=0; i<boxes; i++) {
			row.insertCell(1+i).appendChild(placeHolders[i]);
		}
		//Insert next
		row.insertCell(boxes+1).appendChild(nextButton);
		//Apply size
		var size=Math.round(slideBoxW/(boxes+2))+"%";
		for(var i=0; i<row.cells.length; i++) {
			row.cells[i].width=size;
			row.cells[i].width=navigatorH;
			row.cells[i].align="center";
			row.cells[i].valign="middle";
		}
		//READY!
		container.appendChild(boxNavigator);
		setInterval("nextBox()", scrollTime*1000);
		
		currentBox=1;
		container.style.display="block";
	}
	function nextBox() {
		if (linear_animate(-slideBoxW, animationTime, table, function() { adjustBoxes(); })) { currentBox++; applyColor(); }
	}
	function previousBox() {
		if (linear_animate(slideBoxW, animationTime, table, function() { adjustBoxes(); })) { currentBox--; applyColor(); }
	}
	function switchTo(num) {
		var absBox=currentBox-1;
		if(num!=absBox) {
			//Evaluate the movement
			var offset=-(num-absBox)*slideBoxW;
			if(linear_animate(offset, animationTime, table, function() { adjustBoxes(); })) { currentBox=num+1; applyColor(); }
		}
	}
	function applyColor() {
		//Obtain image index
		var imgIndex=currentBox-1;
		//Check
		if(imgIndex<0) imgIndex=boxes-1;
		if(imgIndex>=boxes) imgIndex=0;
		//Apply image..!
		for(var i=0; i<boxes; i++) {
			if(i==imgIndex) {
				placeHolders[i].firstChild.src=images[3];
			} else {
				placeHolders[i].firstChild.src=images[0];
			}
		}
	}
	function adjustBoxes() {
		if(currentBox<=0) {
			//Bring to the last
			currentBox=boxes;
			table.style.left="-"+(slideBoxW*boxes)+"px";
		} else if(currentBox>boxes) {
			//Bring to the first
			currentBox=1;
			table.style.left="-"+slideBoxW+"px";
		}
	}
	function getFirstGoodChild(obj) {
		if(obj.hasChildNodes()) {
			var i=0;
			while(obj.childNodes[i].nodeType!=1&&obj.childNodes[i]!=null) i++;
			return obj.childNodes[i];
		}
		return null;
	}
	function getLastGoodChild(obj) {
		if(obj.hasChildNodes()) {
			var el=obj.lastChild;
			while(el.nodeType!=1&&el!=null) el=el.previousSibling;
			return el;
		}
		return null;
	}
	function getNextGoodSibling(obj) {
		var el=obj.nextSibling;
		while(el.nodeType!=1&&el!=null) el=el.obj.nextSibling;
		return el;
	}
