/**
* JWCarousel 1.0
* (c) 2009 Julian White
* Written: 08/26/2009
* Last Modified: 08/26/2009
*/


function JWCarousel(instanceName) {
	//Settings.
	this.flicker = 200; //refresh rate, ms per refresh
	this.fps = 1; //frames per second
	this.radius = 400; //diameter of carousel rotation
	this.pause = 4000; //duration of pause on frame while running (ms)
	this.foreshortening = 0.1; //foreshortening ratio of radius
	this.lens = 0.4; //scale at foreshortened depth
	this.centerX = 500; //offset of carousel from stage's left edge.
	this.centerY = 50; //offset of carousel from top of stage.
	this.baseLayer = 300; //z-index of stage

	//Private.
	this.instanceName = instanceName; //variable name of the carousel instance.
	this.imgs = null; //slides
	this.running = false; //whether or not the carousel is running
	this.ttl = 0; //counter for carousel advance motion
	this.direction = -1; //positive = forward; negative = backward
	this.pauseTimer = null; //timer for pausing on a slide momentarily.
	this.stepTimer = null; //timer for refreshing the animated movement of the slides.

	//Functions.
	this.stop = function() {
		this.running = false;
	} //function

	this.play = function() {
		this.running = true;
		this.run();
	} //function

	this.run = function() {
		if(this.running) {
			this.forward();
			this.pauseTimer = setTimeout(this.instanceName+".run()",this.pause);
		} //if
	} //function

	this.forward = function() {
		var duration = 1000/this.fps;
		this.ttl = duration/this.flicker;
		this.direction = -1;
		for(var j=0; j<this.imgs.length; j++) {
			var img = this.imgs[j];
			img.position += this.direction;
			if ( img.position < 0 ) img.position += this.imgs.length;
			if ( img.position >= this.imgs.length ) img.position -= this.imgs.length;
			if ( img.position == 0 ) {
				img.style.backgroundColor = "#FFFFFF"
				img.style.borderStyle = "solid"
				img.style.borderColor = "#999999"
				img.style.padding = "15"
				var captionPane = document.getElementById("jwCarouselCaption");
				if ( captionPane ) captionPane.innerHTML = img.getAttribute("alt");
			} //if
			else if ( img.position > 0 ) {
				img.style.borderStyle = "none"
				img.style.padding = "0"
			} //if
		} //for
		this.step();
	} //function

	this.back = function() {
		var duration = 1000/this.fps;
		this.ttl = duration/this.flicker;
		this.direction = 1;
		this.running = false;
		for(var j=0; j<this.imgs.length; j++) {
			var img = this.imgs[j];
			img.position += this.direction;
			if ( img.position < 0 ) img.position += this.imgs.length;
			if ( img.position >= this.imgs.length ) img.position -= this.imgs.length;
			 //if
		} //for
		this.step();
	} //function

	this.step = function() {
		for(var j=0; j<this.imgs.length; j++) {
			var img = this.imgs[j];
			img.theta += this.direction*this.getStepSize();
			if ( img.theta > 360 ) img.theta -= 360;
			if ( img.theta < 0 ) img.theta += 360;
		} //for
		this.updateImagePositions();
		if ( --this.ttl > 0 ) this.stepTimer = setTimeout(this.instanceName+".step()",this.flicker);
	} //function

	this.loadImages = function() {
		this.imgs = getElementsByClassName('jwCarouselImg');
		for(var j=0; j<this.imgs.length; j++) {
			this.imgs[j].theta = j*360/this.imgs.length-90;
			this.imgs[j].position = j;
			this.imgs[j].originalWidth = this.imgs[j].width;
} //for
		this.updateImagePositions();
	} //function

	this.getStepSize = function() {
		var duration = 1000/this.fps;
		var steps = duration/this.flicker*this.imgs.length;
		return 360/steps;
	} //function

	this.updateImagePositions = function() {
		var stage = document.getElementById("jwCarouselStage");
		var centerX = this.centerX+this.getX(stage);
		var centerY = this.centerY+this.getY(stage);
		for(var j=0; j<this.imgs.length; j++) {
			var img = this.imgs[j];
			img.style.width = Math.round(img.originalWidth - img.originalWidth*this.lens - Math.sin(img.theta*Math.PI/180)*img.originalWidth*this.lens)+"px";
			img.style.left = Math.round(Math.cos(img.theta*Math.PI/180)*this.radius+centerX-(img.width/2))+"px";
			img.style.top = Math.round(Math.sin(img.theta*Math.PI/180)*this.radius*this.foreshortening+centerY)+"px";
			img.style.zIndex = this.baseLayer+this.imgs.length-Math.floor(this.imgs.length*Math.sin(img.theta*Math.PI/180))+1;
		} //for
	} //function



	this.getX = function(obj) {
		var x = 0;
		if (obj.offsetParent) {
			do { x += obj.offsetLeft; }
			while (obj = obj.offsetParent);
		} //if
		return x;
	} //function

	this.getY = function(obj) {
		var y = 0;
		if (obj.offsetParent) {
			do { y += obj.offsetTop; }
			while (obj = obj.offsetParent);
		} //if
		return y;
	} //function

} //JWCarousel

