$(document).ready(
	function() {
		$("form").each(
			function(){
				//alert(this.id);
				
				//Check to see if the poll has an id like "poll_#" 
				var matches = this.id.match(/poll/g)
				if (matches !== null) {
					//Found a node 
					var poll_ID_prefix = $(this).attr('id');
					
					//Check to see whether they are logged in
					if ( $('#login_value').val() != 'logout') {
						//Yes, I should refactor this into its own function - I am lazy
						$('#' + poll_ID_prefix + '_feedback').html('<span class="red">You must be logged in to vote in an SCG poll.  You may log in on the left-hand side, or you can create an account <a href="https://sales.starcitygames.com/userinfo.php">by clicking this link</a>.<br /></span>'); 
						$('#' + poll_ID_prefix + '_submit').val('Please log in to vote!'); 	
						document.getElementById(poll_ID_prefix + '_submit').disabled = true;					
					} else {
					//Check to see whether they have voted, as set in the cookie
					//Note that since the articles are cached, we can't pull the information in from the article dynamically
						var user_has_voted = get_cookie('SCG_' + poll_ID_prefix); 
						//alert('SCG_' + poll_ID_prefix + ' is ' + user_has_voted); 
						if (user_has_voted == 1) {
						
							//Then pull the poll data dynamically and load it into the form via an AJAX call
							var poll_ID = $(this).find('input[name=poll_ID]').val();
							//alert('Loading ' + poll_ID + '!'); 
							$.ajax(
								{
								url: 'http://www.starcitygames.com/polls/poll_display.php?poll_ID=' + poll_ID + '&as_AJAX=1',
								type: 'GET',
								dataType: 'json', 
								success: function(response) {
									var poll_ID_prefix = 'poll_' + response.poll_ID; 
											
									if (response.status != true) {
										var final_error_message = response.status + ': Error in loading poll!'; 

										if (! response.poll_ID) {
											alert(final_error_message); 
										} else {
											$('#' + poll_ID_prefix + '_feedback').html = '<span class="red">' + final_error_message + '</span>'; 
										}
												
										return; 
									} else {
										//Remove the original object and replace it with the results - may cause bugs in Opera
										//alert('#' + poll_ID_prefix + '_wrapper'); 
										//alert(response.HTML_output); 
										$('#' + poll_ID_prefix).remove();  
										$('#' + poll_ID_prefix + '_wrapper').append('<p>' + response.HTML_output + '</p>'); 
									}
								}
							});
						} 
					}
					
					//Now, 
					$(this).submit(function(event){					
						
						var poll_ID_prefix = $(this).attr('id'); 
						
						//Disable the proper form elements
						
						//SCREW YOU JQUERY FOR NOT WORKING HERE (in Firefox and Safari)
						document.getElementById(poll_ID_prefix + '_submit').disabled = true;
						$('#' + poll_ID_prefix + '_submit').val("Please Wait..."); 
						$('#' + poll_ID_prefix + '_feedback').html = ''; 
						
						//alert($('#login_value').val());
						
						if ( $('#login_value').val() != 'logout') {
							$('#' + poll_ID_prefix + '_feedback').html('<span class="red">You must be logged in to vote in an SCG poll.  You may log in on the left-hand side, or you can create an account <a href="https://sales.starcitygames.com/userinfo.php">by clicking this link</a>.<br /></span>'); 
							$('#' + poll_ID_prefix + '_submit').val('Please log in to vote!'); 
							return false; 
						}
	
						var poll_ID = $(this).find('input[name=poll_ID]').val();

						//alert('Finding poll_' + poll_ID); 
						var selected_option = $('input[name=poll_' + poll_ID + ']:checked').val();
						
						if (typeof(selected_option) == "undefined") {
							$('#' + poll_ID_prefix + '_feedback').html('<span class="red">You must choose an option before you can vote!.</span>'); 
							$('#' + poll_ID_prefix + '_submit').val('Vote!'); 
							document.getElementById(poll_ID_prefix + '_submit').disabled = false;
							return false; 
						} 						

						var serialized_data = $(this).serialize();  
						serialized_data += '&as_AJAX=1'; 
						//alert('Serialized data is ' + serialized_data); 
						$.ajax(
							{
							url: 'http://www.starcitygames.com/polls/poll_process.php',
							type: 'POST',
							data: serialized_data,
							dataType: 'json', 
							
							success: function(response) {
							// Print out everything we got back in a div with id='foo'
								//alert('Wow, completed! -- ' + response); 
								
								var poll_ID_prefix = 'poll_' + response.poll_ID; 
										
								if (response.status != true) {
									var final_error_message = response.status + ': Error in processing poll!'; 
									/*
									response.error_messages.each(
										function whatevs(error) {
											final_error_message += error;  
										}
									);
									*/ 
											
									if (! response.poll_ID) {
										alert(final_error_message); 
									} else {
										//alert('Con ' + response.poll_ID + ' ' + final_error_message);
										$('#' + poll_ID_prefix + '_feedback').html = '<span class="red">' + final_error_message + '</span>'; 
									}
											
									return; 
								} else {
									//alert('#' + poll_ID_prefix + '_wrapper'); 
									//alert(response.HTML_output); 
									$('#' + poll_ID_prefix).remove();  
									$('#' + poll_ID_prefix + '_wrapper').append('<p>' + response.HTML_output + '</p>'); 
									/*
									$('#' + poll_ID_prefix).html(response.HTML_output);
									$('#' + poll_ID_prefix + '_wrapper').html(response.HTML_output);
									*/
								}
							}
						});
						return false;  

					});
     			}
			}
		);
	}
);


function get_cookie ( cookie_name ) {
//Cheerfully stolen from http://www.elated.com/articles/javascript-and-cookies/
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

