
$(document).ready(function(){

	StartOver();

	//flip the flashcard on click
	$('.flashcard')
	.add('.flip')
	.click(function(){
		FlipFlashcard();
	});

	//move the flashcard from remaining to finished
	$('.knew').click(function(){
		if(remaining.length ) {
			finished.push(current);
			UpdateCount(remaining.length, finished.length);
			current = remaining.shift();
			ChangeFlashcard(current);
		} else {
			doneCard = new Array("0","You're Done!","You're Done!");
			ChangeFlashcard(doneCard);
		}
	});

	//move the flashcard to the back of finished
	$('.knew-not').click(function(){
		remaining.push(current);
		current = remaining.shift();
		ChangeFlashcard(current);
	});

	//Move all cards to the remaining pile
	$('#start-over').click(function() {
		StartOver();
	});

	//swap the front and back for all cards in remaining
	$('#swap-fb').click(function(){
		remaining.unshift(current)
		remaining = SwapSetFrontAndBack(remaining);
		current = remaining.shift();
		ChangeFlashcard(current)
	})

	//shuffle - take the current card, add it the the remaining, shuffle the deck, and pull the top card
	$('#shuffle').click(function() {
		remaining.push(current);
		remaining = ShuffleSet( remaining );
		current = remaining.shift();
		ChangeFlashcard(current);
	});


	var message = $('#message');
	if(message.length > 0) {
		message.delay(2000).slideUp(400);
	}
});

//Fisher-Yates Sorting Algorithm
function ShuffleSet(myArray) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var temp = myArray[i];
     myArray[i] = myArray[j];
     myArray[j] = temp;
   }
   return myArray;
}

function SwapFrontAndBack(card) {
	temp = card[1];
	card[1] = card[2];
	card[2] = temp;
	return card;
}

function SwapSetFrontAndBack(set) {
	for(i=0;i<set.length;i++) {
		set[i]=SwapFrontAndBack(set[i]);
	}
	return set;
}

function PopulateCardSet(){
	set = Array();
	$('.initial-cards .card').each(function(){
		id = $(this).children('.id').html();
		front = $(this).children('.front').html();
		back = $(this).children('.back').html();
		card = [id,front,back];
		set.push(card);
	})

	return set;
}

function FlipFlashcard() {
	card = $('.flashcard');
	front = card.children('.front');
	back = card.children('.back');
	if( 'block' == front.css('display')) {
		front.fadeOut(100,function(){
			back.fadeIn(100);
		});
	} else {
		back.fadeOut(100,function(){
			front.fadeIn(100);
		});
	}
	//remove "flip" button
	$('.flip-wrapper').hide();

	//fade result buttons in
	$('.result-wrapper').fadeIn(1000);

}

function ChangeFlashcard(new_card) {
	card = $('.flashcard')
	$('.result-wrapper').hide();
	$('.flip-wrapper').fadeIn(100);
	card.children('.front').hide().html(new_card[1]);
	card.children('.back').hide().html(new_card[2]);
	card.children('.front').fadeIn(100);
	updateEditCardLink(new_card[0]);
}

function StartOver() {
	remaining = PopulateCardSet();
	finished = Array();
	UpdateCount(remaining.length, finished.length);
	current = remaining.shift();
	ChangeFlashcard(current);
}

function UpdateCount(remaining, finished, fade){

	$('.remaining .number').fadeOut(100,function(){
		$(this).html(remaining).fadeIn(100);
	});
	$('.finished .number').fadeOut(100,function(){
		$(this).html(finished).fadeIn(100);
	});
}

function updateEditCardLink($card_id) {
	if( $('#sets-view .edit').size()) {
		current_link = $('#sets-view .edit-card a').attr('href');
		parts = current_link.split('/');
		trash = parts.pop();
		parts.push($card_id);
		new_link = parts.join('/');
		$('#sets-view .edit-card a').attr('href',new_link);
	}
}
