// PopCard v1.0
// (c) Copyright 2010 Gecko Tribe, LLC
// by Antone Roundy
// http://WhiteHatCrew.com/web-script-collection/

function PopCard(parentElement,isHorizontal,cardSize,maxExtent) {
	this.parentElement=parentElement;
	this.cards=new Array();
	this.isHorizontal=isHorizontal;
	this.maxExtent=maxExtent;
	this.cardSize=cardSize;
	this.hoverTimer=0;
	this.hoverTimerDelay=2000;
	this.hoverTimerAction='';
	this.hoveredItem='';
	this.hoveredCard='';
	this.cardSpacing=0;
	this.cardSpacingTight=0;
	this.popStep=2;
	this.stepDelay=20;	
	var _self=this;

	this.Init=function() {
		var i,myobj;
		if (myobj=document.getElementById(this.parentElement)) {
			for (i=0;i<myobj.childNodes.length;i++)
				if (myobj.childNodes[i].id&&myobj.childNodes[i].id.length)
					this.cards[this.cards.length]=new Array(myobj.childNodes[i].id,0,0,0); // current position, destination position, home position
			this.cardSpacing=Math.floor(this.maxExtent/this.cards.length);
			this.cardSpacingTight=Math.floor((this.maxExtent-this.cardSize)/(this.cards.length-1));
			for (i=this.cards.length-1;i>=0;i--)
				this.cards[i][1]=this.cards[i][2]=this.cards[i][3]=this.cardSpacing*i;
		} else setTimeout(function() { _self.Init(); },200);
	}
	this.Init();
	
	this.CancelHoverTimer=function() {
		if (this.hoverTimer) {
			clearTimeout(this.hoverTimer);
			this.hoverTimer=0;
		}	
	}
	
	this.SetHoverTimer=function() {
		this.CancelHoverTimer();
		if (this.hoverTimerAction.length)
			this.hoverTimer=setTimeout(function() { _self.DoHoverTimerAction(); },this.hoverTimerDelay);
	}
	
	this.DoHoverTimerAction=function() {
		this.hoverTimerAction(this);
	}
	
	this.Hover=function(cardEl) {
		var passedThisCard=0;
		this.CancelHoverTimer();
		this.SetHoverTimer();
		this.hoveredCard=cardEl.id;
		for (i=this.cards.length-1;i>=0;i--) {
			if (this.cards[i][0]==this.hoveredCard) passedThisCard=1;
			this.cards[i][2]=(this.cardSpacingTight*i)+(passedThisCard?0:(this.cardSize-this.cardSpacingTight));
		}
		this.Step();
	}
	
	this.UnHover=function() {
		var i;
		this.CancelHoverTimer();
		this.hoveredCard='';
		for (i=this.cards.length-1;i>=0;i--) this.cards[i][2]=this.cards[i][3];
		this.Step();
	}
	
	this.Step=function() {
		var i,isDone,thisIsDone,thisObj;
		isDone=1;
		for (i=this.cards.length-1;i>=0;i--) {
			thisIsDone=0;
			if (this.cards[i][1]!=this.cards[i][2])	{
				if ((this.cards[i][1]+this.popStep)<this.cards[i][2]) this.cards[i][1]+=this.popStep;
				else if ((this.cards[i][1]-this.popStep)>this.cards[i][2]) this.cards[i][1]-=this.popStep;
				else {
					this.cards[i][1]=this.cards[i][2];
					thisIsDone=1;
				}
				if (thisObj=document.getElementById(this.cards[i][0]))
					if (this.isHorizontal) thisObj.style.left=this.cards[i][1]+'px';
					else thisObj.style.top=this.cards[i][1]+'px';
				if (!thisIsDone) isDone=0;
			}
		}
		if (!isDone) setTimeout(function() { _self.Step() },this.stepDelay);
	}
}

