var useError=false;
var sendComplete=true;
var animateComplete=true;



$(document).ready(function(){
        var str = location.href;
        
	$("#qq_box .easyRadioBox .radio").each(function(){
		if($(this).parent().find("input").attr("checked"))
			$(this).css("background-image","url('/images/q_radios.png')");
		$(this).parent().find("input").css("display","none");
	});					   
	$("#qq_box .easyRadioBox .radio").click(function(){
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").parent().find(".radio").css("background-image","url('/images/q_radio.png')");
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").parent().find("span").removeClass("active");
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").removeAttr("checked");
		$(this).css("background-image","url('/images/q_radios.png')");
		$(this).parent().find("input").attr("checked","checked");
		$(this).parent().find("span").addClass("active");
	});
	$("#qq_box .easyRadioBox span").click(function(){
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").parent().find(".radio").css("background-image","url('/images/q_radio.png')");
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").removeAttr("checked");
		$("#qq_box .easyRadioBox input[name='"+$(this).parent().find("input").attr("name")+"']").parent().find("span").removeClass("active");
		$(this).parent().find(".radio").css("background-image","url('/images/q_radios.png')");
		$(this).parent().find("input").attr("checked","checked");
		$(this).parent().find("span").addClass("active");
	});
	
	$("#qq_test").click(function(){
		if($(this).attr("show"))
		{
			$(this).removeAttr("show");
			$(this).animate({left:"0px"},500);
			$("#qq_box").animate({left:"-216px"},500);
		}else{
			hideMessage();
			$(this).attr("show","yes");
			$(this).animate({left:"205px"},500);
			$("#qq_box").animate({left:"0px"},500);
		}
	});
	
	$("#qq_right input:first").click(function(){
			if(!$("input[name='usergrp']:checked"))
			{
				alert("Select poll!");
				return false;
			}
			$("#qq_test").removeAttr("show");
			$("#qq_test").animate({left:"0px"},500,function(){sendHere();});
			$("#qq_box").animate({left:"-216px"},500);
	});
	
	
	
});

function displayMessage()
{
	if(!sendComplete)
		return false;
	var obj=$("#qq_message");
	obj.css("top",(parseInt($("#qq_test").css("top")) + $("#qq_test").height())+"px");
	obj.css("left",$("#qq_test").position().left+"px");
	obj.css("display","block");
	obj.height(0);
	if(useError)
		obj.css("background-image","url('/images/error.png')");
	else
		obj.css("background-image","url('/images/thanks.png')");
	obj.animate({height:"100px"},500);
	//useError=!useError;
	setTimeout(hideMessage,2500);
}

function hideMessage()
{
	var obj=$("#qq_message");
	obj.stop(true,true).stop(true,true);
	obj.animate({left:"-30px"},500,function(){$(this).css("display","none");});
}

function sendHere()
{
	$.post("/poll?ajax=true",{voted_id:$("input[name='voted_id']").val(),usergrp:$("input[name='usergrp']:checked").val()},function(data){if(data=="bad")useError=true;displayMessage();});
}
