var totalItems = 0;
var selectedPage = 1;
var itemsPerPage = 2;
//var XMLresultFile = "xml/competition_results_0910.xml";

 $(document).ready(function(){
	$.ajax({
		type: "GET",
		url: XMLresultFile,
		dataType: "xml",
      error: function(XMLHttpRequest, textStatus, errorThrown) {
		  alert(XMLHttpRequest);
		  alert(textStatus);
		  alert(errorThrown);
		} ,
		success: function(xml) {
			var textcontainer = $('#section_1');
			itemsPerPage = 2;
			totalItems = $(xml).find('competition').length;
			var count = 0;
			var strText = "<div class='section_1'>";
			$(xml).find('competition').each(function(){
				var comp_type = $(this).find('comp_type').text();
				if (comp_type == 'Competition') {
					if (count < itemsPerPage) {
						strText += "<div class='calendar_title'>";
						var title = $(this).find('title').text();
						var date = $(this).find('date').text();
						strText += title+ " (" + date + ")</div>";
						strText +=  "<div class='header'>";
						var winning_entry = $(this).find('winning_entry').text();
						var winner = $(this).find('winner').text();
						strText += "<p class='red_bold_title'>Winner: " +winning_entry +"<br />by " + winner + "</span>";
						var win_linkedMovie = $(this).find('win_linkedMovie').text();
						var win_linkedMovieType  = $(this).find('win_linkedMovieType ').text();
						if (win_linkedMovie.length && win_linkedMovieType.length ) {
							if (win_linkedMovieType == "youTube") {
								strText += "<a href='#' class='movieicon' onclick=\"popYouTube('" + win_linkedMovie + "',520,400);return false;\"></a>";
							} else if (win_linkedMovieType == "movie") {
								//
							} 
						}
						var win_points = $(this).find('win_points').text();
						if (win_points.length) {
							strText += "<p class='centered_text'> (" + win_points + " Points)</p>";
						}
						var win_image = $(this).find('win_image').text();
						if (win_image.length) {
							strText += "<img class='sectionImg' src='"+ win_image + "' width='420' height='280' />";
						}
						var second = $(this).find('second').text();
						if (second.length) {
							var second_points = $(this).find('second_points').text();
							strText += "<p class='blue_bold centered_text'>2nd " + second + " (" + second_points + " Points)</p>";
						}
						var third = $(this).find('third').text();
						if (third.length) {
							var third_points = $(this).find('third_points').text();
							strText += "<p class='blue_bold centered_text'>3rd " + third + " (" + third_points + " Points)</p>";
						}
						var others = $(this).find('others').text();
						if (others.length) {
							strText += "<p class='blue_bold_large centered_text'>Runners up</p>";
							strText += "<p class='blue_bold centered_text'>" + others + "</p>";
						}
						strText += "<hr />";
					}
					count++;
				} else if (comp_type == 'Awards') {
					if (count < itemsPerPage) {
						var title = $(this).find('title').text();
						var announce_date = $(this).find('announce_date').text();
						var group_photo = $(this).find('group_photo').text();
						strText += "<div class='calendar_title'>" + title + " (" + announce_date + ")</div>";
						awards = this;
						$(awards).find('award').each(function(){
								var award_name = $(this).find('award_name').text();
								var winning_entry = $(this).find('winning_entry').text();
								var winner = $(this).find('winner').text();
								if (winner.length) {
									strText += "<p class='blue_bold_awardname'>" + award_name +"</p>";
									strText += "<p class='red_bold_award'>Winner: " + winner +"</p>Awarded for <span class='blue_bold_award'>" + winning_entry + "</span>";
									var win_linkedMovie = $(this).find('win_linkedMovie').text();
									var win_linkedMovieType  = $(this).find('win_linkedMovieType ').text();
									if (win_linkedMovie.length && win_linkedMovieType.length ) {
										if (win_linkedMovieType == "youTube") {
									//		strText += "<a href='#' class='movieicon' onclick=\"popYouTube('" + win_linkedMovie + "',520,400);return false;\"></a>";
										} else if (win_linkedMovieType == "movie") {
											//
										} 
									}
								}
						});
					strText += "<hr style='margin: 8px 0 20px 0;'/>";
					count++;
					}
				}
			});
			strText += "</div>";
			textcontainer.append(strText);
			setButtons (1);
		}//,
		//failure: function(xml) {(alert('oh no'))};
	});
});


function setButtons (btnNo) {
	var buttoncontainer1 = $('#pageButtons1');
	var buttoncontainer2 = $('#pageButtons2');
	var btnStr = '<span id="pageLabel"> Page:</span>';
	if (totalItems > 0) {
		for(var i=1; i <= Math.floor(totalItems/itemsPerPage) ; i++) 
			if (btnNo == i) {
				btnStr += ('<a class="selectedPage" href="#">(' + i + ')</a></span>');
			} else {
				btnStr += ('<a class="pageButton" href="#" onclick="refresh(' + i + ');">' + i + '</a>&nbsp;&nbsp;');
			}
		btnStr += ('<p />');
	}
	document.getElementById('pageButtons1').innerHTML = btnStr;
	document.getElementById('pageButtons2').innerHTML = btnStr;
}




function refresh (pageNo) {
	$.ajax({
		type: "GET",
		url: XMLresultFile,
		dataType: "xml",
		success: function(xml) {
			var div = document.getElementById("section_1");
			while (div.hasChildNodes()) {
				div.removeChild(div.lastChild);
			}
			var textcontainer = $('#section_1');
			var count = 0;
			var strText = "<div class='section_1'>";
			$(xml).find('competition').each(function(){
				var comp_type = $(this).find('comp_type').text();
				if (comp_type == 'Competition') {
					if (count >= itemsPerPage*(pageNo -1) && count < itemsPerPage*pageNo) {
						strText += "<div class='calendar_title'>";
						var title = $(this).find('title').text();
							var date = $(this).find('date').text();
						strText += title+ " (" + date + ")</div>";
						strText +=  "<div class='header'>";
						var winning_entry = $(this).find('winning_entry').text();
						var winner = $(this).find('winner').text();
						strText += "<p class='red_bold_title'>Winner: " +winning_entry +"<br />by " + winner + "</span>";
						var win_linkedMovie = $(this).find('win_linkedMovie').text();
						var win_linkedMovieType  = $(this).find('win_linkedMovieType ').text();
						if (win_linkedMovie.length && win_linkedMovieType.length ) {
							if (win_linkedMovieType == "youTube") {
								strText += "<a href='#' class='movieicon' onclick=\"popYouTube('" + win_linkedMovie + "',520,400);return false;\"></a>";
							} else if (win_linkedMovieType == "movie") {
								//
							} 
						}
						var win_points = $(this).find('win_points').text();
						if (win_points.length) {
							strText += "<p class='centered_text'> (" + win_points + " Points)</p>";
						}
						var win_image = $(this).find('win_image').text();
						if (win_image.length) {
							strText += "<img class='sectionImg' src='"+ win_image + "' width='420' height='280' />";
						}
						var second = $(this).find('second').text();
						if (second.length) {
							var second_points = $(this).find('second_points').text();
							strText += "<p class='blue_bold centered_text'>2nd " + second + " (" + second_points + " Points)</p>";
						}
						var third = $(this).find('third').text();
						if (third.length) {
							var third_points = $(this).find('third_points').text();
							strText += "<p class='blue_bold centered_text'>3rd " + third + " (" + third_points + " Points)</p>";
						}
						var others = $(this).find('others').text();
						if (others.length) {
							strText += "<p class='blue_bold_large centered_text'>Runners up</p>";
							strText += "<p class='blue_bold centered_text'>" + others + "</p>";
						}
						strText += "<hr />";
					}
					count++;
				} else if (comp_type == 'Awards') {
					if (count >= itemsPerPage*(pageNo -1) && count < itemsPerPage*pageNo) {
						var title = $(this).find('title').text();
						var announce_date = $(this).find('announce_date').text();
						var group_photo = $(this).find('group_photo').text();
						strText += "<div class='calendar_title'>" + title + " (" + announce_date + ")</div>";
						awards = this;
						$(awards).find('award').each(function(){
								var award_name = $(this).find('award_name').text();
								var winning_entry = $(this).find('winning_entry').text();
								var winner = $(this).find('winner').text();
								if (winner.length) {
									strText += "<p class='blue_bold_awardname'>" + award_name +"</p>";
									strText += "<p class='red_bold_award'>Winner: " + winner +"</p>Awarded for <span class='blue_bold_award'>" + winning_entry + "</span>";
									var win_linkedMovie = $(this).find('win_linkedMovie').text();
									var win_linkedMovieType  = $(this).find('win_linkedMovieType ').text();
									if (win_linkedMovie.length && win_linkedMovieType.length ) {
										if (win_linkedMovieType == "youTube") {
									//		strText += "<a href='#' class='movieicon' onclick=\"popYouTube('" + win_linkedMovie + "',520,400);return false;\"></a>";
										} else if (win_linkedMovieType == "movie") {
											//
										} 
									}
								}
						});
					strText += "<hr style='margin: 8px 0 20px 0;'/>";
					}
				count++;
				}
			});
			//strText += "</div>";
			textcontainer.append(strText)
		}
	});
	setButtons (pageNo);
}

function getSeasonWinners () {
	$.ajax({
		type: "GET",
		url: "competition_trophies.xml",
		dataType: "xml",
		success: function(xml) {
			var div = document.getElementById("section_1");
			while (div.hasChildNodes()) {
				div.removeChild(div.lastChild);
			}
			var textcontainer = $('#section_1');
			var count = 0;
			var strText = "<div class='section_1'>";
			$(xml).find('season_winners').each(function(){
				//if (count >= itemsPerPage*(pageNo -1) && count < itemsPerPage*pageNo) {
					strText += "<div class='calendar_title'>";
					var season = $(this).find('season').text();
					strText += season + "</div>";
					var group_image = $(this).find('group_image').text();
					var filmOfTheYear_image = $(this).find('filmOfTheYear_image').text();
					strText += "<img class='sectionImg' src='"+ group_image + "' width='420' height='280' />";
				
					$(xml.season_winners).find('trophy').each(function(){
					
						strText +=  "<div class='header'>";
						var trophy_name = $(this).find('trophy_name').text();
						var wining_entry = $(this).find('wining_entry').text();
						var winner = $(this).find('winner').text();
						var section = $(this).find('section').text();
						strText += "<p><span class='blue_bold_title'>" + trophy_name + "</span><span class='red_bold_title'>" +winning_entry +"<br />by " + winner + "</span></p>";
						
						strText += "<p class='centered_text'>Awarded for " + wining_entry + " - " + section + "</p>";
					});
					
					strText += "<img class='sectionImg' src='"+ filmOfTheYear_image + "' width='420' height='280' />";
				//}
				count++;
			});
			textcontainer.append(strText)
		}
	});
	//setButtons (pageNo);
}

function getElementsByClassName(classname, node)  {
	if(!node) node = document.getElementsByTagName("body")[0];
		var a = [];
		var re = new RegExp('\\b' + classname + '\\b');
		var els = node.getElementsByTagName("*");
		for(var i=0,j=els.length; i<j; i++)
			if(re.test(els[i].className))a.push(els[i]);
	return a[0];
}
