var sangeet = {},Utils = {},startTime,scoredMarks,totalMarks=0,isNewInstrument=false;
Utils.mobileDeviceFlag = false;
Utils.Path = '';
var toneNumber;
$(function() {
	// check whether it is Tablet or Desktop
	if(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile/i)){
 		Utils.Path=Android.getMediaPath("Sangeet");  
		Utils.mobileDeviceFlag=true;
	}
	else
		Utils.Path='';
	$(".se-pre-con").fadeOut("slow");

  	// setFontFamilyForLang("");
	$('#instrmentsChoice,#containerKeyboard, #Icons').hide();
	$('#containerKeyboard').css('height', '75%');
	$('#instrmentsChoice').css('height', '25%');
	$('.keysMain').css('height', '85%');
	$('.keysBlack').css('height', '55%');
	$("#startBtn").on('click',sangeet.playInstrument);
	$(".instrumentChoice").on('click',sangeet.selectInstrument);
	$("#myHome, #logo").on('click',sangeet.mainMenu);
	$(".playSound").on('click',sangeet.playSound);

	setTimeout(function(){
			sangeet.init();
			
		$('.check').change(function(){
			toneNumber= $(this).val(); 
			sangeet.playTone(toneNumber);           
		});
	},101);

});

sangeet.playTone=function(){
	/* var duration;
		duration=sangeet.tones[toneNumber].toneKeys[currDiv].duration;
    var myInterval = setInterval(function () {
        if (currDiv >= totDivs) {
            clearInterval(myInterval);
            return
				}
				duration=sangeet.tones[toneNumber].toneKeys[currDiv].duration;
				var id=sangeet.tones[toneNumber].toneKeys[currDiv].key;
				
				$("#keyNote"+id).trigger("click");
				currDiv++;
		}, duration); */
		var duration = 600;
		sangeet.totDivs = sangeet.tones[toneNumber].toneKeys.length;
		sangeet.currDiv = 0;
		setTimeout(myFunction, duration);

/* $(document).on("click", "a", function () {
    alert($(this).text());
}); */
}

var myFunction = function() {
	var current;
	var id=sangeet.tones[toneNumber].toneKeys[sangeet.currDiv].key;
	$("#keyNote"+id).trigger("click");

	$('.keysBlack').removeClass('keysBlackActive');
	if(id<17)
	{
		var cssId=id%7;
		//$("#keyNote"+id).addClass(sangeet.buttonPress[cssId]); 
	
		$("#keyNote"+id).addClass(sangeet.buttonPress[cssId]); 
	}
	else{
		//$("#keyNote"+id).addClass("keysBlackActive");
		$("#keyNote"+id).addClass("keysBlackActive");
	}
	counter=sangeet.tones[toneNumber].toneKeys[sangeet.currDiv].duration;
	sangeet.currDiv++;
	if (sangeet.currDiv >= 	sangeet.totDivs ) {
		$('.keysBlack').removeClass('keysBlackActive');
		return;
	}
	else{
	//addClass("keysBlackActive");
		setTimeout(myFunction, counter);			
		
	}
}

// set font family to body for selected lang. if required.
setFontFamilyForLang=function(fontFilePath){
  var newStyle = "<style>"+
            "@font-face {  "+
              "font-family: 'CustomFontFamily';"  +
                "src: url('"+fontFilePath+"') format('truetype');"+
                "}  </style>";
    $("head").append(newStyle);
    $("body").css("font-family","CustomFontFamily");            
}

sangeet.init= function(){
 	$('#wrapper').css("backgroundImage","url('"+Utils.Path+"images/MainBg.png')");
	$('#btnStart').prop("src",Utils.Path+"images/Play.png");
	$('#myLogo').prop("src",Utils.Path+"images/logo.png");
	$('#myHome').prop("src",Utils.Path+"images/homeIcon.png");
	$('#pianoImg').prop("src",Utils.Path+"images/piano.png");
	$('#guitarImg').prop("src",Utils.Path+"images/guitar.png");
	$('#fluteImg').prop("src",Utils.Path+"images/flute.png");
	$('#trumpetImg').prop("src",Utils.Path+"images/trumpet.png");
	$('#violinImg').prop("src",Utils.Path+"images/violin.png");
}

sangeet.mainMenu = function(){
	window.location.href='index.html';
}

sangeet.playInstrument = function(){
	$('#startBtn').hide();
	$('#instrmentsChoice,#containerKeyboard,#Icons').show();
	$('#wrapper').css("backgroundImage","url('"+Utils.Path+"images/bg.jpg')");
	sangeet.insName=$('.firstInst').attr("data");
	var id = $('.firstInst').prop('id');
	$('#instrumentName').html(""+sangeet.Lablels[sangeet.insName].text);
	isNewInstrument=true;
	sangeet.instrumentId=sangeet.Lablels[sangeet.insName].id;
	var d=new Date();
   	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
   
}

sangeet.selectInstrument = function(){
	sangeet.insName=$(this).attr("data");
	$('#instrumentName').html(""+sangeet.Lablels[sangeet.insName].text);
	isNewInstrument=true;
	sangeet.instrumentId=sangeet.Lablels[sangeet.insName].id;
	var d=new Date();
   	startTime=d.getDate()+"-"+(d.getMonth()+1)+"-"+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
   	
}

sangeet.playSound = function(){
	scoredMarks=0;
	if(isNewInstrument==true){
    	console.log(sangeet.instrumentId,scoredMarks,totalMarks,1,startTime);
		if(Utils.mobileDeviceFlag)
			Android.addScore(resId,sangeet.instrumentId,scoredMarks,totalMarks,1,startTime);
    	
   		isNewInstrument=false;
	}
	var id = $(this).prop('id');
	$("#myAudio").prop("src",Utils.Path+"sounds/"+sangeet.insName+"/"+id+".mp3");
	$("#myAudio").trigger("play");
};
