function ImageChanger(id)
{
    this.id = id;
    this.activeEl = "";
    this.activeEvent = "";
    this.timeSpeed = 1000;
    this.fixedWidth = 0;
    this.fixedHeight = 0;
    this.items = new ImageItems();
    this.run = false;
    this.currentImg = 0;
    this.tout = 0;
}
ImageChanger.prototype.Init = function(activeEl, activeEvent, timeSpeed, fixedWidth, fixedHeight)
{
    this.activeEl = activeEl;
    this.activeEvent = activeEvent;
    this.timeSpeed = timeSpeed;
    this.fixedWidth = fixedWidth;
    this.fixedHeight = fixedHeight;
    fadeOpacity.addRule("opacityRule1", 1, 0, 25);
    fadeOpacity.addRule("opacityRule2", 0, 1, 25);
    
//    Event.observe(window, "scroll", setPosition);
//    Event.observe(window, "resize", setPosition);
//    Event.observe($("content"), "scroll", setPosition);
//    Event.observe($("content"), "resize", setPosition);
} 
ImageChanger.prototype.AttachTo = function()
{
    var parentEl = $(this.id);
    parentEl.ecmaClass = this;
    
    if (this.items.count > 0)
    {
        for (var i = 0; i < this.items.count; ++i)
        {
            var imgEl = $c("img", this.id + "_img_" + i, parentEl);
            imgEl.src = this.items.item[i];
            imgEl.setStyle({ position:  "absolute", zIndex: this.items.count - i - 1 });
            imgEl.setStyle({ width:  parentEl.getWidth() + "px", height: parentEl.getHeight() + "px" });
            setElementOpacity(imgEl, 0);
        }    
    } 
    var activeEl = $(this.activeEl);
    if (activeEl != null) 
       Event.observe(activeEl, this.activeEvent, this.Run);
}

ImageChanger.prototype.Run = function(ev)
{
    if (ev == "undefined")
    {
        var ev = (ev) ? ev : ((window.event) ? event: null);
        var el = getCurrentEventTarget(ev);
        var ctx = getEcmaClass(ev);
        if (el._ecmaClass != ctx) 
            el = getParentNode(el,ctx);  
    }
    else
        ctx = this; 
    
    if (!ctx.run)    
    {
        ctx.Clear(); 
        ctx.run = true;
        ctx.tout = setTimeout(ctx.id + ".Fade()", 500); 
    }
    else
        ctx.Clear(); 
}

ImageChanger.prototype.Fade = function()
{
    if (!this.run)
        return;
    var currentId = this.id + "_img_" + this.currentImg;
    var nextId = "";
    if (this.currentImg < (this.items.count - 1))
        nextId = this.id + "_img_" + (this.currentImg + 1);
    else    
        nextId = this.id + "_img_0";
    fadeOpacity(currentId, "opacityRule1");   
    this.tout = setTimeout("fadeOpacity('" + nextId + "', 'opacityRule2')", 500);   // скорость смены изображения
    this.tout = setTimeout(this.id + ".ChangeImage()", 3500);  // время задержки изображения 
}

ImageChanger.prototype.ChangeImage = function()
{
    if (!this.run)
        return;
    if (this.currentImg < (this.items.count - 1))
        this.currentImg++;
    else 
        this.currentImg = 0;
    this.Fade();    
}             

ImageChanger.prototype.Clear = function()
{
    this.run = false;
    this.currentImg = 0;
    var imgEl = null;
    clearTimeout(this.tout);
    fadeOpacity.aProc = {}; 
    imgEl = $(this.id + "_img_0");
    setElementOpacity(imgEl, 1);
    for (var i = 1; i < this.items.count; ++i)
    {
        imgEl = $(this.id + "_img_" + i);
        setElementOpacity(imgEl, 0);
    }
}

// ImageItems
function ImageItems()
{
    this.item = $A(); 
    this.count = 0;
}
ImageItems.prototype.Add = function(imgSrc)    
{
    this.item[this.count] = imgSrc;
    this.count = this.count + 1;
}
ImageItems.prototype.Delete = function(index)    
{
    if ((index > -1) && (index < this.count))
    {
        this.item.splice(index,1);
        this.count--;
    }
}
ImageItems.prototype.Clear = function(text)    
{
    for (var i = 0; i < this.count; ++i)
        this.items.shift();
    this.count = 0;
}

//  ---

function Context(ev)
{   
    return false;
}

//var prevScroll = 0;
//function setPosition(ev)
//{
//    if (slides.items.count > 0)
//    {
//        for (var i = 0; i < slides.items.count; ++i)
//        {
//            var currentScroll = $("content").scrollTop;
//            var dy = (prevScroll < currentScroll) ? currentScroll : prevScroll - currentScroll;
//            $(slides.id + "_img_" + i).setStyle({ top: (-i) * ($("slides").getHeight()) - dy + "px" });
//            prevScroll = currentScroll;
//        }   
//    } 
//}

//--------------------------------------------------------------------
/*
setElementOpacity - установка прозрачности
getOpacityProperty - проверка, есть ли возможность менять прозрачность
fadeOpacity - плавное изменение прозрачности
*/
//--------------------------------------------------------------------

function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = p=="filter"?new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

function getOpacityProperty()
{
	var p;
	if (typeof document.body.style.opacity == 'string') 
	    p = 'opacity';
	else 
	    if (typeof document.body.style.MozOpacity == 'string') 
	        p =  'MozOpacity';
	else 
	    if (typeof document.body.style.KhtmlOpacity == 'string') 
	        p =  'KhtmlOpacity';
	else 
	    if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) 
	        p =  'filter';
	
	return (getOpacityProperty = new Function("return '"+p+"';"))();
}

function fadeOpacity(sElemId, sRuleName, bBackward)
{
	var elem = document.getElementById(sElemId);
	if (!elem || !getOpacityProperty() || !fadeOpacity.aRules[sRuleName]) 
	    return;
	
	var rule = fadeOpacity.aRules[sRuleName];
	var nOpacity = rule.nStartOpacity;
	
	if (fadeOpacity.aProc[sElemId]) 
	{
	    clearInterval(fadeOpacity.aProc[sElemId].tId); 
	    nOpacity = fadeOpacity.aProc[sElemId].nOpacity;
	}
	if ((nOpacity == rule.nStartOpacity && bBackward) || (nOpacity == rule.nFinishOpacity && !bBackward)) 
	    return;

	fadeOpacity.aProc[sElemId] = {'nOpacity':nOpacity, 'tId':setInterval('fadeOpacity.run("'+sElemId+'")', fadeOpacity.aRules[sRuleName].nDalay), 'sRuleName':sRuleName, 'bBackward':Boolean(bBackward)};
}

fadeOpacity.addRule = function(sRuleName, nStartOpacity, nFinishOpacity, nDalay)
{
    fadeOpacity.aRules[sRuleName]={'nStartOpacity':nStartOpacity, 'nFinishOpacity':nFinishOpacity, 'nDalay':(nDalay || 30),'nDSign':(nFinishOpacity-nStartOpacity > 0?1:-1)};
};

fadeOpacity.back = function(sElemId)
{
    fadeOpacity(sElemId, fadeOpacity.aProc[sElemId].sRuleName, true);
};

fadeOpacity.run = function(sElemId)
{
	var proc = fadeOpacity.aProc[sElemId];
	var rule = fadeOpacity.aRules[proc.sRuleName];
	
	proc.nOpacity = Math.round(( proc.nOpacity + .1*rule.nDSign*(proc.bBackward?-1:1))*100)/100;
	setElementOpacity(document.getElementById(sElemId), proc.nOpacity);
	
	if (proc.nOpacity == rule.nStartOpacity || proc.nOpacity == rule.nFinishOpacity) 
	{
	    clearInterval(fadeOpacity.aProc[sElemId].tId);
	}    
}
fadeOpacity.aProc = {};
fadeOpacity.aRules = {};
