jQuery.fn.sexyVote = function(config) {
    config = config || {};
    var defaults = {
        activeImageSrc: "/nick/sexy-vote/active_star.gif",
	passiveImageSrc: "/nick/sexy-vote/passive_star.png",
	maxScore: 5,
	fn: new Function(),
	messages: [
	    "Hai Votato!",
	    "Davvero brutto",
	    "Brutto",
	    "Carino",
	    "Davvero bello",
	    "Bellissimo"
	],
	scores: [
	    "0",
	    "1",
	    "2",
	    "3",
	    "4",
	    "5"
	]
    };


    
    config = jQuery.extend(defaults, config);
    
  
    
    return this.each(function() {
        var $container = jQuery(this);
	
	for (var i = 0, num = config.maxScore * 2; i < num; ++i) {
	    jQuery("<img />").appendTo($container);    
	}
	
	jQuery("<span id='target' />").appendTo($container);
	jQuery("<input name='PublicRatingFake' id='fakeVote' type='hidden' />").appendTo($container);
	
	$container.find("img:even").
	attr("src", config.passiveImageSrc).
	css({display: "inline"}).
	bind("mouseover", function(e) {	    
	    var len = $container.find("img:even").index(e.target) + 1;
	    
	    $container.find("img:even").slice(0, len).css({display: "none"});
	    
	    $container.find("img:odd").slice(0, len).css({display: "inline"});
	    
	    $container.find("span").text(config.messages[len]);
	
		$container.find("input").val(config.scores[len]);
	
	
	    
	    
	}).
	end().
	find("img:odd").
	attr("src", config.activeImageSrc).
	css({display: "none"}).
	bind("mouseout", function(e) {

	    var len = $container.find("img:odd").
	    index(e.target) + 1;

	    $container.find("img:odd")
	    .slice(0, len).
	    css({display: "none"});
	    $container.find("img:even").
	    slice(0,  len).
	    css({display: "inline"});
	    
	    $container.find("span").
	    text("");
	
			$container.find("input").val(0);
	    
	        
	}).
	bind("click", function(e) {
	    $container.find("img").
	    unbind("mouseover").
	    unbind("mouseout").
	    unbind("click");
	    $container.find("span").
	    text(config.messages[0]);
	    config.fn.call(this, e, $container.find("img:odd").index(e.target) + 1);
		$("#voteForm").submit();
		
	});
    });
}; 
