// Animation Timer
var timer = null;
// How many pixel the panel will move at a time
var smooth = 25;
// How often the timer will run
var i_timer = 5;

function slide(direction)
{
	// Don't do anything if the container is moving
	if(timer) return false;

	// Slider
	var slider = document.getElementById("slider");
	// Container
	var	container = document.getElementById("s-panels");
	// Arrows
	var arrow_back = document.getElementById("arrow-back");
	var arrow_next = document.getElementById("arrow-next");
	// Books Image
	var books = document.getElementById("s-books");
	// Books Image Total Width
	var books_width = books.offsetWidth;
	// The width of the panel
	var panel = parseInt(get_style(slider,"width").replace("px",""));
	// Current Left Position of the Container
	var container_left = parseInt(get_style(container,"left").replace("px",""));

	// The new left position
	if(direction)
	{
		var new_left = container_left - panel;
		var next_left = new_left - panel;
	}
	else
	{
		var new_left = container_left + panel;
		var next_left = new_left + panel;
	}

	//alert(container_left);
	//alert(new_left);
	//alert(panel);
	//alert(next_left);
	//alert(books_width);

	if((books_width + next_left) > 0)
	{
		//alert("Forward, yes");
		// Arrows: Forward, yes.
		arrow_next.style.display = "block";
	}
	else
	{
		//alert("Forward, no");
		// Arrows: Forward, no.
		arrow_next.style.display = "none";
	}

	if((new_left + panel) <= 0)
	{
		//alert("Back, yes.");
		// Arrows: Back, yes.
		arrow_back.style.display = "block";
	}
	else
	{
		//alert("Back, no.");
		// Arrows: Back, yes.
		arrow_back.style.display = "none";
	}

	glide(container,container_left,new_left);
}
 
function glide(obj,pos,dest)
{
	var container = obj;
	var container_left = pos;
	var new_left = dest;

	if(new_left > container_left)
	{
		//alert("Going back!");
		timer = setInterval("glide_back("+new_left+")",i_timer);
	}
	else
	{
		//alert("Next");
		timer = setInterval("glide_next("+new_left+")",i_timer);
	}
}

function glide_next(int)
{
	var container = document.getElementById("s-panels");
	var new_left = int;

	container.style.left = (parseInt(get_style(container,"left").replace("px","")) - smooth) + "px";

	if(parseInt(get_style(container,"left").replace("px","")) < new_left)
	{
		container.style.left = new_left + "px";
		if (!timer) return false;
		clearInterval(timer);
		timer = null;
	}
}

function glide_back(int)
{
	var container = document.getElementById("s-panels");
	var new_left = int;

	container.style.left = (parseInt(get_style(container,"left").replace("px","")) + smooth) + "px";

	if(parseInt(get_style(container,"left").replace("px","")) > new_left)
	{
		container.style.left = new_left + "px";
		if (!timer) return false;
		clearInterval(timer);
		timer = null;
	}
}

function get_style(el,el_css)
{
	var css_value = el.style[el_css];

	if(!css_value) // If it's not an inline style
	{
		if(el.currentStyle)
		{
			css_value = el.currentStyle[el_css];
		}
		else if(window.getComputedStyle)
		{
			css_value = document.defaultView.getComputedStyle(el,null).getPropertyValue(el_css);
		}
		else
		{
		css_value = null;
		}
	}
	return css_value;
}

// Featured Photograph

function get_photographs()
{
	var photos = new Array
		(
		"the-girls.jpg",
		"victory.jpg",
		"starfish.jpg",
		"boat.jpg",
		"pepsi.jpg",
		"tourists.jpg",
		"steel-pier.jpg",
		"cameras.jpg",
		"alligator.jpg",
		"swimming.jpg",
		"phillies.jpg"
		);
	return photos;
}

function set_photograph()
{
	var photo = document.getElementById("home-intro");
	var photos = get_photographs();
	var path = "/images/home/";

	// Set the photograph
	var r_number = Math.floor(Math.random() * photos.length);
	photo.style.backgroundImage = "url(" + path + photos[r_number] + ")";
}
