jq = jQuery.noConflict();

jq(document).ready(function()
{
        jq('.must_log').hide();
	var bg_col = jq(".post_rate").css("background-color");
        jq("a[name='is_rated']").css({'background-color' : 'black', 'color' : bg_col})
        jq('.post_rate').each(function()
        {
                if ($(this).text() < 0)
                {
                      $(this).css({'background-color' : 'black', 'color' : bg_col});
                }
        });
        jq('.comment_rate').each(function()
        {
                if ($(this).text() > 0)
                {
                      $(this).css({'color' : 'green'});
                }
        });
        jq("a[name='not_rated_up']").live("click", function()
        {
                var that = jq(this);
		var post_id = jq(this).attr("rel");
                var dataString = 'post_id=' + post_id + '&type=up';
                jq.ajax(
                {
                        type: "POST",
                        url: "../wp-content/themes/zeroprod/rate.php",
                        data: dataString,
                        success: function(msg)
                        {
                                if (msg == "must_log")
                                {
                                        jq('.must_log').fadeIn();
										setTimeout(function(){
											jq('.must_log').fadeOut();
										}, 2000);


	                        }
                                else if (msg == "alrdy_rated")
                                {
                                        //alert("alrdy_rated");
                                        // user already rate
                                }
                                else
                                {
                                        that.parent().children(':first').text(msg);
                                        bg_that = that.css("background-color");
                                        that.css({'background-color' : 'black', 'color' : bg_that});
                                        that.attr("name", "");
                                        that.attr("onclick", "return false");
                                        that.next().attr("name", "");
                                        that.next().attr("onclick", "return false");
                             	        if (msg >= 0)
                              	        {
                                                that.parent().children(':first').css({'background-color' : bg_col, 'color' : 'black'});
                               	        }
                                }
                        }
                });
             return false;
        });

        jq("a[name='not_rated_down']").live("click", function()
        {
                var that = jq(this);
                var post_id = jq(this).attr("rel");
                var dataString = 'post_id=' + post_id + '&type=down';
                jq.ajax(
                {
                        type: "POST",
                        url: "../wp-content/themes/zeroprod/rate.php",
                        data: dataString,
                        success: function(msg)
                        {
                                if (msg == "must_log")
                                {
                                        jq('.must_log').fadeIn();
										setTimeout(function(){
											jq('.must_log').fadeOut();
										}, 2000);
	                        }
                                else if (msg == "alrdy_rated")
                                {
                                        // user already rate
                                }
                                else
                                {
                                        that.parent().children(':first').text(msg);
                                        bg_that = that.css("background-color");
                                        that.css({'background-color' : 'black', 'color' : bg_that});
                                        that.attr("name", "");
                                        that.attr("onclick", "return false");
                                        that.prev().attr("name", "");
                                        that.prev().attr("onclick", "return false");
                                        if (msg < 0)
                              	        {
                                                that.parent().children(':first').css({'background-color' : 'black', 'color' : bg_col});
                               	        }
                                }
                        }
                });
                return false;
        });

jq("a[name='comment_not_rated_up']").live("click", function()
        {
                var that = jq(this);
		var comment_id = jq(this).attr("rel");
                var dataString = 'comment_id=' + comment_id + '&type=up';
                jq.ajax(
                {
                        type: "POST",
                        url: "../wp-content/themes/zeroprod/rate.php",
                        data: dataString,
                        success: function(msg)
                        {
                                if (msg == "must_log")
                                {
	                        }
                                else if (msg == "alrdy_rated")
                                {
                                        // user already rate
                                }
                                else
                                {
                                        if (msg >= 0)
					    {
						that.prev().text('+'+msg);
						that.prev().css({'color' : 'green'});
					    }
                                        else
					    that.prev().text(msg);
					that.css({'background-position' : '0px 0px'});
                                        that.attr("name", "");
                                        that.attr("onclick", "return false");
                                        that.next().attr("name", "");
                                        that.next().attr("onclick", "return false");
                                }
                        }
                });
             return false;
        });
    jq("a[name='comment_not_rated_down']").live("click", function()
        {
                var that = jq(this);
		var comment_id = jq(this).attr("rel");
                var dataString = 'comment_id=' + comment_id + '&type=down';
                jq.ajax(
                {
                        type: "POST",
                        url: "../wp-content/themes/zeroprod/rate.php",
                        data: dataString,
                        success: function(msg)
                        {
                                if (msg == "must_log")
                                {
	                        }
                                else if (msg == "alrdy_rated")
                                {
                                        //alert("alrdy_rated");
                                        // user already rate
                                }
                                else
                                {
                                        if (msg >= 0)
					    {
						that.prev().prev().css({'color' : 'black'});
						that.prev().prev().text('+'+msg);
					    }
                                        else
                                           that.prev().prev().text(msg);
                                        that.css({'background-position' : '25px 0px'});
                                        that.attr("name", "");
                                        that.attr("onclick", "return false");
                                        that.prev().attr("name", "");
                                        that.prev().attr("onclick", "return false");
                                }
                        }
                });
             return false;
        });
});
