// PopCard v1.0.1
// (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=10;
	this.stepDelay=5;
	this.stepping=0;
	this.antiJitter=0;
	this.startedInit=0;
	this.endedInit=0;
	this.checkChildCount=0;
	var _self=this;

	this.Init=function() {
		var i,myobj;
		if (myobj=document.getElementById(this.parentElement)) {
			this.startedInit=1;
			this.cards=new Array();
			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.checkChildCount=i;
			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; 
				if (myobj=document.getElementById(this.cards[i][0])) {
					if (this.isHorizontal) myobj.style.left=(i*this.cardSpacing)+'px';
					else myobj.style.top=(i*this.cardSpacing)+'px';
				}
			}
			this.endedInit=1;
		} else setTimeout(function() { _self.Init(); },200);
	}

	this.ReInit=function() {
		var myobj;
		if (this.startedInit) {
			if (!this.endedInit) setTimeout(function() { _self.ReInit(); },200);
			else if (myobj=document.getElementById(this.parentElement)) {
				if (this.checkChildCount!=myobj.childNodes.length) this.Init();
			}
		}
	}

	if (typeof document.onreadystatechange != 'undefined') document.onreadystatechange=function() { if (document.readyState=='complete') _self.ReInit(); }
	else if (typeof window.addEventListener != 'undefined') window.addEventListener('load',function() {  _self.ReInit(); },false);
	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) {
		if (this.antiJitter) {
			clearTimeout(this.antiJitter);
			this.antiJitter=0;
		}
		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));
		}
		if (!this.stepping) this.Step();
	}
	
	this.EndHover=function() {
		var i;
		this.antiJitter=0;
		this.CancelHoverTimer();
		this.hoveredCard='';
		for (i=this.cards.length-1;i>=0;i--) this.cards[i][2]=this.cards[i][3];
		if (!this.stepping) this.Step();
	}
	this.UnHover=function() {
		this.antiJitter=setTimeout(function() { _self.EndHover(); },50);
	}
	
	this.Step=function() {
		var i,isDone,thisIsDone,thisObj;
		this.stepping=1;
		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) this.stepping=0;
		else setTimeout(function() { _self.Step() },this.stepDelay);
	}
}
