var vidViewer = new Class({
    
        Implements: [Options, Events],
        
        options: {

            container : '',

            container_name : '',

            width: 178,

            columns : 1,
            rows : 2,

            length : 0,

            type: 'horz',

            category : 0,
            show : 'c',
            order : 0,
            query: '',
          
            exception : 0,

            owner : '',

            more : false
            
        },
        initialize: function(opt) {

            this.setOptions(opt);
            this.options.container_name = opt['container'];
            this.options.container = $(opt['container']);
            if (!this.options.container) { alert('Container "'+opt['container']+'" was not found.'); return false; }
            if (this.options.query != '') { this.options.show = ''; }

        },
        view: function(){
        
            var self = this;
            
            var jsonRequest = new Request.JSON({
            
                method: 'post',
                
                url: 'vid_request.php',
                
                data: {'task': 'vidViewer', 'columns': self.options.columns, 'rows': self.options.rows, 'category': self.options.category, 'show': self.options.show, 'order': self.options.order, 'exception': self.options.exception, 'user': self.options.owner, 'length': self.options.length, 'more': self.options.more, 'query': self.options.query}, 
                
                onComplete: function(jsonObj) {
                
                    self.options.container.empty();

                    var nl_end = new Element('div', { 'class': 'vidViewer_nlb' }).inject(self.options.container);

                    var ckey = self.options.columns;
                    var clas = jsonObj.vids.length;

                    var outer_class = (self.options.type == 'horz') ? 'vidViewer_h' : 'vidViewer_v';
                    var first_class = (self.options.type == 'horz') ? 'vidViewer_h-left' : '';
                    var second_class = (self.options.type == 'horz') ? 'vidViewer_h-right' : 'vidViewer_v-bottom';

                    if (self.options.type == 'vert') { new Element('div', { 'class': 'vidViewer_v-middle', 'html': '' }).inject(self.options.container); }

                    var total_results = 0;
                    jsonObj.vids.each(function() { total_results++; });

                    if (total_results > 0) {

                        self.options.container.setStyle('display', 'block');

                        jsonObj.vids.each(function(vid, key) {

                            var h = new Element('div', {

                                'styles': { width: self.options.width },
                                'class': outer_class

                            }).inject(self.options.container);

                            var h_inner = new Element('div').inject(h);

                            var h_first = new Element('div', { 'class': first_class }).inject(h_inner);

                            var h_second = new Element('div', { 'class': second_class }).inject(h_inner);

                            var nl = new Element('div', { 'class': 'vidViewer_nl' }).inject(h_inner);

                            var h_thumb_a = new Element('a', {

                                'class': 'vidViewer_h-thumb',
                                'href': vid.vid_url,
                                'title': vid.vid_title

                            }).inject(h_first);

                            var h_thumb = new Element('img', {

                                'width': '80',
                                'height': '70',
                                'src': vid.vid_thumb,
                                'class': 'vidViewer_h-thumb',
                                'styles': {'width': '80px', 'height': '70px'}

                            }).inject(h_thumb_a);

                            var h_title = new Element('div').inject(h_second);

                            var h_title_a = new Element('a', {

                                'class': 'vidViewer_h-title',
                                'href': vid.vid_url,
                                'title': vid.vid_title,
                                'html': vid.vid_title_short

                            }).inject(h_second);

                            var h_info = new Element('div', {

                                'class': 'vidViewer_h-info',
                                'html': vid.vid_views

                            }).inject(h_second);

                            var h_stars = new Element('div', {

                                'class': 'vidViewer_h-info',
                                'html': vid.vid_stars

                            }).inject(h_second);

                            if ((key + 1) == ckey) {

                                if (self.options.type == 'vert') { new Element('div', { 'class': 'vidViewer_v-middle', 'html': '' }).inject(self.options.container); }
                                new Element('div', { 'class': 'vidViewer_nl' }).inject(self.options.container);
                                if (self.options.type == 'vert' && clas != ckey) { new Element('div', { 'class': 'vidViewer_v-middle', 'html': '' }).inject(self.options.container); }
                                ckey = ckey+self.options.columns;

                            }

                        });
                        
                        new Element('div', { 'class': 'vidViewer_nlb' }).inject(self.options.container);

                        if (jsonObj.more == true) {
                            new Element('div', { 'class': 'vidViewer_moretext', 'html': '[ <a href=\'+vid.vid_title+\'>{/literal}{lang_print id=13500092}{literal}</a> ]' }).inject(self.options.container);
                            new Element('div', { 'class': 'vidViewer_nlbb' }).inject(self.options.container);
                        }

                        if (typeof viewerSuccess == 'function') { viewerSuccess(self.options.container_name); }
                        
                    } else {

                        self.options.container.setStyle('display', 'none');
                        if (typeof viewerFailure == 'function') { viewerFailure(self.options.container_name); }

                    }

                }
                
            }).send();
            
        },
        layout: function(layout) {

            if (layout == 'side') {

                this.options.width = 178;
                this.options.columns = 1;
                this.options.length = 10;

            } else if (layout == 'center') {

                this.options.width = 218;
                this.options.columns = 2;
                this.options.length = 20;

            } else if (layout == 'center-vertical') {

                this.options.width = 80;
                this.options.columns = 5,
                this.options.length = 10;
                this.options.type = 'vert';

            }

        }
});