$(document).ready(function(){

    function jsreport (text) {
        $('#jsreport').html('<div id="systemalert">'+text+'</div>').css('color','black');
    }

    function jssetrating(id, r,f,a){
        $('#post_'+id+' .rating .r').text(r);
        $('#post_'+id+' .rating .f').text(f);
        $('#post_'+id+' .rating .s em').text(a);
    }

    $('.rating a').click(function(){
        var postid = $(this).attr('rel');
        $.ajax({
            url: SD+'bb_ajax.php',
            data: 'op=rating&tp=posts&id=' + postid,
            type: 'GET',
            dataType : 'xml',
            success: function(data, textStatus){
                jsreport($(data).find('responce').find('resault').text());
                ratingr = $(data).find('responce').find('countreg').text();
                ratingf = $(data).find('responce').find('countfree').text();
                ratinga = $(data).find('responce').find('countall').text();
            },
            cache: false,
            complete: function(){
                jssetrating(postid, ratingr, ratingf, ratinga);
            }
        });
        return false;
    });

    $('#tags2edit input:checkbox').click(function(){
        var tagsfield = $('.tags input:text');
        tagsfield.val('');
        $('#tags2edit input:checked').each(function(){
            tagsfield.val(tagsfield.val()+$(this).val()+', ');
        });
    });

    function jssetfav(id, i, t){
        $('#post_'+id+' a.favorite img').attr('src', SD+'images/favorite'+i+'.gif');
        $('#post_'+id+' a.favorite').attr('title',t);
    }

    $('a.favorite').click(function(){
        var postid = $(this).attr('rel');
        $.ajax({
            url: SD+'bb_ajax.php',
            data: 'op=favorite&tp=posts&id=' + postid,
            type: 'GET',
            dataType : 'xml',
            success: function(data, textStatus){
                jsreport($(data).find('responce').find('resault').text());
                favimage = $(data).find('responce').find('favimage').text();
                favtitle = $(data).find('responce').find('favtitle').text();
            },
            cache: false,
            complete: function(){
                jssetfav(postid, favimage, favtitle);
            }
        });
        return false;
    });

    //footer 2 bottom;
    function fe_footer2bottom(){
        if ($('body').height() < $(window).height()) {
            $('#footer').css({'position': 'absolute', 'bottom': '0px'});
        } else {
            $('#footer').css({'position': 'relative', 'bottom': ''});
        }
    }

    fe_footer2bottom();
    $(window).resize(function(){
        fe_footer2bottom();
    });

//    if ($('#systemalert').html().length) {
//        $('#jsreport').html($('#systemalert').text());
//        $('#systemalert').hide();
//    }
});
