var sdthreadrate_getting = 0;
var sdthreadrate_postid = 0;

function sdthreadrate_vote(postid,vote,controltype)
{
	controltype = typeof(controltype) != 'undefined' ? controltype : "thread";
	
	votefeedback = $("#vote_feedback_" + postid + "_" + vote);
	if (votefeedback.length > 0)
	{
		if (votefeedback.find("input").length == 1)
		{
			sdthreadrate_submit(postid,vote,votefeedback.find("input").val(),controltype);
		}
		else
		{
			votefeedback.find(".voteselected").attr("postid",postid);
			votefeedback.find(".voteselected").attr("vote",vote);
			votefeedback.slideDown("fast");
		}
	}
	else
	{
		sdthreadrate_submit(postid,vote,0,controltype);
	}
}

function sdthreadrate_extendedvote(input,controltype)
{
	input = $(input);
	vote = input.attr("vote");
	postid = input.attr("postid")
	$("#vote_feedback_" + postid + "_" + vote).slideUp("fast");
	sdthreadrate_submit(postid,vote,input.val(),controltype);
}

function sdthreadrate_submit(postid, vote, votetypeid, controltype)
{
	controltype = typeof(controltype) != 'undefined' ? controltype : "thread";
	
	if (sdthreadrate_getting == 1)
	{
		return false;
	}
	else
	{
		sdthreadrate_getting = 1;
		setTimeout("sdthreadrate_getting = 0", 1000);
	}

        if (controltype == "coupon") {
            $("#votecontrol_wrap_"+postid).btOff();
        }
        else if (controltype == "permadeal") {
            $("#ratecontrol_"+postid+"_"+vote).css("background-image", "url(http://i.slickdeals.net/images/buttons/ajaxsmall.gif)");
        }

	var sdthreadrate_param = new Object();
	sdthreadrate_param['ajax'] = 1;
	sdthreadrate_param['do'] = "sdthreadratevote"
	sdthreadrate_param['postid'] = postid;
	sdthreadrate_param['vote'] = vote;
	sdthreadrate_param['votetypeid'] = votetypeid;
	sdthreadrate_param['controltype'] = controltype;
	sdthreadrate_param['securitytoken'] = SECURITYTOKEN;
	var voteimgindex = vote + 1;
	$("#ratecontrol_" + postid + " img:eq(" + voteimgindex + ")").attr("src","http://i.slickdeals.net/images/buttons/ajaxsmall.gif");
	$.post('/forums/sdthreadrate_ajax.php', sdthreadrate_param, function(data)
	{
		if ($(data).find("error").length == 0)
		{
			$("#sharethistab:visible").hide();
			$("#showfeedback:hidden").show();
			$("#fistthread_publicopinion:hidden").css("visibility","").hide().fadeIn()
			var postid = geturlvar($(this).attr("data"), "postid");
			var score = $(".vote_score")
			score.hide();
			updateHtmlBits(data);
			score.slideDown("normal");
			var error = $(data).find("errormsg");
			if (error.length > 0)
			{
				errordialog(error.text());
			}
                        else if (controltype == "coupon")
                        {
                                $("#votecontrol_wrap_"+postid).
                                    parents(".coupon_info").
                                    removeClass("pos").
                                    removeClass("neg").
                                    addClass(vote > 0 ? "pos" : "neg");
                        }
                        else if (controltype == "permadeal")
                        {
                            $(".score").hide().fadeIn();
                        }
                        else if (controltype == "fpdeal") {
                            $("#thumbup_"+postid).addClass("pos");
                        }
			else if(controltype == 'hottopic')
			{
				var postid = $(data).find('htmlbits htmlbit').attr('id');
				$('#sdhottopic_' + postid + ' td.htscore').html($(data).find('htmlbits htmlbit').text());
			}
				
		}
		else
		{
			errordialog($(data).find("error").text());
		}
	});

	return false;
}

function sdlocaldeal_vote(threadid, vote, ddid)
{
	var sdthreadrate_param = {};
	sdthreadrate_param['ajax'] = 1;
	sdthreadrate_param['do'] = "sdthreadratevote"
	sdthreadrate_param['threadid'] = threadid;
	sdthreadrate_param['vote'] = vote;
	sdthreadrate_param['votetypeid'] = 0;
	sdthreadrate_param['controltype'] = 'fpdeal';
	sdthreadrate_param['securitytoken'] = SECURITYTOKEN;
	
	$.post('/forums/sdthreadrate_ajax.php', sdthreadrate_param, function(data){
		if ($(data).find("error").length == 0)
		{
			if(vote < 0)
			{
				// $('#localdailydeal_' + ddid).hide();
				$('a#thumbdown_' + threadid).addClass('thumb_selected');
				$('a#thumbup_' + threadid).removeClass('thumb_selected');			
			}	
			else
			{
				$('a#thumbdown_' + threadid).removeClass('thumb_selected');
				$('a#thumbup_' + threadid).addClass('thumb_selected');							
			}
			createCookie('dailydeal_' + ddid, vote, 7);
		}
		else
		{
			errordialog($(data).find("error").text());
		}
	});
	return false;
}

