// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Object.extend(Starboxes.options, {
    buttons: 10,              // amount of clickable areas   
    ghosting: true,           // ghosts the previous vote
    rerate: true,             // allow rerating
    max: 10,                  // the maximum rating of the starbox
    overlay: 'small.png',     // default star overlay image
    stars: 10,                // the amount of stars
  }
);

function createAjaxStarbox(id, average, url, total, rated)
{			
	return new Starbox(id, average, {
		
	    rated: rated,             // or a rating to indicate a vote has been cast	    	    
	    total: total,             // amount of votes cast
	    
	    indicator: '<strong>Rating: #{average}</strong> <span>(#{total} votes)</span>', // use an indicator, default false	    
	    
	    onRate: function(element, memo) {								
	        var indicator = element.down('.indicator');        
		 	if (id = element.id.match(/\d+/).shift()) {		 		 
		 		  new Ajax.Request(url, 
		 	            { 
		 	              method: 'get',
		 	              parameters: 'id=' + id + '&vote=' + memo.rated.toFixed(1),
		 	              
		 	              onCreate:  function(t) {	 							 						 
		 			      		indicator.update('Loading...').appear().highlight();		 			            
		 					},
		 					
		 	              onSuccess: function(t) {	 						
		 						if (parseInt(t.responseText) >= 0) {
		 							window.setTimeout( function() { 								 									 							
			 							indicator.update('You rated ' + memo.rated.toFixed(1) + '. Thanks for voting.');	 							
			 							window.setTimeout( function() {
											indicator.update('If you want to change your score, please vote again.');
											window.setTimeout( function() {
												indicator.update('<strong>Rating: ' + memo.average.toFixed(1) + '</strong> <span>(' + memo.total + ' votes)</span>');
											}, 4000 );											
										}, 2000 );
		 						    }, 500);
		 						} else {
		 							indicator.update('!!! (voting failed: 3) !!!').highlight();
		 						}
		 					},
		 					
		 	              onFailure: function(t) { 
		 						indicator.update('!!! (voting failed: 2) !!!').highlight(); 
		 					},
		 	             }
		 		)
		    } else {
		    	element.down('.indicator').update('!!! (voting failed: 1) !!!').highlight();
		    }	   			     	    		
		}                                              
	  }
	);			
}







