if ($('#poll-container')[0]){
	var poll_id = $('#poll-container').attr('class').replace(/\D+/, '');
	if (poll_id){	// return poll via Ajax from currently selected poll ID
		$.ajax({
			url: '/poll/display_' + poll_id + '.ajax',
				success: function(data){
					$('#poll-container').html(data);
					$('a.prev-poll').each(function() {
						observeLink($(this));
					});
					vote_observe();	// observe vote click
				}
			}
		);
	}
	
	
	// submit vote event listener
	function vote_observe(){
		$('#submit-poll').click(function(e){
			var itemSelected = false;
			var answer = "";
			var radioButtons = $('div.poll-results input[type*=radio]');
			radioButtons.each(function() {
				// checks to see if a select is checked otherwise it won't submit
				if ($(this).attr('checked')) {
					answer = $(this).attr('id');
					answer = answer.replace("poll_answer_","");
					itemSelected = true;
				}
			});
			if (itemSelected){
				poll_id = $('#poll_id').val();
				// tracking code
				var pollQuestion = $("div#current-poll_" + poll_id).children("p.poll-question").text().replace(/'/g, '');
				if (typeof(s) != 'undefined') {
					s.linkTrackTrinity( true, 'poll', {'prop37':pollQuestion,'event':'event11'});
                }
				// end tracking code
				$.ajax({	// register the vote
					url: '/poll/vote.ajax?poll_id=' + poll_id + '&answer_option_id=' + answer,
					traditional: true,
					type: 'post',
					success: function(data) {
						// remove all buttons from the poll & display thanks message
						$('#current-poll_'+poll_id+' label.poll-option').remove();
						$($(e.target).parent()).append(data);
						$(e.target).parent().children().last().delay(5000).fadeOut(500);
						radioButtons.each(function() { $(this).remove() });
						$(e.target).remove();
					}
				});
			}
			return false;
		});	
	}
	function observeLink(link){
		// event listeners to for previous / next links
		link.click(function(e) {
			var pollID = $(e.target).attr('id').replace(/\D+/, '');
			var prevPollHtml = '/poll/display_' + pollID + '.ajax';
			var pollHeight = $('#poll-container').height();
			$('.poll-wrapper').fadeOut(500, function () {
				var pollContainer = $('#poll-container');
				$('a.prev-poll').each(function() { $(this).unbind('click') });
				pollContainer.css({ height: pollHeight + 'px' });
				pollContainer.html('<span class="throbber">Loading</span>');
				pollContainer.show();
				$.ajax({
					url: prevPollHtml,
						success: function(data) {
							pollContainer.removeAttr('style');
							pollContainer.hide();
							pollContainer.html(data);
							pollContainer.fadeIn('fast');
							$('a.prev-poll').each(function() { observeLink($(this)) });
							vote_observe();	// observe vote click
						}
				});
			});
			return false;
		});
	}
}

/*
// poll archive page
if ($('div.poll-container').length > 0 && typeof(nohide) == 'undefined' ){
		$('div.poll_inards').each(function(result) {
				result.hide().identify();
				result.removeClassName('archive');
		});
		$$('p.poll-comments').each(function(question) {
				var html = ' | <a href="#" class="view-result">view result</a>';
				question.update(question.innerHTML + html);
		});
		$$('a.view-result').each(function(link) {
				Event.observe(link, 'click', function(e) {
						Event.stop(e);
						Event.element(e).innerHTML = (Event.element(e).innerHTML != 'view result') ? 'view result' : 'hide result';
						var resultBox = $(Event.element(e)).up('div.tile2').down('p.poll-question').next();
						new Effect.toggle(resultBox, 'Blind', { duration: 0.4 });
				});
		});
}
*/
