var ActivityRatingFull = new Class({
   
    opt: function(id, opt) {
        this.id = id;
        this.opt = {
            num: 5,
            stars: 0,
            countAlign: 'right',
            completedClass: 'rated',
            bg_rated: uri+'img/usr_rated.gif',
            bg_rated_color: '#FFFFFF',
            id_offset: 1
        };
        Object.extend(this.opt,opt||{});
        
        
    },

    initialize: function(id,opt) {
        this.opt(id,opt);
        this.create(); 
    },
    
    create: function() {
        
        this.proto = new Element('a',{
            'class': 'rate',
            'href': '#'
        });
        
        this.notification = new Element('span',{
            'styles': {
                'border':'0px solid #EEEEEE',
                'color':'#8e8e8e',
                'font-family':'tahoma',
                'font-size':'11px',
                'position':'relative',
                'top':'-2px',
                'padding':'0 4px'
            }
        }); 
        $(this.id).setStyles({
            'position':'relative',
            'top':'2px'
        });
        $(this.id).ref = this; 
        $(this.id).addEvent('mouseout',function(){
            if(!$(this.id).hasClass('rated')) {
                var ref = $(this.id).ref; 
                $(this.id).getElements('a').each(function(item,index){
                    if(index<=(ref.opt.stars)-1) {
                        item.addClass('avg'); 
                    };
                }); 
            }
        }); 
        for(var i=1; i<(this.opt.num)+1; i++) {
            var el = (this.proto).clone().injectInside(this.id); 
                el.ref = this; 
                el.setProperty('id',i);
                el.setProperty('title', 'Rate This Activity: '+i+'/'+this.opt.num);
                el.setProperty('alt', 'Rate This Activity: '+i+'/'+this.opt.num);
            var className = i<=(this.opt.stars) ? 'avg' : 'off'; 
                el.addClass(className); 
                el.addEvent('mouseover',function(){
                    this.ref.over(this); 
                }); 
                el.addEvent('mouseout',function(){
                    this.ref.out(this); 
                }); 
                el.addEvent('click',function(ev){
                    this.ref.down(this,ev); 
                }); 
        };
        var n = (this.notification).injectInside(this.id).setText(this.getCountString()); 
    },
    
    over: function(el) {
        if(!$(this.id).hasClass(this.opt.completedClass)) { 
            $(this.id).getElements('a').each(function(item,index){
                item.removeClass('avg'); 
                item.addClass('off'); 
                if(item.id<=el.id){
                    item.removeClass('off'); 
                }
            }); 
        }
    },
    
    out: function(el) {
        if(!$(this.id).hasClass(this.opt.completedClass)) { 
            $(this.id).getElements('a').each(function(item,index){
                if(item.id<=el.id){
                    item.addClass('off'); 
                }
            }); 
        }
    },
    
    check: function() {
        var i=0; 
        $(this.id).getElements('a').each(function(item,index){
            if(!item.hasClass('off')) { i++; }
        }); 
        return i; 
    },
    
    identify: function() {
        var str = $(this.id).id; 
        return str.substr(this.opt.id_offset,str.length); 
    },
    
    getCountString: function() {
        var text = '(';
        text += this.opt.currentCount;
        text += ' User Rating';
        text += (this.opt.currentCount == 1) ? '' : 's';
        text += ')';
        return text;
    },
    
    down: function(el,ev) {
        if ($(this.id).hasClass('rated')) return;
        this.notification.setOpacity(0);
        this.notification.setText('(Rating Saved)');
        this.notification.setStyle('color','#ff7700');
        $(this.id).addClass('rated'); 
        var ajx = new Ajax(uri+'index.php?tpl=rate&aid='+this.identify()+'&r='+this.check(),{method:'get'}).request();
        var event = new Event(ev);
            event.stop();
        this.notification.effect('opacity',{duration: 500}).start(0,1); 
    }

});

