function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}

$(function(){
    $("div.user").hover(function() {
        $(this).stop().animate({
            width: 160
        }, 'fast');
        $(this).css('cursor','hand');
        $(".rating").css("width", "700px");
    }, function() {
        $(this).stop().animate({
            width: 48
        }, 'normal');
        $(this).css('cursor','pointer');
        $(".rating").css("width", "580px");
    });

    $(".language_image").each(function(){
        var current_language = getCookie("language")
        if($(this).attr("alt") === current_language) $(this).attr("src", "/static/images/" + current_language + "32.png")
        else $(this).attr("src", "/static/images/" + $(this).attr("alt") +".png")
    });
    $('#select-lang img').click(function() {
        // Unhighlight all the images
        $('#select-lang img').removeClass('highlighted');

        // Highlight the newly selected image
        $(this).addClass('highlighted');

        var lan = $(this).attr('alt')

        $.ajaxSetup({
            beforeSend: function(xhr, settings) {
                if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                    // Only send the token to relative URLs i.e. locally.

                    xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
                }
            }
        });

        $.ajax({
            url: "/i18n/setlang/",
            data: {language: lan},
            type: "POST",
            success: function(){
                window.location.reload();
            }
        });
});
});

