/**
* Update: 10/29/2008 -- update tip section, will depend on LBP.Tip
*/
LBP.Rating = new Class({
    Implements: [Events, Options],
    options: {
        active: false,  // true to enable voting
        scale: 5,    //the maximal point
        cssName: 'star',
        //style config
        width: 19,
        height: 18,
        fill: 13,
        left: 3,
        right: 3,
        
        tipWidth: 100,
        tipHeight: 13,
        
        ext: '.htmx',        
        urlMarker: 'url',
        ajaxMarker: 'ajax',
        
        caption: ['Dở tệ >.<','Thường thôi','Tạm ổn','Hay đấy chứ!','Tuyệt vời!!!'],
        
        errorMsg: '',       //the msg to fire on window
        onError: $empty,     //fire when user clicked on an inactive rating element
        onRate: $empty  //fire when user vote on an item
    },
    initialize: function(options) {
        this.setOptions(options);
    },
    assign: function(obj,options){
        if(!obj || obj.retrieve('wasAssignedRating')) return;
        obj.store('wasAssignedRating',true);
        this.setOptions(options);
        this.setUp(obj);
    },
    setUp: function(obj) {
        obj.addClass('pkg');
        obj.set('styles',{ height: this.options.height+'px', width: this.options.width*this.options.scale+'px', overflow: 'hidden'});
        var avg = obj.get('avg');
        avg= (avg==null)?0:avg.toFloat();
        var act = avg.toInt();
        var iAct = this.options.scale - act;
        var rem = Math.round( (avg - act)*100)/100;
        this.star = [];
        if(rem > 0) {
            iAct--;
            this.createStar(act,this.options.cssName+'-fill',obj);
            this.createPartial(rem, obj);
        } else if(rem < 0) {
            act--;
            this.createStar(act,this.options.cssName+'-fill',obj);
            this.createPartial(rem, obj);
            
        } else {
            this.createStar(act,this.options.cssName+'-fill',obj);
        }
        this.createStar(iAct,this.options.cssName+'-empty',obj);
        obj.removeProperty('avg');        
        if(this.options.active){
            obj.store('starArr', this.star);
            var temp = obj.get(this.options.urlMarker);
            obj.store('url', temp);
            obj.removeProperty(this.options.urlMarker);
            temp = $pick(obj.get(this.options.ajaxMarker), 'rating');
            obj.store('sendby', temp);
            obj.removeProperty(this.options.ajaxMarker);
            temp = obj.get('captarget');
            if(temp || this.options.caption) {
                obj.removeProperty('captarget');
                var tipCntr = obj.getNext('div.'+temp);
                if(tipCntr) {
                    var dfltTxt = tipCntr.get('html');
                    tipCntr.store('dfltText', dfltTxt);
                } else {
                    tipCntr = new Element("div", { 'class': 'rating-tip'});
                    tipCntr.set('styles', { display: 'none', position: 'absolute', zIndex: 1000, width: this.options.tipWidth+'px', height: this.options.tipHeight+'px'});
                    obj.adopt(tipCntr);
                }
                obj.store('starTip', tipCntr);
            }
            var idxstar= 0;
            this.star.each(function(item){
                item.title=this.options.caption[idxstar] ;
                idxstar++;                
                item.addEvents({
                    'mouseenter': function(event){
                        this.onHover(obj, item);
                    }.bind(this),
                    'mouseleave': function(event){
                        this.onOut(obj,item);
                    }.bind(this),
                    'click': function(event){
                        this.onRate(obj,item, this.options.onRate);
                        event.stopPropagation();
                    }.bind(this)
                });
            }.bind(this));
            obj.addEvents({
                'mouseenter': function(event){
                    this.onEnter(obj);
                }.bind(this),
                'mouseleave': function(event){
                    this.onLeave(obj);
                }.bind(this)
            });
           LBP.Tip.assign(this.star, {position: 'vCenter', tipCss: 'tip-content', margin: 0, opacity: 0.9});
        }
        
    },
    onHover: function(obj, item){
        var stars = obj.retrieve('starArr');
        var idx = stars.indexOf(item);
        for(i=idx; i>=0;i--){
            if(stars[i].hasClass(this.options.cssName+'-hover')) {
                break;
            } else {
                stars[i].addClass(this.options.cssName+'-hover');
            }
        }
        item.addClass(this.options.cssName+'-hover');
    },
    onOut: function(obj,item){
        if(item.hasClass(this.options.cssName+'-hover')) item.removeClass(this.options.cssName+'-hover');
    },
    onEnter: function(obj,item){
        obj.addClass(this.options.cssName+'-focus');
    },
    onLeave: function(obj,item){        
        var stars = obj.retrieve('starArr');
        for(i=0; i<stars.length;i++){
            if(stars[i].hasClass(this.options.cssName+'-hover')) stars[i].removeClass(this.options.cssName+'-hover');
        }
        obj.removeClass(this.options.cssName+'-focus');
    },
    createPartial: function(num, obj){
        var style = { 'display': 'block', 'float': 'left', 'overflow': 'hidden', 'height': this.options.height+'px'};
        var fill = new Element("div", { 'class': this.options.cssName+'-fill'});
        var empty = new Element("div", { 'class': this.options.cssName+'-empty'});
        var wrapper = new Element("div", { 'class': this.options.cssName+'-swrap'});
        
        var width = Math.round(num*this.options.fill);
        fill.set('styles', $merge(style,{
            'width': width+this.options.left+'px'
        }));
        empty.set('styles', $merge(style,{
            'width': this.options.fill-width+this.options.right+'px'
        }));
        wrapper.set('styles', $merge(style,{ 'width': this.options.width+'px'}));
        wrapper.adopt(fill, empty);
        obj.adopt(wrapper);
        this.star.push(wrapper);
    },
    createStar: function(num,css, obj){
        for(i=0;i<num;i++){
            var temp = new Element("div", { 'class': css});
            temp.set('styles',{ 
                'display': 'block',
                'float': 'left',
                'width': this.options.width+'px',
                'height': this.options.height+'px',
                'overflow': 'hidden'
                });
            obj.adopt(temp);
            this.star.push(temp);
        }
    },
    onRate: function(obj, item, fn){
        var stars = obj.retrieve('starArr');
        var value = stars.indexOf(item)+1;
																        /*'Edit o cho nay 1 ti nha,' => dua nao vay? */
        var url = obj.retrieve('url')+value+this.options.ext;
        var sendby = obj.retrieve('sendby');
        var tempRequest = new Request.LBP({
            'url':    url,
            'caller': sendby,
            onRequest: function(){ LBP.toggleLoading(true, this); }
        }).send('sendby=' + sendby + '&dmode=html_ajax');
        if(fn) fn.run(item,value);
    },
    onError: function(msg, fn){
        window.fireEvent('onError', msg);
        if(fn) fn.run();
    }
});