//used by embedded script to display events
var today = "";
var recurring = "";
var days = "";
var savedays = "";
var weeks = "";
var saveweeks = "";
var gbstartdate = "";
var gbenddate = "";
var realstart = "";
var realend = "";
var startday = "";
var endday = "";
var tempweeks1 = "";
var tempweeks2 = "";
var tempdays1 = "";
var tempdays2 = "";
var iid = "";
var wrapperID = "";
var selectorID = "";
var sid = "";
var weburl = "";

function loopitems(){	
	//loop through all event items
	
	$( ".eventitem" ).each(function(i){
		//get values from within the event item spans
		
		recurring = $(this).children(".deets").children(".recurring").text();
		days = $(this).children(".deets").children(".days").text();
		savedays = days;
		weeks = $(this).children(".deets").children(".weeks").text();
		saveweeks = weeks;
		gbstartdate = $(this).children(".deets").children(".gbstartdate").text();
		gbenddate = $(this).children(".deets").children(".gbenddate").text();
		realstart = new Date(FormatDate2010(gbstartdate));
		realend = new Date(FormatDate2010(gbenddate));
		startday = GetDayOfTheWeek(realstart);
		endday = GetDayOfTheWeek(realend);
		itemnumber = $(this).children(".deets").children(".itemnumber").text()
		today = new Date();
		weburl =  $(this).children(".deets").children(".weburl").text()
		
		
		//display website if needed
		if(!weburl == ""){
		$(this).children(".web").children(".url").html(formatURL(weburl));
		}else{
			$(this).children(".web").hide();
		}
		
		
		//handle the weeks per month: convert to 'st', 'nd' and 'th', insert spaces after commas and use an & in place of the last comma
		//so instead of 1,2,3,4 we get 1st, 2nd, 3rd & 4th
		weeks = weeks.replace("1","1st");
		weeks = weeks.replace("2","2nd");
		weeks = weeks.replace("3","3rd");
		weeks = weeks.replace("4","4th");
		//use regex global replace to replace all commas with comma and space
		weeks = weeks.replace(/,/g,", ");
		//same with days
		days = days.replace(/,/g,", ");
		
		
		//replace last occurance of the comma with and ampersand by creating to new substrings
		//tempweeks1 contains everything from weeks up to the last comma
		//tempweeks2 contains the last comma and last week number. We replace the comma in this var with the & and concat them back together
		tempweeks1 = weeks.substring(0,weeks.lastIndexOf(","));
		tempweeks2 = weeks.substring(weeks.lastIndexOf(",")).replace(","," &");
		weeks = tempweeks1.concat(tempweeks2);
		
		//same with the days of the week
		tempdays1 = days.substring(0,days.lastIndexOf(","));
		tempdays2 = days.substring(days.lastIndexOf(",")).replace(","," &");
		days = tempdays1.concat(tempdays2);
		
		var date_info = $(this).children(".dates");
		var when_holder = $(this).children(".dates").children("p").children(".when");
		
		//console.log("start if");
		if (recurring == "Yes"){
			//console.log("recurring");
			if (saveweeks == "1,2,3,4"){
				//console.log("weeks = 1234");
		
				if(gbenddate !== "01/01/2020" && gbenddate !== "01-Jan-2020"){
					//console.log("end NOT 01/01/2020");
					//never ending events are given the end date of 2020
					if(days == "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday & Sunday"){
						//every day
						if(realstart<today){
							//already started - don't show from date
							$(when_holder).html("Every day until " + realend.toDateString());
						} else {
							//starts in the future - show from date
							$(when_holder).html("Every day between "+ realstart.toDateString() + " and " + realend.toDateString());
						}
					}else{
						//not every day
						if(realstart<today){
							//already started - don't show from date
							$(when_holder).html("Every " + days + " until " + realend.toDateString());
						} else {
							//show from date - starts in the future
							$(when_holder).html("Every " + days + " between "+ realstart.toDateString() + " and " + realend.toDateString());
						}	
					}
				}else{
					//console.log("end = 01/01/2020");
					//never ending so dont mention end date
					if(days == "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday & Sunday"){
						//every day
						if(realstart<today){
							//already started - don't show from date
							$(when_holder).html("Every day");
						} else {
							//show from date - starts in the future
							$(when_holder).html("Every day, starting "+ realstart.toDateString());
						}
					}else{
							//not every day
								if(realstart<today){
									//already started - don't show from date
									$(when_holder).html("Every " + days);
								} else {
									$(when_holder).html("Every " + days + ", starting "+ realstart.toDateString());
								}
						}
						
				}
		
			}else{
				//console.log("week NOT 1234");
				if(gbenddate !== "01/01/2020" && gbenddate !== "01-Jan-2020"){
					//console.log("end NOT 01/01/2020");
					//never ending events are given the end date of 2020
					if(realstart<today){
						//already started - don't show from date
						$(when_holder).html("Every " + weeks + " " + days + " of the month until " + realend.toDateString());
					} else {
						$(when_holder).html("Every " + weeks + " " + days + " of the month from " + realstart.toDateString() + " until " + realend.toDateString());
					}
				}else{
					//never ending so dont mention end date
					//console.log("end = 01/01/2020");
					if(realstart<today){
						//already started - don't show from date
						$(when_holder).html("Every " + weeks + " " + days + " of the month");
					} else {
						$(when_holder).html("Every " + weeks + " " + days + " of the month, starting " + realstart.toDateString());
					}
				}
		
			}
		
		}else{
			//console.log("NOT recurring");
			//not recurring
			//if one day, only show that day/date
			if(gbstartdate == gbenddate){
				//is only one day
				//console.log("one day event");
				$(when_holder).html(realstart.toDateString());
			}else{
				//console.log("not one day event");
				if(gbenddate !== "01/01/2020" && gbenddate !== "01-Jan-2020"){
					if(realstart<today){
						//already started - don't show from date
						$(when_holder).html("Daily, until " + realend.toDateString());
					} else {
						$(when_holder).html("Starting " + realstart.toDateString() + ", through until " + realend.toDateString());
					}
				} else{
					if(realstart<today){
						//already started - don't show from date
						$(when_holder).html("Daily");
					} else {
						$(when_holder).html("Daily, from.. " + realstart.toDateString());
					}
				}
			};
		
		};
		
		if($(when_holder).html() === ""){
			$(when_holder).html("No information supplied");
		}
		
		//$(this).show("fast");
	
	});

}


function formatURL(url) {
	url = url.replace("http://","");
	url = url.replace("www.","");
	url = "http://" + url;
	var newurl = "<a href='" + url + "' target='_blank'>" + url + "</a>";
	return newurl;
}

function FormatDate2010(d)
{
	var strDate = d;
	var arrDate = new Array();
	arrDate = strDate.split("-");
	var fullMonth = "";
	
	//return the full month
	switch (arrDate[1])
{
case "Jan":
  fullMonth = "January";
  break;
case "Feb":
  fullMonth = "Feb";
  break;
  case "Mar":
  fullMonth = "Mar";
  break;
  case "Apr":
  fullMonth = "April";
  break;
  case "May":
  fullMonth = "May";
  break;
  case "Jun":
  fullMonth = "June";
  break;
  case "Jul":
  fullMonth = "July";
  break;
  case "Aug":
  fullMonth = "August";
  break;
  case "Sep":
  fullMonth = "September";
  break;
  case "Oct":
  fullMonth = "October";
  break;
  case "Nov":
  fullMonth = "November";
  break;
  case "Dec":
  fullMonth = "December";
  break;
default:
   fullMonth = "January";
}

var newdate = fullMonth + " " + arrDate[0] + "," + arrDate[2];
return newdate;

}



$(document).ready(function(){
	loopitems();
});




