/***********************
* 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) {
         
         var answer = sym.$("answer");
         inputMessage = $('<input />').attr({'type':'text','rows':'3','value':'', 'id':'message'});
         inputMessage.css ('line-height', 1);
         inputMessage.css ('font-size', 15);
         inputMessage.css ('color', '#fffc02');
         inputMessage.css ('resize', 'none');
         inputMessage.css ('background-color', 'transparent');
         inputMessage.css ('border', 0);
         inputMessage.css ('box-shadow', 'transparent');
         inputMessage.css ('width', 60);
         inputMessage.css ('text-align','center');
         inputMessage.css ('height', 20);
         inputMessage.appendTo(answer);
         
         var btn = sym.$("btnaction"); 
         var btnsym = sym.getSymbol("btnaction"); 
         //supbtn  -- put -- mix
         var buck = sym.$("bucket");
         var bucksym = sym.getSymbol("bucket");
         /*
         	cone_animate --animate
         	waterspoil
         	beaker  -- a0 -> a4
         	single_ice  - animate
         */
         var table = sym.getSymbol("sym_table");
         var checkaction = false;
         var j = 1;
         var tempArray =[];
         var firstTime =false;
         var countryText="";
         var nightText="";
         var dayText="";
         var d = 0;
         var canclickBtn = true;
         btn.bind("click",function(){
         	if(canclickBtn){
         	if(!checkaction){
         			if(d<5){
         			canclickBtn = false;
         			bucksym.getSymbol("single_ice").play("animate");
         			myVar=setTimeout(function(){
         				d++;
         				console.log(j);
         				bucksym.getSymbol("beaker").stop("a1");
         				btnsym.play(0);
         				btnsym.getSymbol("supbtn").stop("mix");
         				checkaction = true;
         				canclickBtn = true;
         				clearTimeout(myVar)
         
         			},2000)
         
         		}
         	}else{
         			bucksym.getSymbol("cone_animate").play("animate");
         			bucksym.getSymbol("spinbaker").play("animate");
         			canclickBtn = false;
         			if(d==4){
         				bucksym.getSymbol("socool").play("animate");
         			}
         			myVar=setTimeout(function(){
         
         				switch(d){
         				case 1: dayText="ไม่พบ";nightText="24";
         				bucksym.getSymbol("text_temp").$("text").html("24");
         				break;
         				case 2: dayText="ไม่พบ";nightText="23";
         				bucksym.getSymbol("text_temp").$("text").html("23");
         				break;
         				case 3: dayText="ไม่พบ";nightText="22";
         				bucksym.getSymbol("text_temp").$("text").html("22");
         				break;
         				case 4: dayText="พบ";nightText="21";
         				bucksym.getSymbol("text_temp").$("text").html("21");
         	
         				times=setTimeout(function(){sym.getSymbol("mascotAnimate").play("animate");clearTimeout(times);},1400)
         
         				btn.hide();
         				}
         					parseText(d);
         					btnsym.play(0);
         					btnsym.getSymbol("supbtn").stop("put");
         					bucksym.getSymbol("beaker").stop("a0");
         					bucksym.getSymbol("spinbaker").stop(0);
         					canclickBtn = true;
         					clearTimeout(myVar);
         					checkaction = false;
         			},2000)
         	}
         	}else{
         	 console.log("can't click btn waiting during time");
         	}
         
         
         });
         
         
         function parseText(idc){
         	var bool = arraysEqual(idc);
         	var atList = 'text'+j;
         	var day = 'day'+j;
         	var night = 'night'+j;
         
         	if(bool){
         		console.log("true");
         		console.log("tempA = "+tempArray);
         		tempArray.push(idc);
         		if(j<=4){
         			console.log(atList);
         			table.$(atList).html(j);
         			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");
         	}
         }
         sym.$('sym_table').bind("mousedown",function(){
         	 showTable();
         });

      });
      //Edge binding end

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

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

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

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

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

      });
      //Edge binding end

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

      });
      //Edge binding end

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

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

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

      });
      //Edge binding end

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

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

      });
      //Edge binding end

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

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

      });
      //Edge binding end

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

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

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

      });
      //Edge binding end

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

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