/***********************
* 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 RTL = false; // RIGHT TO LEFT
         	var clicked = 0;
         	var panelVis = false;
         	var speed = 200;
         	
         	var sourceStyle = {"background-color":"rgba(0,0,0,1)", "height":"auto", "padding-left":"25px","padding-right":"4px","padding-top":"2px", "pointer-events":"none"};
         	var headline;
         	var headlinetxt = sym.getSymbol("styleHead").$("headlineTxt").css({"pointer-events":"none"});
         	var headlinemob;
         	var headlinetxtmob = sym.$("text").css({"pointer-events":"none"});
         	var source;
         	var sourcetxt = sym.getSymbol("styleBase").getSymbol("toggleSource").$("sourceTxt").css(sourceStyle);
         	
         	var mapPath = sym.getSymbol("scrollMc").getSymbol("scrollableMc");
         	
         //********************************************************************************************************
         // CSS STYLES
         //********************************************************************************************************
         	var whiteTxtBox = {"border-color":"white", "border-width":"1px","border-style":"solid", "border-radius":"5px", "color":"rgba(0,0,0,1)",
         	"background-color":"#f9a51a", "height":"auto","width":"auto", "padding":"6px"};
         
         	var blackTxtBox = {"border-color":"white", "border-width":"0px","border-style":"solid", "border-radius":"5px", "color":"rgba(255,255,255,1)",
         	"background-color":"rgba(0,0,0,1)", "height":"auto","width":"auto", "padding":"6px"};
         
         	var grey  = {"background-color":"rgba(160,160,160,1)"};
         	var red = {"background-color":"rgba(218,0,0,1)"};
         	var black  = {"background-color":"rgba(0,0,0,1)"};
         	var white  = {"background-color":"rgba(255,255,255,1)"};
         
         //********************************************************************************************************
         // ADD CALENDAR
         //********************************************************************************************************
         calArray = []; //the array that will hold references to your new symbols.
         
         for (var i = 0; i < 38; i++){
          var calObject = sym.createChildSymbol("dateBox", "panelMc");  // Create an instance element of a symbol as a child of the given parent element
          calObject.setVariable("id", i);  //set the value of a Symbol variable
          calArray.push(calObject);  //push it into the array
         }
         
         for (var i=0; i<calArray.length; i++){
         	calArray[i].getSymbolElement().css({'top':'24px', 'left':'4px'});
         	calArray[i].getSymbolElement().css({'position':'relative','float':'left'});
         	calArray[i].$('no').css({"color":"white"});
         }
         for (var i=0; i<7; i++){
         	calArray[i].$('bgRight').css({'background-color':'#fff'});
         	calArray[i].$('bgLeft').css({'background-color':'#fff'});
         }
         for (var i=7; i<11; i++){
         	calArray[i].$('bgRight').css({'background-color':'#c6c6c6'});
         	calArray[i].$('bgLeft').css({'background-color':'#c6c6c6'});
         }
         for (var i=28; i<38; i++){
         	calArray[i].$('bgRight').css({'background-color':'#c6c6c6'});
         	calArray[i].$('bgLeft').css({'background-color':'#c6c6c6'});
         }
         for (var i=7; i<38; i++){
         	calArray[i].$('no').html(i-6);
         }
         calArray[24].$('bgRight').css({'background-color':'#1382c5'});
         calArray[24].$('bgLeft').css({'background-color':'#1382c5'});
         
         calArray[26].$('bgRight').css({'background-color':'#e21932'});
         calArray[26].$('bgLeft').css({'background-color':'#e21932'});
         
         //********************************************************************************************************
         // TEXT AND XML
         //********************************************************************************************************
         	var txt=[];
         	for (var i=0; i<3; i++){
         		txt[i] = sym.getSymbol("keyMc").getSymbol("key"+i).$("txt").css({"pointer-events":"none"});
         	}
         	for (var i=3; i<16; i++){
         		txt[i] = mapPath.$("txt"+i).css({"pointer-events":"none"});
         	}
         	txt[16] = sym.getSymbol("panelMc").$("txt16").css({"pointer-events":"none"});
         	txt[17] = sym.$("txt17").css({"pointer-events":"none"});
         	txt[18] = sym.getSymbol("panelMc").$("txt18").css({"pointer-events":"none"});
         	txt[19] = sym.$("txt19").css({"pointer-events":"none"});
         	txt[20] = sym.$("txt20").css({"pointer-events":"none"});
         	var txtArr=[];
         
         	//AJAX retrieves data from xml source specified in url, maps xml variable to DOM object variable
         	$.ajax({	
         		type: "GET",
         		url: "GN34324.xml",
         		dataType: "xml",
         		success: function(xml) {
         			headerBackgroudColour = $(xml).find('headerBackgroudColour').text();
         			headline = $(xml).find('headline').text();
         			headlinetxt.html(headline);
         			headSize = $(xml).find('headSize').text();
         			headlinemob = $(xml).find('headlinemob').text();
         			headlinetxtmob.html(headlinemob);
         			sym.getSymbol("styleHead").$("headerBox").css({"background-color":headerBackgroudColour});
         			source = $(xml).find('source').text();
         			sourcetxt.html(source);
         
         			var fontSize= $(xml).find('fontSize').text();
         			var lineHeight= $(xml).find('lineHeight').text();
         			for (var i=0; i<20; i++){
         				txtArr[i]=$(xml).find('txt'+i).text();
         				txt[i].html(txtArr[i]);
         				txt[i].css({'font-size':fontSize});
         				txt[i].css({'line-height':lineHeight});
         			}
         
         			var dayString = $(xml).find('txt17').text();
                  weekDays = dayString.split(";");
                  for (var w=0; w<7; w++){
         				calArray[w].$('no').html(weekDays[w]);
         				calArray[w].$('no').css({"color":"gray"});
         			}
         
         			mapPath.$("txt3").css({'line-height':'20px'});
         			mapPath.$("txt9").css(whiteTxtBox);
         			mapPath.$("txt13").css({'line-height':'20px'});
         		},
            complete:function(){
         		initSize();
            }
         
         	}); // END XML
         
         //********************************************************************************************************
         // PANEL BTN
         //********************************************************************************************************
         sym.$("panelBtn").on('touchstart click', function(e){
         		e.stopPropagation(); e.preventDefault();
         		panelCheck();
         	});
         
         function panelCheck(){
         	if (panelVis == false){
         		speed = 200;
         		panelOpen();
         	}else{
         		speed = 200;
         		panelClose();
         	}
         }
         function panelClose(){
         	sym.$("panelMc").stop().animate({left:stageWidth+10}, speed, 'easeOutCubic');
         	sym.getSymbol("panelBtn").$("arrow").css({'transform':'scaleX(1)','-webkit-transform':'scaleX(1)','-ms-transform':'scaleX(1)'});
         	panelVis = false;
         
         }
         function panelOpen(){
         	sym.$("panelMc").stop().animate({left:stageWidth-160}, speed, 'easeOutCubic');
         	sym.getSymbol("panelBtn").$("arrow").css({'transform':'scaleX(-1)','-webkit-transform':'scaleX(-1)','-ms-transform':'scaleX(-1)'});
         	panelVis = true;
         }
         
         //********************************************************************************************************
         // INIT SIZE
         //********************************************************************************************************
         function initSize(){
         	stageWidth=sym.$("Stage").width();
         	sym.$("panelBtn").css({"left":(stageWidth-40)+"px"});
         	speed = 0;
         
         
         	if (stageWidth<640){
         		sym.$("keyMc").css({"width":stageWidth+"px"});
         		sym.$("scrollMc").css({"width":stageWidth+"px"});
         		sym.$("panelBtn").css({"top":"0px"});
         		panelClose();
         	}else{
         		sym.$("keyMc").css({"width":(stageWidth-160)+"px"});
         		sym.$("scrollMc").css({"width":(stageWidth-160)+"px"});
         		sym.$("panelBtn").css({"top":"-50px"});
         		panelOpen();
         	}
         	if(stageWidth<480){
         		sym.getSymbol("styleHead").$("headlineTxt").empty();
         		sym.getSymbol("styleHead").$("headlineTxt").append(headlinemob);
         		sym.$("scrollMc").css({'overflow-x':'auto'});
         	}else{
         		sym.getSymbol("styleHead").$("headlineTxt").empty();
         		sym.getSymbol("styleHead").$("headlineTxt").append(headline);
         		sym.$("scrollMc").css({'overflow-x':'hidden'});
         	}
         	setStyle();
         }
         
         //********************************************************************************************************
         // TIMER
         //********************************************************************************************************
         var timer;
         
         function startTimer() {
           timer = setInterval(startReplay, 150);
         }
         function startReplay() {
         	var pos = sym.getSymbol("scrollMc").getSymbol("scrollableMc").getPosition();
         	var dotPos = sym.getSymbol("scrollMc").getSymbol("scrollableMc").$("dotMc").position();
         	if (stageWidth <470){
         	if(dotPos.left < stageWidth-180){
         		sym.$("scrollMc").stop().animate({scrollLeft: 0}, 500);
         	} else {
         		sym.$("scrollMc").stop().animate({scrollLeft: 150}, 500);
         	}
         	} else {
         		sym.$("scrollMc").stop().animate({scrollLeft: 0}, 500);
         	}
         	sym.$("scrollMc").css({'overflow-x':'hidden'});
         	if (pos == 0 || pos == 20000) {
         		stopTimer();
         		initSize();
         	}
         }
         function stopTimer() {
           clearInterval(timer);
         }
         startTimer();
         
         //********************************************************************************************************
         // PLAY BUTTON
         //********************************************************************************************************
         sym.getSymbol("scrollMc").getSymbol("scrollableMc").$("playBtn").on('touchstart click', function(e){
         	e.stopPropagation(); e.preventDefault();
         	startTimer();
         	sym.getSymbol("scrollMc").getSymbol("scrollableMc").play(0);
         	});
         
         //********************************************************************************************************
         // ARABIC RTL
         //********************************************************************************************************
         if (RTL == true){
         	for (var i=0; i<calArray.length; i++){
         	calArray[i].getSymbolElement().css({'top':'24px', 'left':'-2px'});
         	calArray[i].getSymbolElement().css({'position':'relative','float':'right'});
         	}
         	sym.getSymbol("panelMc").$("calPeeps").css({"left":"60px"});
         	sym.getSymbol("panelMc").$("txt18").css({"text-align":"right"});
         	for (var i=0; i<3; i++){
         		txt[i] = sym.getSymbol("keyMc").getSymbol("key"+i).$("txt").css({"text-align":"right", "left":"-2px"});
         		sym.getSymbol("keyMc").getSymbol("key"+i).$("RoundRect").css({"left":"60%"});
         	}
         }
         
         //********************************************************************************************************
         //----------------------------------------- TEMPLATE CODE BELOW ------------------------------------------
         //********************************************************************************************************
         // SET STYLE  
         function setStyle(){
         	var stageWidth=sym.$("Stage").width();
         	sym.getSymbol("Stage").stop(stageWidth*10);
         	sym.$("stageWidthTxt").text(stageWidth + "px");
         	sym.getSymbol("keyMc").getSymbol("key0").$("RoundRect").css({"background-color":"#0071bb"});
         	sym.getSymbol("keyMc").getSymbol("key1").$("RoundRect").css({"background-color":"#ec1c24"});
         	sym.getSymbol("keyMc").getSymbol("key2").$("RoundRect").css({"background-color":"#f9a51a"});
         	sym.$("scrollMc").css({'overflow-y':'hidden'});
         }
         setStyle();
         
         //********************************************************************************************************
         // SOURCE BUTTON
         sym.getSymbol("styleBase").$("toggleSource").on('touchstart click', function(e){
         	e.preventDefault(); e.stopPropagation();
         	sym.getSymbol("styleBase").$("toggleSource").stop().animate ({height:sym.getSymbol("styleBase").getSymbol("toggleSource").$("sourceTxt").height()+20}, 0);
         	if (sym.getSymbol("styleBase").getSymbol("toggleSource").$("plus").is(":visible") ) { 
         		sym.getSymbol("styleBase").getSymbol("toggleSource").$("plus").fadeOut(0);
         		sym.getSymbol("styleBase").$("toggleSource").stop().animate ({top:0-sym.getSymbol("styleBase").getSymbol("toggleSource").$("sourceTxt").height()+10}, 200, 'easeOutCubic');
         	}else{
         		closeSource();
         	}
         }); 
         function closeSource(){
         	sym.getSymbol("styleBase").getSymbol("toggleSource").$("plus").fadeIn(0);
         	sym.getSymbol("styleBase").$("toggleSource").stop().animate ({top:0}, 200, 'easeInCubic');
         }
         
         //********************************************************************************************************
         $(window).on('resize',initSize);
         //********************************************************************************************************
         //-------------------------------------------------- END -------------------------------------------------
         //********************************************************************************************************

      });
      //Edge binding end

      

      

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

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

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

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

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

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

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

   //=========================================================
   
   //Edge symbol: 'Down-level'
   (function(symbolName) {   
   
   })("Down-level");
   //Edge symbol end:'Down-level'

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

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

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

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

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

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

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

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

})(window.jQuery || AdobeEdge.$, AdobeEdge, "GNEWS34324");