var AssetsPage = Class.create({
    initialize: function () {
        this.init_many($(document.body));
    },
    init_one: function (el) {
        el.observe('click', this.asset_control_click.bindAsEventListener(this));
    },
    init_many: function (el) {
        el.select('div.b-asset').invoke('observe', 'click', this.asset_control_click.bindAsEventListener(this));
    },
    asset_control_click: function (e) {
        var el = e.element();
        if ((el.hasClassName('rate_up') || el.hasClassName('rate_down')) && this.check_guest(el, e)) {
            Utils.fetch('/my/comment/asset_rate/'+this.asset_id(el), this.asset_rated.bind(this), new Hash({
                'rate': el.hasClassName('rate_up')?1:-1
            }));
            e.stop();
        } else if (el.hasClassName('spam') && this.check_guest(el, e) && window.confirm(I18N.report_violation)) {
            Utils.fetch('/my/comment/spam/'+this.asset_id(el), function () { Utils.growl(I18N.thank_you) });
            e.stop();
        }
    },
    check_guest: function (el, e) {
        if (!$('content').hasClassName('guest')) {
            return true;
        } else {
            Utils.growl(I18N.no_guest_rate);
            e.stop();
        }
        return false;
    },
    asset_id: function (el) {
        this.last_asset = parseInt(el.up('div.asset').id.substring(6));
        return this.last_asset;
    },
    asset_rated: function (response) {
        if (!('error' in response)) {
            var el;
            $$('div.asset_'+this.last_asset).concat($('asset_'+this.last_asset)).compact().each(function (response, el) {
                el.down('.rate_up').update(response.rate_positive).next().update(response.rate_negative);
                if ((el = el.up('div.b-asset').down('div.overall_rate'))) {
                    el.down('span.value').update(response.rate);
                }
            }.bind(this, response));
        } else {
            alert(I18N.error +': '+ response.error);
        }
    }
});

