/***********************
* 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.bindElementAction(compId, symbolName, "document", "compositionReady", function(sym, e) {
         // insert code to be run when the composition is fully loaded here
         
         window.alertMessage   = function(){
         		 alertify.alert("เกิดข้อผิดพลาด","ยังทำแบบฝึกหัดไม่ครบทุกข้อเลย").setting({transition:'fade'}).show(); 
         	}
         
         var j = 1;
         var ship = sym.$("ship");
         var detail = "";
         var table = sym.getSymbol("sym_table");
         
         var boolf = false;
         var tempFlag="";
         var tempNumFlag;
         
         var tempArray =[];
         
         var countryText="";
         var nightText="";
         var dayText="";
         
         var firstTime = false;
         
         for(var i=1;i<=8;i++){
         		sym.$("btn_"+i).bind("click",function(){
         			 var posX = sym.$(this).position().left;
         			 var posY = sym.$(this).position().top;
         			 var res = this.id.substring(10);
         			 moveShip(posX,posY,this.id);
         		});
         }
         
         function moveShip(getX,getY,getI){
         
         
         
         
         		var res = getI.substring(10)
         		var numFlag = parseInt(res);
               var flag = sym.$("c"+numFlag);
         		console.log(numFlag);
         		sym.$("btn_"+numFlag).hide();
         		sym.$("btn_"+tempNumFlag).show();
         
         
         		if(numFlag == 1){
         				TweenMax.to(ship,1.2,{css:{x:getX+20,y:getY+80},ease:Quad.easeOut});
         		}
         		if(numFlag == 2){
         				TweenMax.to(ship,1.2,{css:{x:getX+10,y:getY+90},ease:Quad.easeOut});
         		}
         		if(numFlag == 3){
         				TweenMax.to(ship,1.2,{css:{x:getX-10,y:getY+100},ease:Quad.easeOut});
         		}
         		if(numFlag == 4){
         				TweenMax.to(ship,1.2,{css:{x:getX+20,y:getY+80},ease:Quad.easeOut});
         		}
         		if(numFlag == 5){
         				TweenMax.to(ship,1.2,{css:{x:getX-15,y:getY+100},ease:Quad.easeOut});
         		}
         		if(numFlag == 6){
         				TweenMax.to(ship,1.2,{css:{x:getX-20,y:getY+100},ease:Quad.easeOut});
         		}
         		if(numFlag == 7){
         				TweenMax.to(ship,1.2,{css:{x:getX+10,y:getY+100},ease:Quad.easeOut});
         		}
         		if(numFlag == 8){
         				TweenMax.to(ship,1.2,{css:{x:getX-30,y:getY},ease:Quad.easeOut});
         		}
         
         
         			if(tempFlag!=flag){
         					TweenMax.to(flag,.8,{css:{scaleX:"1%",scaleY:"1%"},ease:Back.easeOut});
         					TweenMax.to(tempFlag,1.2,{css:{scaleX:"0%",scaleY:"0%"},ease:Quad.easeOut});
         					tempFlag = flag;
         			}		
         
         			tempNumFlag = numFlag;
         		sym.$("c"+numFlag).bind("click",function(){
         				TweenMax.to(this,.5,{css:{scaleX:"0%",scaleY:"0%"},ease:Quad.easeOut});
         		});
         		switch(numFlag){
         			case 1: countryText="สหรัฐอเมริกา";dayText="3";nightText="11";
         			break;
         			case 2: countryText="สหรัฐอเมริกา";dayText="4";nightText="-2";
         			break;
         			case 3: countryText="สหรัฐอเมริกา";dayText="8";nightText="11";
         			break;
         			case 4: countryText="บราซิล";dayText="17";nightText="28";
         			break;
         			case 5: countryText="ไนเจอร์";dayText="9";nightText="28";
         			break;
         			case 6: countryText="เกาะ/มัลดีฟ";dayText="22";nightText="28";
         			break;
         			case 7: countryText="จีน";dayText="4";nightText="11";
         			break;
         			case 8: countryText="จีน";dayText="1";nightText="-2";
         			break;
         
         		}
         
         		parseText(res);
         }
         
         
         function parseText(idc){
         	var bool = arraysEqual(idc);
         	var atList = 'text'+j;
         	var contry = 'con'+j;
         	var day = 'day'+j;
         	var night = 'night'+j;
         
         	if(bool){
         		console.log("true");
         		console.log("tempA = "+tempArray);
         		tempArray.push(idc);
         		if(j<=5){
         			table.$(atList).html(j);
         			table.$(contry).html(countryText);
         			table.$(day).html(dayText);
         			table.$(night).html(nightText);
         		}
         		j++;
         
         	}else{
         	   console.log("false");
         	   console.log("not record");
         	}
         
         	if(!firstTime){
         		setTimeout(function(){showTable();},1000);
         		firstTime = true;
         	} 
         
         }
         
         function arraysEqual(a) {
           if(tempArray.length>4) return false;
         
           for (var i = 0; i < tempArray.length; ++i) {
             if (a == tempArray[i]) return false;
           }
           return true;
         }
         function showTable(){
         
         	var element = sym.$("sym_table");
         	var foo = sym.getVariable("showed");
         	var stageWidth = sym.$("Stage").width();
         	var tableWidth = element.width();
         	sym.setVariable("showed","false");
         	if(foo == "true"){
         		TweenMax.to(element,1,{css:{x:stageWidth-62},ease:Quad.easeOut});
         		sym.setVariable("showed","false");
         	}else{
         		TweenMax.to(element,1,{css:{x:stageWidth-tableWidth},ease:Quad.easeOut});
         		sym.setVariable("showed","true");
         	}
         }
         
         var labelArray = ["s2","s3","s4"];
         var labelIndex = 0;
         sym.$('btn_next').bind("click",function(){
         	TweenMax.to(ship,2,{css:{x:-200,y:-200},ease:Quad.easeOut});
         	TweenMax.to(tempFlag,1.2,{css:{scaleX:"0%",scaleY:"0%"},ease:Quad.easeOut});
         	if(labelIndex <3){
         		sym.stop(labelArray[labelIndex]);
         		labelIndex++;
         	}
         	sym.getSymbol('mascotMove').stop(0);
         	sym.getSymbol('mascotMove').getSymbol("balloonText").stop(0);
         	var foo = sym.getVariable("showed");
         	if(foo == "true")showTable();
         });
         sym.$('sym_table').bind("mousedown",function(){
         	 showTable();
         });
         
         
         

      });
      //Edge binding end

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

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 250, function(sym, e) {
         
         //input massage
         var answer = sym.$("answer");
         inputMessage = $('<textarea />').attr({'type':'textarea','rows':'3','value':'', 'id':'message'});
         inputMessage.css ('line-height', 2.3);
         inputMessage.css ('font-size', 18);
         inputMessage.css ('color', '#ffffff');
         inputMessage.css ('resize', 'none');
         inputMessage.css ('background-color', 'transparent');
         inputMessage.css ('border', 0);
         inputMessage.css ('box-shadow', 'transparent');
         inputMessage.css ('width', 371);
         inputMessage.css ('height', 112);
         inputMessage.appendTo(answer);
         
         //check Choice
         var tempBtn = "test";
         var boolf = false;
         var chooseCh =0; 
         var activeChoice =0;
         function enable(btncurr){
         	sym.getSymbol(btncurr).stop("out");
         
         	sym.$(btncurr).bind("click", function(){
         			disable(btncurr,this.id);
         	});
         
         	//console.log("btncurr = "+btncurr);
         }
         
         function disable(btnch,getID) {   
         					sym.getSymbol(btnch).stop("over");
                   		sym.$(btnch).unbind("click");
         					var res = getID.substring(13);
         					if(res >3 && res <6){
         						activeChoice=3;
         					}
         					if(res >5){
         						activeChoice=4;
         					}
         					chooseCh = res;
         					console.log(res);
         					if(boolf){
         							if(tempBtn!=btnch){
         									//console.log("true");
         									enable(tempBtn);
         							}else{
         									//console.log("false");
         
         							}
         					}else{
         					   boolf = true;
         
         					}
         
                   		tempBtn = btnch;
                   		//console.log("tempBtn = " + tempBtn);
               }
         
         for(var s=1;s<=7;s++){
         		sym.$("btn_q1_"+s).bind("click",function(){
         			console.log("clicked");
         			disable(this,this.id);
         		});
         }
         
         function checkAns1(){
         	if( !boolf){
         		return false;
         	}else{
         		return true
         	}
         }
         
         function checkAns2(){
         	var contentText = message.value; 
         	if(contentText == ''){
         		return false;
         	}else{
         		return true
         	}
         }
         function checkAns3(){
         	if( !boolf){
         		return false;
         	}else{
         		return true
         	}
         }
         
         sym.$('checkAns1').bind("click",function(){
         	var ifdone = checkAns1();
         	if(ifdone){
         	if(chooseCh==3){
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text1");
         	}else{
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text2");
         	}
         
         	}else{
         		alertMessage();
         	}
         });
         sym.$('checkAns2').bind("click",function(){
         	var ifdone = checkAns2();
         	if(ifdone){
         
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text3");
         
         	}else{
         		alertify.alert("เกิดข้อผิดพลาด","กรุณาเติมคำลงในช่องว่าง").setting({transition:'fade'}).show(); 
         	}
         });
         
         sym.$('checkAns3').bind("click",function(){
         
         	if(activeChoice ==3 ){
         
         	if(chooseCh==4){
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text1");
         	}else{
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text2");
         	}
         
         	}else{
         		alertMessage();
         	}
         });
         
         sym.$('checkAns4').bind("click",function(){
         
         	if(activeChoice ==4 ){
         
         	if(chooseCh==6){
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text4");
         	}else{
         		sym.getSymbol('mascotMove').play("move");
         		sym.getSymbol('mascotMove').getSymbol("balloonText").stop("text2");
         	}
         
         	}else{
         		alertMessage();
         	}
         });

      });
      //Edge binding end

      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");
           }
         });

      });
      //Edge binding end

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

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

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

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

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

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

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

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

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

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

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

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