var User = {
    options : {
        id      : 0,
        isValid : 0
    },

    init : function(options) {
        Object.extend(this.options, options || {});
        this.initObservers();
    },

    initObservers : function() {
        try {
            if (!this.options.isValid) {
                $('add-review').onclick = function() {
                    location.href = '/join';
                    return false;
                }.bind(this);

                $('view-all-reviews').onclick = function() {
                    location.href = '/join';
                    return false;
                }.bind(this);
            } else {
                $('send-message').onclick = function() {
                    Application.openChatWindow(this.options.id);
                    return false;
                }.bind(this)

                $('add-to-favorites').onclick = function() {
                    this.addUserToFavorites(this.options.id);
                    return false;
                }.bind(this)

                $('view-all-reviews').onclick = function() {
                    this.getUserReviews('all');
                    Element.scrollTo('reviews-list');
                    return false;
                }.bind(this);

                $('add-review').onclick = function() {
                    $('add-review-form').toggleClassName('visible');
                    Element.scrollTo('add-review-form');
                    return false;
                }.bind(this);

                $('add-review-form').onsubmit = function() {
                    this.getUserReviews('simple', $('review-body').value);
                    return false;
                }.bind(this);
            }
        } catch (e) {};
    },

    getUserReviews : function(type, body) {
        new Ajax.Updater('reviews-list', '/ajax/user/reviews/',
        {
            method     : 'post',
            parameters : {
                id     : this.options.id,
                list   : type || 'simple',
                body   : body || null
            },
            onComplete : function() {
                if (body) {
                    $('add-review-form').toggleClassName('visible').reset();
                }
            }
        })
    },

    addUserToFavorites : function(id) {
        new Ajax.Request('/ajax/user/add-to-favorites/id/' + id,
        {
            method : 'get',
            onComplete : function(response) {
                var r = response.responseJSON;
                var txt = '';
                if ('success' == r) {
                    txt = 'Added Successfully'
                } else if ('auth-fail' == r) {
                    txt = 'You need to login';
                } else if ('exists' == r) {
                    txt = 'Already added';
                }
                $('add-to-favorites').update(txt);
                return false;
            }
        });
    }
}