$(function(){
	Stars.init('');
});

var Stars = {
	
	// vars
	star_width : 17, // width in pixels of the star image
	single_voting : false, // once voted, the stars lock and do not allow you to change your vote
	max : 5, // total number of stars
	container_width : 0, // this is set in the init(), no need to set a value here.
	wrap_class : '',
	
	// methods
	init : function(sel, max){
		if(max){
		    Stars.max = max;
		}
		if(!sel){
		    sel = '.stars';
		}
		if(Stars.wrap_class){
		    Stars.wrap_class = ' ' + Stars.wrap_class;
		}
		
		var id = 'star_obj_'; // this can be named whatever. just a unique identifier.
		
		var $stars = $(sel);
		
		$stars.each(function(i){
			var $el = $(this);
			    var readonly = $el.attr('readonly');
			//var $obj = $('<div class="stars_container'+Stars.wrap_class+'" id="'+id+i+'" title="'+$el.val()+' star'+($el.val() != 1 ? 's' : '')+'"></div>');
			var $obj = $('<div class="stars_container large" id="'+id+i+'" title="'+$el.val()+' star'+($el.val() != 1 ? 's' : '')+'"></div>');
			
			// set container width so just the right number of stars display
			Stars.container_width = Stars.star_width * Stars.max;
			//$obj.css('width', Stars.container_width+'px');
			$obj.css('width', '85px');
			
			// append the input to post the value.
			var $input = $('<input type="hidden" name="'+$el.attr('name')+'" value="'+$el.val()+'" />');
			    $obj.append($input);
			
			if(!readonly){
				// append all stars div
				for(var v=1; v <= Stars.max; v++){
					$obj.append('<a href="#star-'+v+'" class="star"></a>');
				}
			}
			
			// append star value object
			var $star_val = $('<div class="stars_value"></div>');
			
			// set initial width
			var value = $el.val() ? $el.val() : 0;
			$star_val.css('width', Stars.get_width(id+i, value));
			$obj.append($star_val);
			
			// drop the text input, replace it with stars
			$el.replaceWith($obj);
			
			
			if(!readonly){
				
				// star events
				
				$obj.hover(
					function(n){
						if(!Stars.locked(id+i)){
							$star_val.addClass('hover');
						}
					},
					function(out){
						$star_val.removeClass('hover');
						$star_val.css('width', Stars.get_width(id+i));
					}
				);
				
				$obj.children('a.star').hover(
					function(n){
						if(!Stars.locked(id+i)){
							var val = $(this).attr('href').split('#star-')[1];
							$star_val.css('width', Stars.get_width(id+i, val));
						}
						    }).live('click',function(c){
						if(!Stars.locked(id+i)){
							var val = $(this).attr('href').replace('#star-','');
							$input.val(val);
							Stars.vote(id+i);
						}
						return false;
					});
			}
		});
	},
	
	voted : function(id){ // if the user has voted with this star set.
		return $('#'+id).hasClass('voted');
	},
	
	vote : function(id){ // cast the vote.
		var $obj = $('#'+id);
		$obj.addClass('voted');
		if(Stars.single_voting){
			$obj.addClass('locked');
		}
	},
	
	locked : function(id){ // if user has voted and voting is now locked.
	//return $('#'+id).hasClass('locked');
	return false;
	},
	
	get_width : function(id, val){ // method used for setting width of star value block.
		var alt = ($('#'+id+' input[type="hidden"]').length > 0) ? $('#'+id+' input[type="hidden"]').val() : 0;
		var value = (val) ? parseInt(val) : alt;
		
		//return Math.round((valud / Stars.amx)) * '85') + 'px';
		return Math.round((value / Stars.max) * Stars.container_width) + 'px';
    },
	spec_init: function(){
    }
	
}
