/***********************
* 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 gnNo=32496;
         //DATE STUFF
         var todayDate;
         var nextDate;
         function getToday(){
         todayDate = new Date();
         nextDate = new Date(todayDate.getDate()+1);
         }
         getToday();
         
         var headline;
         var mainPic = sym.$("otdImg0");
         var hiddenPic = sym.$("otdImg1");
         var nextPic = sym.$("otdImg2");
         var month;
         var months;
         var day;
         var days;
         var nextmonth;
         var nextday;
         var mm;
         var dd;
         var nextmm;
         var nextdd;
         var picturetxt;
         var picture;
         
         function getImages(){
         month = todayDate.getMonth();
         day = todayDate.getDate();
         mm = (month+1<10)?"0"+(month+1): (month+1)+ "";
         dd = (day<10)?"0" + day: day + "";
         nextmonth = nextDate.getMonth();
         nextday = nextDate.getDate();
         nextmm = (nextmonth+1<10)?"0"+(nextmonth+1): (nextmonth+1)+ "";
         nextdd = (nextday<10)?"0" + nextday: nextday + "";
         hiddenPic.attr("src", mm +"/"+ mm + "_" + dd +".jpg");
         nextPic.attr("src", nextmm +"/"+ nextmm + "_" + nextdd +".jpg");
         }
         getImages();
         mainPic.attr("src", mm +"/"+ mm + "_" + dd +".jpg");
         
         function mainXml(){
         $.ajax({	
         	type: "GET",
         	url: "GN"+gnNo+".xml",
         	dataType: "xml",
         	success: function(xml) {
         		headline = $(xml).find('headline').text();
         		sym.$("headlineTxt").html(headline);
         		var string = $(xml).find('monthnames').text();
         		months = string.split(",");
         		sym.$("dateTxt").html(months[month]+" "+day);
         		picturetxt = $(xml).find('picture').text();
         		var string2 = $(xml).find('daynames').text();
         		days = string2.split(",");
         		}
         });
         }
         mainXml();
         function changeDate(){
         $.ajax({	
         	type: "GET",
         	url: mm+"/GN"+gnNo+"_"+mm+".xml",
         	dataType: "xml",
         	success: function (xml){
         	sym.$("textBox").empty();
         	$(xml).find("day").each(function(){
         								var $eventdate = $(this);
         								if ($eventdate.attr("date")==dd+"-"+mm){
         								picture = $eventdate.find('picture').text();
         								sym.$("pictureTxt").html(picturetxt + picture);
         								var $events = $eventdate.find('event');
         								$events.each(function(){														
         									var $event = $(this);
         									var cap2 = $event.find('year').text();                   
         									var cap3 = $event.find('caption').text();
         									var html = "<div style='clear:both; margin-bottom:5px'><div style='float:left; font-size:24px; font-weight:600; color:rgba(0,0,0,0.5); text-shadow:2px 2px rgba(255,255,255,1.0); padding-right:10px; padding-top:5px'>"+cap2+"</div><div style='font-family:pragmatica-web, sans-serif; font-size:14px'>"+cap3+"</div></div><hr>";
         									sym.$("textBox").append(html);
         									}); 
         								} 
         		});
         	}
         });
         }
         changeDate();
         
         menuArray = [];
                  for (var m = 0; m < 2; m++){
                  	 var menuBtnSymbols = sym.getSymbol("mainBtn" +m);
                  	 menuBtnSymbols.setVariable("menuId", m);
                  	 menuArray.push(menuBtnSymbols);
                  }
                  //a 'for each' loop through the newly populated array.
                  $.each(menuArray, function( count, btnInTheArray ){
                  	 var menuBtnItem = $(btnInTheArray);
                  	 var menuBtnElement = btnInTheArray.getSymbolElement();
                  	 menuBtnElement.bind ("click", {menuId:count}, menuBtnClick);
                  	 menuBtnElement.bind ("mouseover", {menuId:count}, menuBtnOver);
                  	 menuBtnElement.bind ("mouseout", {menuId:count}, menuBtnOut);
                  	}
                  );
         var imgPos = 0;
                function menuBtnClick (e){
                  	var selectedMenu_id = (e.data.menuId); //get the id of the clicked item
         		 if (imgPos==0){
         						imgPos=1;
         						mainPic= sym.$("otdImg0");
         						hiddenPic = sym.$("otdImg1");
         				}else{
         						imgPos=0;
         						mainPic= sym.$("otdImg1");
         						hiddenPic = sym.$("otdImg0");
         				}
         				nextPic =  sym.$("otdImg2");
                  	if (selectedMenu_id ==0){ //back button
         					todayDate.setDate(todayDate.getDate()-1);
         					nextDate.setDate(todayDate.getDate()-2);
         					getImages();
         					//hiddenPic.animate({left:0, width:0, opacity:0.2},0);
         					//hiddenPic.animate({width:320, opacity:1.0},500);
         					hiddenPic.stop().animate({left:0, width:0, opacity:0.2},0,function(){ hiddenPic.animate({width:320, opacity:1.0},500);});
         					mainPic.stop().animate({left:320, width:0, opacity:0.2},500);
         				}else if (selectedMenu_id ==1){ //forward button
         					todayDate.setDate(todayDate.getDate()+1);
         					nextDate.setDate(todayDate.getDate()+2);
         					getImages();
         					//hiddenPic.animate({left:320, width:0, opacity:0.2},0);
         					//hiddenPic.animate({left:0, width:320, opacity:1.0},500);
         					hiddenPic.stop().animate({left:320, width:0, opacity:0.2},0,function(){hiddenPic.animate({left:0, width:320, opacity:1.0},500);});
         					mainPic.stop().animate({left:0, width:0, opacity:0.2},500);
         				}
         				month = todayDate.getMonth();
         				day = todayDate.getDate();
         				mm = (month+1<10)?"0"+(month+1): (month+1)+ "";
         				dd = (day<10)?"0" + day: day + "";
         				sym.$("dateTxt").html(months[month]+" "+day);
         				changeDate();
         				e.preventDefault();
                  	}
         function menuBtnOver (e){
                  var selectedMenu_id = (e.data.menuId);
                  sym.getSymbol("mainBtn"+selectedMenu_id).stop(0);
                  }
         function menuBtnOut (e){
                  var selectedMenu_id = (e.data.menuId);
                  sym.getSymbol("mainBtn"+selectedMenu_id).play();
                  }  	
         var textShow = "hide";
         sym.$("textBox").bind('click', function(){
         		if (textShow=="hide"){
         		sym.$("otdImg0").animate({height:43},400);
         		sym.$("otdImg1").animate({height:43},400);
         		sym.$("mainBtn0").animate({top:-257},400);
         		sym.$("mainBtn1").animate({top:-257},400);
         		sym.$("headlineTxt").animate({top:5},400);
         		sym.$("dateTxt").animate({top:42},400);
         		sym.$("textBox").animate({top:64, height:374},400);
         		sym.$("bg1").animate({top:64, height:374},400);
         		sym.$("imgBorder").hide();;
         		textShow="show";
         		}else{
         		sym.$("otdImg0").animate({height:299},400);
         		sym.$("otdImg1").animate({height:299},400);
         		sym.$("mainBtn0").css({top:0});
         		sym.$("mainBtn1").css({top:0});
         		sym.$("headlineTxt").animate({top:262},400);
         		sym.$("dateTxt").animate({top:299},400);
         		sym.$("textBox").animate({top:318, height:120},400);
         		sym.$("bg1").animate({top:318, height:120},400);
         		sym.$("imgBorder").delay(400).fadeIn(200);
         		textShow="hide";
         		}
         		e.preventDefault();
         });
         //Calendar
         var calShow="hide";
         sym.$("calBtn").bind('click', calShowHide);
         sym.getSymbol("calendar").$("hideCal").bind('click',calShowHide);
         function calShowHide(){
         if(calShow=="hide"){
         sym.$("calendar").fadeIn(400);
         sym.$("calBtn").fadeOut(400);
         calShow="show";getCalendar();
         }else{
         sym.$("calendar").fadeOut(400);
         sym.$("calBtn").fadeIn(400);
         calShow="hide";
         }
         e.preventDefault();
         }
         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"];
         	var nextMonthC = monthC+1; //+1; //Used to match up the current month with the correct start date.
         	var prevMonthC = monthC-1;
         	 // days in previous month and next one , and day of week.
         	var firstOfMonth = new Date(yrC,monthC,1);
         	var weekdays= firstOfMonth.getDay();
         	var weekdays2 = weekdays;
         	var numOfDays = daysPerMonth[monthC];
         	var calContent="";
         	var calendarBody="";
         	  sym.getSymbol("calendar").$("monthTxt").text(months[monthC]);
         var counter=1;
         // leave blank for days of previous month
         while (weekdays>0){
             calContent += "<td class='monthPre'></td>";
             weekdays--;
          }
          while (counter <= numOfDays){
             if (weekdays2 > 6){ // start new line.
                 weekdays2 = 0;
                 calContent += "</tr><tr height='32px'>";
             }
             if (counter == day){ // if counter is current day.
                 calContent +="<td id='mm"+monthC+"dd"+counter+"' class='dayNow'>"+counter+"</td>";
             }else{
                 calContent +="<td id='mm"+monthC+"dd"+counter+"' class='monthNow'>"+counter+"</td>";    
             }
         	weekdays2++;
             counter++;
          }
          // building the calendar html body.
         calendarBody +="<table class='calendar' style='font-family:pragmatica-web,sans-serif; font-size:14px' width='100%'><tr class='daysOfWeek'><td>"+days[0]+"</td><td>"+days[1]+"</td><td>"+days[2]+"</td><td>"+days[3]+"</td><td>"+days[4]+"</td><td>"+days[5]+"</td><td>"+days[6]+"</td></tr>";
          calendarBody += "<tr height='32px'>";
          calendarBody += calContent;
          calendarBody += "</tr></table>";
          sym.getSymbol("calendar").$("calTxt").append(calendarBody);
          $(".monthNow").css({'background-color':'rgb(255,255,255)','text-align':'center'});
          $(".daysOfWeek td").css({'width':'14%','font-weight':700,'color':'rgb(100,100,100)','text-align':'center'});
          $(".dayNow").css({'color':'rgb(255,255,255)','background-color':'rgb(255,0,0)','text-align':'center','font-weight':700});
          for (i=0; i< numOfDays+1; i++){  
            $("#mm"+monthC+"dd"+i).css({"cursor":"pointer"});
             $("#mm"+monthC+"dd"+i).click(calDate(yrC,monthC,i));
         	}
         }
         var calDate = function(yrC,monthC,dayC){ //to handle lexical scope
         	return function (e) {
         					var oldDate = todayDate;
         					todayDate = new Date(yrC,monthC,dayC);
         					nextDate = new Date(yrC,monthC,dayC+1);
         					getImages();
         					imageSwap(oldDate,todayDate);
         					e.preventDefault();
         	};
         };
         //Home button
         sym.getSymbol("calendar").$("homeBtn").bind('click', goHome);
         sym.getSymbol("calendar").$("homeBtn").css({'cursor':"pointer"});
         function goHome(){
         	var oldDate = todayDate;
         	var homeDate = new Date();
         	var nextHome = new Date(homeDate.getDate()+1);
         	todayDate= homeDate;
         	nextDate = nextHome;
         	getImages();
         	imageSwap(oldDate,todayDate);
         	e.preventDefault();
         }
         function imageSwap(oldDate,todayDate){
         	if (oldDate<todayDate){
         	//hiddenPic.animate({left:320, width:0, opacity:0.2},0);
         	//hiddenPic.animate({left:0, width:320, opacity:1.0},500);
         	hiddenPic.animate({left:320, width:0, opacity:0.2},0,function(){hiddenPic.animate({left:0, width:320, opacity:1.0},500);});
         	mainPic.animate({left:0, width:0, opacity:0.2},500);					
         	}else if (oldDate>todayDate){
         	//hiddenPic.animate({left:0, width:0, opacity:0.2},0);
         	//hiddenPic.animate({width:320, opacity:1.0},500);
         	hiddenPic.animate({left:0, width:0, opacity:0.2},0,function(){hiddenPic.animate({width:320, opacity:1.0},500);});
         	mainPic.animate({left:320, width:0, opacity:0.2},500);					
         	}
         	sym.$("dateTxt").html(months[month]+" "+day);
         	changeDate();
         	sym.$("calendar").fadeOut(400);
         	sym.$("calBtn").fadeIn(400);
         	calShow="hide";
         }
         //forward/back buttons	
         var calArray = ["monthBtn0","monthBtn1"];
         for (var i=0; i<2; i++){
         		sym.getSymbol("calendar").$("monthBtn"+i).bind("click",calMonth);
         		sym.getSymbol("calendar").$("monthBtn"+i).bind("mouseover",calOver);
         		sym.getSymbol("calendar").$("monthBtn"+i).bind("mouseout",calOut);
         		sym.getSymbol("calendar").$("monthBtn"+i).css({"cursor":"pointer"});
         		}
         function calMonth() {
         			var selected_id = $(this).attr("id");
         			if(selected_id == "Stage_calendar_monthBtn0"){
         				todayDate.setMonth(todayDate.getMonth()-1);
         			}else if(selected_id == "Stage_calendar_monthBtn1"){
         				todayDate.setMonth(todayDate.getMonth()+1);
         			}
         			getCalendar();
         }
         function calOver() {
         			$(this).css({"opacity":0.5});	
         }
         function calOut() {
         			$(this).css({"opacity":1.0});	
         }
         //TOUCH SWIPE CONTROLS
         var swipeStartX;
         var swipeEndX;
         sym.$("Stage").bind("touchstart", swipeStart);
         sym.$("Stage").bind("touchend", swipeEnd);
         function swipeStart(e){	
         	if ( e.pageX == undefined ) {         
            var touch = e.originalEvent.touches [0] || e.originalEvent.changedTouches [0];        
            swipeStartX = touch.pageX;
         	}
         }
         function swipeEnd(e){
                  if ( e.pageX == undefined ) {        
                  	var touch = e.originalEvent.touches [0] || e.originalEvent.changedTouches [0];
                  	swipeEndX = touch.pageX;
                  	onSwipe();
                  }
         }
         var swipePadding=60;
         var onSwipe = function(){	
                  	if (imgPos==0){
         						imgPos=1;
         						mainPic= sym.$("otdImg0");
         						hiddenPic = sym.$("otdImg1");
         						nextPic =  sym.$("otdImg2");
         				}else{
         						imgPos=0;
         						mainPic= sym.$("otdImg1");
         						hiddenPic = sym.$("otdImg0");
         						nextPic =  sym.$("otdImg2");
         				}
         				// swipe right (scrub to left)
                  	if( swipeStartX < swipeEndX - swipePadding )
                  	{
                  		todayDate.setDate(todayDate.getDate()-1);
         					nextDate.setDate(todayDate.getDate()-2);
         					getImages();
         					//hiddenPic.animate({left:0, width:0, opacity:0.2},0);
         					//hiddenPic.animate({width:320, opacity:1.0},500);
         					hiddenPic.animate({left:0, width:0, opacity:0.2},0,function(){hiddenPic.animate({width:320, opacity:1.0},500);});
         					mainPic.animate({left:320, width:0, opacity:0.2},500);
                  	// swipe left (scrub to right)
                  	}else if( swipeEndX < swipeStartX - swipePadding){
         					todayDate.setDate(todayDate.getDate()+1);
         					nextDate.setDate(todayDate.getDate()+2);
         					getImages();
         					//hiddenPic.animate({left:320, width:0, opacity:0.2},0);
         					//hiddenPic.animate({left:0, width:320, opacity:1.0},500);
         					hiddenPic.animate({left:320, width:0, opacity:0.2},0,function(){hiddenPic.animate({left:0, width:320, opacity:1.0},500);});
         					mainPic.animate({left:0, width:0, opacity:0.2},500);
                  	}
                  	month = todayDate.getMonth();
         				day = todayDate.getDate();
         				mm = (month+1<10)?"0"+(month+1): (month+1)+ "";
         				dd = (day<10)?"0" + day: day + "";
         				sym.$("dateTxt").html(months[month]+" "+day);
         				changeDate();
         				e.preventDefault();
                  }
         sym.$("imgBorder").css({"border-style":"solid","border-width":"5px","border-top-color":"rgba(255,255,255,0.9)","border-right-color":"rgba(100,100,100,0.5)","border-bottom-color":"rgba(0,0,0,0.5)","border-left-color":"rgba(204,204,204,0.5)"});
         sym.$("mainBorder").css({"border-style":"solid","border-width":"5px","border-top-color":"rgba(255,255,255,0.9)","border-right-color":"rgba(100,100,100,0.5)","border-bottom-color":"rgba(0,0,0,0.5)","border-left-color":"rgba(204,204,204,0.5)"});

      });
      //Edge binding end

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

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

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

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

   //=========================================================
   
   //Edge symbol: 'arrow_down'
   (function(symbolName) {   
   
      Symbol.bindTriggerAction(compId, symbolName, "Default Timeline", 4000, function(sym, e) {
         // play the timeline from the given position (ms or label)
         sym.play(0);

      });
      //Edge binding end

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

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

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