// 
//  rotateImage.js
//  Rotate a sprite on a timer
//  
//  Created by Jay Contonio on 2009-04-30.
//  Copyright 2009 GIRAFFE. All rights reserved.
// 

/* To use

<script type="text/javascript">
	rotateImage(jQueryObject, "/path/to/promo.jpg", width, height, [
		"http://google.com",
		"http://apple.com"
	]);
	startTimer(time_in_seconds);
</script>

--------------------------------------------- */
var intervalID;
var images = [];
var counter = 0;
var max;
var externalLinks = [];
var link;
var imgHeight;

function rotateImage(id,img,width,height,links) {
	var div = $("<div class='rotatingPromo'></div>");
	$(div).css({
		'height':height + 'px',
		'width':width + 'px'
	});
	$(div).html("<img src='" + img + "' width='" + width + "' />");
	$(id).html(div);
	max = links.length - 1;
	imgHeight = height;
	externalLinks.push(links);
	images.push(div);
}

function startTimer(speed) {
	rotate();
	intervalID = setInterval(rotate, speed * 1000);
}

function rotate() {
	if(counter > max) counter = 0;
	for(var i = 0; i < images.length; i++) {
		var img = $(images[i]).find('img');
		//	Remove the click on the image
		$(img).unbind('click');
		//	Move the position of the image within the div
		$(img).css({ "top":-(counter * imgHeight) + "px" });
		//	Add a new click event to the image using the appropriate link
		link = externalLinks[i][counter];
		$(img).click(function() {
			location.href = link;
		});
	}
	counter++;
}
