$(document).ready(function() {
	
	var all_daquiris = $('ul.ratingList li');
	var all_daquiri_links = $('ul.ratingList li a');
	//var blog_daquiris = $('div.post ul.ratingList li');
	//var guide_daquiris = $('div.guide ul.ratingList li');
	
	initDaquiris(all_daquiris);
	initDaquiriLinks(all_daquiri_links);
	
	function initDaquiris(oDaqGroup){
		oDaqGroup.each(function(a){
			$(this).hover(
			
				function(){
					var item_class = $(this).attr('class');
					var item_number = item_class.substr(item_class.length-1,1);
					$(this).find("a").attr('class','on');
					
					if (item_number > 1) {
						var previous_element = $(this).next();
						for (i=item_number; i >= 1; i--){
							previous_element.find('a').attr('class','on');
							previous_element = previous_element.next();
						}	
					}	
				},//end mouseover
				
				function(){
					var item_class = $(this).attr('class');
					var item_init_state = $(this).attr('init');
					var item_number = item_class.substr(item_class.length-1,1);
					$(this).find('a').attr('class',item_init_state);
					
					var previous_element = $(this).next();
					
					if (item_number > 1) {
						for (i=item_number; i >= 1; i--){
							previous_element.find('a').attr('class',previous_element.attr('init'));
							previous_element = previous_element.next();
						}	
					}
				}// end mouseout
				
			)//end hover	
		});//end all daquiris selector
	}

	
	function initDaquiriLinks(oDaqGroup){
		oDaqGroup.each(function(b){
			var post_id = $(this).parent().parent().attr('postid');
			var post_only_links = $(this).parent().parent().find('a');
			var post_only_li = $(this).parent().parent().find('li');
			var item_type = $(this).parent().parent().parent().parent().parent().parent().attr("class");
			var rating_fuseaction = "";
			
			if (item_type == "post"){
				rating_fuseaction = "ajaxratings.ratePost";
			}
			else if (item_type == "guide") {
				rating_fuseaction = "ajaxratings.rateGuide";
			}
			
			$(this).click(
			
				function(){
					
					// get what the vote was
					var user_vote = $(this).attr('voteval');
					
					// send to ajax
					$.get("index.cfm", { fuseaction: rating_fuseaction, postid: post_id, vote: user_vote },
						function(data){
							var new_rating = jQuery.trim(data);
							post_only_links.each(function(e){
								var rating_item_diff = ($(this).attr('voteval') - new_rating);
								if (new_rating >= $(this).attr('voteval')) {
									$(this).attr('class','on');
								}
								else if ((rating_item_diff > 0) && (rating_item_diff < 1)) {
									$(this).attr('class','half');
								}
								else {
									$(this).attr('class','off');
								}
							});
						}
					);
				
					// remove hover actions
					post_only_li.each(function(c){
						$(this).unbind('mouseenter');
						$(this).unbind('mouseleave');
					});
					
					// remove click actions
					post_only_links.each(function(d){
						$(this).unbind('click');
						$(this).bind('click', killLink);
						$(this).css('cursor','default');
					});
					
					// disable clickthrough
					return false;
				}
			)
		}); // all_daquiri_links
	}
	
	function killLink(){return false;}
	
	
}); // end document ready
