//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 = "";

$j(document).ready(function(){
	loopitems();
});

function loopitems(){	
	//loop through all event items
	$j( ".eventitem" ).each(function(i){
	
		
		//get values from within the event item spans
		
		recurring = $j(this).children(".deets").children(".recurring").text();
		days = $j(this).children(".deets").children(".days").text();
		savedays = days;
		weeks = $j(this).children(".deets").children(".weeks").text();
		saveweeks = weeks;
		gbstartdate = $j(this).children(".deets").children(".gbstartdate").text();
		gbenddate = $j(this).children(".deets").children(".gbenddate").text();
		realstart = new Date(FormatDate(gbstartdate));
		realend = new Date(FormatDate(gbenddate));
		startday = GetDayOfTheWeek(realstart);
		endday = GetDayOfTheWeek(realend);
		itemnumber = $j(this).children(".deets").children(".itemnumber").text()
		today = new Date();
		weburl =  $j(this).children(".deets").children(".weburl").text()
		
		if(FormatDate(gbstartdate)<today){
			alert("already started");
		}
		
		
		//display website if needed
		if(!weburl == ""){
		$j(this).children(".web").children(".url").html(formatURL(weburl));
		}else{
			$j(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);
		
		
		
		//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"){
					//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
						$j(this).children(".dates").children(".when").html("Every day.<br/>Between "+ realstart.toDateString() + " and " + realend.toDateString());
					}else{
						//not every day
						$j(this).children(".dates").children(".when").html("Every " + days + " .<br/>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
						$j(this).children(".dates").children(".when").html("Every day.<br/>Starting "+ realstart.toDateString());
					}else{
							//not every day
							$j(this).children(".dates").children(".when").html("Every " + days + ".<br/>Starting "+ realstart.toDateString());
						}
						
				}
		
			}else{
				//console.log("week NOT 1234");
				if(!gbenddate == "01/01/2020"){
					//console.log("end NOT 01/01/2020");
					//never ending events are given the end date of 2020
					$j(this).children(".dates").children(".when").html("Every " + weeks + " " + days + " of the month.<br/>From " + realstart.toDateString() + " through until " + realend.toDateString());
				}else{
					//never ending so dont mention end date
					//console.log("end = 01/01/2020");
					$j(this).children(".dates").children(".when").html("Every " + weeks + " " + days + " of the month.<br/>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");
				$j(this).children(".dates").children(".when").html(realstart.toDateString());
			}else{
				//console.log("not one day event");
				$j(this).children(".dates").children(".when").html("Between " + realstart.toDateString() + " and " + realend.toDateString());
			};
		
		};
		
		$j(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;
}


