/***********************
* Adobe Edge Animate Composition Actions
*
* Edit this file with caution, being careful to preserve 
* function signatures and comments starting with 'Edge' to maintain the 
* ability to interact with these actions from within Adobe Edge Animate
*
***********************/
(function($, Edge, compId){
var Composition = Edge.Composition, Symbol = Edge.Symbol; // aliases for commonly used Edge classes

   //Edge symbol: 'stage'
   (function(symbolName) {
      
      
      Symbol.bindSymbolAction(compId, symbolName, "creationComplete", function(sym, e) {
         yepnope({
             load: [
         		"js/edgedock/js/min/EdgeCommons.js",
         		"js/alertifyjs/alertify.min.js",
         		"js/TweenMax.min.js",
         		"js/energize.js",
         		"js/alertifyjs/css/alertify.min.css",
         		"js/alertifyjs/css/themes/default.min.css",
         		"js/edgedock/css/style.css"
         	],
         	complete: function () {
         
              console.log("allscript complete");
              sym.play();
           }
         });

      });
      //Edge binding end

      Symbol.bindElementAction(compId, symbolName, "document", "compositionReady", function(sym, e) {
function widthWindow(){
	nowWidth = $(document).width();
	return nowWidth;
}    
sym.$("solutionBtn").hide();
         var tempmenu = "test";
         var boolf = false;
         var score = 0;
         var checked = false;
         var test1Done = false;
         var test2Done = false;
         var test3Done = false;
         var tempScore = 0;
         sym.getSymbol("testcon").$("group2").hide();
			sym.getSymbol("testcon").$("group3").hide();

         function enable(btncurr){
         	sym.getSymbol(btncurr).stop("out");
         	sym.$(btncurr).bind("touchend click", function(){
         								  disable(btncurr);
         			if(btncurr == "btn_ch1"){
         					//sym.getSymbol('testcon').stop("test1");
         					checkgroup(1);
         					sym.getSymbol("testcon").$("group1").show();
         					sym.getSymbol("testcon").$("group2").hide();
         					sym.getSymbol("testcon").$("group3").hide();

         			}
         			if(btncurr == "btn_ch2"){
         					//sym.getSymbol('testcon').stop("test2");
         					checkgroup(2);
         					sym.getSymbol("testcon").$("group2").show();
         					sym.getSymbol("testcon").$("group1").hide();
         					sym.getSymbol("testcon").$("group3").hide();

         			}
         			if(btncurr == "btn_ch3"){
         					//sym.getSymbol('testcon').stop("test3");
         					checkgroup(3);
         					sym.getSymbol("testcon").$("group3").show();
         					sym.getSymbol("testcon").$("group1").hide();
         					sym.getSymbol("testcon").$("group2").hide();
         			}
         	});
         }

         function disable(btnch) {   
         					sym.getSymbol(btnch).stop("over");
                   		sym.$(btnch).unbind("touchend click");

         					if(boolf){

         							if(tempmenu!=btnch){
         									//console.log("true");
         									enable(tempmenu);

         							}else{
         									//console.log("false");

         									//tempBtn = btnch;
         							}
         					}else{
         					   boolf = true;

         					}

                   		tempmenu = btnch;
                   		//console.log("tempBtn = " + tempBtn);
               }





         sym.$('btn_ch1').bind("touchend click", function(){
         		  disable("btn_ch1");
         		  sym.getSymbol("testcon").$("group1").show();
						sym.getSymbol("testcon").$("group2").hide();
						sym.getSymbol("testcon").$("group3").hide();
         		  checkgroup(1);
           });

         sym.$('btn_ch2').bind("touchend click", function(){

         		 disable("btn_ch2");
         		  sym.getSymbol("testcon").$("group2").show();
						sym.getSymbol("testcon").$("group1").hide();
						sym.getSymbol("testcon").$("group3").hide();
         		 checkgroup(2);

           });

         sym.$('btn_ch3').bind("touchend click", function(){
         		  disable("btn_ch3");
         		  checkgroup(3);
         		  sym.getSymbol("testcon").$("group3").show();
         			sym.getSymbol("testcon").$("group1").hide();
         			sym.getSymbol("testcon").$("group2").hide();

           });


         disable("btn_ch1");

          // Test codeing  ##########################################

         var checkGroup1 = false;
         var checkGroup2 = false;
         var checkGroup3 = false;

         // Group2

         // Group3

         function checkgroup(forg){
            switch(forg){
             case 1:
         			 if(!checkGroup1){
         			 		checkGroup1 = true;
         					goG1();
         			 }

             		break;
             case 2:
         			 if(!checkGroup2){
 							checkGroup2 = true;
 							goG2();
         			 }

             		break;
             case 3:
         			 if(!checkGroup3){
							checkGroup3 = true;
							 goG3();
         			} 
             		break;
            }
         }


         checkgroup(1);

			function updateScore(getScore){
         			var text = getScore +" คะแนน";
         			sym.getSymbol("mascotMove").getSymbol("balloonScore").$("scoreText").html(text);
         	}
			function alertMessage(){
				 alertify.alert("เกิดข้อผิดพลาด","กรุณาเลือกชุดข้อความทางซ้ายก่อน").setting({transition:'fade'}).show(); 
			}
         // GO1 ##############################################################################################################################
         function goG1(){
			// Group1
         	var menuArray = ["menuL1","menuL2","menuL3","menuL4","menuL5","menuL6","menuL7","menuL8"];
         	var abcArray = ["0","0","0","0","v1","v2","v3","v4"];
         	var groupA = false;
         	var testBox = sym.getSymbol("testcon").getSymbol("group1");
         	var tempID;
         	var tempBtn;
         	var tempValue;
         	var tempA;
         	var tempB;
         	var chooseA= false;
         	var chooseB= false;
				var allChoice = 0;

         	function checkWorking(boolA){
         		if(boolA) {return false;}
         		else{return true;}
         	}

         	function chooseProgress(getID){

         		var res = getID.substring(26);
         		var resName =getID.substring(21);
         		var intRes = parseInt(res);
         		var checkState = checkWorking(groupA);
         		var indexArr= intRes-1;
         		//console.log(res);
         		//console.log(resName);
         		if(intRes <=4){
         			//Left Choose
         			//console.log("choose left ...");
         			if(!groupA || intRes<=4){
         				if(intRes ==tempID){
         						//console.log("same id");
         						testBox.getSymbol(menuArray[indexArr]).stop("normal");
         						groupA= false;
         						tempID = 0;
         						chooseA= false;
         						tempA = "";
         				}else if(intRes != tempID && !groupA){
         						//console.log("plz choose right");
         						testBox.getSymbol(menuArray[indexArr]).stop("inac");
         						groupA = true;
         						chooseA= true;
         						tempID = intRes;
         						tempA = intRes;
         				}
         			}


         		}
         		if(intRes >4){
         			//Right Choose
         			if(!groupA){
         				//console.log("choose right ...");
         				alertMessage();
         				chooseB= false;
         			}else{
         				testBox.getSymbol(resName).stop("inac");
         				chooseB= true;
         				tempB = intRes;
         				bindMenu(chooseA,chooseB,tempA,tempB);
         				groupA= false;
         			}
         		}
         	}

         	function bindMenu(valueA,valueB,getA,getB){
         		var indexArrA= getA-1;
         		var indexArrB= getB-1;

         		var posMenuX = testBox.$(menuArray[indexArrA]).position().left-16;
         		var posMenuY = testBox.$(menuArray[indexArrA]).position().top+8;
         		var instanceABC = testBox.$(abcArray[indexArrB]);

					if(allChoice<3){
         				allChoice+=1;
         				console.log(allChoice);
         			}else{
							test1Done = true;
							console.log("test 1 is done");
         			}

         		if(valueA && valueB){
         			chooseA= false;
         			chooseB= false;
         			testBox.$(menuArray[indexArrA]).unbind("click");
         			testBox.$(menuArray[indexArrB]).unbind("click");
         			TweenMax.to(instanceABC,.8,{css:{x:posMenuX,y:posMenuY},ease:Quad.easeOut});

         			if(getA ==1 && getB==7 || getA ==2 && getB==8 ||getA ==3 && getB==6 ||getA ==4 && getB==5){
         				score += 1;
         				updateScore(score);
         			}else{
         			  console.log("wrong");
         			}




         		}else{
         			console.log("notsuccess");
         		}          

         	}


         	for(var i = 0; i<menuArray.length; i++){
         		testBox.$(menuArray[i]).bind("click",function(){
         				chooseProgress(this.id);
         		});

         	}
         }


         // GO2 ##############################################################################################################################
			function goG2(){

			var menuArray = ["choice_2_1","choice_2_2","choice_2_3","choice_2_4","choice_2_5","choice_2_6"];
         	var groupB = false;
         	var testBox = sym.getSymbol("testcon").getSymbol("group2");
         	var tempID;
         	var tempBtn;
         	var tempValue;
         	var tempA;
         	var tempB;
         	var chooseA= false;
         	var chooseB= false;
				var allChoice =0;
				var indexArrTemp;

         	function chooseProgress(getID){
         		var res = getID.substring(30);
         		var resName =getID.substring(21);
         		var intRes = parseInt(res);
         		var indexArr= intRes-1;

         		//console.log(res);
         		//console.log(resName);

         		if(intRes <=4){
							if(intRes ==tempID){
											//console.log("same id");
											testBox.getSymbol(menuArray[indexArr]).stop("normal");
											groupB= false;
											tempID = 0;
											chooseA= false;
											tempA = "";
							}else if(intRes != tempID && !groupB){
											//console.log("choose the bottom box");
											testBox.getSymbol(menuArray[indexArr]).stop("inac");
											groupB = true;
											chooseA= true;
											tempID = intRes;
											tempA = intRes;
							}
         		}

         		if(intRes >4){
         			//Box click
         			if(!groupB){
         				//alert("กรุณาเลือกข้อความทางซ้ายมือ");
         				chooseB= false;
         			}else{
         				//testBox.getSymbol(resName).stop("inac");
         				chooseB= true;
         				tempB = intRes;
         				bindMenu(chooseA,chooseB,tempA,tempB);
         				groupB= false;
         			}
         		}

         	}

				var marginX1 = 0;
				var marginX2 = 0;
         	function bindMenu(valueA,valueB,getA,getB){
         		var indexArrA= getA-1;
         		var indexArrB= getB-1;

         		var posMenuX = testBox.$(menuArray[indexArrB]).position().left;
         		var posMenuY = testBox.$(menuArray[indexArrB]).position().top;
					var distantX = testBox.$(menuArray[indexArrA]).width();

					var maginBox1 = posMenuX+marginX1;
					var maginBox2 = posMenuX+marginX2;

					if(allChoice<2){
         				allChoice+=1;
         				console.log(allChoice);
         			}else{
							test2Done = true;
							console.log("test 2 is done");
         			}

         		if(valueA && valueB){
         			chooseA= false;
         			chooseB= false;
         			testBox.$(menuArray[indexArrA]).unbind("click");
						var widthWin = widthWindow();

 						if(indexArrB == 4){
 							if(widthWin <=1024){
 								TweenMax.to(testBox.$(menuArray[indexArrA]),.8,{css:{x:maginBox1,y:posMenuY+10},ease:Quad.easeOut});
 							}else{
 								TweenMax.to(testBox.$(menuArray[indexArrA]),.8,{css:{x:maginBox1,y:posMenuY},ease:Quad.easeOut});
 							}

 							marginX1 += distantX;
 						}
 						if(indexArrB == 5){
							if(widthWin <1100 ){
									TweenMax.to(testBox.$(menuArray[indexArrA]),.8,{css:{x:maginBox2,y:posMenuY+17},ease:Quad.easeOut});
								}else{
									TweenMax.to(testBox.$(menuArray[indexArrA]),.8,{css:{x:maginBox2,y:posMenuY},ease:Quad.easeOut});
								}

 							marginX2 += distantX;
 						}

         			if(getA ==4 && getB==6){
         				score += 1;
         				updateScore(score);
         			}else if(getA ==1 && getB==5 || getA ==2 && getB==5){
         			  score += 0.5;
         				updateScore(score);
         			}else{
         				console.log("wrong");
         			}

         		}else{
         			console.log("notsuccess");
         		}



         	}

         	for(var i = 0; i<menuArray.length; i++){
         		testBox.$(menuArray[i]).bind("click",function(){
         				chooseProgress(this.id);
         		});

         	}
         }
         // GO3 ##############################################################################################################################

			function goG3(){
				var choiceArr = {choice1:"",choice2:"",choice3:"",choice4:""}
				var testBox = sym.getSymbol("testcon").getSymbol("group3");
				var menuArray = ["ic_1","ic_2","ic_3","ic_4","ic_5","ic_6","ic_7","ic_8"];
				var s1 = 0;
				var s2 = 0;
				var s3 = 0;
				var s4 = 0;


				function chooseProgress(getID){
         		var res = getID.substring(24);
         		var resName =getID.substring(21);
         		var intRes = parseInt(res);
         		var indexArr= intRes-1;

         		if(intRes < 3){
         			switch (intRes){
         			case 1: choiceArr.choice1 = "non";
         			s1 = 0; 
         			testBox.getSymbol(menuArray[0]).stop("active");testBox.getSymbol(menuArray[1]).stop("normal");
         			break;
         			case 2: choiceArr.choice1 = "yep";
         			s1 = 1;
         			testBox.getSymbol(menuArray[0]).stop("normal");testBox.getSymbol(menuArray[1]).stop("active");
         			break;
         			}
         		}else if(intRes>2 && intRes<=4){
         			switch (intRes){
         			case 3:choiceArr.choice2 = "non";
         			s2 = 0;
         			testBox.getSymbol(menuArray[2]).stop("active");testBox.getSymbol(menuArray[3]).stop("normal");
         			break;
         			case 4:choiceArr.choice2 = "yep";
         			s2 = 1;
         			testBox.getSymbol(menuArray[2]).stop("normal");testBox.getSymbol(menuArray[3]).stop("active");
         			break;
         			}
         		}else if(intRes>4 && intRes<=6){
         			switch (intRes){
         			case 5:choiceArr.choice3 = "non";
         			s3 = 0;
         			testBox.getSymbol(menuArray[4]).stop("active");testBox.getSymbol(menuArray[5]).stop("normal");
         			break;
         			case 6:choiceArr.choice3 = "yep";
         			s3 = 1;
         			testBox.getSymbol(menuArray[4]).stop("normal");testBox.getSymbol(menuArray[5]).stop("active");
         			break;
         			}
         		}else if(intRes>6 && intRes<=8){
         			switch (intRes){
         			case 7:choiceArr.choice4 = "yep";
         			s4 = 1;
         			testBox.getSymbol(menuArray[6]).stop("active");testBox.getSymbol(menuArray[7]).stop("normal");
         			break;
         			case 8:choiceArr.choice4 = "non";
         			s4 = 0;
         			testBox.getSymbol(menuArray[6]).stop("normal");testBox.getSymbol(menuArray[7]).stop("active");
         			break;
         			}
         		}
						if(choiceArr.choice1 != "" && choiceArr.choice2 != "" && choiceArr.choice3 != "" && choiceArr.choice4 != ""){
								test3Done = true;
								tempScore = s1+s2+s3+s4;
								console.log("test 4 is done");
						}
         		console.log(choiceArr.choice1+"\n"+choiceArr.choice2+"\n"+choiceArr.choice3+"\n"+choiceArr.choice4+"\n");
         	}



				for(var i = 0; i<menuArray.length; i++){
         		testBox.$(menuArray[i]).bind("click",function(){
         				chooseProgress(this.id);
         		});

         	}
			}



sym.$('btnCheck').bind("click",function(){
	var testBox = sym.getSymbol("testcon").getSymbol("group3");
	var menuArray = ["ic_1","ic_2","ic_3","ic_4","ic_5","ic_6","ic_7","ic_8"];


		if(test1Done == true && test2Done == true && test3Done == true){
			sym.getSymbol("mascotMove").play("answer");
			sym.$("solutionBtn").fadeIn();
						sym.$("solutionBtn").bind("click",function(){
								var config = {
									 width: 498,
									 height: 511,
									 type: "image",
									 source: "images/solution/as1.jpg"
								};		
								EC.Spotlight.open(config);
						});


			if(!checked){
					score += tempScore;
					updateScore(score);
					checked= true;
			}

			for(var i = 0; i<menuArray.length; i++){
         		testBox.$(menuArray[i]).unbind("click");
         	}

		}else{
			console.log("can't check");
			alertify.alert("เกิดข้อผิดพลาด","ยังทำแบบฝึกหัดไม่ครบทุกข้อเลย").setting({transition:'fade'}).show();
		}
});




if (navigator.userAgent.match(/webOS/i) ||
    navigator.userAgent.match(/iPhone/i) ||
    navigator.userAgent.match(/iPad/i) ||
    navigator.userAgent.match(/iPod/i) ||
    navigator.userAgent.match(/BlackBerry/) || 
    navigator.userAgent.match(/Windows Phone/i) || 
    navigator.userAgent.match(/ZuneWP7/i)
    ) {
             console.log("tablle or mobile");
             sym.$("btn_howto").bind("click",function(){
             	document.location.href = "ipst://vid=1";
					var config = {
						 width: 640,
						 height: 480,
						 type: "animate",
						 source: "pop_1_9.html"
					};		
					EC.Spotlight.open(config);

				});
      }else if(navigator.userAgent.match(/Android/i)){
      console.log("Android Divice");


      }else{
      		console.log("Desktop PC, MAC");
      		 sym.$("btn_howto").bind("click",function(){
      		 console.log(this.id);
      		 document.location.href = "ipst://vid=1";
					var config = {
						 width: 640,
						 height: 480,
						 type: "animate",
						 source: "pop_1_9.html"
					};		
					EC.Spotlight.open(config);

				});
      }


      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         sym.getSymbol("btn_ch1").stop("over");

      });
      //Edge binding end

      

   })("stage");
   //Edge symbol end:'stage'

   //=========================================================
   
   //Edge symbol: 'mascot'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 2000, function(sym, e) {
         sym.playReverse();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         this.play();

      });
      //Edge binding end

   })("mascot");
   //Edge symbol end:'mascot'

   //=========================================================
   
   //Edge symbol: 'btnchk'
   (function(symbolName) {   
   
   })("btnchk");
   //Edge symbol end:'btnchk'

   //=========================================================
   
   //Edge symbol: 'btn_ch1'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 250, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

   })("btn_ch1");
   //Edge symbol end:'btn_ch1'

   //=========================================================
   
   //Edge symbol: 'btn_ch2'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

   })("btn_ch2");
   //Edge symbol end:'btn_ch2'

   //=========================================================
   
   //Edge symbol: 'btn_ch3'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

   })("btn_ch3");
   //Edge symbol end:'btn_ch3'

   //=========================================================
   
   //Edge symbol: 'testcon'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         

      });
      //Edge binding end

      

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 701, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 1500, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 2500, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

   })("testcon");
   //Edge symbol end:'testcon'

   //=========================================================
   
   //Edge symbol: 'previewtest'
   (function(symbolName) {   
   
   })("previewtest");
   //Edge symbol end:'previewtest'

   //=========================================================
   
   //Edge symbol: 'inac1'
   (function(symbolName) {   
   
   })("inac1");
   //Edge symbol end:'inac1'

   //=========================================================
   
   //Edge symbol: 'Symbol_1'
   (function(symbolName) {   
   
   })("Symbol_1");
   //Edge symbol end:'Symbol_1'

   //=========================================================
   
   //Edge symbol: 'inac3'
   (function(symbolName) {   
   
   })("inac3");
   //Edge symbol end:'inac3'

   //=========================================================
   
   //Edge symbol: 'inac4'
   (function(symbolName) {   
   
   })("inac4");
   //Edge symbol end:'inac4'

   //=========================================================
   
   //Edge symbol: 'inac5'
   (function(symbolName) {   
   
   })("inac5");
   //Edge symbol end:'inac5'

   //=========================================================
   
   //Edge symbol: 'inac6'
   (function(symbolName) {   
   
   })("inac6");
   //Edge symbol end:'inac6'

   //=========================================================
   
   //Edge symbol: 'inac7'
   (function(symbolName) {   
   
   })("inac7");
   //Edge symbol end:'inac7'

   //=========================================================
   
   //Edge symbol: 'inac8'
   (function(symbolName) {   
   
   })("inac8");
   //Edge symbol end:'inac8'

   //=========================================================
   
   //Edge symbol: 'balloonScore'
   (function(symbolName) {   
   
   })("balloonScore");
   //Edge symbol end:'balloonScore'

   //=========================================================
   
   //Edge symbol: 'group1'
   (function(symbolName) {   
   
   })("group1");
   //Edge symbol end:'group1'

   //=========================================================
   
   //Edge symbol: 'group2'
   (function(symbolName) {   
   
   })("group2");
   //Edge symbol end:'group2'

   //=========================================================
   
   //Edge symbol: 'choice_2_1'
   (function(symbolName) {   
   
   })("choice_2_1");
   //Edge symbol end:'choice_2_1'

   //=========================================================
   
   //Edge symbol: 'choice_2_2'
   (function(symbolName) {   
   
   })("choice_2_2");
   //Edge symbol end:'choice_2_2'

   //=========================================================
   
   //Edge symbol: 'choice_2_3'
   (function(symbolName) {   
   
   })("choice_2_3");
   //Edge symbol end:'choice_2_3'

   //=========================================================
   
   //Edge symbol: 'choice_2_4'
   (function(symbolName) {   
   
   })("choice_2_4");
   //Edge symbol end:'choice_2_4'

   //=========================================================
   
   //Edge symbol: 'group3'
   (function(symbolName) {   
   
   })("group3");
   //Edge symbol end:'group3'

   //=========================================================
   
   //Edge symbol: 'ic_yes_1'
   (function(symbolName) {   
   
   })("ic_yes_1");
   //Edge symbol end:'ic_yes_1'

   //=========================================================
   
   //Edge symbol: 'ic_no_1'
   (function(symbolName) {   
   
   })("ic_no_1");
   //Edge symbol end:'ic_no_1'

   //=========================================================
   
   //Edge symbol: 'mascotMove'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 0, function(sym, e) {
         sym.stop();

      });
      //Edge binding end

   })("mascotMove");
   //Edge symbol end:'mascotMove'

   //=========================================================
   
   //Edge symbol: 'btn_howto'
   (function(symbolName) {   
   
   })("btn_howto");
   //Edge symbol end:'btn_howto'

})(jQuery, AdobeEdge, "EDGE-168487978");