var LocationRating = new function() {

    this.cachedRating;
	this.blocked = false;

	this.hover = function(rating) {
		if (this.blocked != true) {
            this.cachedRating = $('rating').getAttribute("value");
			this.set(rating);
            selectedMarker.changeSize(rating);
		}
	};

	this.rollOut = function() {
		if (this.blocked != true) {
			this.set(this.cachedRating);

            if (this.cachedRating == 0) {
                var friendsRating = $('friendsRecommendation').getAttribute("value");
                if (friendsRating > 0) {
                    selectedMarker.changeSize(friendsRating);
                } else {
                    var spottingRating = $('spottingRecommendation').getAttribute("value");
                    selectedMarker.changeSize(spottingRating);
                }
            } else {
                selectedMarker.changeSize(this.cachedRating);
            }
		}
	};

	this.set = function(rating) {

		for (var i = 1 ; i <= 10 ; i++) {
			var star = $('star'+i);

            if (rating == 0) {
                star.src = appPath + "/images/stars/not_rated.png";
                continue;
            }

			if(i<=rating) {
				star.src = appPath + "/images/stars/star_green.png";
			} else {
				star.src = appPath + "/images/stars/rating_empty.png";
			}
		}
	};

	this.storeToDb = function(rating) {
		if (this.blocked != true) {
			this.block();
			this.showLoading();
            new Ajax.Request(appPath + '/rating/ajaxRateLocation',{
                method:'post',
                parameters: {locationId:selectedId,rating:rating},
                asynchronous:true,
                evalJS:true,
                onSuccess:function(request) {
                    request.responseText;
                }
            });
		}
	};

    this.update = function(rating) {
        $('rating').setAttribute("value",rating);
        this.cachedRating = rating;
        this.set(rating);
        this.hideLoading();
        setRatingMessage("Danke fürs Bewerten.");
        showPublishDiv();
        showDeleteRatingDiv();
        selectedMarker.setRatedFlag(true);
        selectedMarker.setRating(rating);
        this.unblock();
    };

	this.block = function() {
		this.blocked = true;
	};

    this.unblock = function() {
        this.blocked = false;
    };

	this.click = function(rating) {
		this.storeToDb(rating);
	};

	this.hideLoading = function() {
		$('ratingLoader').setStyle({display:'none'});
	};

	this.showLoading = function() {
		$('ratingLoader').setStyle({display:'inline'});
	};

    this.getRating = function() {
        return this.cachedRating;
    };
};

var SpottingRecommendation = new function() {

    this.set = function(rating) {

		for (var i = 1 ; i <= 10 ; i++) {
			var star = $('spotting'+i);

			if(i<=rating) {
				star.src = appPath + "/images/stars/star_green.png";
			} else {
				star.src = appPath + "/images/stars/rating_empty.png";
			}
		}
	};

};

