/*
 * @author Pandi Networks
 * @version 0.1
 */


/*
 * Other events
 */
$(function()
{
    // Hot stuff title bar
    $("div#hot-stuff > a").hover(function()
    {
        $("span.info", this).stop().animate({
            bottom: 0
        }, 250);
    },
    function()
    {
        $("span.info", this).stop().animate({
            bottom: -54
        }, 200);
    });
    
    
    // Auto-complete
    $("#search-query").autocomplete({ url: "/search.php" });


    // Dropdown
    $("span.dropdown-handle").click(function()
    {
        var $h = $(this);
        var $d = $h.closest('div.dropdown');
        
        if($d.hasClass('opened'))
        {
            $d.removeClass('opened');
            $h.removeClass('opened');
        }
        else
        {
            $d.addClass('opened');
            $h.addClass('opened');
        }
    });
    
    // Comments hover
    $("div.comment > div.tools").hide();
    
    $("div.comment").hover(function()
    {
        $("div.tools", this).slideDown(500);
    }, function()
    {
        $("div.tools", this).slideUp(500);
    });
    
    ///////////////////////// Likes plugin /////////////////////////
    $("a.like").click(function()
    {
        var $e = $(this);
        var like = {
            type: $e.attr("data-like-type"),
            id: $e.attr("data-like-id")
        };
        
        $.get("/includes/functions/php/get_likes.php", like, function(data)
        {
            switch(data)
            {
                case 'like':
                    $("img.like-image", $e).attr("src", "/assets/gfx/icons/dislike.png");
                    
                    var likeCount = $(".like-count", $e);
                    if(likeCount.length == 1)
                    {
                        var count = parseInt(likeCount.text());
                        if(!isNaN(count))
                        {
                            count++;
                            likeCount.text(count);
                        }
                    }
                    break;
                case 'dislike':
                    $("img.like-image", $e).attr("src", "/assets/gfx/icons/like.png");
                    
                    var likeCount = $(".like-count", $e);
                    if(likeCount.length == 1)
                    {
                        var count = parseInt(likeCount.text());
                        if(!isNaN(count))
                        {
                            count--;
                            likeCount.text(count);
                        }
                    }
                    break;
                default:
                    alert("Unexcepted error trying to like [#" + like.id +"]!");
                    break;
            }
        });
    });
});
