
var IRM = {

	baseFolder: 'images/ads/',
	images:[],		// {title, src, link, img}
	currentIndex: -1,
	toutRef: null,
	aRef: null,
	imgRef: null,
	
	init: function()
	{
		if(IRM.images.length == 0) return;
		
		for(var i = 0; i < IRM.images.length; i++)
		{
			IRM.images[i].img = new Image();
			IRM.images[i].img.src = IRM.images[i].src;
		}
		
		IRM.next();
		IRM.toutRef = setInterval(IRM.next, 6000);
	},
	
	update: function()
	{
		if(!IRM.aRef)
		{
			IRM.aRef = $('<a target="_blank"></a>').appendTo('#ads');
			IRM.imgRef = $('<img alt="" />').appendTo(IRM.aRef);
		}
		
		$(IRM.aRef).attr({'href': IRM.images[IRM.currentIndex].href});
		$(IRM.aRef).attr({'title': IRM.images[IRM.currentIndex].title});
		$(IRM.imgRef).attr({'src': IRM.baseFolder + IRM.images[IRM.currentIndex].src});
		//alert($('#ads')[0].innerHTML);
	},
	
	next: function()
	{
		IRM.setNextIndex();
		IRM.update();
	},
	
	setNextIndex: function()
	{
		if(IRM.currentIndex >= IRM.images.length - 1)
			IRM.currentIndex = 0;
		else IRM.currentIndex ++;
	},
	
	/*
	setNextIndex: function()
	{
		for(var i = 0; i < 10; i++)
		{
			var r = IRM.random(IRM.images.length - 1);
			if(r != IRM.currentIndex)
			{
				IRM.currentIndex = r;
				break;
			}
		}
	},
	*/
	
	random: function(max)
	{
		return Math.floor((max + 1) * Math.random());
	}
	
}
