jQuery(function($) {
	
	var rating = 0;
	
	// Reassign class on non-html links
	// $('a.non-html').each(function(index) {
// 		this.title = 'Opens in a new window: ' + this.href;
// 		//$(this).removeClass('non-html').addClass('newWin');
// 		$(this).click( openInNewWindow(this) );
// 	});
	
	// Capture favorites click
	$('li.fave a').click(function(event) {
		var id 	= this.id.substr(1);
		var target 	= $('#tool-save'+id);
		var spinner	= $('#tool-save-loading'+id);
		
		spinner.show();
		target.hide().load(this.href, function(responseText, status, response) {
			spinner.hide();
			target.empty().append('Saved').show();
		});
		
		event.preventDefault();
	});
	
	// Capture rating mouseover
	$('a.star').mouseover(function(event) {
		rate(event, this);
	});
	
	// Capture rating mouseout
	$('a.star').mouseout(function(event) {
		rate(event, this);
	});
	
	// Capture rating star click and kill it
	$('a.star').click(function(event) {
		rating = $('img', this).attr('alt');
		$('input[@name=rating]').val(rating);
		
		event.preventDefault();
		return false;
	});
	
	// Capture rating submit button click
	$('a.r_submit').click(function(event) {
		if (rating != 0) {
			$('input[@name=rating]', this.parent).val(rating);

			$(this).parent('form').submit();

			rating = 0;
		} else {
			alert('You must click on the stars before you can save the rating.');
		}
		
		event.preventDefault();
		return false;
	});
	
	function rate(event, el) {
		var id 		= (event.type == 'mouseout') ? rating : $('img', el).attr('alt');
		var stars 	= $(el).siblings().add(el);
		
		$.each(stars, function(i, n) {
			$('img', n).attr("src","/themes/rating_themes/stars/images/star-0.gif").removeClass('on');
			
			if ($('img', n).attr('alt') <= id) {
				$('img', n).attr("src","/themes/rating_themes/stars/images/star-100.gif").addClass('on');
			}
		});
	}
	
	// function openInNewWindow(link) {
// 		// "newWindow"  loads all links in the same new window... Change to "_blank" otherwise
// 	    var newWindow = window.open(link.href, 'newWindow');
// 	    //newWindow.focus();
// 	    return false;
// 	}
	
});

