function VIET_vnl_Rating() {
    this.SAVE_INTERVAL = 5; // interval betweeen saving votes (in minutes)
    this.oSaveRatingTimer = null;

    this.iCurrGuideId = 0;
    this.bUserRated = false;
    this.iUserRating = 0;
}
VIET_vnl_Rating.prototype = {

    updateRatingInfo: function(guideId) {

        $('showRating_starcurr').style.width = '0px';
        $('showViews').innerHTML = '';

        var handleSuccess = function(o){
            try {
                obj = eval("(" + o.responseText + ")");

                var rate = 0;
                if( obj['ratingTotal']!=null && obj['ratingCount']!=null && obj['ratingCount']>0 )
                    rate = obj['ratingTotal']/obj['ratingCount'];
                
                var views = obj['views'] || 0;

                $('showRating_staruser').style.backgroundPosition = '0px 0px';
                $('showRating_starcurr').style.visibility = 'visible';
                $('showRating_starcurr').style.width = Math.round(90*(rate/5)) + 'px';
                $('showViews').innerHTML = views;
            }
            catch(ex) {}
        }
        YAHOO.util.Connect.asyncRequest('GET', '/rating/guideinfo/guideId/' + guideId + '/format/json', {success:handleSuccess, failure:function() {}});

        this.iCurrGuideId = guideId;
        this.bUserRated = false;
    },

    rateCurrGuide: function(rating) {
        if( this.iCurrGuideId<=0 ) return;

        this.bUserRated = true;
        this.iUserRating = rating;

        // update display
        $('showRating_starcurr').style.visibility = 'hidden';
        $('showRating_staruser').style.backgroundPosition = '0px -' + (17*this.iUserRating) + 'px';
        
        // current ratings
        var s = $_COOKIES('rating') || '';
        var pairs = s.split(';');
        var currRatings = [];
        for( var i=0; i<pairs.length; i++ ) {
            var pair = pairs[i];
            if( pair.indexOf('=')<=0 )
                continue;
                
            var namevalue = pair.split('=');
            if( namevalue.length!=2 )
                continue;
            
            currRatings[namevalue[0]] = namevalue[1];
        }
        
        // update rating
        currRatings['' + this.iCurrGuideId] = rating;
        
        // rewrite to cookies
        var s = '';
        for( var name in currRatings ) {
            s += (s!='' ? ';' : '') + name + '=' + currRatings[name];
        }
        setCookie('rating', s, 30);
        
        // start the saving timer if not already started
        if( this.oSaveRatingTimer==null ) {
            this.oSaveRatingTimer = new VIET_vnl_util_Timer(this.SAVE_INTERVAL*60);
            var me = this;
            this.oSaveRatingTimer.RegisterEvent(function() { me.saveRatings(); });
            setTimeout( function() {me.oSaveRatingTimer.Start();}, this.SAVE_INTERVAL*60*1000 );
        }

        $('showRating').style.display = 'none';
        $('showRating_thanks').style.display = '';
        setTimeout("oRating.hideThanksMsg()", 1000);

    },
    
    //
    // periodically call to server to save votes
    //
    saveRatings: function() {
        var sCk = $_COOKIES('rating') || '';
        if( sCk=='' )
            return;

        var me = this;
        var handleSuccess = function(o){
            try {
                setCookie('rating', '', 30);
            }
            catch(ex) {}
        }
        
        YAHOO.util.Connect.asyncRequest('GET', '/rating/batchguide/query/' + sCk + '/format/json', {success:handleSuccess, failure:function() {}});
    },
    
    hideThanksMsg: function() {
        $('showRating').style.display = '';
        $('showRating_thanks').style.display = 'none';
    },
    
    stars_mouseover: function(num) {
        if( this.bUserRated==false )
            $('showRating_starcurr').style.visibility = 'hidden';

        $('showRating_staruser').style.backgroundPosition = '0px -' + (17*num) + 'px';
    },

    stars_mouseout: function() {
        if( this.bUserRated )
            $('showRating_staruser').style.backgroundPosition = '0px -' + (17*this.iUserRating) + 'px';
        else {
            $('showRating_starcurr').style.visibility = 'visible';
            $('showRating_staruser').style.backgroundPosition = '0px 0px';
        }
    },
    
    stars_click: function(num) {
        this.rateCurrGuide(num);
    }
    
}
var oRating = new VIET_vnl_Rating();

