var onniRotatorDefault = {		
	duration		: 500,						// Duration Betwwen loading Thumbnails in miliseconds
	viewTime		: 4000,						// View Time of Thumb View and PicView in miliseconds
	dataUrl			: 'data.xml',				// XML Data file where images are listed
	
	replaceImage	: "blank.gif",		// Change this if directory of blank file is different			
	
	thumbCount		: 9	,						// Number of Thumbnails in container
	
	imgWidth		: 160,						// Width of thumb image	
	imgHeight		: 116						// Height of thumb image
};

var onniRotator = {
	
	current_group : 0,
	current_item : 0,
	el_containers : [],
	blocks : [],
	init : function(obj) {
		if(!obj) return false;
		var self = this;
		if(typeof(obj)!='object')	
			this.obj = $(obj);
		else
			this.obj = obj;
			
		new Ajax.Request(onniRotatorDefault.dataUrl, {
		  method: 'get',
		  onSuccess: function(transport) {
			
				var sets = transport.responseXML.getElementsByTagName('displaygroup');
				var sl = sets.length;
				
				for(var i = 0; i < sl; i++)
				{
					var subset = sets[i].getElementsByTagName('smallimage');
					var ssl = subset.length;
					var bigimg = sets[i].getElementsByTagName('largeimage');
					var _bigimage = new Image();
						_bigimage.src = bigimg[0].childNodes[0].nodeValue;
					var set_content = {
						url : bigimg[0].getAttribute('url'),		
						imgs : bigimg[0].childNodes[0].nodeValue,
						_image : _bigimage,
						images : []
					};
					//self.preload(bigimg[0].childNodes[0].nodeValue);
					
					for(var a = 0; a < ssl; a++) 
					{
						var url = subset[a].getAttribute('url');
						var imgs = subset[a].childNodes[0].nodeValue;
						var _image = new Image();
						_image.src = imgs;
						set_content.images.push({"url":url, "imgs":imgs, '_image':_image});
					}
					var len = self.blocks.push(set_content);
				}
				self.build();
		  }
		});
	},
	
	build : function (){
		var self = onniRotator;
		self.obj.innerHTML = '';
		
		self.obj.style.backgroundImage =  "url('"+self.blocks[0].imgs+"')";
		for(var i = 0; i < onniRotatorDefault.thumbCount; i++) 
		{
			self.el_containers[i] = new Element('a', {'href':self.blocks[0].url}).update('<img src="' + onniRotatorDefault.replaceImage + '" style="width:' + onniRotatorDefault.imgWidth + 'px; height: ' + onniRotatorDefault.imgHeight + 'px">');
			self.obj.appendChild(self.el_containers[i]);
		}
		self.processStack(0);
	},
	processStack : function (stck){
		var self = onniRotator;
		if (stck >= self.blocks.length) stck = 0;
		
		self.current_group = stck;
		self.obj.style.backgroundImage =  "url('"+self.blocks[stck].imgs+"')";
		window.setTimeout(onniRotator.nextThumb, onniRotatorDefault.viewTime );
	},
	nextThumb : function (){
		var self = onniRotator;
		if(self.current_item  < 9){
			self.el_containers[self.current_item].href = self.blocks[self.current_group].images[self.current_item].url;
			$(self.el_containers[self.current_item]).update('<img src="' + self.blocks[self.current_group].images[self.current_item].imgs + '" style="width:' + onniRotatorDefault.imgWidth + 'px; height: ' + onniRotatorDefault.imgHeight + 'px">'); 
			self.current_item++;
			window.setTimeout(onniRotator.nextThumb, onniRotatorDefault.duration );
		} else {
			self.current_item  = 0;
			var nextbg =  (self.current_group + 2 > self.blocks.length) ? 0 : (self.current_group + 1);
			self.obj.style.backgroundImage =  "url('"+self.blocks[nextbg].imgs+"')";
			window.setTimeout(onniRotator.cleanThumb, onniRotatorDefault.viewTime );
		}
	},
	cleanThumb : function (){
		var self = onniRotator;
		var nextbg =  ((self.current_group + 2) > self.blocks.length) ? 0 : (self.current_group + 1);
		
		var url = self.blocks[nextbg].url
		if(self.current_item  < 9){
			
			self.el_containers[self.current_item].href = url;
			$(self.el_containers[self.current_item]).update('<img src="' + onniRotatorDefault.replaceImage + '" style="width:' + onniRotatorDefault.imgWidth + 'px; height: ' + onniRotatorDefault.imgHeight + 'px">'); 
			self.current_item++;
			window.setTimeout(onniRotator.cleanThumb, onniRotatorDefault.duration );
		} else {
			self.current_item  = 0;
			self.processStack(self.current_group + 1);
		}
	},
	preload : function(url) {
		var s = new Image();
		s.src = url;
	}
};
