var _A = new Array();
var Animation = {SlideRight:1, SlideLeft:2, FadeOut:3};
function FrameAnimator(){    
    this.Frames = [ {Id:null, Obj:null}, {Id:null, Obj:null}];    
    this.GlobalIndex = null;
    this.TimeoutId = null;
    this.IsActive = false;
    this.FrameWidth = 260; // px
    this.FrameHeight = null; // px
    this.FrameSpacing = 10; // px
    this.FrameStart = 10; //px
    this.TimeoutInterval = 10; //ms
    this.SlideStep = 10; //px
    this.opacityStep = 0.1;//*100
    this.CurrentIndex = 0;
    this.Animation = Animation.SlideLeft;  
    this.opacity = 1;  
    this.TimeoutOpacityInterval = 20; //ms
}

FrameAnimator.prototype.GetNextFrame = function(){
    var i = (frameAnimatorManset.CurrentIndex==1)?2:1;
    return this.Frames[i].Obj;
};

FrameAnimator.prototype.SetNextFrame = function(stringHtml, animateToNext, animation){
   this.GetNextFrame().innerHTML = stringHtml; 
   if(animateToNext){ this.Animate();}
};

FrameAnimator.prototype.ChangeContent = function(stringHTML, index){
  var index = (this.Frames.length>index)?index:0; 
  this.Frames[index].Obj.innerHTML = stringHTML;  
};

FrameAnimator.prototype.Animate = function(slideAction){
    var slideAction = (typeof(slideAction)=="undefined")? this.Animation:slideAction;
	if(this.IsActive){ return;}
	this.IsActive = true;	
	switch(slideAction){	
	    case Animation.SlideLeft:
	        this.SlideLeft();
	        return;  	        
        case Animation.SlideRight:	        
	        this.SlideRight();
	        return;	    
        case Animation.FadeOut:        
            var n = (this.CurrentIndex+1)%2;     
            this.Frames[this.CurrentIndex].Obj.style.zIndex = 2;
            this.Frames[n].Obj.style.left = 0 +'px';
            this.Frames[n].Obj.style.zIndex = 1;
            this.SlideFadeOut();
            return;	        
	}		
    SlideLeft(this);   
	return; 
};

FrameAnimator.prototype.SlideFadeOut = function(){   
    var n = (this.CurrentIndex+1)%2;      
    setOpacity(this.Frames[this.CurrentIndex].Obj, this.opacity);
    setOpacity(this.Frames[n].Obj, 1-this.opacity);
	this.opacity-=this.opacityStep;	
	if(this.opacity<=0){ 
	    this.opacity = 1;	    
	    this.Frames[this.CurrentIndex].Obj.style.left = this.FrameWidth + this.FrameSpacing + 'px';	    
	    setOpacity(this.Frames[this.CurrentIndex].Obj, this.opacity);
	    this.CurrentIndex = (this.CurrentIndex+1)%2;
	    clearTimeout(this.TimeoutId); 
	    this.IsActive = false;	    
	    return;
	}
    this.TimeoutId = setTimeout("_A[" + this.GlobalIndex + "].SlideFadeOut()", this.TimeoutOpacityInterval)    
}

FrameAnimator.prototype.SlideLeft = function(){
    this.Frames[this.CurrentIndex].Obj.style.left = this.left + "px";       
    var n = (this.CurrentIndex+1)%2;     
	this.Frames[n].Obj.style.left = (this.left - this.FrameSpacing - this.FrameWidth) + "px";	
	this.left+=this.SlideStep;		
	if(this.left>= this.FrameWidth + this.FrameSpacing *2 + this.FrameStart){ 
	    this.CurrentIndex = (this.CurrentIndex+1)%2;
		clearTimeout(this.TimeoutId); 
		this.IsActive = false;
		this.left = this.FrameStart;
		return;
	}		
	this.TimeoutId = setTimeout("_A[" + this.GlobalIndex + "].SlideLeft()", this.TimeoutInterval);
};

FrameAnimator.prototype.SlideRight = function(){
    this.Frames[this.CurrentIndex].Obj.style.left = (- this.left) + "px";            
    var n = (this.CurrentIndex+1)%2;
	this.Frames[n].Obj.style.left = (-this.left + this.FrameSpacing + this.FrameWidth) + "px";	
	this.left+=this.SlideStep;		
	if(Math.abs(this.left)>= this.FrameWidth + this.FrameSpacing *2 + this.FrameStart){ 
	    this.CurrentIndex = (this.CurrentIndex+1)%2;
		clearTimeout(this.TimeoutId); 
		this.IsActive = false;
		this.left = this.FrameStart;
		return;
	}		
	this.TimeoutId = setTimeout("_A[" + this.GlobalIndex + "].SlideRight()", this.TimeoutInterval);
};
	
FrameAnimator.prototype.Create = function(aID, bID, attachEvent, frameWidth, frameSpacing, frameStart){
    if(aID == bID) return;
    this.FrameWidth = frameWidth;
    this.FrameSpacing = frameSpacing;
    this.FrameStart = frameStart;    
    this.Frames[0].Id = aID;
    this.Frames[0].Obj = $(this.Frames[0].Id);    
    
    this.Frames[1].Id = bID;
    this.Frames[1].Obj = $(this.Frames[1].Id);      
    this.GlobalIndex = _A.length;
    this.left = this.FrameStart;
    _A[_A.length] = this;
    var s = this;
//    if(attachEvent){
//        this.Frames[0].Obj.onclick = function(){ _A[s.GlobalIndex].Animate();};
//        this.Frames[1].Obj.onclick = function(){ _A[s.GlobalIndex].Animate();};
//    }
    return this;
};

function setOpacity(object, opacity) {
	if(object){
		object.style.opacity = (opacity == 1 ? 0.99 : opacity);
		object.style.MozOpacity = (opacity == 1 ? 0.99 : opacity);
		object.style.KhtmlOpacity = opacity;
		if (object.style.setAttribute) {
			if(opacity==1){
				object.style.setAttribute("filter","");
			}
			else{
				object.style.setAttribute("filter",  "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (opacity * 100) + ")");
			}
		}
	}
}

function getOpacity(object){
	if(object){
		if(object.style.display == 'none')
			return 0;
		if(object.style.opacity)
			return parseFloat(object.style.opacity);
		if(object.style.MozOpacity)
			return parseFloat(object.style.MozOpacity);
		if(object.style.KhtmlOpacity)
			return parseFloat(object.style.KhtmlOpacity);
		if(object.style.filter){
			var opacity = object.style.filter.match(/alpha\(opacity=(.*)\)/i);
			if(opacity[1]) return parseFloat(opacity[1]) / 100;
		}
	}
	return 1.0;
}
