var jpwhcore_rating = new Class({
    
        Implements: [Options],
        
        options: {

            container : '',
            
            id : 0,

            full_star : "./images/icons/jpwhcore_rating_full.png",
            
            partial_star : "./images/icons/jpwhcore_rating_partial.png",
            
            empty_star : "./images/icons/jpwhcore_rating_empty.png",
            
            total: 0,
            
            allowed: 0,

            exists: 0,
            
            exists_text: "",
            
            table: '',
            
            prefix: ''

        },
        initialize: function(opt) {

            this.setOptions(opt);
            
            this.options.container = $(opt['container']);
            if ((this.options.table == '') || (this.options.prefix == '')) { alert('Some information is missing.'); return false; }
            if (!this.options.container) { alert('Container "'+opt['container']+'" was not found.'); return false; }
            
            this.create();

        },
        create: function(){
                
            var self = this;
            
            var getRequest = new Request.JSON({
            
                url: 'jpwhcore_request.php',
                
                method: 'post',
                
                data: {'task': 'rating', 'id': self.options.id, 'table': self.options.table, 'prefix': self.options.prefix },

                onComplete: function(jsonObj)
                {
                    self.options.container.empty();

                    self.options.container.set({'styles' : { 'padding-left': '0px' }});

                    self.options.full = jsonObj.rating[0].full;
                    
                    self.options.partial = jsonObj.rating[0].partial;
                    
                    self.options.empty = jsonObj.rating[0].empty;
                    
                    self.options.total = jsonObj.rating[0].total;
                    
                    self.options.allowed = jsonObj.rating[0].allowed;

                    self.options.exists = jsonObj.rating[0].exists;

                    var id = 1;
                    
                    for (i=0; i<self.options.full; i++)
                    {
                        new Element('img', {
                            
                            'src': self.options.full_star,

                            'name': self.options.prefix+'_star_'+id,

                            'id': self.options.prefix+'_star_'+id,

                            'styles': { 'float':'left', 'padding-left':'3px' } 
                            
                        }).inject(self.options.container);
                            
                        id+=1;
                    }
                    
                    for (i=0; i<self.options.partial; i++)
                    {
                        new Element('img', { 
                        
                            'src': self.options.partial_star,
                            
                            'name': self.options.prefix+'_star_'+id,
                            
                            'id': self.options.prefix+'_star_'+id,
                            
                            'styles': { 'float':'left', 'padding-left':'3px' } 
                        
                        }).inject(self.options.container);
                        
                        id+=1;
                    }
                    
                    for (i=0; i<self.options.empty; i++)
                    {
                        new Element('img', { 
                    
                            'src': self.options.empty_star,
                            
                            'name': self.options.prefix+'_star_'+id,
                            
                            'id': self.options.prefix+'_star_'+id,
                            
                            'styles': { 'float':'left', 'padding-left':'3px' } 
                        
                        }).inject(self.options.container);
                        
                        id+=1;
                    }

                    self.textfield = new Element('div', { 
                    
                        'html': self.options.total,
                            
                        'styles': { 'float':'left', 'padding-left':'10px', 'height': '27px', 'line-height': '27px' } 
                        
                    }).inject(self.options.container);

                    if (self.options.allowed == 1) {
                  
                        $$('img[id^='+self.options.prefix+'_star_]').addEvent('mouseenter', function(evt){

                            this.setStyle('cursor','pointer');
                            
                            self.enter(this.get('id').replace(self.options.prefix+'_star_', ''));
                                
                        });

                        $$('img[id^='+self.options.prefix+'_star_]').addEvent('mouseleave', function(evt){

                            this.setStyle('cursor','default');

                            self.leave();
                                
                        });

                        $$('img[id^='+self.options.prefix+'_star_]').addEvent('click', function(evt){

                            self.rate(this.get('id').replace(self.options.prefix+'_star_', ''));
                                
                        });
                        
                    } 

                    if (self.options.exists == 0) {

                        self.options.container.addEvent('mouseenter', function(evt){

                                self.textfield.set('html', self.options.exists_text);
                                
                        });

                        self.options.container.addEvent('mouseleave', function(evt){

                                self.textfield.set('html', self.options.total);
                                
                        });

                    }

                }
                
            }).send();

        },

        enter: function(rating){
        
            for(var x=1; x<=5; x++) {
                            
                 if(x <= rating) {
                                
                      $(this.options.prefix+'_star_'+x).setProperty('src', this.options.full_star);
                                    
                 } else {
                                
                      $(this.options.prefix+'_star_'+x).setProperty('src', this.options.empty_star);
                                    
                 }
                                
             }
            
        },

        leave: function(){
        
            for(var x=1; x<=5; x++) {
                            
                 var f2 = this.options.full+1;
                                
                 if(x <= this.options.full) {
                                
                      $(this.options.prefix+'_star_'+x).setProperty('src', this.options.full_star);
                                    
                 } else if(this.options.partial != 0 && x == f2) {
                                
                      $(this.options.prefix+'_star_'+x).setProperty('src', this.options.partial_star);
                                    
                 } else {
                                
                      $(this.options.prefix+'_star_'+x).setProperty('src', this.options.empty_star);
                                    
                 }
                                
            }
            
        },

        rate: function(value){

            var self = this;

            this.options.container.empty();
            this.options.container.set({'html': 'Rating...', styles: { 'padding-left': '10px', 'height': '27px', 'line-height': '27px' }});
            
            var rateRequest = new Request.JSON({
            
                url: 'jpwhcore_request.php',
                
                method: 'post',
                
                data: {'task': 'rate', 'id': self.options.id, 'value': value, 'table': self.options.table, 'prefix': self.options.prefix },

                onComplete: function(jsonObj)
                {
                    self.create();
                }
                
            }).send();
            
        }
        
});
