var cardPlacingOffset=9;
//var destinationX = 200;
//var destinationY = 200;
var imagePrefix = 'image';
//var deckSize = 22;
//var urn = new Array(deckSize);
//var cards = new Array(	'dwaas','magier','hogepriesteres','keizerin','keizer','hogepriester',
//						'geliefden','zegewagen','kracht','kluizenaar','radvanfortuin',
//						'gerechtigheid','gehangene','dood','gematigdheid','duivel',
//						'toren','ster','maan','zon','oordeel','wereld');
var planeten = new Array(	'zon', 'maan', 'mercurius', 'venus', 'mars', 'jupiter', 
							'saturnus', 'uranus', 'neptunus', 'pluto', 'noordelijkemaansknoop', 'zuidelijkemaansknoop');
var planeten_urn = new Array(planeten.length);							
var huizen = new Array('huis1', 'huis2', 'huis3', 'huis4', 'huis5', 'huis6', 'huis7', 'huis8', 'huis9', 'huis10', 'huis11', 'huis12');
var huizen_urn = new Array(huizen.length);
var tekens = new Array('ram', 'stier', 'tweelingen', 'kreeft', 'leeuw', 'maagd', 'weegschaal', 'schorpioen', 'boogschutter', 'steenbok', 'waterman', 'vissen')
var tekens_urn = new Array(tekens.length);
						
var placeHolders = new Array('verleden','heden','toekomst');						
var cardMoving = false;
var sourceX, sourceY, destinationX, destinationY;

function body_onload()
{
	document.getElementById('planeet_container').value='';
	document.getElementById('teken_container').value='';
	document.getElementById('huis_container').value='';
	for (var i = 0; i < planeten_urn.length; i++)
	{
		planeten_urn[i]=false;
	}
	for (var i = 0; i < huizen_urn.length; i++)
	{
		huizen_urn[i]=false;
	}
	for (var i = 0; i < tekens_urn.length; i++)
	{
		tekens_urn[i]=false;
	}
	
	for (var i = 0; i < planeten_urn.length; i++)
	{
		var cardId = getCardFromUrn(planeten_urn);
		addCard(document.getElementById('planeten_deck'),cardPlacingOffset + (i*14),0,i+1,cardId,'planeet',planeten[cardId]);
	}
	for (var i = 0; i < huizen_urn.length; i++)
	{
		var cardId = getCardFromUrn(huizen_urn);
		addCard(document.getElementById('huizen_deck'),cardPlacingOffset + (i*14),0,i+1,cardId,'huis',huizen[cardId]);
	}
	for (var i = 0; i < tekens_urn.length; i++)
	{
		var cardId = getCardFromUrn(tekens_urn);
		addCard(document.getElementById('tekens_deck'),cardPlacingOffset + (i*14),0,i+1,cardId,'teken',tekens[cardId]);
	}
}

function getCardFromUrn(urn)
{
	var found = false;
	while (!found)
	{
	   var value = Math.floor(Math.random() * urn.length);
	   if (!urn[value])
	   {
			urn[value]=true;
			found = true;	   	
	   }
	}
	return value;
}

function addCard(container,xpos,ypos,zindex, id,destination,card)
{
	var node = document.createElement('div');
	//node.innerHTML='<p>' + id + '</p>';
	node.style.left=xpos;
	node.style.top=ypos;
	node.style.zIndex=zindex;
	node.className='playing_card';
	node.id=destination+'_'+id;
	node.setAttribute('cardNumber',id);
	node.onclick = function() {moveCard(destination+'_'+id,destination)};
	node.onmouseover = function() {node.style.cursor='pointer'};
	node.onmouseout = function() {node.style.cursor=''};
	node.setAttribute('xposition',xpos)
	node.setAttribute('yposition',ypos)
	node.setAttribute('xvelocity',0);
	node.setAttribute('yvelocity',0);
	node.setAttribute('cardSelected','FALSE');
	var imageNode = document.createElement('img');
	imageNode.setAttribute('src','img/kaart.jpg');
	
	node.setAttribute('imageFacedDown',card);
	
	imageNode.id=imagePrefix+id;
	
	
	node.appendChild(imageNode);
	container.appendChild(node);
			
	//alert(document.getElementById('card_container').innerHTML);
	
}

function destinationSet(destination)
{
	return document.getElementById(destination+'_container').value!='';
}

function moveCard(divId,destination)
{
	
	var card = document.getElementById(divId);
	
	// placeHolderPosition>2
	if (cardMoving || destinationSet(destination) || card.getAttribute('cardSelected')!='FALSE')
	{
		return false;	
	}
	cardMoving = true;
	card.style.zIndex=50;
	
	var card = document.getElementById(divId);
	cardX = parseInt(card.getAttribute('xposition'),10);
	cardY = parseInt(card.getAttribute('yposition'),10);
	
	sourceX = getAbsoluteX(document.getElementById(divId));
	sourceY = getAbsoluteY(document.getElementById(divId));
	destinationX = cardX + getDestinationX(destination)-sourceX;
	destinationY = cardY + getDestinationY(destination)-sourceY;
			
	window.setTimeout('cardTimer(\''+divId+'\',\''+destination+'\');',5);
}

function getAbsoluteY(destination)
{
	var offset = 0;
	while( destination != null ) {
		offset += destination.offsetTop;
		destination = destination.offsetParent;
	}
	return offset;
	
}

function getAbsoluteX(destination)
{
	var offset = 0;
	while( destination != null ) {
		offset += destination.offsetLeft;
		destination = destination.offsetParent;
	}
	return offset;
	
}

function getDestinationX(destination)
{
	//var card = document.getElementById(placeHolders[placeHolderPosition]);
	var card = document.getElementById(destination);
	//alert(getAbsoluteX(card));
	return getAbsoluteX(card);
}

function getDestinationY(destination)
{
	//var card = document.getElementById(placeHolders[placeHolderPosition]);
	var card = document.getElementById(destination);
	return getAbsoluteY(card);
}

function cardTimer(divId,destination)
{
	var reached = true;
	var card = document.getElementById(divId);
	card.setAttribute('cardSelected','TRUE');
	var xpos = parseFloat(card.getAttribute('xposition'),10);
	var ypos = parseFloat(card.getAttribute('yposition'),10);
	var xvelocity = parseFloat(card.getAttribute('xvelocity'),10);
	var yvelocity = parseFloat(card.getAttribute('yvelocity'),10);

	
	if (xvelocity==0)
	{
		//destinationX = getDestinationX(destination)
		xvelocity = (destinationX-xpos) /  25;
		card.setAttribute('xvelocity',xvelocity);
	}
	if (yvelocity==0)
	{
		//destinationY = 100;//getDestinationY(destination)
		yvelocity = (destinationY-ypos) /  25;
		card.setAttribute('yvelocity',yvelocity);
//		alert(xvelocity + ' ' + yvelocity);
	}
	

	if (yvelocity < 0)
	{
		if (ypos > destinationY)
		{
			reached = false;				
			ypos += yvelocity;
		}		
		else
		{
			ypos = destinationY;			
		}
	}
	else if (yvelocity > 0)
	{
		if (ypos < destinationY)
		{
			reached = false;				
			ypos += yvelocity;
		}		
		else
		{
			ypos = destinationY;			
		}		
	}
	else
	{
		ypos = destinationY;					
	}
	card.style.top = Math.round(ypos);
	card.setAttribute('yposition',ypos);			
	if (xvelocity < 0)
	{
		if (xpos > destinationX)
		{
			reached = false;				
			xpos += xvelocity;
		}		
		else
		{
			xpos = destinationX;			
		}
	}
	else if (xvelocity > 0)

	{
		if (xpos < destinationX)
		{
			reached = false;				
			xpos += xvelocity;
		}		
		else
		{
			xpos = destinationX;			
		}		
	}
	else
	{
		xpos = destinationX;			
		
	}
	
	card.style.left = Math.round(xpos);
	card.setAttribute('xposition',xpos);			
		
	if (!reached)
	{
		window.setTimeout('cardTimer(\''+divId+'\',\''+destination+'\');',5);
	}
	else
	{

		cardImage = card.childNodes[0];
		cardImage.src=getCardSource(card.getAttribute('imageFacedDown'));
		
		var placeHolder =document.getElementById(destination+'_container');
				
		
		//var placeHolder = document.getElementById(placeHolders[placeHolderPosition]+ '_container');		
		placeHolder.value = card.getAttribute('cardNumber');
		cardMoving = false;
	}
	
}

function getCardSource(cardname)
{

	//var cardName = cards[cardId];
	
	///if (cardId.length<2)
	//	cardId = '0' + cardId;

	return 'img/'+cardname + 'kl.gif';
}

function validateForm()
{
	var carderror=false;
	if (document.getElementById('huis_container').value=='')
	{
		carderror = true;
	}
	if (document.getElementById('planeet_container').value=='')
	{
		carderror = true;
	}
	if (document.getElementById('teken_container').value=='')
	{
		carderror = true;
	}	
	if (carderror)
	{
		alert('Niet alle kaarten zijn getrokken.');
		return false;
	}
	else
	{
		return true;
	}
}
