var Guest = new function() {
    this.fullpath = '/';
    this.guest_id = 0;
    
    this.next = function() {
        if (arguments.length == 0) return;
        if (this.guest_id == 0) {
            this.guest_id = arguments[0];
        } else {
            var index = this.getIndex(arguments, this.guest_id) + 1;
            if (arguments.length > index) {
                this.guest_id = arguments[index];
            } else {
                this.guest_id = arguments[0];
            }
        }
        
        
        this.load();
    }
    
    this.prev = function() {
        if (arguments.length == 0) return;
        if (this.guest_id == 0) {
            var index = arguments.length - 2;
            if (index < 0) index = 0;
            this.guest_id = arguments[index];
        } else {
            var index = this.getIndex(arguments, this.guest_id) - 1;
            if (index > 0) {
                this.guest_id = arguments[index];
            } else {
                this.guest_id = arguments[arguments.length - 1];
            }
        }
        
        this.load();
    }
    
    this.load = function() {
        this.prepare();
        JsHttpRequest.query(
            this.fullpath+'?action=ajaxGetItem&cache='+Utils.getCacheValue(),
            {'id': this.guest_id},
            function(result, error) {
                if (error) alert(error);
                if (result['error'].length) {
                    for(var i in result['error']) {
                        alert(result['error'][i]);
                    }
                } else {
                    $('#guest_body').empty().append(result['body']);
                    $('#thr-pics').empty();
                    if (result['img'] != null) {
                        var img = result['img'].img;
                        $('#thr-pics').append(
                            Utils.create('img', {'src': img.small.src, 'width': img.small.width, 'height': img.small.height, 'alt': ''})
                        );
                    }
                    Doc.animateHeight();
                }
            }
        );
    }
    
    this.getIndex = function(arr, value) {
        for(var i = 0; i < arr.length; i++) {
            if (arr[i] == value) return i;
        }
        return -1;
    }
    
    this.prepare = function() {
        $('#guest_body').empty().append('<div class="loader"></div>');
        $('#thr-pics').empty().append('<div class="loader"></div>');
    }
    
    return this;
} ();