var heroArray = new Array();

jQuery(document).ready(function() {

   /* Welcome */

   $("#welcome").append("</br><strong>Welcome to the Heroes Page</strong></br></br>");
   for ( var i = 1; i <= 14; i++ ) {
     $("#welcome").append("<img src= 'h" + i + ".jpg' class='greenpic' height='70px' >");
   }

   $("#welcome").append("</br></br>");

   /* Scores */
 
   $("#scores").append("</br><strong>Please rate the Irish heroes: top rating is five.</strong></br></br>");
   $("#scores").append("<a href='#'> Click to get the scores so far </a> </br></br>");

   /* Highscore */
 
   $("#highscore").append("<a href='#'> (Favourite hero) </a> </br></br>");
  

   /* St Patrick */

   $("#stpatrick").append("</br><img src='St_Patrick_main.jpg' alt='St Patrick' class='greenpic' ></br>");

   $("#stpatrick").append("</br>Vote for St. Patrick.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#stpatrick").append("<a href='#'>" + i + "</a> ");
     $("#stpatrick").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#stpatrick").append("</br></br>");

   /* Brian Boru */

    $("#brianboru").append("</br><img src='BrianBoru_Main.jpg' alt='Brian Boru' class='greenpic' ></br>");

   $("#brianboru").append("</br>Vote for Brian Boru.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#brianboru").append("<a href='#'>" + i + "</a> ");
     $("#brianboru").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#brianboru").append("</br></br>");

   /* Thomas Fitzgerald, 10th Earl of Kildare */

   $("#kildare").append("</br><img src='Kildare_Main.jpg' alt='Silken Thomas' class='greenpic' ></br>");

   $("#kildare").append("</br>Vote for Thomas Fitzgerald, 10th Earl of Kildare.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#kildare").append("<a href='#'>" + i + "</a> ");
     $("#kildare").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#kildare").append("</br></br>");

   /* Hugh O'Neill */

    $("#hughoneill").append("</br><img src='HughONeill_Main.jpg' alt='Hugh O'Neill' class='greenpic' ></br>");
    $("#hughoneill").append("</br>Vote for Hugh O'Neill.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#hughoneill").append("<a href='#'>" + i + "</a> ");
     $("#hughoneill").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#hughoneill").append("</br></br>");

   /* Phelim O'Neill */

    $("#phelimoneill").append("</br><img src='PhelimONeill_Main.JPG' alt='Phelim O'Neill ' class='greenpic' ></br>");

   $("#phelimoneill").append("</br>Vote for Phelim O'Neill.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#phelimoneill").append("<a href='#'>" + i + "</a> ");
     $("#phelimoneill").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#phelimoneill").append("</br></br>");

   /* Patrick Sarsfield */

    $("#sarsfield").append("</br><img src='PatrickSarsfield_Main.jpg' alt='Patrick Sarsfield' class='greenpic' ></br>");

   $("#sarsfield").append("</br>Vote for Patrick Sarsfield.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#sarsfield").append("<a href='#'>" + i + "</a> ");
     $("#sarsfield").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#sarsfield").append("</br></br>");

   /* Theobald Wolfe Tone */

    $("#tone").append("</br><img src='WolfeTone_Main.jpg' alt='Theobald Wolfe Tone' class='greenpic' ></br>");

    $("#tone").append("</br>Vote for Theobald Wolfe Tone.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#tone").append("<a href='#'>" + i + "</a> ");
     $("#tone").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#tone").append("</br></br>");

   /* Robert Emmet */

    $("#emmet").append("</br><img src='Emmet_Main.jpg' alt='Robert Emmet' class='greenpic' ></br>");

    $("#emmet").append("</br>Vote for Robert Emmet.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#emmet").append("<a href='#'>" + i + "</a> ");
     $("#emmet").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#emmet").append("</br></br>");  

   /* Daniel O'Connell */

    $("#oconnell").append("</br><img src='OConnell_Main.jpg' alt='Daniel O'Connell' class='greenpic' ></br>");

   $("#oconnell").append("</br>Vote for Daniel O'Connell.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#oconnell").append("<a href='#'>" + i + "</a> ");
     $("#oconnell").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#oconnell").append("</br></br>");

   /* Thomas Meagher */

    $("#meagher").append("</br><img src='Meagher_Main.jpg' alt='Thomas Meagher' class='greenpic' ></br>");

    $("#meagher").append("</br>Vote for Thomas Meagher.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#meagher").append("<a href='#'>" + i + "</a> ");
     $("#meagher").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#meagher").append("</br></br>");

   /* James Stephens */

    $("#stephens").append("</br><img src='JamesStephens_Main.jpg' alt='James Stephens' class='greenpic' ></br>");

   $("#stephens").append("</br>Vote for James Stephens.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#stephens").append("<a href='#'>" + i + "</a> ");
     $("#stephens").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#stephens").append("</br></br>");

   /* Jeremiah O'Donovan Rossa */

    $("#rossa").append("</br><img src='Rossa_Main.jpg' alt='Jeremiah O'Donovan Rossa' class='greenpic' ></br>");

   $("#rossa").append("</br>Vote for Jeremiah O'Donovan Rossa.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#rossa").append("<a href='#'>" + i + "</a> ");
     $("#rossa").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#rossa").append("</br></br>");

   /* Charles Stewart Parnell */

    $("#parnell").append("</br><img src='Parnell_Main.jpg' alt='Charles Parnell' class='greenpic' ></br>");
   
    $("#parnell").append("</br>Vote for Charles Stewart Parnell.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#parnell").append("<a href='#'>" + i + "</a> ");
     $("#parnell").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#parnell").append("</br></br>");

   /* Patrick Pearse */

    $("#pearse").append("</br><img src='Pearse_Main.jpg' alt='Patrick Pearse' class='greenpic' ></br>");

   $("#pearse").append("</br>Vote for Patrick Pearse.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#pearse").append("<a href='#'>" + i + "</a> ");
     $("#pearse").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#pearse").append("</br></br>");

   /* Michael Collins */

    $("#collins").append("</br><img src='Collins_Main.JPG' alt='Michael Collins' class='greenpic' ></br>");

    $("#collins").append("</br>Vote for Michael Collins.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#collins").append("<a href='#'>" + i + "</a> ");
     $("#collins").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#collins").append("</br></br>");

   /* John Hume */

    $("#hume").append("</br><img src='JohnHume_Main.gif' alt='John Hume' class='greenpic' ></br>");

    $("#hume").append("</br>Vote for John Hume.</br></br>");
   
   for ( var i = 1; i <= 5; i++ ) {
     $("#hume").append("<a href='#'>" + i + "</a> ");
     $("#hume").append("<img src= 's" + i + ".jpg' width='30px' >");
   }

   $("#hume").append("</br></br>");

   $("#highscore a").click(function(e){

	

	 // stop normal link click
	     e.preventDefault();
     
     	// send request
     	$.post("Vote/rate.php", {scores: $(this).html()}, function(xml) {

	
	heroArray = [];
	
	heroArray.push(new Hero($("stpatrickscore", xml).text(), "St Patrick"));
	heroArray.push(new Hero($("brianboruscore", xml).text(), "Brian Boru"));
	heroArray.push(new Hero($("kildarescore", xml).text(), "Silken Thomas"));
	heroArray.push(new Hero($("hughoneillscore", xml).text(), "Hugh O'Neill"));
	heroArray.push(new Hero($("phelimoneillscore", xml).text(), "Phelim O'Neill"));
	heroArray.push(new Hero($("sarsfieldscore", xml).text(), "Patrick Sarsfield"));
	heroArray.push(new Hero($("tonescore", xml).text(), "Theobald Wolfe Tone"));
	heroArray.push(new Hero($("emmetscore", xml).text(), "Robert Emmet"));
	heroArray.push(new Hero($("oconnellscore", xml).text(), "Daniel O'Connell"));
	heroArray.push(new Hero($("meagherscore", xml).text(), "Thomas Meagher"));
	heroArray.push(new Hero($("stephensscore", xml).text(), "James Stephens"));
	heroArray.push(new Hero($("rossascore", xml).text(), "Jeremiah O'Donovan Rossa"));
	heroArray.push(new Hero($("parnellscore", xml).text(), "Charles Stewart Parnell"));
	heroArray.push(new Hero($("pearsescore", xml).text(), "Patrick Pearse"));
	heroArray.push(new Hero($("collinsscore", xml).text(), "Michael Collins"));
	heroArray.push(new Hero($("humescore", xml).text(), "John Hume"));


	sortScores();

	
    	$("#highscore").empty();
	

	$("#highscore").append("<strong>" + heroArray[0].name + "</strong> is winning most votes</br></br>");

     });

   });	


   $("#scores a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {scores: $(this).html()}, function(xml) {

	
	heroArray = [];
	
	heroArray.push(new Hero($("stpatrickscore", xml).text(), "St Patrick"));
	heroArray.push(new Hero($("brianboruscore", xml).text(), "Brian Boru"));
	heroArray.push(new Hero($("kildarescore", xml).text(), "Silken Thomas"));
	heroArray.push(new Hero($("hughoneillscore", xml).text(), "Hugh O'Neill"));
	heroArray.push(new Hero($("phelimoneillscore", xml).text(), "Phelim O'Neill"));
	heroArray.push(new Hero($("sarsfieldscore", xml).text(), "Patrick Sarsfield"));
	heroArray.push(new Hero($("tonescore", xml).text(), "Theobald Wolfe Tone"));
	heroArray.push(new Hero($("emmetscore", xml).text(), "Robert Emmet"));
	heroArray.push(new Hero($("oconnellscore", xml).text(), "Daniel O'Connell"));
	heroArray.push(new Hero($("meagherscore", xml).text(), "Thomas Meagher"));
	heroArray.push(new Hero($("stephensscore", xml).text(), "James Stephens"));
	heroArray.push(new Hero($("rossascore", xml).text(), "Jeremiah O'Donovan Rossa"));
	heroArray.push(new Hero($("parnellscore", xml).text(), "Charles Stewart Parnell"));
	heroArray.push(new Hero($("pearsescore", xml).text(), "Patrick Pearse"));
	heroArray.push(new Hero($("collinsscore", xml).text(), "Michael Collins"));
	heroArray.push(new Hero($("humescore", xml).text(), "John Hume"));


	sortScores();

       $("#scores").html("</br><strong><i>CURRENT SCORES</i></strong></br></br>" + 
         "<strong><span class = 'red'>" + heroArray[0].name + "</span></strong> : " + heroArray[0].score +
	 "</br><strong><span class = 'blue'>" + heroArray[1].name + "</span></strong> : " + heroArray[1].score +
	 "</br><strong><span class = 'green'>" + heroArray[2].name + "</span></strong> : " + heroArray[2].score +
	 "</br><strong>" + heroArray[3].name + "</strong> : " + heroArray[3].score +
	 "</br><strong>" + heroArray[4].name + "</strong> : " + heroArray[4].score +
	 "</br><strong>" + heroArray[5].name + "</strong> : " + heroArray[5].score +
	 "</br><strong>" + heroArray[6].name + "</strong> : " + heroArray[6].score +
	 "</br><strong>" + heroArray[7].name + "</strong> : " + heroArray[7].score +
	 "</br><strong>" + heroArray[8].name + "</strong> : " + heroArray[8].score +
	 "</br><strong>" + heroArray[9].name + "</strong> : " + heroArray[9].score +
	 "</br><strong>" + heroArray[10].name + "</strong> : " + heroArray[10].score +
	 "</br><strong>" + heroArray[11].name + "</strong> : " + heroArray[11].score +
	 "</br><strong>" + heroArray[12].name + "</strong> : " + heroArray[12].score +
	 "</br><strong>" + heroArray[13].name + "</strong> : " + heroArray[13].score +
	 "</br><strong>" + heroArray[14].name + "</strong> : " + heroArray[14].score +
	 "</br><strong>" + heroArray[15].name + "</strong> : " + heroArray[15].score +
	 "</br></br> " +
	 "<input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
      
     });

   });


   $("#stpatrick a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {stpatrick: $(this).html()}, function(xml) {
       // format and output result
       $("#stpatrick").html("</br></br><img src='St_Patrick.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for St Patrick. The average vote for St Patrick is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });
  


   $("#brianboru a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {brianboru: $(this).html()}, function(xml) {
       // format and output result
         $("#brianboru").html("</br></br><img src='BrianBoru.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Brian Boru. The average vote for Brian Boru is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#kildare a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {kildare: $(this).html()}, function(xml) {
       // format and output result
       $("#kildare").html("</br></br><img src='Kildare.JPG' class='pic'> </br></br>" + 
         "Thanks for voting for Silken Thomas. The average vote for Silken Thomas is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#hughoneill a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {hughoneill: $(this).html()}, function(xml) {
       // format and output result
      $("#hughoneill").html("</br></br><img src='HughONeill.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Hugh O'Neill. The average vote for Hugh O'Neill is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#phelimoneill a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {phelimoneill: $(this).html()}, function(xml) {
       // format and output result
       $("#phelimoneill").html("</br></br><img src='PhelimONeill.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Phelim O'Neill. The average vote for Phelim O'Neill is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#sarsfield a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {sarsfield: $(this).html()}, function(xml) {
       // format and output result
       $("#sarsfield").html("</br></br><img src='PatrickSarsfield.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Patrick Sarsfield. The average vote for Patrick Sarsfield is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#tone a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {tone: $(this).html()}, function(xml) {
       // format and output result
      $("#tone").html("</br></br><img src='WolfeTone.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Theobald Wolfe Tone. The average vote for Wolfe Tone is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#emmet a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {emmet: $(this).html()}, function(xml) {
       // format and output result
       $("#emmet").html("</br></br><img src='Emmet.png' class='pic'> </br></br>" + 
         "Thanks for voting for Robert Emmet. The average vote for Robert Emmet is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

   $("#oconnell a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {oconnell: $(this).html()}, function(xml) {
       // format and output result
       $("#oconnell").html("</br></br><img src='OConnell.png' class='pic'> </br></br>" + 
         "Thanks for voting for Daniel O'Connell. The average vote for Daniel O'Connell is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#meagher a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {meagher: $(this).html()}, function(xml) {
       // format and output result
       $("#meagher").html("</br></br><img src='Meagher.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Thomas Meagher. The average vote for Thomas Meagher is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#stephens a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {stephens: $(this).html()}, function(xml) {
       // format and output result
       $("#stephens").html("</br></br><img src='stephens.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for James Stephens. The average vote for James Stephens is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#rossa a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {rossa: $(this).html()}, function(xml) {
       // format and output result
       $("#rossa").html("</br></br><img src='Rossa.png' class='pic'> </br></br>" + 
         "Thanks for voting for Jeremiah O'Donovan Rossa. The average vote for O'Donovan Rossa is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#parnell a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {parnell: $(this).html()}, function(xml) {
       // format and output result
      $("#parnell").html("</br></br><img src='Parnell.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Charles Stewart Parnell. The average vote for Parnell is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#pearse a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {pearse: $(this).html()}, function(xml) {
       // format and output result
       $("#pearse").html("</br></br><img src='Pearse.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Patrick Pearse. The average vote for Patrick Pearse is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#collins a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {collins: $(this).html()}, function(xml) {
       // format and output result
      $("#collins").html("</br></br><img src='Collins.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for Michael Collins. The average vote for Michael Collins is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

  $("#hume a").click(function(e){
     // stop normal link click
     e.preventDefault();
     
     // send request
     $.post("rate.php", {hume: $(this).html()}, function(xml) {
       // format and output result
       $("#hume").html("</br></br><img src='Hume.jpg' class='pic'> </br></br>" + 
         "Thanks for voting for John Hume. The average vote for John Hume is: " +
         $("average", xml).text() +
         ", number of votes: " +
         $("count", xml).text() +
   	 "</br></br><input type='button' value='Refresh Page' onclick='refresh()'/></br></br>"
       );
     });
   });

});

function refresh()
{
window.location.reload()
}

function Hero(argA, argB)
{
	 this.score = argA;
	 this.name = argB;
	 
}

function sortScores() {
	// Ok, now sort the array! hard part
	// Just try an unsorted print first
	
	for (var x = 0; x < 35; x++) {
	
		for (var i = 0; i < heroArray.length - 1; i++) {
			
			var current = heroArray[i];
			var next = heroArray[i + 1];

			if (current.score < next.score) {
			
				heroArray[i] = next;
				heroArray[i + 1] = current;
			
			}
		}
	}
	
}


