/**
 * Плавно меняет изображения
 * @todo затенение в Opera
 *
 * @param string objName имя объекта
 */
function ImageChanger (objName) {

	/**
	 * Шаг изменения прозрачности. Прозрачность измеряется числами от 0 до 100
	 *
	 * @var int
	 */
	this.opacityStep = 2;

	/**
	 * Время в милисекундах до следующего изменения прозрачности изображения
	 *
	 * @var int
	 */
	this.opacityStepTime = 40;

	/**
	 * Время в милисекундах отображения изображения
	 *
	 * @var int
	 */
	this.freezeTime = 4000;

	this._images = new Array();
	this._objName = objName;
	this._objElement = undefined;
	this._timeExpId = null;
	this._currImageNum = 0;
	this._currImageShow = true;
	this._currImageOpacityPercent = 100;
}

/**
 * Добавляет изображения для отображения
 *
 * @param array urls массив адресов изображений
 */
ImageChanger.prototype.addImages = function (urls) {
	for (var i=0; i<urls.length; i++) this.addImage(urls[i]);
}

/**
 * Добавляет изображение для отображения
 *
 * @param string url адрес изображения
 */
ImageChanger.prototype.addImage = function (url) {
	var img = new Image();
	img.src = url;
	this._images[this._images.length] = img;
}

/**
 * Выводит в документ тег изображения и запускает механизм замены
 *
 */
ImageChanger.prototype.write = function () {
	with (this._images[this._currImageNum])
		var imgParams = ' src="'+src+'"' + (width?' width="'+width+'"':'') + (height?' height="'+height+'"':'');
	document.write('<img id="'+this._objName+'"'+imgParams+'>');
	this._objElement = document.getElementById(this._objName);
	this._timeExpId = setInterval(this._objName+'._opacityStart()', this.freezeTime);
}

ImageChanger.prototype._opacityStart = function () {
	if (this._timeExpId) clearInterval(this._timeExpId);
	this._timeExpId = setInterval(this._objName+'._opacityAction()', this.opacityStepTime);
}

ImageChanger.prototype._opacityAction = function () {
	this._currImageOpacityPercent += (this._currImageShow ? -1 : 1) * this.opacityStep;
	if (this._currImageOpacityPercent > 100) {
		this._currImageShow = true;
		clearInterval(this._timeExpId);
		this._timeExpId = setInterval(this._objName+'._opacityStart()', this.freezeTime);
	} else if (this._currImageOpacityPercent >= 0) {
		this._setOpacity(this._objElement, this._currImageOpacityPercent);
	}
	else {
		this._setOpacity(this._objElement, 0);
		this._currImageOpacityPercent = 0;
		this._currImageShow = false;
		this._currImageNum = (this._images.length-1 <= this._currImageNum) ? 0 : this._currImageNum + 1;
		with (this._images[this._currImageNum]) {
			this._objElement.src = src;
			this._objElement.width = width;
			this._objElement.height = height;
		}
	}
}

ImageChanger.prototype._setOpacity = function (el, opacityVal) {
	with (el.style) {
		if (typeof(opacity) != 'undefined') opacity = opacityVal/100;
		else if (typeof(KhtmlOpacity) != 'undefined') KhtmlOpacity = opacityVal/100;
		else if (typeof(MozOpacity) != 'undefined') MozOpacity = opacityVal/100;
		else if (typeof(el.filters)!='undefined' && typeof(el.filters['DXImageTransform.Microsoft.alpha'])!='undefined')
		    el.filters['DXImageTransform.Microsoft.alpha'].opacity = opacityVal;
		else if (typeof(filter) != 'undefined') filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+opacityVal+")";
	}
}
