/***********************
* Adobe Edge 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
*
***********************/
(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 headline;
	var headlinetxt = sym.getSymbol("styleHead").$("headlineTxt").css("pointer-events","none");
	var stand;
	var standtxt = sym.getSymbol("styleHead").$("standTxt").css("pointer-events","none");
	var source;
	var sourcetxt = sym.getSymbol("styleBase").$("sourceTxt").css("pointer-events","none");
	
	var txt=[];
	txt[0] = sym.getSymbol("mapPointMc").$("txt0").css({"border-width":"1px","border-style":"solid","border-color":"rgba(255,255,255,1)","background-color":"rgba(248,175,0,1)","border-radius":"4px","padding":"6px"});
	txt[1] = sym.getSymbol("mapPointMc").$("txt1").css({"border-width":"1px","border-style":"solid","border-color":"rgba(255,255,255,1)","background-color":"rgba(248,175,0,1)","border-radius":"4px","padding":"6px"});
	txt[2] = sym.getSymbol("txtMc").$("txt2").css("pointer-events","none");
	txt[3] = sym.getSymbol("txtMc").$("txt3").css("pointer-events","none");
	txt[4] = sym.getSymbol("txtMc").$("txt4").css("pointer-events","none");
	txt[5] = sym.getSymbol("txtMc").$("txt5").css("pointer-events","none");
	txt[6] = sym.$("txt6").css("pointer-events","none");
	txt[7] = sym.getSymbol("txtMc").$("txt7").css("pointer-events","none");
	txt[8] = sym.getSymbol("txtMc").$("txt8").css("pointer-events","none");
	txt[9] = sym.getSymbol("txtMc").$("txt9").css("pointer-events","none");
	txt[10] = sym.getSymbol("txtMc").$("txt10").css("pointer-events","none");
	txt[11] = sym.getSymbol("txtMc").$("txt11").css("pointer-events","none");
	txt[12] = sym.getSymbol("txtMc").$("txt12").css("pointer-events","none");
	txt[13] = sym.getSymbol("txtMc").$("txt13").css("pointer-events","none");
	txt[14] = sym.getSymbol("txtMc").$("txt14").css("pointer-events","none");
	txt[15] = sym.getSymbol("txtMc").$("txt15").css("pointer-events","none");
	txt[16] = sym.getSymbol("txtMc").$("txt16").css("pointer-events","none");
	txt[17] = sym.getSymbol("txtMc").$("txt17").css("pointer-events","none");
	txt[18] = sym.getSymbol("txtMc").$("txt18").css("pointer-events","none");
	txt[19] = sym.getSymbol("txtMc").$("txt19").css("pointer-events","none");
	txt[20] = sym.getSymbol("txtMc").$("txt20").css("pointer-events","none");
	txt[21] = sym.getSymbol("txtMc").$("txt21").css("pointer-events","none");
	txt[22] = sym.getSymbol("txtMc").$("txt22").css("pointer-events","none");
	txt[23] = sym.getSymbol("txtMc").$("txt23").css("pointer-events","none");
	txt[24] = sym.getSymbol("txtMc").$("txt24").css("pointer-events","none");
	txt[25] = sym.getSymbol("txtMc").$("txt25").css("pointer-events","none");
	txt[26] = sym.$("txt26").css("padding-right","10px");
	txt[27] = sym.$("txt27").css("pointer-events","none");
	txt[28] = sym.$("txt28").css("pointer-events","none");
	txt[29] = sym.$("txt29").css("pointer-events","none");
	txt[30] = sym.$("txt30").css("pointer-events","none");
	txt[31] = sym.$("txt31").css("pointer-events","none");
	txt[32] = sym.$("txt32").css("pointer-events","none");
	txt[33] = sym.$("txt33").css("pointer-events","none");
	txt[34] = sym.$("txt34").css("pointer-events","none");
	txt[35] = sym.$("txt35").css("pointer-events","none");
	txt[36] = sym.$("txt36").css("pointer-events","none");
	txt[37] = sym.$("txt37").css("pointer-events","none");
	txt[38] = sym.$("txt38").css("pointer-events","none");
	txt[39] = sym.getSymbol("txtLast").$("txt39").css("pointer-events","none");
	txt[40] = sym.getSymbol("startMc").$("txt40").css({"border-width":"1px","border-style":"solid","border-color":"rgba(255,255,255,1)","background-color":"rgba(0,0,0,1)","border-radius":"8px","padding":"20px"});
	
	var txtArr=[];
	
	//AJAX retrieves data from xml source specified in url, maps xml variable to DOM object variable
	$.ajax({	
		type: "GET",
		url: "GN31602.xml",
		dataType: "xml",
		success: function(xml) {
			headline = $(xml).find('headline').text();
			headlinetxt.html(headline);
			stand = $(xml).find('stand').text();
			standtxt.html(stand);
			source = $(xml).find('source').text();
			sourcetxt.html(source);
	
			//create array from xml, put it into array of text elements
			for (var i=0; i<41; i++){
			txtArr[i]=$(xml).find('txt'+i).text();
			txt[i].html(txtArr[i]);
	
			}
	
			sym.$("txt26").empty();
			sym.getSymbol("mapPointMc").$("txt0").animate({left:542 - sym.getSymbol("mapPointMc").$("txt0").width()-6}, 0);
			sym.getSymbol("txtLast").$("txt39").animate({left:0 - sym.getSymbol("txtLast").$("txt39").width()-3}, 0);
	
	
	
			var st1 = "<div class='blackstrap'>" + txtArr[26] + "</div><div class='captionNote'>" + txtArr[27]+"</div>" + "<div class='blackstrap'>"+txtArr[28] 
			+ "</div><div class='captionNote'>" + txtArr[29]+"</div>" + "<div class='blackstrap'>"+txtArr[30] + "</div><div class='captionNote'>" + txtArr[31]+"</div>";
			sym.$("txt26").append(st1);
	
			$('.blackstrap').css({'background-color':'rgba(235,236,241,1)', 'padding-right':'10px', 'padding-left':'10px', 'padding-bottom':'6px', 'padding-top':'6px'});
			$('.captionNote').css({'background-color':'rgba(215,218,227,1)', 'padding-right':'10px', 'padding-left':'10px', 'padding-bottom':'6px', 'padding-top':'6px'});
	
	
			//txt[39].css({left:524 - sym.$("txt39").width()+6}, 0);
		}
	}); // END XML
	
	sym.$("btn1").on('touchstart click', function(e){
		e.stopPropagation(); e.preventDefault();
		sym.getSymbol("btnsMc").$("btn1Click").fadeIn(0);
		sym.getSymbol("btnsMc").$("btn2Click").fadeOut(0);
		sym.$("m").stop().animate ({opacity:0}, 0);
		sym.$("km").stop().animate ({opacity:1}, 0);
		sym.$("miles").stop().animate ({opacity:0}, 0);
		sym.$("kms").stop().animate ({opacity:1}, 0);
	});
	sym.$("btn1").mouseover(function(){
		sym.getSymbol("btnsMc").$("btn1Over").fadeIn(0);
	});
	sym.$("btn1").mouseout(function(){
		sym.getSymbol("btnsMc").$("btn1Over").fadeOut(0);
	});
	
	sym.$("btn2").on('touchstart click', function(e){
		e.stopPropagation(); e.preventDefault();
		sym.getSymbol("btnsMc").$("btn2Click").fadeIn(0);
		sym.getSymbol("btnsMc").$("btn1Click").fadeOut(0);
		sym.$("m").stop().animate ({opacity:1}, 0);
		sym.$("km").stop().animate ({opacity:0}, 0);
		sym.$("miles").stop().animate ({opacity:1}, 0);
		sym.$("kms").stop().animate ({opacity:0}, 0);
	});
	sym.$("btn2").mouseover(function(){
		sym.getSymbol("btnsMc").$("btn2Over").fadeIn(0);
	});
	sym.$("btn2").mouseout(function(){
		sym.getSymbol("btnsMc").$("btn2Over").fadeOut(0);
	});
	
	sym.getSymbol("btnsMc").$("btn1Click").fadeIn(0);
	sym.getSymbol("btnsMc").$("btn2Click").fadeOut(0);
	sym.getSymbol("btnsMc").$("btn1Over").fadeOut(0);
	sym.getSymbol("btnsMc").$("btn2Over").fadeOut(0);
	sym.$("m").stop().animate ({opacity:0}, 0);
	sym.$("miles").stop().animate ({opacity:0}, 0);
	sym.$("kms").stop().animate ({opacity:1}, 0);
	sym.$("miles").fadeOut(0);
	sym.$("kms").fadeOut(0);
	

         	});//Edge binding end	
		
		// VIDEO BTN 1 =========================================================
         	
			
         	
         		
         	
			
			
         	// VIDEO BTN 2 =========================================================
         	
         	
         	
         		
         	
         	


      Symbol.bindTimelineAction(compId, symbolName, "Default Timeline", "update", function(sym, e) {
         var pos = sym.getPosition();
         
         sym.$("km").text((pos / 200).toFixed(2) + "km");
         
         sym.$("m").text((pos / 322.1).toFixed(2) + "m");
         
         if (pos >= 10) {
         	sym.$("startMc").fadeOut(200);
         }
         
         if (pos >= 8250) {
         	sym.$("kms").fadeIn(200);
         	sym.$("miles").fadeIn(200);
         } else {
         	sym.$("kms").fadeOut(200);
         	sym.$("miles").fadeOut(200);
         }

      });
      //Edge binding end

      

      

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

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

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

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

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

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

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

   //=========================================================
   
   //Edge symbol: 'TimelineController'
   (function(symbolName) {   
   
      Symbol.bindSymbolAction(compId, symbolName, "creationComplete", function(sym, e) {
         // flag of use keyboard event.
         
         var useKeyBoardEvent = true;

         // key code of right/left key

         var KEY_RIGHT = 39;

         var KEY_LEFT =  37;

         // params

         var power = 1.5;

         var declineRate = 0.5;

         var speedWidth = 30;

         
         // try to hide the location bar at Safari,if need, comment out it. 

         // setTimeout(function() { window.scrollTo(0, 0); }, 250);

         
         var fps = 60;

         var interval = (1000/fps)|0;

         var speed = 0;

         var isKeyPress = false, isMousePress = false;

         
         var centerX,target,v;

         
         if(useKeyBoardEvent)$(window).bind("keydown",onStartControll).bind("keyup",onStopControll);  

         
         var el = $(sym.getSymbolElement());

         el.mousedown(onStartControll)

           .mouseup(onStopControll)

           .mouseout(onStopControll)

           .mousemove(onMouseMove)

           .bind("touchstart",onStartControll)

           .bind("touchmove",onMouseMove)

           .bind("touchend",onStopControll);

         
         function onStartControll(evt){
         
           evt.preventDefault();

           target = sym.getParentSymbol();

           centerX = el.offset().left+el.width()/2;

           if(evt.type=="keydown" && isKeyPress===false){

             speed = 

               (evt.which==KEY_RIGHT)*interval*power

                  + (evt.which==KEY_LEFT)*interval*-1*power;

             speed|=0;

             isKeyPress = true;
           }else if(evt.type=="mousedown" || evt.type=="touchstart"){

             isMousePress = true;
           }

           if(v===undefined)v=setInterval(onControll,interval);
         }

         function onStopControll(evt){
     
           if(evt.type=="keyup"){

             isKeyPress = false;
           }else{

             isMousePress = false;
           }
         }

         function onControll(){

           if(isKeyPress===false && isMousePress===false){

             speed = (speed*declineRate)|0;

             if(-1<speed && speed<1){

               clearInterval(v);

               v = undefined;

             }

           }

           var n = target.getPosition()+speed;

           if(-1<n && n<=target.getDuration())target.stop(n);
         }

         function onMouseMove(evt){

           evt.preventDefault();

           evt = (evt.originalEvent.touches && evt.originalEvent.touches[0]) || evt;

           if(isMousePress===true){

             speed = (((evt.clientX - centerX)/speedWidth)*power*interval)|0;

           }
         }

         

      });
      //Edge binding end

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

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

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

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

})(jQuery, AdobeEdge, "GraphicNews31602");