$(document).ready(function(){
	//show more inputs for more choices/options for the poll
	$(".add_more_choices").click(function(){
		//total current options
		var total = 0;
		//count all of them
		var type = $(this).attr("id");
		if (type == "new"){
			$(".poll_choices").find('input[type=text]').each(function() {
				total++;
			});
			//increase the total ammount for the next item
			total++;
			//add one more input for one more option on the poll
			$(this).before('<label>'+total+'.</label>&nbsp;<input type="text" name="poll_choices_input[]" /><br /><br />');
		}else if (type == "edit"){
			$(this).parent().find('.poll_choices_edit').each(function() {
				total++;
			});
			//add one more input for one more option on the poll
			$(this).before('<label>'+total+'.</label>&nbsp;<input type="text" name="poll_choices_input[]" class="poll_choices_edit" /><input type="text" style="width:30px;" name="edit_choice_votes[]" /> <span style="display:inline-block;margin-top:2px;">Votes</span><br /><br />');
		}
	});
	//when we hover over the poll container we show the actions that we can do on the poll
	$(".poll_container").hover(
		function(){
			//show the actions bar
			$(".poll_actions", this).show();
		},
		function(){
			//hide the actions bar
			$(".poll_actions", this).hide();
		}
	);
	//view the current poll
	$(".admin_view_poll").toggle(
		function(){
			//if results is already visible, show the arrow
			if ($(this).parent().next().find(".poll_results_view_container").is(":visible")){
				$(this).parent().next().find(".point_results").show();
			}
			//show the poll
			$(this).parent().next().find(".poll_item_container").css("display", "inline-block");
		},
		function(){
			//if results is already visible, hide the arrow
			if ($(this).parent().next().find(".poll_results_view_container").is(":visible")){
				$(this).parent().next().find(".point_results").hide();
			}
			//hide the poll
			$(this).parent().next().find(".poll_item_container").css("display", "none");
		}
	);
	//view the current poll
	$(".admin_view_poll_results").toggle(
		function(){
			//if poll is already visible, show the arrow
			if ($(this).parent().next().find(".poll_item_container").is(":visible")){
				$(this).parent().next().find(".point_results").show();
			}
			//show the results
			$(this).parent().next().find(".poll_results_view_container").css("display", "inline-block");
		},
		function(){
			//hide the results
			$(this).parent().next().find(".poll_results_view_container").css("display", "none");
			//hide the arrow
			$(this).parent().next().find(".point_results").hide();
		}
	);
	//view the results of the poll
	$(".view_poll_results").click(function (){
		$(this).parents(".poll_item_container").hide();
		$(this).parents(".poll_item_container").next().css("display", "inline-block");
	});
	//view the poll and hide the results
	$(".view_poll_item").click(function (){
		$(this).parents(".poll_results_view_container").hide();
		$(this).parents(".poll_results_view_container").prev().show();
	});
	//toggle the edit container of the poll
	$(".admin_edit_poll").toggle(
		function () {
			//show the container
			$(this).parent().parent().find(".admin_poll_edit").before("<br /><br />");
			$(this).parent().parent().find(".admin_poll_edit").css("display", "inline-block");
		},
		function (){
			//hide it
			$(this).parent().parent().find(".admin_poll_edit").hide();
		}
	);
	//when clicking on percentage bar of the poll, show its option total votes
	$(".result_bar_outer, .row_label2").click(function() {
		var type = $(this).attr("id");
		if (type == "out" && type != "in"){
			if ($(this).find(".row_label2").find(".total_votes_result_bar").is(":visible")){
				$(this).find(".row_label2").find(".total_votes_result_bar").hide().stop();
			}else
				$(this).find(".row_label2").find(".total_votes_result_bar").show().stop();
			return false;
		}else if (type == "in" && type != "out"){
			if ($(this).find(".total_votes_result_bar").is(":visible")){
				$(this).find(".total_votes_result_bar").hide().stop();
			}else
				$(this).find(".total_votes_result_bar").show().stop();
			return false;
		}
	});
});
