/***********************
* 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 sources;
         var latest;
         var table1;
         var table2;
         var table3;
         var table4;
         var table5;
         var table6;
         var table7;
         var table8;
         var table9;
         var team1;
         var team2;
         var score;
         var match;
         var cap1;
         var cap2;
         var cap3;
         
         
         var d=new Date();
         
         //AJAX retrieves data from xml source specified in url, maps xml variable to DOM object variable
         
         $.ajax({	
         	type: "GET",
         	url: "GN29595.xml",
         	dataType: "xml",
         	success: function(xml) {
         	
         		headline = $(xml).find('headline').text();
         		sym.$("headlineTxt").html(headline);				
         		sources = $(xml).find('sources').text();
         		sym.$("sourcesTxt").html(sources);		
         		latest = $(xml).find('latest').text();
         		sym.getSymbol("panel01").$("latestTxt").html(latest);
         		
         		table1 = $(xml).find('table1').text();
         		sym.getSymbol("panel01").$("table1Txt").html(table1);
         		table2 = $(xml).find('table2').text();
         		sym.getSymbol("panel01").$("table2Txt").html(table2);
         		table3 = $(xml).find('table3').text();
         		sym.getSymbol("panel01").$("table3Txt").html(table3);
         		table4 = $(xml).find('table4').text();
         		sym.getSymbol("panel01").$("table4Txt").html(table4);
         		table5 = $(xml).find('table5').text();
         		sym.getSymbol("panel01").$("table5Txt").html(table5);
         		table6 = $(xml).find('table6').text();
         		sym.getSymbol("panel01").$("table6Txt").html(table6);
         		table7 = $(xml).find('table7').text();
         		sym.getSymbol("panel01").$("table7Txt").html(table7);
         		
         		table8 = $(xml).find('table8').text();
         		sym.getSymbol("panel01").$("matches").append(table8);
         		table9 = $(xml).find('table9').text();
         		sym.getSymbol("panel01").$("table").append(table9);
         
         		cap1 = $(xml).find('cap1').text();
         		cap2 = $(xml).find('cap2').text();
         		
         		btn1 = $(xml).find('btn1').text();
         		sym.getSymbol("matchtracker").$("tabBtn1").html(btn1);
         		btn2 = $(xml).find('btn2').text();
         		sym.getSymbol("matchtracker").$("tabBtn2").html(btn2);
         		ko = $(xml).find('ko').text();
         		sym.getSymbol("matchtracker").getSymbol("timer").getSymbol("timeline").$("koTxt").html(ko);
         		cap1 = $(xml).find('cap1').text();
         		sym.getSymbol("matchtracker").getSymbol("teams").$("cap1Txt").html(cap1);
         		sym.getSymbol("matchtracker").getSymbol("teams").$("cap2Txt").html(cap1);
         
         
         		}
         	});
         
         function doTable() {           
          var tableData;
          $.ajax({ type: "GET", url: "//www2.graphicnews.com/infostradasports/api.php",
                         data: "gn=29595&phaseID=98028",
                         contentType: "text/plain", dataType: "jsonp", jsonp: "callback",
                         async: true,
                         crossDomain: true,
                         success: function (data) {
                             tableData = data;
                             for (var i = 0; i < tableData.length; i++) {
                            		if (i==0){
         		sym.getSymbol("panel01").$("table").empty();
         		}			
         		
         				if (i==0){
         				var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(245,230,140,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}else if (i< 5){
         				if (i%2 == 0){
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(230,230,190,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}else{
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(250,250,220,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}
         				}else if (i>16){
         				if (i%2 == 0){
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(255,190,160,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}else{
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(255,220,205,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}
         				}else{
         				if (i%2 == 0){
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(215,230,245,0.5);'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}else{
                       var tableTxt = "<table width='100%' style='font-size:12px;font-family:pragmatica-web, sans-serif;'><tr><td width='5%' id='rank" + i + "'></td><td width='38%' id='team" + i + "'></td><td width='7%' style='text-align:right' id='p" + i + "'></td><td width='7%' style='text-align:right' id ='w" + i + "'></td><td width='7%' style='text-align:right' id ='d" + i + "'></td><td width='7%' style='text-align:right' id ='l" + i + "'></td><td width='10%' style='text-align:right' id ='f" + i + "'></td><td width='10%' style='text-align:right' id ='a" + i + "'></td><td width='9%' style='text-align:right; font-weight:bold' id ='pts" + i + "'></td></tr></table>";
         				}
         				}
         							
                       sym.getSymbol("panel01").$("table").append(tableTxt);
                         $('#team' + i).text(tableData[i].c_Team);
                         $('#rank' + i).text(i+1);
                         $('#p' + i).text(tableData[i].n_Matches);
                         $('#w' + i).text(tableData[i].n_MatchesWon);
                         $('#d' + i).text(tableData[i].n_MatchesDrawn);
                         $('#l' + i).text(tableData[i].n_MatchesLost);
                         $('#f' + i).text(tableData[i].n_GoalsFor);
                         $('#a' + i).text(tableData[i].n_GoalsAgainst);
                         $('#pts' + i).text(tableData[i].n_Points);
                             } 
                         }
                     });
         }
         doTable();
         intervalId = setInterval(doTable,300000);
         
         
          function getDDMMYY(thisDateStr) {
                     var dateTxt = thisDateStr.substring(thisDateStr.indexOf('(') + 1, thisDateStr.indexOf(')'));
                     var thisDate = new Date(parseInt(dateTxt, 10));
                     var mm = thisDate.getMonth() + 1;
                     var dd = thisDate.getDate();
                     mm = ((mm < 10) ? '0' : "") + mm;
                     dd = ((dd < 10) ? '0' : "") + dd;
                    return dd + "-" + mm + "-" + (thisDate.getFullYear()).toString();
                    //return dd + "-" + mm;
                 }
                 
          function parsedDate(thisDateStr){
         var matchRndDate = thisDateStr.substring(thisDateStr.indexOf('(') + 1, thisDateStr.indexOf(')'));
         var dDate = parseInt(matchRndDate,10);
         return dDate;
         }      
         
         var startSeason= Date.parse("August 18, 2012");
         var endSeason = Date.parse("May 19, 2013");
         var oneDay = 86400000;
         var todayDate = new Date();
         var todayTime = todayDate.getTime();
                       
         var matchID;
         var matchteam1;
         var matchteam2;
         var matchscore;
         var matchDetails;
         var matchStatus;
         var homeTeam = sym.getSymbol("matchtracker").$("team1Txt");
         var awayTeam = sym.getSymbol("matchtracker").$("team2Txt");
         var score = sym.getSymbol("matchtracker").$("scoreTxt");
         var crestH = sym.getSymbol("matchtracker").$("crest1");
         var crestA = sym.getSymbol("matchtracker").$("crest2");
         
         
                 
          var fixtureData;   
         function getFixtures() { 
          
          $.ajax({ type: "GET", url: "//www2.graphicnews.com/infostradasports/api.php",
                         data: "gn=29595&editionID=19660",
                         contentType: "text/plain", dataType: "jsonp", jsonp: "callback",
                         async: true,
                         crossDomain: true,
                         success: function (data) {
                              fixtureData = data;
                             for (var i = 0; i < fixtureData.length; i++) {
                            		if (i==0){
         		sym.getSymbol("panel01").$("matches").empty();
         		}
         		var matchesRnd = parsedDate(fixtureData[i].d_Date);
         		if (((todayTime<startSeason)&&(i<10))||((todayTime>endSeason)&&(i>fixtureData.length-10))||((matchesRnd>todayTime-(oneDay*2))&&(matchesRnd<todayTime+(oneDay*5)))){
         
         		//make the following a function						
         					if (i%2 == 0){
                       var fixtureTxt = "<table width='100%' height='13px' style='font-size:12px;font-family:pragmatica-web, sans-serif; background:rgba(215,230,245,0.5);' id='match" + i + "'><tr><td width='45%' style='padding-left:3px' id='teamh" + i + "'></td><td width='10%' style='text-align:center' id='score" + i + "'></td><td width='45%' style='text-align:right' id ='teama" + i + "'></td></tr></table>";
         				}else{
                       var fixtureTxt = "<table width='100%' height='13px' style='font-size:12px;font-family:pragmatica-web, sans-serif;' id='match" + i + "'><tr><td width='45%' style='padding-left:3px' id='teamh" + i + "'></td><td width='10%' style='text-align:center' id='score" + i + "'></td><td width='45%' style='text-align:right' id ='teama" + i + "'></td></tr></table>";
         				}
                         sym.getSymbol("panel01").$("matches").append(fixtureTxt);                
                         $('#teamh' + i).text(fixtureData[i].c_HomeTeam);
                         $('#teama' + i).text(fixtureData[i].c_AwayTeam);
                         if (fixtureData[i].n_HomeGoals!==null||undefined){
                         $('#score' + i).html('<b>' + (fixtureData[i].n_HomeGoals)+ '-' +(fixtureData[i].n_AwayGoals) + '</b>');
                         }else{
                         $('#score' + i).html('');
                         }
                         
                       
         			matchteam1=(fixtureData[i].c_HomeTeam);
         			matchteam2=(fixtureData[i].c_AwayTeam);
         			matchscore=(fixtureData[i].c_Score);
         			var imgH = "crests/"+ fixtureData[i].n_HomeTeamID + ".png";
         			var imgA = "crests/"+ fixtureData[i].n_AwayTeamID + ".png";
         			matchID = fixtureData[i].n_MatchID; 
                        
         			$('#match' + i).css('cursor', 'pointer');				
         
         /*			$("#match" + i).on("click", function(){
         			sym.$("panel01").hide();
         			sym.$("matchtracker").show();
         			crestH.attr("src",imgH);				
         			crestA.attr("src",imgA);				
         			homeTeam.text(matchteam1);
         			awayTeam.text(matchteam2);
         			score.text(matchscore);				
         			getmatchDetails(matchID);
         				
         //alternative                             
         		 $("#match" + i).on("click", clickFn(i));
         		 function clickFn(i){
                        sym.$("panel01").hide();
                        sym.$("panel02").show();
                        crestH.attr("src", "crests/"+ fixtureData[i].n_HomeTeamID + ".png");				
                        crestA.attr("src", "crests/"+ fixtureData[i].n_AwayTeamID + ".png");				
                        homeTeam.text(fixtureData[i].c_HomeTeam);
                        awayTeam.text(fixtureData[i].c_AwayTeam);
                        score.text(fixtureData[i].c_Score);				
                    }
         
         								});
         */								} //closes if parsedate				
                             } //closes for                                   
                         } //closes success                                                    	           
                     }); //closes ajax
         }
         getFixtures();
         intervalId = setInterval(getFixtures,300000);
         
         

      });
      //Edge binding end

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

//=========================================================
   //Edge symbol: 'panel01'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'panel02'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'timer'
   (function(symbolName) {

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 3000000, function(sym, e) {
         sym.stop();
         // insert code here

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 6000000, function(sym, e) {
         sym.stop();
         // insert code here

      });
      //Edge binding end

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

//=========================================================
   //Edge symbol: 'timeline'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'redaway'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'yella'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'suba'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'goala'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'goalh'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'yellh'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'subh'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'redh'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'matchtracker'
   (function(symbolName) {

      Symbol.bindElementAction(compId, symbolName, "${_tabBtn2}", "click", function(sym, e) {
         var swivel = sym.getVariable("swivelstate");
         if (swivel=="lineups"){
         sym.$("matchtracker").playReverse();
         sym.$("timeTxt").show();
         sym.$("tabBtn2").css("color","rgba(0,0,0,1.0)");
         sym.$("tabBtn1").css("color","rgba(162,172,212,1.0)");
         sym.$("icon").attr("src","images/clock.png");
         }

      });
         //Edge binding end

      Symbol.bindElementAction(compId, symbolName, "${_tabBtn1}", "click", function(sym, e) {
         var swivel = sym.getVariable("swivelstate");
         if (swivel=="tracker"){
         sym.$("matchtracker").play("swivel");
         sym.$("timeTxt").hide();
         sym.$("tabBtn1").css("color","rgba(0,0,0,1.0)");
         sym.$("tabBtn2").css("color","rgba(162,172,212,1.0)");
         sym.$("icon").attr("src","images/team.png");
         }

      });
         //Edge binding end

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

//=========================================================
   //Edge symbol: 'timer'
   (function(symbolName) {

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 3000000, function(sym, e) {
         sym.stop();
         // insert code here

      });
      //Edge binding end

      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 5700500, function(sym, e) {
         sym.stop();
         // insert code here

      });
      //Edge binding end

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

//=========================================================
   //Edge symbol: 'Teams'
   (function(symbolName) {

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

//=========================================================
   //Edge symbol: 'timeline'
   (function(symbolName) {

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

})(jQuery, AdobeEdge, "GRAPHICNEWS-29595");