var aCards = new Array();

function initCards()
{
	
	var aDiv = document.getElementById('cardwrapper').getElementsByTagName("div");
	for( var i=aDiv.length-1; i>=0; i-- )
		if( aDiv[i].className == "card" )
			aCards[aCards.length] = new Card(aDiv[i], aCards.length);
	//setTimeout("aCards[aCards.length-1]._center()",1000);
	aCards[aCards.length-1]._center();
}

function nextCard()
{
	for( var i=0; i<aCards.length; i++ )
		if( aCards[i]._active )
			 aCards[i]._fadeInFront();
}

function getStyle(eElement,sStyle)
{
	if (eElement.currentStyle)
		var mReturn = eElement.currentStyle[sStyle];
	else if (window.getComputedStyle)
		var mReturn = document.defaultView.getComputedStyle(eElement,null).getPropertyValue(sStyle);
	return mReturn;
}

function Card( eCard, id )
{
	eCard._parent	= this;
	this._card		= eCard;
	this._id		= id;
	this._active	= false;
	this._position	= Array(this._card.offsetLeft, this._card.offsetTop);
	var aCards = this._card.getElementsByTagName("div");
	for( var i=0; i<aCards.length; i++ )
	{
		if( aCards[i].className == "cardback" )
			this._cardback = aCards[i];
		else if( aCards[i].className == "cardfront" )
		{
			this._cardfront = aCards[i];
			this._cardfront._parent = this;
		}
	}	
	this._clickable		= getStyle(this._cardfront,"cursor") == "pointer";
	this._image			= this._cardfront.getElementsByTagName("img")[0];
	if( this._card.offsetTop > 0 )
	{
		this._offscreen = false;
		this._card.style.position	= "absolute";
		this._card.style.top		= this._position[1]+"px";
		this._card.style.left		= this._position[0]+"px";
		this._image.style.width		= "100%";
		this._image.style.height	= "100%";
	}
	else
	{
		this._offscreen = true;
		this._shadow = document.getElementById("shadow").getElementsByTagName("img")[0];
	}
}

Card.prototype._center = function()
{
	if( !this._parent )
		this._parent = this;
	this._parent._active = true;
	this._parent._card.style.zIndex = 1;	
	var oAnimation = new Keen.animation( this._parent._card );
	oAnimation._parent = this._parent;
	if( !this._parent._offscreen )
	{
		this._parent._image.src = this._parent._image.src.replace("_small","_big");
		oAnimation.slidegrow( 150, 0, 685, 490, Keen.movement.elastic, 20, this._parent.__onCenter );
	}
	else
	{
		//this._parent._shadow.style.display = 'block';
		oAnimation.slide( 150, 0, Keen.movement.elastic, 1 );
		setTimeout("document.location='http://www.centraalbeheer.nl/cbi/cb/thema.jsp?ordeningId=ORD0001';", 3000);
		//var oAnimation = new Keen.animation( this._parent._shadow );
		//oAnimation.grow( 685, 49, Keen.movement.elastic, 20 );
	}
}

Card.prototype.__onCenter = function()
{
	this._parent._cardback.style.display = 'block';
	if( !this._parent._clickable )
		setTimeout("aCards["+this._parent._id+"]._fadeOutFront()",2000);
	else
		this._parent._cardfront.onclick = this._parent._fadeOutFront;
}

Card.prototype._fadeOutFront = function()
{
	if( !this._parent )
		this._parent = this;

	// ie6 needs hard width and height
	this._parent._cardfront.style.width		= "685px";
	this._parent._cardfront.style.height	= "490px";

	var oAnimation = new Keen.animation( this._parent._cardfront);
	oAnimation._parent = this._parent;
	oAnimation.fade( 0, "", 10, this._parent.__onFadeOutFront );
}

Card.prototype.__onFadeOutFront = function()
{
	this._parent._cardfront.style.display = 'none';
}

Card.prototype._fadeInFront = function()
{
	this._parent._cardfront.style.display = 'block';
	var oAnimation = new Keen.animation( this._parent._cardfront );
	oAnimation._parent = this._parent;
	oAnimation.fade( 100, "", 10, this._parent._return );
}

Card.prototype._return = function()
{
	if( !this._parent )
		this._parent = this;

	this._parent._cardfront.style.width		= "100%";
	this._parent._cardfront.style.height	= "100%";

	this._parent._active = false;
	this._parent._cardback.style.display = 'none';
	var oAnimation = new Keen.animation( this._parent._card );
	oAnimation._parent = this._parent;
	if( !this._parent._offscreen )
		oAnimation.slidegrow( this._parent._position[0], this._parent._position[1], 154, 108, Keen.movement.smooth, 20, this._parent.__onReturn );
	else
	{
		oAnimation.slide( this._parent._position[0]-5, this._parent._position[1]-5, Keen.movement.smooth, 20, this._parent.__onReturn );
		var oAnimation = new Keen.animation( this._parent._shadow );
		oAnimation.grow( 70, 49, Keen.movement.smooth, 20 );
	}
}

Card.prototype.__onReturn = function()
{
	if( !this._parent._offscreen )
	{
		this._parent._image.src = this._parent._image.src.replace("_big","_small");
		this._parent._card.style.zIndex = 0;
	}
	else
		this._parent._shadow.style.display = 'none';
	aCards[this._parent._id-1]._center();
}

