var total_cards ;
var current_card = 0;
var systemAudioPlayed = false;
  	var cardArr=new Object();

$(document).ready(function(){
	loadData();

})

function loadData(){
  	 cardArr=myObj;
}


var randArr = [];

function init(){
    total_cards = cardArr.length;
   fillArray();
   createFlashCard();
   addListener();


}

function fillArray(){
	for(var i = 0;i<total_cards;i++) randArr.push(i);
}

function addListener(){
	$(".flip").click(flipFlashCard);
	$("#prevFlashCard").click(showPreviousCard);
	$("#nextFlashCard").click(showNextCard);
	$("#shuffleFlashCard").click(shuffleCards);

	$("#flip_close").click(closeFlashCard);
	$(".front_cover .f_audioBtn").click(playAudio);
  	$(".back_cover .f_audioBtn").click(playAudio1);

	$("#flashCardBtn").click(showFlashCardAct)

}

function showFlashCardAct(e){
	if( !systemAudioPlayed ){
		systemAudioPlayed = true;
		if( $("a").length > 0 ){
			$("a")[0].click();
		}
	}

	$(".flip_cont").show();
}



function createFlashCard(){

    $(".front_cover").removeClass("fc_animation");
	$(".back_cover").removeClass("fc_animation");
	$(".front_cover").removeClass("front_fc_flipped");
	$(".back_cover").removeClass("back_fc_flipped");


	$(".front_cover").find("img").attr({src:""});
	$(".front_cover").find("img").attr({src:cardArr[randArr[current_card]].imagePath});
	$(".fc_desc").html(cardArr[randArr[current_card]].description);

	setTimeout(function(){
		$(".front_cover").addClass("fc_animation");
		$(".back_cover").addClass("fc_animation");
	},100);

	if( cardArr[randArr[current_card]].audio ){
		$(".f_audioBtn").show();
	}else{
		$(".f_audioBtn").hide();
	}

	setNavBtns();
}

function showPreviousCard(e){
	if( current_card > 0 ){
		current_card--;
		createFlashCard();
	}
}

function showNextCard(e){
	if( current_card < (total_cards-1) ){
		current_card++;
		createFlashCard();
	}
}

function flipFlashCard(){
	if( $(".front_cover").hasClass("front_fc_flipped") ){
		$(".front_cover").removeClass("front_fc_flipped")
		 $(".back_cover").removeClass("back_fc_flipped")
	}else{
	     console.log("add class");
		 $(".front_cover").addClass("front_fc_flipped")
		 $(".back_cover").addClass("back_fc_flipped")
	}
}

function setNavBtns(){
	if( current_card == 0 ){
		$("#prevFlashCard").addClass("disableButton");
		$("#nextFlashCard").removeClass("disableButton");
	}else if( current_card >= (total_cards-1)){
		$("#nextFlashCard").addClass("disableButton");
		$("#prevFlashCard").removeClass("disableButton");
	}else{
		$("#prevFlashCard").removeClass("disableButton");
		$("#nextFlashCard").removeClass("disableButton");
	}
}

function shuffleCards(){
	//randArr = randArr.sort(function(){ return Math.random()*5 })
	randArr = shuffleArray(randArr);
	current_card = 0;
	createFlashCard();
}

function closeFlashCard(e){
	$(".flip_cont").hide();
}

function shuffleArray(arr){
	var temp = [];
	var arrLen = arr.length;
	for(var i = 0;i<arrLen;i++){
		var rand = Math.round( Math.random() * (arr.length - 1) );
		temp.push(arr[rand]);
		arr.splice(rand,1)
	}
	console.log(temp);
	return temp;
}

function playAudio(e){
	var audioUrl = cardArr[randArr[current_card]].audio;
	var _audio = new Audio();
	_audio.src = audioUrl;
	_audio.play();
}
function playAudio1(e){
	var audioUrl = cardArr[randArr[current_card]].audio1;
	var _audio = new Audio();
	_audio.src = audioUrl;
	_audio.play();
}


$(document).ready(init);
