var Ratings = function(){
    var className = "Ratings";
    var privateVar = "privateVar";
    var name = className;
    var averageRating = 0;
    var userRating = 0;
    var enabled = true;
    var initialized = false;
    
    
    
    function setAverageRating(value){
        averageRating = formatDecimalValue(value);
        intValue = Math.round(averageRating);
        showAverageRating(intValue);
    }
    
    function formatDecimalValue(value){
        // convert decimal value to n.x where n is 0 - 5 and x is 0 - 9
        var num = (isNaN(value)) ? 0 : value;
        return Math.round(num * 10) / 10;
    }
    
    function setUserRating(value){
        userRating = value;
        showUserRating(userRating);
        
    }
    
    function showStars(evt){
        //console.info("showStars " + Event.element(evt))
        if (this.enabled) {
            if (evt) {
                var ele = Event.element(evt);
                //console.info(ele.id)
                var rating = 0;
                if (ele) {
                    rating = getRatingFromID(ele.id);
                }
                showUserRating(rating);
            }
        }
    }
    
    function showRate(evt){
        if (this.enabled) {
            //console.info("show rating "+evt.target.id+ "userRating:"+userRating)
            showUserRating(userRating);
        }
    }
    
    function setRating(evt){
        if (evt) {
            Event.stop(evt);
        }
        if (this.enabled) {
            var rating = 0;
            if (evt) {
                var ele = Event.element(evt);
                if (ele) {
                    rating = getRatingFromID(ele.id);
                }
                console.info("set Rating to " + ele.id);
                userRating = rating;
                showUserRating(userRating);
                sendrating(userRating);
                //showAverageRating(userRating)
            }
        }
        return false;
    }
    
    function showUserRating(rating){
        //console.info("showUserRating(" + rating + ")");
        var id = "your_rating";
        var classNames = ["rate0", "rate1", "rate2", "rate3", "rate4", "rate5"];
        if (rating !== undefined && rating > -1 && rating < classNames.length) {
            for (var i = 0, len = classNames.length; i < len; i++) {
                var className = classNames[i];
                //console.info("Element.removeClassName("+id+", "+className+")");
                Element.removeClassName(id, className);
            }
            
            Element.addClassName(id, classNames[rating]);
        }
    }
    
    function showAverageRating(rating){
        var id = "average_rating";
        var classNames = ["avrate0", "avrate1", "avrate2", "avrate3", "avrate4", "avrate5"];
        if (rating !== undefined && rating > -1 && rating < classNames.length) {
            for (var i = 0, len = classNames.length; i < len; i++) {
                var className = classNames[i];
                // console.info("Element.removeClassName(" + id + ", " + className + ")");
                Element.removeClassName(id, className);
                var str;
                if (averageRating < 1) {
                    str = "Be the first";
                    
                } else {
                    str = averageRating + ' out of 5 stars';
                }
                var imgsrc = window.location.protocol + "//" + window.location.host + "/motorsports/images/global/transparent_pxl.gif";
                var img = '<img src="' + imgsrc + '" width="79px" height="13px" />';
                var msg = '<a href="#" onclick="return false" title="' + str + '" >' + img + '</a>';
                Element.update(id, msg);
            }
            
            Element.addClassName(id, classNames[rating]);
        }
    }
    
    
    
    
    function getRatingFromID(id){
        var rating = 0;
        if (id) {
            var idparts = id.split("_");
            if (idparts && idparts.length > 1) {
                rating = parseInt(idparts[1], 10);
                if (isNaN(rating)) {
                    rating = 0;
                }
            }
        }
        return rating;
        
    }
    
    function init(){
        if (!initialized) {
            initialized = true;
            //console.info('Rating.init()');
            Event.observe('r_1', 'mouseover', this.showStars.bind(this));
            Event.observe('r_1', 'mouseout', this.showRate.bind(this));
            Event.observe('r_1', 'click', this.setRating.bind(this));
            Event.observe('r_2', 'mouseover', this.showStars.bind(this));
            Event.observe('r_2', 'mouseout', this.showRate.bind(this));
            Event.observe('r_2', 'click', this.setRating.bind(this));
            Event.observe('r_3', 'mouseover', this.showStars.bind(this));
            Event.observe('r_3', 'mouseout', this.showRate.bind(this));
            Event.observe('r_3', 'click', this.setRating.bind(this));
            Event.observe('r_4', 'mouseover', this.showStars.bind(this));
            Event.observe('r_4', 'mouseout', this.showRate.bind(this));
            Event.observe('r_4', 'click', this.setRating.bind(this));
            Event.observe('r_5', 'mouseover', this.showStars.bind(this));
            Event.observe('r_5', 'mouseout', this.showRate.bind(this));
            Event.observe('r_5', 'click', this.setRating.bind(this));
        }
    }
    
    return {
        name: name,
        averageRating: averageRating,
        userRating: userRating,
        init: init,
        enabled: enabled,
        showStars: showStars,
        showRate: showRate,
        setRating: setRating,
        setAverageRating: setAverageRating,
        setUserRating: setUserRating
    };
}();

function sendrating(rating){
    Ratings.enabled = false;
	var url = getVideoUrl();
    if (!isNaN(rating) && rating >= 0 && rating <= 5) {
        toyotaRacingListener.initMethod(url, rating, function(data){
            Ratings.setAverageRating(data);
            Ratings.enabled = true;
        });
    }
}


function initRatings(){
    var url = getVideoUrl();
	toyotaRacingListener.findAvgMethod(url, function(data){
        Ratings.setAverageRating(data);
        Ratings.enabled = true;
    });
}


function getUserRating(){
    var url = location.href;
    ///*
    toyotaRacingListener.getUserRating(url, function(data){
        Ratings.enabled = true;
        responseAverage = Math.round(data);
        Ratings.setUserRating(responseAverage);
    });
    //*/
    Ratings.setUserRating(0);
}


function doPageLoad(){

    if (!window.console) {
        window.console = {};
        window.console.info = function(){
        };
        
    }
    
    
    Ratings.init();
    Ratings.enable = false;
    initRatings();
    getUserRating();
    //
}

function getVideoUrl() {
	var loc = location.href;
	if (loc.indexOf("video.html") >= 0) {
		return loc;
   	 }
	loc = location.protocol + "//" + location.hostname + "/motorsports/video.html" + location.hash;
	return loc;
}

Event.observe(window, 'load', doPageLoad, false);





