var points = 0;

var array_of_colours = ["Green", "Gray", "Pink", "Purple"];
var current_colour;
var div1 = null;
var div2 = null;

var Gray = ["drag1", "drag2"];
var Green = ["drag4", "drag5"];
var Pink = ["drag2", "drag3"];
var Purple = ["drag3", "drag4"];

function allowDrop(ev)
{
	ev.preventDefault();
}
		
function drag(ev)
{
	ev.dataTransfer.effectAllowed='move';
	ev.dataTransfer.setData("Text",ev.target.id);
}
		
function drop(ev)
{
	ev.preventDefault();
	var data=ev.dataTransfer.getData("Text");
	ev.target.appendChild(document.getElementById(data));
	

	if(div1 == null)
	{
		div1 = data;
	}
	else
	{
		div2 = data;
		// Check for Gray
		if (current_colour == "Gray")
		{
			if((div1 == Gray[0] && div2 == Gray[1]) || (div1 == Gray[1] && div2 == Gray[0]))
			{ 	
				addscore();
				hiScore();
				reset();
			}
			else
			{
				wrong();
			}
		}
		// Check for Green
		if (current_colour == "Green")
		{
			if((div1 == Green[0] && div2 == Green[1]) || (div1 == Green[1] && div2 == Green[0]))
			{
				addscore();
				hiScore();
				reset();
			}
			else
			{
				wrong();
			}
		}
		// Check for Pink
		if (current_colour == "Pink")
		{
			if((div1 == Pink[0] && div2 == Pink[1]) || (div1 == Pink[1] && div2 == Pink[0]))
			{
				addscore();
				hiScore();
				reset();
			}
			else
			{
				wrong();
			}
		}
		// Check for Purple
		if (current_colour == "Purple")
		{	
			if((div1 == Purple[0] && div2 == Purple[1]) || (div1 == Purple[1] && div2 == Purple[0]))
			{ 
				addscore();
				hiScore();
				reset();
			}
			else
			{
				wrong();
			}
		}
	}
}

function addscore() 
{
	//lägg till ett poäng för varje rätt svar
	points++;
	var pointdiv = document.getElementById("stats");
	pointdiv.innerHTML = points;
}

function hiScore()
{
	if (points==4)
	{
		$(".endgame").toggle(200, function() 
		{
			$('.endgame').toggle(150000);
		});
		
		hide();
	}
	else
	{
		$(".good").toggle(200, function() 
		{
			$('.good').toggle(2500);
		});
	}
}

function wrong() 
{
	//återställ positioner, radera data
	div3.appendChild(document.getElementById(div1));
	div3.appendChild(document.getElementById(div2));
	this.div1 = null;
	this.div2 = null;
}
	
function reset()
{	
	//återställ positioner, radera data, slumpa fram ny färg
	div3.appendChild(document.getElementById(div1));
	div3.appendChild(document.getElementById(div2));
	this.div1 = null;
	this.div2 = null;
	show();
}
	
var show = function() 
{
	//slumpa fram ny färg
	var index = Math.floor(Math.random() *array_of_colours.length);
	current_colour = array_of_colours[index];
	array_of_colours.splice(index,1);

	console.log(current_colour)
	$("#svar").text(current_colour);
}

