/***********************
* 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 gnNo=32764;
var thisMonth=2;//Change this each month: January=0, December=11
var thisYear=2015;// Change this each year
var RTL=false;//Change for RTL translations


//TOUCH OR CLICK
var clickEventType = ('ontouchstart' in document.documentElement ? "touchstart" : "click");//EDIT TO ALLOW FOR FF36 etc - include keyboard arrows
//DATE STUFF
var todayDate; var nextDate; var startDate; var endDate;

function getToday(){
//var startMonth=(thisMonth==0)?11:thisMonth-1;
var startMonth=thisMonth;
var startYear=thisYear;
//var startYear=(thisMonth==0)?thisYear-1:thisYear;
var endMonth=(thisMonth==11)?0:thisMonth+1;
var endYear=(thisMonth==11)?thisYear+1:thisYear;
var leap=((thisYear%4 == 0)&&(thisYear%100 != 0)||(thisYear%400 == 0))?"true":"false";
var febDays=(leap=="true")?29:28;
var dayspm=[0,2,4,7,9];
if (dayspm.indexOf(thisMonth)>=0){
var endDay=(dayspm.indexOf(thisMonth)==0)?febDays:30;
}else{
var endDay=31;
}
startDate=new Date(startYear,startMonth,2);
endDate=new Date(endYear,endMonth,endDay);
todayDate = new Date();
todayDate=(todayDate<startDate||todayDate>endDate)?new Date(startYear,startMonth,1):new Date();
//todayDate=(todayDate<startDate)?new Date(startYear,startMonth,1):todayDate=(todayDate>endDate)?new Date(thisYear,thisMonth,1):new Date();
nextDate = new Date(todayDate.getDate()+1);
}
getToday();

var headline;
var mainPic = sym.$("mainImg");
var globe = sym.$("globe");
var space = sym.$("space");
var nextarrow=sym.$("arrowright");
var year;
var month;
var months=[];
var day;
var days;
var mm;
var dd;
var yyyy;
var picturetxt;
var picture;
var numOfDays;
function newDay(){ //creates selcted date
					year = todayDate.getFullYear();
					month = todayDate.getMonth();
					day = todayDate.getDate();
					mm = (month+1<10)?"0"+(month+1): (month+1)+ "";
					dd = (day<10)?"0" + day: day + "";
					yyyy=year;
}
newDay();

function closestNum (num, arr) {
	var curr = arr[0];
	var diff = Math.abs(num - curr);
	for (var i = 0; i < arr.length; i++) {
		if (num<=arr[i]){
			var newdiff = Math.abs (num - arr[i]);
			if (newdiff < diff) {
				diff = newdiff;
				curr = arr[i];
			}
		}
	}
	return curr;
}
var fontSize;
$.ajax({	
	type: "GET",
	url: "GN"+gnNo+".xml",
	dataType: "xml",
	success: function(xml) {
		var string = $(xml).find('monthnames').text();
		months = string.split(",");
		var string2 = $(xml).find('daynames').text();
		days = string2.split(",");
		picturetxt = $(xml).find('picture').text();
		headline = $(xml).find('headline').text();
		sym.$("headlineTxt").html(headline+" "+months[month]);
		fontSize = $(xml).find('fontsize').text();
		}
});

var picNo=0;
var eventPix = [];
var eventDates=[];
var eventCount=[];
var dayPics;
var totalPics;
var xTxt;
var yTxt;
function getDay(){ //change the day xml, pictures
$.ajax({	
	type: "GET",
	url: yyyy+"/"+mm+"/GN"+gnNo+"_"+mm+".xml",
	dataType: "xml",
	success: function (xml){
		sym.$("textBox").empty();
		sym.$("textBox").css({'top':597,'height':'auto'});
		$(".picCounter").empty();
		globe.fadeIn(0);
		eventPix.length=0;
		var txtHeight;
		var thumbRowPic;
		var thumbCount=[];			
//var eventcount=[];
		var order;
		var ddInt;var mmInt;
		if(todayDate<=endDate){
			$(xml).find("day").each(function(){
				var $eventdate = $(this);
				eventPix.push($eventdate);		
			if(parseInt(dd,10)<=eventDates[eventDates.length-1]){ //If dd is less than the last day in the current xml
				var todaydd=closestNum(parseInt(dd,10),eventDates); //make todaydd the closest day to today
				dd=(todaydd<10)?"0"+todaydd:todaydd+"";
				day=todaydd;
				if ($eventdate.attr("date")==dd+"-"+mm){
					if (picNo>0){
						var t="_"+picNo;
						order=picNo;
					}else{
						var t="";
						order=0;
					}
					var $events = $eventdate.find('event');
					$events.each(function(){					
						var $event = $(this);
						thumbCount.push($event);
						if($event.attr("order")==order){												
							var cap = $event.find('caption').text();
							var picCap=$event.find('picture').text();
							xTxt=$event.attr('long');
							yTxt=$event.attr('lat');
							if(RTL==true){
								var html = "<div style='width:400px; text-align:right; float:right; direction:rtl; font-family:pragmatica-web,sans-serif; font-size:"+fontSize+"px; padding-left:10px; padding-right:10px; padding-top:5px; padding-bottom:8px'>"+cap+" <span style='font-size:12px'> "+picCap+"</span></div>";
							}else{
								var html = "<div style='width:400px; float:left; font-family:pragmatica-web,sans-serif; font-size:"+fontSize+"px; padding-left:10px; padding-right:10px; padding-top:5px; padding-bottom:8px'>"+cap+" <span style='font-size:12px'> "+picCap+"</span></div>";
							}
							sym.$("textBox").append(html);
							txtHeight=sym.$("textBox").height();
							sym.$("textBox").stop().animate ({top:490, height:100}, 500);
							if(yTxt==""){
								globe.fadeOut(0,function(){ space.attr("src", yyyy+"/"+mm +"/globe"+ mm + "_" + dd +t+".png"); space.fadeIn(400); });
							}else{
								spinGlobe();
								space.fadeOut(0);
							}
						mainPic.stop().animate({'opacity':0.5},100,function(){ mainPic.attr("src", yyyy+"/"+mm +"/"+ mm + "_" + dd +t+".jpg"); mainPic.stop().animate({'opacity':1.0},800); });
						}					
					});
					ddInt=parseInt(dd,10); mmInt=parseInt(mm,10);
					$('#mm'+(mmInt-1)+'dd'+ddInt).addClass('dayNow');
					$(".dayNow").css({'color':'rgb(255,255,255)','background-color':'rgb(255,0,0)'});
					$("#dd"+ddInt+"id"+picNo).addClass('listNow');
					$(".listNow").css({'font-weight':700});
					todayDate.setDate(ddInt);
				}					
			}else{//Otherwise change month
				console.log(endDate.getMonth());
				if(todayDate.getMonth()<endDate.getMonth()){
					if (todayDate.getMonth()==11){
						todayDate.setFullYear(todayDate.getFullYear());
					}
					todayDate.setMonth(todayDate.getMonth()+1);
				}else{
					todayDate.setMonth(thisMonth);
				}
				todayDate.setDate(1);
				newDay();
				getCalendar();
			}
		});
	}else{
		todayDate.setMonth(thisMonth);
		todayDate.setDate(1);
		newDay();
		getCalendar();
	}
			dayPics=thumbCount.length;
				for (var i=0;i<dayPics;i++){
					var dotTxt="<img id='pic"+i+"' src='images/whitedot.png' class='bluedot'>";
					$("#picdiv"+ddInt).append(dotTxt);
				}
				//$("#pic"+picNo).css({'-webkit-filter':'hue-rotate(180deg)'});
				$("#pic"+picNo).attr('src','images/reddot.png');
				$(".picCounter").css({"padding":"1px"});
				sym.$("subjectTxt").stop().animate({scrollTop:$(".listNow").position().top},200)

//Edit to limit dates shown to thisMonth+1

			sym.$("arrowleft").unbind(clickEventType);
			sym.$("arrowright").unbind(clickEventType);
			if((todayDate<=startDate)&&(parseInt(dd,10)==eventDates[0])&&(picNo==0)==true){sym.$("arrowleft").fadeOut(0); }else{ sym.$("arrowleft").fadeIn(0); }
			if((todayDate.getMonth()==endDate.getMonth())&&(parseInt(dd,10)==eventDates[eventDates.length-1])&&(picNo==(thumbCount.length-1))==true||(todayDate>=endDate&&picNo==thumbCount.length-1)){sym.$("arrowright").fadeOut(0); }else{ sym.$("arrowright").fadeIn(0); }
			if (thumbCount.length>1){
				if(picNo==0){
										sym.$("arrowleft").on(clickEventType, picChange);
				}else if(picNo>0){
										sym.$("arrowleft").on(clickEventType, eventChange);
				}
				if(picNo+1<thumbCount.length){
										sym.$("arrowright").on(clickEventType, eventChange);
				}else if(picNo+1==thumbCount.length){
										sym.$("arrowright").on(clickEventType, picChange);
				}
			}else{
				sym.$("arrowleft").on(clickEventType, picChange);
				sym.$("arrowright").on(clickEventType, picChange);
			}
	},
error: function(){
				//Otherwise show previous month
				console.log("Error");
				todayDate.setMonth(todayDate.getMonth()-1);
				todayDate.setDate(1);
				getCalendar();
				newDay();
	}

});
}

function getMonthf(){ //change the month
	eventDates.length=0;
	$.ajax({	
		type: "GET",
		url: yyyy+"/"+mm+"/GN"+gnNo+"_"+mm+".xml",
		//cache:false,
		dataType: "xml",
		success: function (xml){
			todayDate.setYear(todayDate.getFullYear());
			sym.$("subjectTxt").empty();	
			sym.$("headlineTxt").html(headline+" "+months[parseInt(mm,10)-1]);//Dec 8 moved from getCalendar
			eventCount.length=0;
			$(xml).find("day").each(function(){
				var $eventdate = $(this);
				//create events for month on calendar and matching thumbnails
				var eDate=$eventdate.attr("date");
				var ddselect = eDate.substr(0,2);
				var mmselect = eDate.substr(3,2);
								var yselect = yyyy;
				eventDates.push(parseInt(ddselect,10));
				$('#mm'+(parseInt(mmselect,10)-1)+'dd'+parseInt(ddselect,10)).addClass('select');
				$('#mm'+(parseInt(mmselect,10)-1)+'dd'+parseInt(ddselect,10)).on(clickEventType, calDate($eventdate.attr("date"),todayDate.getFullYear(),0));
				var listTxt = "<div class='list' id='listdd"+parseInt(ddselect,10)+"'>";
				listTxt += "</div>";
				sym.$('subjectTxt').append(listTxt);
					var $events = $eventdate.find('event');
					$events.each(function(){
						var $event = $(this);
						var id=$event.attr('order');
						eventCount.push($event);
						var listItem = "<div class='listsubject' id='dd"+(parseInt(ddselect,10))+"id"+id+"'>"+$event.find('subject').text()+"</div>";
						$('#listdd'+parseInt(ddselect,10)).prepend(listItem);
						$("#dd"+(parseInt(ddselect,10))+"id"+id).on(clickEventType, calDate($eventdate.attr("date"),todayDate.getFullYear(),id));
					});
					var listDate = "<div class='listdate'>"+(parseInt(ddselect,10))+"</div>";
					$('#listdd'+parseInt(ddselect,10)).prepend(listDate);
			});
			if(RTL==true){
			$(".list").css({'text-align':'right','direction':'rtl','width':'98%','padding-bottom':'8px','padding-right':'2%','margin':'5px 0','display':'table','font-family':'pragmatica-web,sans-serif','font-size':Math.round(fontSize*0.9)+'px','border-bottom':'groove 2px rgb(200,200,200)'});
			$(".listdate").css({'text-align':'center','width':'25px','background-color':'rgb(255,255,255)',
									'font-size':Math.round(fontSize*1.05)+'px','font-weight':700,'float':'right',
									'border':'solid 1px rgb(100,100,100)','border-radius':'4px',
									'box-shadow':'-1px -1px 2px rgb(150,150,150)','margin-right':'8px',});
			$(".listsubject").css({'margin-right':'40px','width':'500px','font-weight':400,'font-size':'100%','cursor':'pointer'});
			}else{
			$(".list").css({'width':'98%','padding-bottom':'8px','padding-left':'2%','margin':'5px 0','display':'table','font-family':'pragmatica-web,sans-serif','font-size':Math.round(fontSize*0.9)+'px','border-bottom':'groove 2px rgb(200,200,200)'});
			$(".listdate").css({'text-align':'center','width':'25px','background-color':'rgb(255,255,255)',
									'font-size':Math.round(fontSize*1.05)+'px','font-weight':700,'float':'left',
									'border':'solid 1px rgb(100,100,100)','border-radius':'4px',
									'box-shadow':'-1px -1px 2px rgb(150,150,150)','margin-right':'8px',});			
			$(".listsubject").css({'margin-left':'35px','width':'500px','font-weight':400,'font-size':'100%','cursor':'pointer'});
			}
			$(".select").css({'font-weight':700,'color':'rgb(0,0,0)','background-color':'rgb(255,255,255)','cursor':'pointer'});
			getDay();
		}
	});
}

var calDate = function(dateselect,yrC,thumbno){ //sets new date, number of events per date
	return function (e) {
					//change class of selcted event date to highlight with border/bg-color
					$('.monthNow').removeClass('dayNow');
					$(".select").css({'font-weight':700,'color':'rgb(0,0,0)','background-color':'rgb(255,255,255)','cursor':'pointer'});
					$(".listsubject").removeClass('listNow');
					$(".listsubject").css({'font-weight':400});
					var dayC = parseInt(dateselect.substr(0,2),10);
					var monthC = parseInt(dateselect.substr(3,2),10)-1;					
					todayDate = new Date(yrC,monthC,dayC);
					picNo=thumbno;
					newDay();
					getDay();
					e.preventDefault();
					e.stopPropagation();
	};
};

var thumbsCount = 0;
// HEADER DAY BTNS
sym.$("arrowleft").css({'cursor':'pointer'});
sym.$("arrowright").css({'cursor':'pointer'});

function eventChange(){
e.preventDefault();
e.stopPropagation();
var selected=$(this).attr("id");
if (selected=="Stage_arrowright"){
 picNo=picNo+1;
}else if(selected=="Stage_arrowleft"){
 picNo=picNo-1;
}
getDay();
}

function picChange(){
	e.preventDefault();
	e.stopPropagation();
	var selected=$(this).attr("id");
	var loadFunction;
	picNo=0;	
	$('.monthNow').removeClass('dayNow');
	$(".select").css({'font-weight':700,'color':'rgb(0,0,0)','background-color':'rgb(255,255,255)','cursor':'pointer'});	
	$(".listsubject").removeClass('listNow');
	$(".listsubject").css({'font-weight':400,});
	for (var i=0;i<eventDates.length; i++){
		if(eventDates[i]==day){
			if (selected=="Stage_arrowright"){
				if(i==eventDates.length-1){
					if (eventDates[i]==numOfDays){
						todayDate.setDate(todayDate.getDate()+1);
					}else{
						todayDate.setMonth(todayDate.getMonth()+1);
						todayDate.setDate(1);					
					}
					loadFunction=0;
				}else{
					todayDate.setDate(eventDates[i+1]);
					loadFunction=1;
				}
			}else if(selected=="Stage_arrowleft"){
				if(i==0){
					todayDate.setMonth(todayDate.getMonth()-1);
					todayDate.setDate(1);
					loadFunction=0;			
				}else{
					todayDate.setDate(eventDates[i-1]);
					loadFunction=1;	
				}
			}
		}
	}
	newDay();
	if(loadFunction==0){getCalendar();}else if(loadFunction==1){getDay();}
}

//CREATE CALENDAR
function getCalendar(){
		sym.getSymbol("calendar").$("calTxt").empty();
		var yrC = todayDate.getFullYear();
		var monthC = todayDate.getMonth();
		var leap;
		if (monthC==1){
			leap=((parseInt(yrC)%4 == 0)&&(parseInt(yrC)%100 != 0)||(parseInt(yrC)%400 == 0))?"true":"false";
			var febDays=(leap=="true")?29:28;
		}
		var daysPerMonth = ["31",febDays,"31","30","31","30","31","31","30","31","30","31"];
		numOfDays = daysPerMonth[monthC];
		var calContent="";
		var calendarBody="";
		var counter=1;
		while (counter <= numOfDays){		
			calContent +="<td class='cal'><div id='mm"+monthC+"dd"+counter+"' class='monthNow'>"+counter+"</div><div id='picdiv"+counter+"' class='picCounter'></div></td>";    			
			counter++;
		}
		// building the calendar html body.
		calendarBody +="<table class='calendar' width='100%'>";
		if(RTL==true){
			calendarBody += "<tr height='13px' class='rtl'>";
		}else{
			calendarBody += "<tr height='13px'>";
		}
		calendarBody += calContent;
		calendarBody += "</tr></table>";
		sym.getSymbol("calendar").$("calTxt").append(calendarBody);
		$(".calendar").css({'font-family':'pragmatica-web,arial,sans-serif', 'font-size':'11px','width':'100%','border-collapse':'collapse'});
		$(".rtl").css({'transform':'scaleX(-1)','-webkit-transform':'scaleX(-1)','-ms-transform':'scaleX(-1)'});
		if(RTL==true){
		$(".monthNow").css({'transform':'scaleX(-1)','-webkit-transform':'scaleX(-1)','-ms-transform':'scaleX(-1)','color':'rgb(150,150,150)','background-color':'rgba(255,255,255,0.5)'});
		}else{
		$(".monthNow").css({'color':'rgb(150,150,150)','background-color':'rgba(255,255,255,0.5)'});
		}
		$(".cal").css({'vertical-align':'text-top','text-align':'center','width':'19px','padding':0});
		getMonthf();
}
getCalendar();

var showlist=0;
sym.$('listBtn').on(clickEventType,showList);
sym.$('listBtn').css({'cursor':'pointer'})
function showList(){
 if (showlist==0){
 sym.$('subjectTxt').fadeIn(400);
$('#Stage_arrows').fadeOut(0);
 showlist=1;
 }else if (showlist==1){
 sym.$('subjectTxt').fadeOut(400);
$('#Stage_arrows').fadeIn(0);
 showlist=0;
 }
}
//SPIN THE GLOBE
function spinGlobe(){
		var x=-590-(parseInt(xTxt,10)*100/72);
		var xBack=-420+(parseInt(xTxt,10)*100/72);
		var y =105-(Math.sin((yTxt)*Math.PI*0.74/180)*115);
		sym.getSymbol("globe").getSymbol("continents").$("mapFront").stop().animate ({left:x}, 700, 'easeOutCubic');
		sym.getSymbol("globe").getSymbol("continents").$("mapBack").stop().animate ({left:xBack}, 700, 'easeOutCubic');
		sym.getSymbol("globe").$("dot0").stop().animate({'top':y},700, 'easeOutElastic');
}

sym.$('arrows').on('mouseover',function(){ sym.$('arrows').stop().animate({'opacity':1.0},200); });
sym.$('arrows').on('mouseout',function(){ sym.$('arrows').stop().animate({'opacity':0},200); });
setTimeout(function(){ sym.$('arrows').animate({'opacity':0},200); },800);
$(".continents").css({"-webkit-border-radius":"50%","-khtml-border-radius":"50%","-moz-border-radius":"50%","border-radius":"50%",
"-webkit-mask-image": "-webkit-gradient(linear, left top, right top, color-stop(0, rgba(0,0,0,1)), color-stop(1, rgba(0,0,0,1)))"});

if(RTL==true){
sym.$("globe").animate({"left":"-412px"},0);
sym.$("space").animate({"left":"10px"},0);
sym.$("creditTxt").css({"left":"10px","text-align":"left"});
sym.$("textBox").css({'text-align':'right'});
sym.$("arrows").css({'transform':'scaleX(-1)','-webkit-transform':'scaleX(-1)','-ms-transform':'scaleX(-1)'});
}

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


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

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

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

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

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

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

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

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

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

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

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

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

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

      

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

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

      

   })("continents-animation");
   //Edge symbol end:'continents-animation'

   //=========================================================

   //=========================================================
   
   //Edge symbol: 'flatmap_two'
   (function(symbolName) {   
   
   })("flatmap-two");
   //Edge symbol end:'flatmap-two'

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

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