/*
* BOTROTATE
* - 
* REQUIRES
* - Mootools 1.2
* 
* Jay Richardson
* Werkbot Studios - www.werkbot.com
*/
var periodical;
var botrotate = new Class({
						  
	Implements: [Options],
	options: {
		image_set: "",
		image_timer: 5,
		image_active: 0,
		image_total: 0,
		image_set_active: -1,
		periodical: null,
		button_option: true
	},	initialize: function(options) {
		//SET OUR OPTIONS
			this.setOptions(options);
		//SET OUR ACTIVE IMAGE TO THE FIRST
			options.image_active = 0;
		//GRAB ALL IMAGES INSIDE OUR TAG
			var e = $(document.body).getElement("#"+options.image_set);
			var images = e.getElements("img");
			this.options.images_total = images.length; 
		//CREATE OUR BUTTON AREA
			if(options.button_option){
				var button_area = new Element('div', {'class':'botrotate_button_area'});
				e.adopt(button_area);
			}
		//HIDE THE IMAGES
			var count = 0;
			images.each(function(image){
				//CREATE A BUTTON
					if(options.button_option){
						var button = new Element('div', {'class':'botrotate_button-'+count});
						button.set("opacity", 0.3);
						button.addEvent("click", function(e){
							var num = button.get("class").split("-");
							this.options.image_set_active = Number(num[1]);
							this.rotateImage();										}.bind(this));
						button_area.adopt(button);
					}
				//CHECK FOR ANY LINKS AND HIDE THEM
					var link_tag = options.image_set+"-links-"+count;
					if($(link_tag)===undefined || $(link_tag)===null){
					}else{
						$(options.image_set+"-links-"+count).setStyles({
							"position":"absolute",
							"visibility":"hidden",
							"opacity": 0
						});
					}
				//
					image.set("id", options.image_set+count);
					image.setStyles({
						"position":"absolute",
						"visibility":"hidden",
						"opacity": 0
					});						count++;
			}.bind(this));
		//SHOW THE FIRST ONE
			$(options.image_set+options.image_active).setStyles({
				"visibility": "visible",
				"opacity": 1
			});
			//SHOW LINKS IF AVAILABLE
				if($(options.image_set+"-links-"+options.image_active)){
					$(options.image_set+"-links-"+options.image_active).setStyles({
						"visibility": "visible",
						"opacity": 1
					});
				}
			//
				if(options.button_option){
					$$(".botrotate_button-"+options.image_active).addClass("active");
					$$(".botrotate_button-"+options.image_active).set("opacity", 1);
				}
		//START OUR TIMER
			periodical = this.rotateImage.periodical((1000*options.image_timer), this);
	},
	rotateImage: function(){
		//HIDE OUR CURRENT IMAGE
			$(this.options.image_set+this.options.image_active).fade('out');
			//HIDE ANY LINKS
				var link_tag = this.options.image_set+"-links-"+this.options.image_active;
				if($(link_tag)===undefined || $(link_tag)===null){
				}else{
					$(link_tag).fade('out');				}
		//
			if(this.options.button_option){
				$$(".botrotate_button-"+this.options.image_active).removeClass("active");
				$$(".botrotate_button-"+this.options.image_active).set("opacity", 0.3);
			}
		//
			if(this.options.image_set_active>=0){
				this.options.image_active = this.options.image_set_active;
				this.options.image_set_active = -1;
				//CLEAR THE TIMER
					$clear(periodical);
				//THEN RESTART THE TIMER
					periodical = this.rotateImage.periodical((1000*this.options.image_timer), this);
			}else{
				if(this.options.image_active<this.options.images_total-1){
					this.options.image_active++;
				}else{
					this.options.image_active = 0;
				}
			}
		//SHOW OUR CURRENT IMAGE
			$(this.options.image_set+this.options.image_active).fade('in');
			//SHOW ANY LINKS
				var link_tag = this.options.image_set+"-links-"+this.options.image_active;
				if($(link_tag)===undefined || $(link_tag)===null){
				}else{
					$(link_tag).fade('in');				}
		//
			if(this.options.button_option){
				$$(".botrotate_button-"+this.options.image_active).addClass("active");
				$$(".botrotate_button-"+this.options.image_active).set("opacity", 1);
			}
			}
});
