$.fn.planevo_rater = function(options) {
    var opts = $.extend({}, $.fn.planevo_rater.defaults, options);
    return this.each(function() {
        var $this = $(this);
        var $on = $this.find('#baro');
        var $off = $this.find('#baroback');
        if (opts.size == undefined) opts.size = $off.height();
        if (opts.rating == undefined) opts.rating = $on.height() / opts.size;
        if (opts.id == undefined) opts.id = $this.attr('id');
        if (opts.noFloat == undefined) opts.noFloat = ($this.attr('name').substring(0,$this.attr('name').indexOf('.')) == 0 ? false : true);
	if (opts.textCount == undefined) opts.textCount = ($this.attr('name').substring($this.attr('name').indexOf('.')+1).substr(0,$this.attr('name').indexOf('_')-2) == 0 ? false : true);
	if (opts.textRateing == undefined) opts.textRating = ($this.attr('name').substring($this.attr('name').indexOf('.')).substring($this.attr('name').indexOf('_')) == 0 ? false : true);
        $off.mousemove(function(e) {
            var pageYOff = 0;
            if(jQuery.browser.msie) {
              pageYOff = document.body.scrollTop;
            }
            else {
              pageYOff = pageYOffset;
            }
            var top = $off.offset().top + 125 - (pageYOff + e.clientY);
	    var height = $off.height() - ($off.height() - top);
            //height = Math.min(Math.ceil(height / (opts.size / opts.step)) * opts.size / opts.step, opts.size * opts.ratings.length);
            height = Math.ceil(height / (opts.size / opts.step)) * opts.size / opts.step;
//$i('error').innerHTML = "e.clientY: " + e.clientY + " $off.offset().top: " + $off.offset().top + " $off.height(): " + $off.height() + " top: " + top + " height: " + height;
//$i('error').innerHTML += "<br />x + 125 - y1 = " + $off.offset().top + " + 125 - " + e.clientY + " = " + ($off.offset().top + 125 - e.clientY);
//$i('error').innerHTML += "<br />x + 125 - (s + y2) = " + $off.offset().top + " + 125 - ( " + pageYOff + " + " + e.clientY + ") = " + ($off.offset().top + 125 - ( pageYOff + e.clientY)); 
            $on.height(height);
            var r = Math.round($on.height() / $off.height() * (opts.ratings.length * opts.step)) / opts.step;
            $this.attr('title', opts.ratings[r - 1] == undefined ? r : opts.ratings[r - 1]);
        }).hover(function(e) { $on.addClass('baroHover'); }, function(e) {
            $on.removeClass('baroHover'); $on.height(opts.rating * opts.size);
        }).click(function(e) {
	    $off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
            var r = Math.round($on.height() / $off.height() * (opts.ratings.length * opts.step)) / opts.step;
            $.fn.planevo_rater.rate($this, opts, r);
        }).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
    });
};

$.fn.planevo_rater.defaults = {
    postHref: location.href,
    ratings: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100'],
    step: 1,
    size: 1
};

$.fn.planevo_rater.rate = function($this, opts, rating) {
    var $on = $this.find('#baro');
    var $off = $this.find('#baroback');
    $off.fadeTo(500, 0.4, function() {
        $.ajax({
            url: opts.postHref,
            type: "POST",
            data: 'id=' + opts.id + '&rating=' + rating,
            complete: function(req) {
                if (req.status == 200) { //success
		    if(opts.noFloat) { opts.rating = parseInt(req.responseText); }
		    else { opts.rating = parseFloat(req.responseText); }
                    //$off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
                    $off.css('cursor', 'default'); $on.css('cursor', 'default');
                    $off.fadeTo(600, 0.1, function() {
                        $on.css("height",""+((opts.noFloat ? ((opts.rating / 20 ) * opts.size): (opts.rating * opts.size))));
//                        var $count = $this.find('.ui-rater-rateCount');
//                        if(opts.textCount) $count.text(parseInt($count.text()) + 1);
			//if(opts.textRating) {
                        //  $this.find('.ui-rater-rating').text((opts.noFloat ? parseInt(opts.rating.toFixed(1)) : opts.rating.toFixed(1)));
			//}
                        $off.fadeTo(500, 1);
			//if(!opts.noFloat) $this.attr('title', 'Your rating: ' + rating.toFixed(1));
                    });
                } else { //failure
                    alert(req.responseText);
                    $off.fadeTo(2200, 1);
                }
            }
        });
    });
};

