/*
    Copyright © 2006, 2007 Positive Friends Inc.
    All Rights Reserved.
*/

// If we have jQuery loaded
if (typeof(jQuery) != 'undefined'){
	
	// Extend jQuery with a new function
	jQuery.fn.rating = function(url, options){
		
		// If we have no URL to send the query too, then quit
		if (url == null) return;
		
		// This is the settings object we will be using
		var settings = {
			
			// This is the URL we will be sending the data too
			url: url,
			
			// This is the maximum number of stars
			maxStars: 5,
			
			// This is the number of selected stars
			curStar: 0,
			
			// This is the id of the item we are rating
			id: 0,
			
			// This is wether or not we want to show a cancel button or not
			cancel: false
		};
		
		// If we have options set, merge them into our settings object
		if (options) jQuery.extend(settings, options);
		
		// Get a reference to the container node
		var containerNode = jQuery(this);
		
		// Add more options to the container object
		jQuery.extend(containerNode, {
			
			// This is the average rating the item has recieved
			averageRating: settings.curValue,
			
			// This is the Url we will be posting too
			url: settings.url
		});
		
		// If we are supposed to make a cancel button
		if (settings.cancel == true){
			
			// Create a new node with the cancel button markup
			var newNode = '<div class="cancel"><a href="#0" title="Cancel Rating">Cancel Rating</a></div>';
			
			// Add the new node to the containerNode
			containerNode.append(newNode);
		}
		
		// Untill we have made all the stars
		for (var i = 1; i <= settings.maxStars; i++){
			
			// Create a new star node from markup
			var newNode = '<div class="star"><a href="#'+i+'" title="Give it '+i+'/'+settings.maxStars+'">'+i+'</a></div>';
			
			// Append the new node to the conainerNode
			containerNode.append(newNode);
		}
		
		// Get all the star objects
		var stars = jQuery(containerNode).children('.star');
		
		
		// Attach the mouseover event
		stars.mouseover(function(){
			
			// Make all the stars empty
			event.drain();
			
			// Fill up to the cursor position
			event.fill(this);
		}).
		
		// Attach the mouseout event
		mouseout(function(){
			
			// Make all the stars empty
			event.drain();
			
			// Reset the fill to its normal position
			event.reset();
		}).
		
		// Attach the focus event
		focus(function(){
			
			// Make all the stars empty
			event.drain();
			
			// Fill up to the focus position
			event.fill(this);
		}).
		
		// Attach the blur event
		blur(function(){
			
			// Make all the stars empty
			event.drain();
			
			// Reset the fill to its normal position
			event.reset();
		});
		
		// Attach the event for when a star gets clicked
		stars.click(function(){
			
			// If the max number of stars is larger than 1
			if (settings.maxStars > 1){
				
				// Set the current value
				settings.curStar = stars.index(this) + 1;
				
				// Make the ajax call to the server
				jQuery.getJSON(url, {id: settings.id, rating: settings.curStar}, function(json){
					
					// Nothing to do here
   				});
   				
   				// We're done
   				return false;
			}
			
			// We're done
			return true;
		});
			
		// If we are cancelling
		if (settings.cancel == true){
			
			// Get a reference to the cancel button
			var cancel = jQuery(container).children('.cancel');
			
			// Attach the mouseover event
			cancel.mouseover(function(){
				
				// Drain the stars
				event.drain();
				
				// Turn the cancel button on
				jQuery(this).addClass('on');
			})
			
			// Attach the mouse out event
			.mouseout(function(){
				
				// Reset the fill
				event.reset();
				
				// Turn the cancel button off
				jQuery(this).removeClass('on');
			})
			
			.focus(function(){
				
				// Drain the stars
				event.drain();
				
				// Turn the cancel button on
				jQuery(this).addClass('on');
			})
			
			.blur(function(){
				
				// Reset the fill
				event.reset();
				
				// Turn the cancel button off
				jQuery(this).removeClass('on');
			});
			
			// When the cancel button is clicked
			cancel.click(function(){
				
				// Drain the stars
				event.drain();
				
				// Set the current star to 0
				settings.curStar = 0;
				
				// Make the JSON request
				$.getJSON(settings.url, {id: settings.id, rating: 0}, function(){
					
					// Nothing to do here
				});
				 
				// We're done here
				return false;
			});
		}
		
		// Build the events object
		var event = {
			
			// Fill the stars up to the mouse position
			fill: function(element){
				
				// Get the index of the element
				var index = stars.index(element) + 1;
				
				// Add a 100% width on all the a tags
				stars.children('a').css('width', '100%').end();
				
				// Add the hover class on all the stars before us
				stars.lt(index).addClass('hover').end();
			},
			
			// This will drain all the stars
			drain: function(){
				
				// Remove all the 'on' classes
				stars.filter('.on').removeClass('on').end();
				
				// Remove all the hover classes
				stars.filter('.hover').removeClass('hover').end();
			},
			
			// Reset the stars to the default
			reset: function(){
				
				// Add the 'on' class on all the stars up to the current star
				stars.lt(settings.curStar).addClass('on').end();
			}
		}
		
		// Reset the stars at the beginning
		event.reset();
		
		// Make sure we don't kill the daisy chain
		return(this);	
	}
}