function RotatE() {
	this.iteration = 0;
	this.fadeInterval = 3000;
	this.isDisplayMode = true;
}

RotatE.prototype.rotateInit = function(contId, instance, interval) {

	this.instance = instance;
	if (typeof(interval) == 'undefined') {
		this.displayInterval = 0;
		this.isManual = true;
	} else {
		this.displayInterval = parseInt(interval, 10) * 1000;
		this.isManual = this.displayInterval == 0;
	}

	// iteration variable
	var i, img_index, div_index;

	this.images = new Array();
	this.divs = new Array();

	// divA - attachments container
	this.cont = document.getElementById(contId);

	// run the cycle to compile the image array
	var images = this.cont.getElementsByTagName('IMG');
	for (i = 0; i < images.length; i++) {
		if (images[i].className.match(/attachmentImage/)) {
			this.images[this.images.length] = images[i];
		}
	}

	// go through child nodes, arrange images in DIV containers where necessary
	var divs = this.cont.getElementsByTagName('DIV');
	for (i = 0; i < divs.length; i++) {
		if (divs[i].className.match(/attachment/)) {

			div_index = this.divs.length;
			img_index = div_index + 1;
			if (img_index == this.images.length) {
				img_index = 0;
			}

			// set background properties
			divs[i].style.backgroundImage = 'url(' + this.images[img_index].src  + ')';
			divs[i].style.backgroundRepeat = 'no-repeat';
			this.divs[div_index] = divs[i];
		}
	}

	// hide all nodes besides the first one
	for (i = 1; i < this.divs.length; i++) {
		this.cont.removeChild(this.divs[i]);
	}

	this.isManual ? this.rotateManual(true) : this.rotateAuto();
}

RotatE.prototype.rotateAuto = function() {

	if (this.isDisplayMode) {
		this.setTransparency(0, this.images[this.iteration]);
		this.cont.replaceChild( this.divs[this.iteration], this.cont.getElementsByTagName('DIV')[0] );
	} else {
		this.initFade(this.fadeInterval);
		this.iteration = this.iteration == this.divs.length - 1 ? 0 : this.iteration + 1;
	}

	window.setTimeout(this.instance + '.rotateAuto();',
		this.isDisplayMode ? this.displayInterval : this.fadeInterval);

	this.isDisplayMode = !this.isDisplayMode;
}

RotatE.prototype.initFade = function(fadeInterval) {
	if (fadeInterval == 0) {
		this.setTransparency(0);
	} else {
		var speed = Math.round(fadeInterval / 100);
		for (var i = 1; i <= 100; i++) {
			window.setTimeout(this.instance + '.setTransparency(' + i + ')', (i * speed));
		}
	}
}

RotatE.prototype.setTransparency = function(transparency, object) {

	if (typeof(object) == 'undefined') {
		// the value of the rotator is increased by 1 by now, so calculate it again
		var img_index = this.iteration - 1;
		if (img_index == -1) {
			img_index = this.images.length - 1;
		}
		object = this.images[img_index];
	}

	var value = 1 - (transparency / 100);
	object.style.opacity = value;
	object.style.MozOpacity = value;
	object.style.KhtmlOpacity = value;
	object.style.filter = 'alpha(opacity=' + (100 - transparency) + ')';
}

RotatE.prototype.rotateManual = function(isInit) {
	isInit = typeof(isInit) == 'undefined' ? false : isInit;
	if (this.isInit && this.images.length == 1) {
		document.getElementById('attachRotateNavNext').style.visibility = 'hidden';
		document.getElementById('attachRotateNavPrev').style.visibility = 'hidden';
	}
	this.cont.replaceChild( this.divs[this.iteration], this.cont.getElementsByTagName('DIV')[0] );
}

RotatE.prototype.nextImg = function() {
	this.iteration = this.iteration + 1;
	if (this.iteration == this.images.length)
		this.iteration = 0;
	this.rotateManual();
}

RotatE.prototype.prevImg = function() {
	this.iteration = this.iteration - 1;
	if (this.iteration == -1)
		this.iteration = this.images.length - 1;
	this.rotateManual();
}
