$(document).ready(function() {
	$(".answer").click(function() {
		var id_pieces = $(this).attr("id").split("_");
		if ($("#multi_choice").val() == 'y') {
			if ($(this).find("img").attr("src").match('quote_green_small')) {
				// remove selection
				$(this).find("img").attr("src", "/images/quote_purple_small.png");
				$(this).css({
					'color': '#8a90ca'
				});
				$("#answers_hidden_"+id_pieces[1]).remove();
			}
			else {
				// add selection
				$("#survey_form").append('<input class="answers" type="hidden" id="answers_hidden_'+id_pieces[1]+'" name="answers[]" value="'+id_pieces[1]+'" />');
				$(this).find("img").attr("src", "/images/quote_green_small.png");
				$(this).css({
					'color': '#9fc03e'
				});
			}
		}
		else {
			$("#survey_form").find('input[type="hidden"].answers').remove();
			$(".answer").find("img").attr("src", "/images/quote_purple_small.png");
			$(".answer").css({
				'color': '#8a90ca'
			});
			$("#survey_form").append('<input class="answers" type="hidden" id="answers_hidden_'+id_pieces[1]+'" name="answers[]" value="'+id_pieces[1]+'" />');
			$("#answer_"+id_pieces[1]).find("img").attr("src", "/images/quote_green_small.png");
			$("#answer_"+id_pieces[1]).css({
				'color': '#9fc03e'
			});
		}

		return false;
	});

	$("#survey_submit").click(function() {
		$("#survey_form").append('<img src="/images/loading.gif" width="15" height="15" border="0" alt="loading..." class="loading" />');
		$.ajax({
			type: 'PUT',
			url: '/rest/answer/vote',
			dataType: 'json',
			data: $("#survey_form").serialize(), 
			beforeSend: function (xhr) {
				xhr.setRequestHeader(
					'X-SocialTooKey',
					$("#api_key").val()
				);
			},
			success:function(response) {
				$("img.loading").remove();
				$("#survey").animate({opacity: 0});
				$("#survey").load('/survey/ads/ads_pre_view', function() {
					$("#survey").animate({opacity: 1});
					setTimeout(function() {
						$("#survey").animate({opacity: 0});
						$("#survey").load('/survey/show_results/'+response[0].survey_id, function() {
							$("#survey").animate({opacity: 1});
							$(".dynamic_bar").each(function() {
								var line_width = $(this).html();
								$(this).html('');
								$(this).css({ visibility: 'visible' });
								$(this).animate({
									width: line_width
								}, 1000, 'linear');
							});
						});
					}, 2500);
				});
			},
			error: function(x,e) {
				var error = x.responseText;
				if (error.match('No answers provided')) {
					error = 'Please choose at least one answer.';
				}
				$("img.loading").remove();
				$("#error").find(".details").html(error);

				$("#error").overlay({
					effect: 'apple',
					api: true,
					top: 'center'
				}).load();
			}
		});
		return false;
	});

	$("#survey_comment_submit").click(function() {
		$("#survey_comment_form").append('<img src="/images/loading.gif" width="15" height="15" border="0" alt="loading..." class="loading" />');
		$.ajax({
			type: 'POST',
			url: '/rest/survey_comment',
			dataType: 'json',
			data: $("#survey_comment_form").serialize(), 
			beforeSend: function (xhr) {
				xhr.setRequestHeader(
					'X-SocialTooKey',
					$("#api_key").val()
				);
			},
			success:function(response) {
				$("img.loading").remove();
				var comment_html = '\
<div class="survey-comment s_comment_item" id="survey_comment_'+response.comment_id+'">\
	<div class="left_img">';
	if (response.twitter_user_id || response.facebook_id) {
		comment_html += '<a href="';
		if (response.twitter_user_id) {
			comment_html += 'http://twitter.com/'+response.screen_name;
		}
		else {
			comment_html += 'http://facebook.com/index.php?profile_id='+response.facebook_id;
		}
		comment_html += '" target="_blank">';
		if (response.twitter_user_id) {
			comment_html += '<img src="http://img.tweetimag.es/i/'+response.screen_name+'_n" align="top" border="0" width="48" height="48" />';
		}
		else {
			comment_html += '<fb:profile-pic uid="'+response.facebook_id+'" size="square"></fb:profile-pic>';
		}
		comment_html += '</a>';
	}
				comment_html += '</div>\
<div class="right_content">\
	<div class="profile_img"><img src="/images/favicon.png" width="15" height="15" border="0" align="left" />';
	if (response.twitter_user_id) {
		comment_html += '<a href="http://twitter.com/'+response.screen_name+'">'+response.name+'</a>';
	}
	else {
		comment_html += '&nbsp;';
	}
				comment_html += '</div>\
<div class="text">'+response.comment_text+'</div>\
	<div class="date">\
		<abbr class="timeago" title="'+response.post_date+'">'+jQuery.timeago(new Date())+'</abbr>\
	</div>\
</div>\
</div>';
				$("#indi_comments").prepend(comment_html);
				$("#comment_text").val('');
			},
			error: function(x,e) {
				var error = x.responseText;
				$("img.loading").remove();
				$("#error").find(".details").html(error);

				$("#error").overlay({
					effect: 'apple',
					api: true,
					top: 'center'
				}).load();
			}
		});

		return false;
	});

	$(".dynamic_bar").each(function() {
		var line_width = $(this).html();
		$(this).html('');
		$(this).css({ visibility: 'visible' });
		$(this).animate({
			width: line_width+'%'
		}, 1000, 'linear');
	});
});

