function SlidingLayer(itemId,element,source,radioBehaviour,isFading,isSliding,animationSpeed,slidingDirection,sourceAnchor,targetAnchor)
{
	this.baseContructor=AnimatedItem;
	this.baseContructor(itemId,element,source,radioBehaviour,false);

	this.init=SlidingLayer_Init;
	this.init(isFading,isSliding,animationSpeed,slidingDirection,sourceAnchor,targetAnchor);
}

function SlidingLayer_Init(isFading,isSliding,animationSpeed,slidingDirection,sourceAnchor,targetAnchor)
{
	if (this._itemElement)
	{
		this._contentPanel=this._itemElement.firstChild;
		while(this._contentPanel.nodeType!=1 && this._contentPanel.nextSibling!=null) this._contentPanel=this._contentPanel.nextSibling;
	}
	
	this._isFading=isFading;
	this._isSliding=isSliding;
	this._animationSpeed=animationSpeed?animationSpeed:10;
	this._slidingDirection=slidingDirection;
	this._sourceAnchor=sourceAnchor;
	this._targetAnchor=targetAnchor;

	this.paint=SlidingLayer_Paint;
	this.isAnimated=SlidingLayer_IsAnimated;
	this.startAnimation=SlidingLayer_StartAnimation;
	this.continueAnimation=SlidingLayer_ContinueAnimation;
	this.endAnimation=SlidingLayer_EndAnimation;
	this.childrenStartAnimation=SlidingLayer_ChildrenStartAnimation;
	this.initSliding=SlidingLayer_InitSliding;
	this.calculateInitValues=SlidingLayer_CalculateInitValues;
	this.calculateOverFlows=SlidingLayer_CalculateOverFlows

	this.setIsShowing(this.getVisible());
	this._percent=this.getVisible()?1:0;
}

function SlidingLayer_IsAnimated()
{
	return this._isFading || this._isSliding;
}

function SlidingLayer_Paint(percent)
{
	var height=0;
	var width=0;
	
	if (this._isSliding)
	{
		height=Math.round(this._slidingDirectionSin*this._itemHeight*(1-percent));
		width=-Math.round(this._slidingDirectionCos*this._itemWidth*(1-percent));

		var clip="rect(";
		clip+=(this._slidingDirectionSin>0?height:0)+"px ";
		clip+=(this._slidingDirectionCos>0?(this._itemWidth+width):this._itemWidth)+"px ";
		clip+=(this._slidingDirectionSin<0?(this._itemHeight+height):this._itemHeight)+"px ";
		clip+=(this._slidingDirectionCos<0?+width:0)+"px)";
		this._itemElement.style.clip=clip;
	}

	this._itemElement.style.top=(this._itemTop-height)+"px";
	this._itemElement.style.left=(this._itemLeft-width)+"px";
	this._itemElement.style.zIndex=this.isShowing?200:100;
	
	if (this._isFading)
	{
		this._itemElement.style.opacity=percent;
		if (this._itemElement.filters) this._itemElement.style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+Math.round(percent*100)+");";
	}
}

function SlidingLayer_StartAnimation(show,source)
{
	if (!this.getVisible())
	{
		this._itemElement.style.visibility="hidden";
		this.setVisible(true);
		this.initSliding(source);
		this.paint(0);
		this._itemElement.style.visibility="";
	}
	
	this.setIsShowing(show);
	this.setIsInAnimation(true);
	this.setDirection(show?1:-1);
	this._percentIncrement=this._isSliding?this._animationSpeed/this._itemElement.offsetHeight:1/this._animationSpeed;
	if (show) this.handleRadioBehaviour();
	this.getOwner().startAnimation(this,show);
}

function SlidingLayer_ContinueAnimation() 
{
	var visible=this.getVisible();
	if (!this.isAnimated()) this._percent=visible?1:0;
	else
	{
		var direction=this.getDirection();
		this._percent+=direction*this._percentIncrement;
		if (this._percent>1) this._percent=1;
		else if (this._percent<0) this._percent=0;
	}
	this.paint(this._percent);
	
	if (this._percent>=1 || this._percent<=0) this.endAnimation();
}

function SlidingLayer_EndAnimation() 
{
    this.setIsInAnimation(false);
	this.setVisible(this.getDirection()==1);
	this.getOwner().endAnimation(this);
}

function SlidingLayer_ChildrenStartAnimation(animatedItem,show)
{
	if (show && !this.getVisible()) this.startAnimation(true);
}

function SlidingLayer_InitSliding(source)
{
    this._itemHeight=this._itemElement.offsetHeight;
	this._itemWidth=this._itemElement.offsetWidth;
	
	var initValues=this.calculateInitValues(source);
	var itemTop=initValues[0];
	var itemLeft=initValues[1];
	var slidingDirectionSin=initValues[2];
	var slidingDirectionCos=initValues[3];
	
	/*
	if (this.onOverFlowAction==1)
	{
		if (layerTop<0) layerTop=0;
		if (layerTop+this.layerHeight>clientHeight) layerTop=clientHeight-this.layerHeight;
		
		if(layerLeft<0)layerLeft=0;
		if (layerLeft+this.layerWidth>clientWidth) layerLeft=clientWidth-this.layerWidth;
	}
	else if (this.onOverFlowAction==2)
	{
		var slidingDirection=this.slidingDirection;
		var overFlows=this.calculateOverFlows(initValues);

		if (layerTop<0 || layerTop+this.layerHeight>clientHeight)
		{
			slidingDirection=-slidingDirection;
			var newInitValues=this.calculateInitValues(source,this.layerAnchor^2,this.sourceAnchor^2,-slidingDirection);
			
			var newOverFlows=this.calculateOverFlows(newInitValues);
			if (newInitValues[0]>0 && newOverFlows[4]<overFlows[4]) 
			{
				overFlows=newOverFlows;
				layerTop=newInitValues[0];
				layerLeft=newInitValues[1];
				slidingDirectionSin=newInitValues[2];
				slidingDirectionCos=newInitValues[3];
			}
		}
		
		if (layerLeft<0 || layerLeft+this.layerWidth>clientWidth)
		{
			slidingDirection=Math.PI-slidingDirection;
			var newInitValues=this.calculateInitValues(source,this.layerAnchor^1,this.sourceAnchor^1,slidingDirection);
			var newOverFlows=this.calculateOverFlows(newInitValues);
			if (newInitValues[1]>0 && newOverFlows[5]<overFlows[5])
			{
				overFlows=newOverFlows;
				layerTop=newInitValues[0];
				layerLeft=newInitValues[1];
				slidingDirectionSin=newInitValues[2];
				slidingDirectionCos=newInitValues[3];
			}
		}
	}
	*/
	
	this._itemTop=itemTop;
	this._itemLeft=itemLeft;
	
	this._slidingDirectionSin=slidingDirectionSin;
	this._slidingDirectionCos=slidingDirectionCos;
}

function SlidingLayer_CalculateInitValues(source,layerAnchor,sourceAnchor,slidingDirection)
{
	source=getElement(source);
	if (source!=null) this.source=source;
	if (layerAnchor==null) layerAnchor=this._targetAnchor;
	if (sourceAnchor==null) sourceAnchor=this._sourceAnchor;
	if (slidingDirection==null) slidingDirection=this._slidingDirection;

	var slidingDirectionSin;
	var slidingDirectionCos;
	if (slidingDirection!=null)
	{
		slidingDirectionSin=Math.sin(slidingDirection);
		slidingDirectionCos=Math.cos(slidingDirection);

		if (Math.abs(slidingDirectionSin)<1E-15) slidingDirectionSin=0;
		if (Math.abs(slidingDirectionCos)<1E-15) slidingDirectionCos=0;
	}
	
	var layerTop=getRealTop(this.source);
	var layerLeft=getRealLeft(this.source);
	
	if ((layerAnchor&1)==1) layerLeft-=this._itemWidth;
	if ((layerAnchor&2)==2) layerTop-=this._itemHeight;
	if ((sourceAnchor&1)==1) layerLeft+=source.offsetWidth;
	if ((sourceAnchor&2)==2) layerTop+=source.offsetHeight;
	
	return new Array(layerTop,layerLeft,slidingDirectionSin,slidingDirectionCos);
}

function SlidingLayer_CalculateOverFlows(values)
{
	var overFlows=new Array(6);
	var x1=values[0];
	var y1=values[1];
	var x2=getClientHeight()-values[0]-this.layerHeight;
	var y2=getClientWidth()-values[1]-this.layerWidth;

	overFlows[0]=x1<0?-x1:0;
	overFlows[1]=y1<0?-y1:0;
	overFlows[2]=x2<0?-x2:0;
	overFlows[3]=y2<0?-y2:0;
	overFlows[4]=(overFlows[0]+overFlows[2])*this.layerWidth;
	overFlows[5]=(overFlows[1]+overFlows[3])*this.layerHeight;

	return overFlows;
}