
/*# AVOID COLLISIONS #*/
;if(jQuery) (function($){
/*# AVOID COLLISIONS #*/

$.fn.rating = function(settings) {
 settings = $.extend({
		cancel: 'Atšaukti balsą', // advisory title for the 'cancel' link
		cancelValue: '-1',         // value to submit when user click the 'cancel' link
		required: false,         // disables the 'cancel' button so user can only select one of the specified values
		readOnly: false          // disable rating plugin interaction/ values cannot be changed
	}, settings || {});
 
 // multiple star ratings on one page
 var groups = {};
 
	// plugin events
 var event = {
  fill: function(n, el, style){ // fill to the current mouse position.
	  this.drain(n);
	  $(el).prevAll('.star').andSelf().addClass( style || 'star_hover' );
  },
  drain: function(n) { // drain all the stars.
  	$(groups[n].valueElem).siblings('.star').
		 removeClass('star_on').removeClass('star_hover');
  },
  reset: function(n){ // Reset the stars to the default index.
  	if (!$(groups[n].currentElem).is('.cancel')) {
 		 $(groups[n].currentElem).prevAll('.star').andSelf().addClass('star_on');
	  }
  },
  click: function(n, el) { // Selected a star or cancelled
			groups[n].currentElem = el;
			var curValue = $(el).children('a').text();
			// Set value
			$(groups[n].valueElem).val(curValue);
			// Update display
			event.drain(n);
			event.reset(n);
			// callback function, as requested here: http://plugins.jquery.com/node/1655
			if(settings.callback) settings.callback.apply(groups[n].valueElem, [curValue, el]);
  }      
 };
 
	// loop through each matched element
 this.each(function(i){
		// grouping:
		var n = this.name;
		if(!groups[n]) groups[n] = {count: 0};
		i = groups[n].count;
		groups[n].count++;
		
		// Things to do with the first element...
		if(i == 0){
			// Accept readOnly setting from 'disabled' property
			settings.readOnly = $(this).attr('disabled') || settings.readOnly;
			// Create value element (disabled if readOnly)
		 groups[n].valueElem = $('<input type="hidden" name="' + n + '" value=""' + (settings.readOnly ? ' disabled="disabled"' : '') + '>');
			// Insert value element into form
   $(this).before(groups[n].valueElem);
 		
			if(settings.readOnly || settings.required){
			// DO NOT display 'cancel' button
			     if ($(this).attr('c') == 'true'){
			         $(this).before(
                      $('<div class="cancel"><a title="' + settings.cancel + '">' + settings.cancelValue + '</a></div>')
					.mouseover(function(){ event.drain(n); $(this).addClass('star_on'); })
					.mouseout(function(){ event.reset(n);	$(this).removeClass('star_on'); })
					.click(function(){ event.click(n, this); }) 
                    ); 
                 }
			}
			else{
			// Display 'cancel' button
 			/*$(this).before(
     $('<div class="cancel"><a title="' + settings.cancel + '">' + settings.cancelValue + '</a></div>')
					.mouseover(function(){ event.drain(n); $(this).addClass('star_on'); })
					.mouseout(function(){ event.reset(n);	$(this).removeClass('star_on'); })
					.click(function(){ event.click(n, this); }) 
    ); */
			}
		}; // if (i == 0) (first element)
		
		// insert rating option right after preview element
		eStar = $('<div class="star"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
		$(this).after(eStar);
		
		if(settings.readOnly){
			// Mark star as readOnly so user can customize display
			$(eStar).addClass('star_readonly');
		}
		else{
			// Attach mouse events
			$(eStar)
			.mouseover(function(){ event.drain(n); event.fill(n, this); 
            if ($(this).children('a').text() == 1)  $('#vstatus').html('Blogietis');
            if ($(this).children('a').text() == 2)  $('#vstatus').html('Blogas');
            if ($(this).children('a').text() == 3)  $('#vstatus').html('Neutralus');
            if ($(this).children('a').text() == 4)  $('#vstatus').html('Geras');
            if ($(this).children('a').text() == 5)  $('#vstatus').html('Gerietis');
            })
			.mouseout(function(){ event.drain(n); event.reset(n); $('#vstatus').html($('#cstatus').val())})
			.click(function(){ event.click(n, this); });
		};
		
		//if(console) console.log(['###', n, this.checked, groups[n].initial]);
		if(this.checked) groups[n].currentElem = eStar;
		
		//remove this checkbox
		$(this).remove();
		
		// reset display if last element
		if(i + 1 == this.length) event.reset(n);
	
	}); // each element
  
	// initialize groups...
	for(n in groups)//{ not needed, save a byte!
		if(groups[n].currentElem){
			event.fill(n, groups[n].currentElem, 'star_on');
			$(groups[n].valueElem).val($(groups[n].currentElem).children('a').text());
		}
	//}; not needed, save a byte!
	
	return this; // don't break the chain...
};



/*# AVOID COLLISIONS #*/
})(jQuery);
/*# AVOID COLLISIONS #*/

