   
// load feed
google.load("feeds", "1"); 

 function initialize() { 
	// get feed from location
	var feed = new       google.feeds.Feed("http://www.google.com/calendar/feeds/purpleradio.calendar%40googlemail.com/public/full?orderby=starttime&sortorder=ascending&singleevents=true&max-results="+unique+"&start-min="+google_start_min+"&start-max="+google_start_max); 
	feed.setResultFormat(google.feeds.Feed.XML_FORMAT); 

	// Set maximum number of items - set to 100 for PR (default was 4)
	feed.setNumEntries(100); 
	feed.load(function(result) { 

		// Start building the DOM with the feed element
		//var container = document.getElementById("feed"); 
		var container = document.getElementById("feed"); 

		// Check the feed loaded sucessfully
		if (!result.error) { 
	   
			//Declare the vars outside the loop
			var whentimestring = ''; 
			//set the incredmental date (used for the checking whether the event date is greater than the previous and if so displaying it
			var incrementing_date = d;
			//set entries to equal the results of the feed
			var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry"); 
 
			var ul = document.createElement("ul");



			 
			//Loop though the entries (events)
			for (var i = 0; i < entries.length; i++) { 
				//retrieve the title
				var titleElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "title")[0]; 
				var title = titleElement.firstChild.nodeValue; 
				
				//retrieve the anchor
				//var anchorElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "where")[0]; 
				
				//retrieve the event description
				var textElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "content")[0]; 
				if(typeof textElement == undefined) {
					var text = textElement.firstChild.nodeValue; 
				}else{
					var text = ""; 
				};
				
				//retrieve the date/time element
				var whenElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "when");
				
				//set the start time
				var starttime = whenElement[0].getAttribute('startTime'); 
				
				var month=new Array(12);
				month[0]="01";
				month[1]="02";
				month[2]="03";
				month[3]="04";
				month[4]="05";
				month[5]="06";
				month[6]="07";
				month[7]="08";
				month[8]="09";
				month[9]="10";
				month[10]="11";
				month[11]="12";
				
				//set the end time, this is more complicated as there are two endtimes in the feed and if the event is recurrent then the first is empty
				//this loops though the feed and sets the even when the enddate is found (is not indefined)
				var endtime;
					for(var ii = 0; ii < whenElement.length; ii++)
					{
					if (whenElement[ii].getAttribute('endTime') != undefined){
						endtime = whenElement[ii].getAttribute('endTime');
						break;
					}
				}
				
				//retrieve the date/time element
				var whereElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "where");
				
				var anchor;
				//alert(whereElement.length);
				for(var iii = 0; iii < whereElement.length; iii++){
					//alert(whereElement[iii].getAttribute('valueString'));
					if (whereElement[iii].getAttribute('valueString') != ''){
						//anchor = whereElement[iii].getAttribute('where');
						//alert(anchor);
						anchor = whereElement[iii].getAttribute('valueString');
						//retrieve the link element (only used if you want to link to the Google calendar event, we want to link to the DJ profile
						var mylink = "http://www.purple-radio.co.uk/profiles.php#"+anchor; 

						//alert(anchor);
						break;
					}else{
					//retrieve the link element (only used if you want to link to the Google calendar event, we want to link to the DJ profile
					var mylinkElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "link")[0]; 
					var mylink = mylinkElement.getAttribute('href'); 
					};
				}

				// declare/set the startdate as a ISO8601 date
				var startdate = new Date(); 
				startdate.setISO8601(starttime, "-0700" );  

				//this allows us to retrieve the individual items using javascript functions 
				//Javascript is a but rubbish as it removes the proceeding 0 from any dates (ie: 3 instead of 03) the following adds these back
				var start_hour = startdate.getHours();
				start_hour = start_hour + "";
				if (start_hour.length == 1){
					start_hour = "0" + start_hour;
				}

				var start_min = startdate.getMinutes();
				start_min = start_min + "";
				if (start_min.length == 1){
					start_min = "0" + start_min;
				}
			
				//Do the same thing with the end date
				var enddate = new Date(); 
				enddate.setISO8601(endtime, "-0700" ); 

				var end_hour = enddate.getHours();
				end_hour = end_hour + "";
				if (end_hour.length == 1){
					end_hour = "0" + end_hour;
				}
				
				var end_min = enddate.getMinutes();
				end_min = end_min + "";
				if (end_min.length == 1){
					end_min = "0" + end_min;
				}

				
				//If this is the first time we loop though the feed then set the incrementing date (the previous events date to be the startdate
				if (i == 0){
					var incrementing_date = startdate;
					var previousEndDate = startdate;
					var beforefour = 0;
					
				var nextday = (startdate.getDate()+1);
				var nextdate = new Date();
				nextdate.setDate(nextday);
				nextdate.setHours("04");
				nextdate.setMinutes("00");
				nextdate.setSeconds("00");
				
				
				
				
				
				}

				var previous_hour = previousEndDate.getHours();
				previous_hour = previous_hour + "";
				if (previous_hour.length == 1){
					previous_hour = "0" + previous_hour;
				}
				var previous_min = previousEndDate.getMinutes();
				previous_min = previous_min + "";
				if (previous_min.length == 1){
					previous_min = "0" + previous_min;
				}
				
						


			
				//Convert to strings so that the dates can be compared - this would be better served by a compare date function but all the ones found compare including a value
				//TODO: look at setting the time stamps to 00:00:00 and then use dat comparison, this would have to be done on new vars anyway however so this does serve the same purpose
				var whentimestring = (start_hour+":"+start_min+" - "+end_hour+":"+end_min);
				
				//Set the string values and add 1 to the month string as Javascript is a bit rubbish and starts the month numbers at 0
				var incrementing_datestring = (incrementing_date.getMonth() + 1) + '/' + incrementing_date.getDate() + '/' + incrementing_date.getFullYear();
				var startdatestring = (startdate.getMonth() + 1) + '/' + startdate.getDate() + '/' + startdate.getFullYear(); 
				//TODO: don't really understand why the end date has a day added
				var enddatestring = (enddate.getMonth() + 1) + '/' + (enddate.getDate() + 1) + '/' + enddate.getFullYear(); 

				// If there is a gap in the schedule
				if (previousEndDate < startdate){

					//start creating the values for the next column
					//var cell = document.createElement("td");
					var displaydate = previousEndDate.getDate() + '/' +  previousEndDate.getMonth()+ '/' + previousEndDate.getFullYear() + ' ('+ daynameshort[previousEndDate.getDay()] + ')';
							
					
					if (Date.parse(enddate) > Date.parse(nextdate)){
						var cellTime = document.createTextNode(displaydate + ' '+ previous_hour + ":" + previous_min+"- 04:00");		
					}else{
						// create the text nodes and add a linebreak
						var cellTime = document.createTextNode(displaydate + ' '+previous_hour + ":" + previous_min+" - "+start_hour + ":" + start_min);
					};
					
					var cellTitle = document.createTextNode("Purple Radio Listen again");
							
					//embed the event date in a span element with class="when" 		   
					var spantime = document.createElement("span"); 
					spantime.setAttribute("class", "when"); 
					
					var spantitle = document.createElement("span"); 
					spantitle.setAttribute("class", "title");
					
					var linebreak=document.createElement('br')
					
					spantime.appendChild(cellTime); 
					spantime.appendChild(linebreak); 
					spantitle.appendChild(cellTitle); 
					
					
					//each feed entry is embedded in an HTML li element
					var li = document.createElement("li");
					//var a = document.createElement("a");
					//a.setAttribute("href", newlink);
					//a.appendChild(document.createTextNode(title + ' '));
					//li.appendChild(a);
					li.setAttribute("class", "schedule_list");
					li.appendChild(spantime);
					li.appendChild(spantitle);
					ul.appendChild(li);
					
				}
				
				//alert (startdate);

					// If this events date is greater than the previous one on display, or this is the first time we are looping though
					if ((Date.parse(startdate) >= Date.parse(nextdate)) || (i == 0)) { 

					//alert(i);
						//Set the incrementing date to the date of the event, ready for the next loop
						var incrementing_date = startdate;
						var beforefour = 0;
						if ((Date.parse(startdate) > Date.parse(nextdate)) || (i == 0)) { 
						
							/*
							// Shows the date at the top of the entry
							
							var startday_date = startdate.getDate();
							if (startday_date==1) suffix=("st");
							else if (startday_date==2) suffix=("nd");
							else if (startday_date==3) suffix=("rd");
							else if (startday_date==21) suffix=("st");
							else if (startday_date==22) suffix=("nd");
							else if (startday_date==23) suffix=("rd");
							else if (startday_date==31) suffix=("st");
							else suffix=("th");
							
							var todaysdate = dayname[startdate.getDay()] + ' ' + startday_date + suffix + ' ' +  monthname[startdate.getMonth()]+ ' ' + startdate.getFullYear();
							//alert(displaydate);
							
							var cellDateDisplay = document.createTextNode(todaysdate);
									
							//embed the event date in a span element with class="when" 		   
							var spandate = document.createElement("span"); 
							spandate.setAttribute("class", "date");
							
							var linebreak=document.createElement('br')
							
							spandate.appendChild(cellDateDisplay); 
							spandate.appendChild(linebreak); 
				
							//each feed entry is embedded in an HTML li element
							var li = document.createElement("li");
							li.setAttribute("class", "date");
							li.appendChild(cellDateDisplay);
							ul.appendChild(li);*/
							
							//Set the content of the left hand cell (show the date)
							//alert (startdate.getMonth());
							//var cellText = document.createTextNode(dayname[startdate.getDay()] + " " +startdate.getDate()+" "+monthname[startdate.getMonth()]);	
							//var d=new Date();
	
							
							//alert("The current month is " + month[startdate.getMonth()]);

							
							var displaydate = startdate.getDate() + '/' +  month[startdate.getMonth()] + '/' + startdate.getFullYear() + ' ('+ daynameshort[startdate.getDay()] + ')';
							
							//alert (displaydate);
							
							var cellTime = document.createTextNode(displaydate + " 04:00 - "+start_hour+":"+start_min);
							// create the text nodes and add a linebreak
							var cellTitle = document.createTextNode("Purple Radio Listen again");

							//embed the event date in a span element with class="when" 		   
							var spantime = document.createElement("span"); 
							spantime.setAttribute("class", "when"); 
							
							var spantitle = document.createElement("span"); 
							spantitle.setAttribute("class", "title");
							
							var linebreak=document.createElement('br')
							
							spantime.appendChild(cellTime); 
							spantime.appendChild(linebreak); 
							spantitle.appendChild(cellTitle); 
							
							//each feed entry is embedded in an HTML li element
							var li = document.createElement("li");
							//var a = document.createElement("a");
							//a.setAttribute("href", mylink);
							//a.appendChild(document.createTextNode(title + ' '));
							//li.appendChild(a);
							li.setAttribute("class", "schedule_list");
							li.appendChild(spantime);
							li.appendChild(spantitle);
							ul.appendChild(li);
					
							// creates a table row
							//var row = document.createElement("tr");
							// creat a cell (table column)
							//var cell = document.createElement("td");
					
							//var cellText = document.createTextNode(" ");	

						//} else {
							//var cellText = document.createTextNode(dayname[startdate.getDay()] + " " +startdate.getDate()+" "+monthname[startdate.getMonth()]);	
						};
						var nextday = (startdate.getDate()+1);
						nextdate.setDate(nextday);
						
					} else {
						//otherwise set an empty value
						//var cellText = document.createTextNode(' ');
						var beforefour = 1;	
					}				
				//}else {
						//otherwise set an empty value
						//var cellText = document.createTextNode(' ');
				//}
										
				//Display the date or blank value
				//cell.appendChild(cellText);
				
				//row.appendChild(cell);
				
				//start creating the values for the next column
				//var cell = document.createElement("td");
				var displaydate = startdate.getDate() + '/' +  month[startdate.getMonth()]+ '/' + startdate.getFullYear() + ' ('+ daynameshort[startdate.getDay()] + ')';
					
				//Create the title string
				var cellTimestring = displaydate + " " + start_hour+":"+start_min+" - "+end_hour+":"+end_min+" ";
				
				//Create the title string
				//var celltitlestring = title;
				var bposition = title.indexOf(';');
				
				bleft = title.substring(0,bposition);
				bright = title.substring((bposition+1));
				//alert (bleft +" "+ bright);
				
				//Resolve bug where if there is no ; the title goes empty and the description holds the title
				if (bleft == '') {
					bleft = bright;
					bright = '';
					}
				
				// create the text nodes and add a linebreak
				var cellTime = document.createTextNode(cellTimestring)
				
				var cellTitle = document.createTextNode(bleft);
				//var cellTitle.style.color = 'lime';
				var cellDesc = document.createTextNode(bright);
				
				

				//cell.setAttribute("NOWRAP"); 


				//embed the event date in a span element with class="when" 		   
				var spanwhen = document.createElement("span"); 
				spanwhen.setAttribute("class", "when"); 
				
				//embed the event date in a span element with class="when" 		   
				var spantitle = document.createElement("span"); 
				spantitle.setAttribute("class", "title"); 
				
				var spandesc = document.createElement("span"); 
				spandesc.setAttribute("class", "desc");
				
				var linebreak1=document.createElement('br')
				var linebreak2=document.createElement('br')
				
				
				
				spanwhen.appendChild(cellTime); 
				spantitle.appendChild(linebreak1); 
				
				spantitle.appendChild(cellTitle); 
				spantitle.appendChild(linebreak2); 
				
				
				spandesc.appendChild(cellDesc); 
				
				//each feed entry is embedded in an HTML li element
				var li = document.createElement("li");
				//var a = document.createElement("a");
				//a.setAttribute("href", newlink);
				//a.appendChild(document.createTextNode(title + ' '));
				//li.appendChild(a);
				li.setAttribute("class", "schedule_list");
				
				li.appendChild(spanwhen);
				li.appendChild(spantitle);
				li.appendChild(spandesc);
				
				ul.appendChild(li);
				
				var previousEndDate = enddate;
				
			} //close the loop
			
			
			
			
			// creates a table row
			//var row = document.createElement("tr");
			// creat a cell (table column)
			//var cell = document.createElement("td");
			
			//var datedisplay = ("");		
			// cellText = document.createTextNode(datedisplay);
			
			//Display the date or blank value
			//cell.appendChild(cellText);
			 
			//row.appendChild(cell);
			
			//start creating the values for the next column
			//var cell = document.createElement("td");
			
			
			var displaydate = enddate.getDate() + '/' +  enddate.getMonth()+ '/' + enddate.getFullYear() + ' ('+ daynameshort[enddate.getDay()] + ')';
					
			var cellTime = document.createTextNode(displaydate + ' '+end_hour+":"+end_min+" - 04:00 ");
			var cellTitle = document.createTextNode("Purple Radio Listen again");
				// create the text nodes and add a linebreak
				//var cellTitle = document.createTextNode(end_hour+":"+end_min+"- 04:00 Purple Radio Listen again");
				//var cellDesc = document.createTextNode(text);
				//var linebreak=document.createElement('br')
				//would the below work as well?
				//cell.appendChild(linebreak)
			
			
			//create the link object and apend the values to the cell
			//cell.appendChild(cellTitle);
			
			//row.appendChild(cell);
			
			// add the row to the end of the table body
			//tblBody.appendChild(row);
			

			
			//embed the event date in a span element with class="when" 		   
			var spantime = document.createElement("span"); 
			spantime.setAttribute("class", "when");
			
			var spantitle = document.createElement("span"); 
			spantitle.setAttribute("class", "title");
			
			var linebreak=document.createElement('br')
			
			spantime.appendChild(cellTime); 
			spantime.appendChild(linebreak); 
			spantitle.appendChild(cellTitle); 
			
            //each feed entry is embedded in an HTML li element
            var li = document.createElement("li");
            //var a = document.createElement("a");
            //a.setAttribute("href", newlink);
           // a.appendChild(document.createTextNode(title + ' '));
            //li.appendChild(a);
			li.setAttribute("class", "schedule_list");
            li.appendChild(spantime);
			li.appendChild(spantitle);
            ul.appendChild(li);
		   
			// put the <tbody> in the <table>
			//tbl.appendChild(tblBody);
			
			// sets the border attribute of tbl to 2;
			//tbl.setAttribute("border", "1");
			//tbl.setAttribute("cellpadding", "2");
			//tbl.setAttribute("cellspacing", "1");
			//tbl.setAttribute("bordercolor", "#333366");
			// sets the width to 300
			//tbl.setAttribute("width","99%");

			//add all the elements to the table
			container.appendChild(ul); 
			//container_new.appendChild(ul);

			
			
		} //close the if error
	}); //close the feed
} // close the function

// initialize the script
google.setOnLoadCallback(initialize); 
