/*
* BOTROTATE
* - 
* REQUIRES
* - jquery 1.3.2
* 
* Jay Richardson
* Werkbot Studios - www.werkbot.com
*/
var default_time = 5000;
var total_images = 0;
var image_set_count = 0;
var next = 1;
var current = 0;
var next_click = -1;
var next_set_count = 0;
var time = 0;
var timerinterval = new Array();

function getOptions(e){
	if($(e).length>0 && $(e).attr("rel")!== undefined){
		var options = e.attr("rel").split("|");
		if(options[2]===undefined){
			options[2] = 0;
		}
		if(options[3]===undefined){
			options[3] = 1;
		}
	}else{
		var options = new Array(5, false);	
	}
	return options;
}
function rotate(id){
	//GRAB THE OPTIONS FOR THIS
	//TIME | BUTTONS 
	//options[0] | options[1]
		var options = getOptions($("#"+id+"-0").parent());
		if(options[0]>0){
			time = 1000*Number(options[0]);
		}else{
			//DEFAULT TO FIVE SECONDS
				time =  default_time;
		}
		current = options[2];
		next = options[3];
	//
		var current_image = "#"+id+"-"+current;
		var current_links = "#"+id+"-links-"+current;
	//
		if(next_click>=0){
			//
				next = next_click;
				next_click = -1;
				clearInterval(timerinterval[id]);
				timerinterval[id] = setInterval("rotate('"+id+"')", time);
		}
	//FADE OUT OUR ACTIVE ELEMENT
		$(current_image).fadeOut("slow");
		if(options[1]=="true"){
			$(".botrotate_button-"+current).removeClass("active");
		}
		//FADE OUT OUR ACTIVE ELEMENT LINKS
			$(current_links).fadeOut("slow");
	//
		var next_image = "#"+id+"-"+next;
		var next_links = "#"+id+"-links-"+next;
	//FADE IN OUR NEXT ELEMENT
		$(next_image).fadeIn("slow");
		if(options[1]=="true"){
			$(".botrotate_button-"+next).addClass("active");
		}
		//FADE IN OUR NEXT ELEMENT LINKS
			$(next_links).fadeIn("slow");
		//DETERMINE OUR NEXT ELEMENT	
			current = next;
			if(next<total_images-1){
				next++;
			}else{
				next = 0;
			}
	//SET OPTIONS
		$(current_image).parent().attr("rel", options[0]+"|"+options[1]+"|"+current+"|"+next);
}

$(document).ready(function() {	
	//HIDE ALL IMAGES IN OUR TAG EXCEPT THE FIRST ONE
		$(".botrotate").each( function() {
			//GRAB THE OPTIONS FOR THIS
			//TIME | BUTTONS | CURRENT | NEXT
			//options[0] | options[1]
				var options = getOptions($(this));
				total_images = $(this).find("img").length;
			//
				$(this).css("position", "relative");
			//CREATE OUR BUTTON AREA
				if(options[1]=="true"){
					var button_area = $("<div>").attr({
						'class': "botrotate_button_area"
					});
					$(this).append(button_area);
				//CREATE A BUTTON FOR EACH IMAGE
					var count = 0;
					$(this).find("img").each( function() {
						var button = $("<div>").attr({
							'id': "botrotate_button-"+image_set_count+"-"+count,
							'class': "botrotate_button-"+count+" botrotate_button"
						}).html(
							$(this).attr("title")
						);
						count++;
						button.click(function () {
							var num = $(this).attr("id").split("-");
							next_click = Number(num[2]);
							rotate("botrotate-"+Number(num[1]));
						});
						button_area.append(button);
					});
				}
			//
				var id = "botrotate-"+image_set_count;
				$(this).attr("id", id);
				image_set_count++;
			//GIVE EACH IMAGE AN ID
				var image_count = 0;
				$(this).children("img").each( function() {
					$(this).attr("id", id+"-"+image_count);
					image_count++;
				});	
			//HIDE ALL THE IMAGES
				$(this).children("img").css({
					"position":"absolute",
					"display":"none"
				});	
			//HIDE ALL THE LINKS
				$(this).children("div.links").css({
					"position":"absolute",
					"display":"none"
				});	
			//SHOW THE FIRST IMAGE
				$(this).find("img:first").
					css({
						"display": "block"							
					});
				//SHOW THE FIRST LINKS
					$(this).find("div.links:first").
						css({
							"display": "block"							
						});
				if(options[1]=="true"){
					$(".botrotate_button-0").addClass("active");
				}
			//START OUR TIMER
				if(options[0]>0){
					time = 1000*Number(options[0]);
				}else{
					//DEFAULT TO FIVE SECONDS
						time =  default_time;
				}
				timerinterval[id] = setInterval("rotate('"+id+"')", time);
		});
});
