var Rating = new Class({
    opt: function(id, opt) {
        this.id = id;
        this.rated = false;
        this.opt = {
            rate_id: null,
            num: 5,
            stars: 0,
            completedClass: 'rated',
            bg_rated: af_uri+'img/rated.gif',
            bg_rated_color: '#FFFFFF',
            id_offset: 1,
            getKey: 'id',
            login: false 
        };
        Object.extend(this.opt,opt||{});
        this.proto = new Element('a',{
                'class': 'rate',
                'href': '#'
            });
        this.notification = new Element('div',{
                'class': 'notification',
                'styles': {
                    'height': '15px',
                    'width': '200px',
                    'float': 'left',
                    'position': 'absolute',
                    'top': '1px',
                    'left': '0px',
                    'border-top': '2px solid '+String(this.opt.bg_rated_color),
                    'background': String(this.opt.bg_rated_color)+' url('+this.opt.bg_rated+') no-repeat top left'
                }
            }); 
    },
    initialize: function(id,opt) {
        this.opt(id,opt);
        this.create(); 
    },
    create: function() {
        $(this.id).ref = this; 
        $(this.id).addEvent('mouseout',function(){
            if(!$(this.id).hasClass(this.ref.opt.completedClass)) {
                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).setOpacity(0); 
    },
    over: function(el) {
        if(this.opt.multipleRatings || !$(this.id).hasClass(this.opt.completedClass)) { 
            $(this.id).getElements('a').each(function(item,index){
                item.removeClass('avg'); 
                item.addClass('off'); 
                if(Number(item.id)<=Number(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);*/
        return this.opt.rate_id;  
    },
    down: function(el,ev) {
	if(!this.opt.login) return alert('Alert! You must first register before ranking videos.');
	if(this.getCookie()) return alert('Alert! You may only vote once per day.');
        var event = new Event(ev);
            event.stop();
        //if(this.rated && !this.opt.multipleRatings) {
        if(this.rated) {
            return false;
        } 
        this.rated = true; 
	this.setCookie();
        var ajx = new Ajax(af_uri+'?'+this.opt.getKey+'='+this.identify()+'&r='+this.check(),{method:'get'}).request();
        $(this.id).addClass(this.opt.completedClass); 
        (this.notification).effect('opacity',{duration: 500, onComplete: 
                function(obj){ 
                    (function(){obj.effect('opacity',{duration: 500}).start(1,0)}).delay(500);
                }
            }).start(0,1); 
    }, 
    reset: function(id,rating) {
        this.rated = false;
        this.opt.rate_id = id;
        this.opt.stars = rating!=undefined ? rating : this.opt.stars; 
        var ajx = new Ajax(af_uri+'?'+this.opt.getKey+'='+this.identify(),{method:'get',onComplete:function(response){
                this.opt.stars = response; 
            }.bind(this)}).request();
        $(this.id).removeClass(this.opt.completedClass)
        $(this.id).getElements('a').each(function(item,index){
            var className = index<=(this.opt.stars) ? 'avg' : 'off';
            item.addClass(className);
        }, this); 
    },
    setCookie: function(){
	var myCookie = Cookie.set('usafRating',1,{duration:1});
    },
    getCookie: function() {
	return Cookie.get('usafRating');
    }
});
