imageRotate = {
	imgs: [],
	urls: [],
	imgReady: [],
	time: 5000,
	obj: null,
	index: 0,
	add: function(img, url) {
		this.imgs[this.imgs.length] = img;
		this.urls[this.urls.length] = url;
		this.imgReady[this.imgs.length] = false;
	},
	run: function() {
		this.index = Math.round(Math.random() * this.imgs.length);
		var a = $('<a href="' + this.urls[this.index] + '" target="_blank"></a>').appendTo(this.obj);
		for(var i = 0; i<this.imgs.length; i++) {
			$('<img src="' + this.imgs[i] + '" />').appendTo(a)
				.css({
					'opacity': 0,
					position: 'absolute'
				});
			window.setTimeout('imageRotate.setLocation(' + i + ')', 300);
		};
		$(window).resize(function() {
			imageRotate.resize();
		});
		this.rotate();
	},
	resize: function () {
		for(var i = 0; i<this.imgs.length; i++) this.setLocation(i);
	},
	setLocation: function(i) {
		var img = this.obj.children("a").children(":eq(" + i + ")");
		if(!img.width()) {
			window.setTimeout('imageRotate.setLocation(' + i + ')', 500);
			return;
		}
		var pos = this.obj.offset();
		var posLeft = this.obj.width() + pos.left;
		img.css({
			left: posLeft - img.width(),
			top: pos.top + (this.obj.height() - img.height()) / 2
		});
		this.imgReady[i] = true;
	},
	rotate: function() {
		index2 = this.index;
		this.index = ++this.index >= this.imgs.length ? 0 : this.index;
		if(!this.imgReady[this.index]) {
			window.setTimeout("imageRotate.rotate()", 500);
			return;
		}
		var a = this.obj.children("a");
		a.attr("href", this.urls[this.index]);
		a.children(":eq(" + index2 + ")").animate({opacity:0}, 300);
		a.children(":eq(" + this.index + ")").animate({opacity:1}, 500);
		window.setTimeout("imageRotate.rotate()", this.time);
	}
};
