$(document).ready(function(){
	
	$("input[name='suchStadt']").autocomplete("/ajax/cities.php");	
	
	
	
	var old;
	
	$('.rBoxField').mouseover(function(){
		old = $(this).parent().css('background-position');
		var x = -15*$(this).attr('name');
		
		$(this).parent().css('background-image','url(/bilder/rating_stars_green.png)');
		$(this).parent().css('background-position',x+'px 0px');
		$(this).parent().parent().children('.ratingHeadline').html('Ihre Bewertung');
		});
	
	$('.rBoxField').mouseout(function(){
		
		$(this).parent().css('background-image','url(/bilder/rating_stars.png)');
		$(this).parent().css('background-position',old);
		$(this).parent().parent().children('.ratingHeadline').html('Bewertung');
		});
	
	$('.rBoxField').click(function(){
		
		var rateCompany = $(this).parent().parent().attr('id');
		var rateValue   = $(this).attr('name');

		$.getJSON('/ajax/rating.php', { company : rateCompany, rating : rateValue }, function(json){

			if( json.aktion == 'voted' )
			{
				var bgOffset = (5-json.newrating)*-15;
				
				$('#'+json.id).children('.ratingBox').html(' ');
				$('#'+json.id).children('.ratingBox').css('background-image','url(/bilder/rating_stars.png)');
				$('#'+json.id).children('.ratingBox').css('background-position',bgOffset+'px 0px');
				
				$('#'+json.id).children('.ratingHeadline').html('Bewertung');
				$('#'+json.id).children('.ratingSubline').html('&oslash; '+json.newavg+' / '+json.newvotes+' Stimmen<br><b>Danke für Ihre Stimme!</b>');
				//alert('AVG: '+json.newavg+' NEW: '+json.newrating+' VOTES: '+json.newvotes+' OFFSET: '+bgOffset+' OLD: '+json.oldvars);
			}
			else if( json.aktion == 'missing' )
			{
				$('#'+json.id).children('.ratingSubline').html('Es fehlen Paramter zur Stimmabgabe!');
			}
			else if( json.aktion == 'notallowed' )
			{
				$('#'+json.id).children('.ratingSubline').html('Sie haben bereits abgestimmt!');
			}
			else if( json.aktion == 'novote' )
			{
				$('#'+json.id).children('.ratingSubline').html('Ihre Stimme konnte nicht abgegeben werden!');
			}
			else
			{
				$('#'+json.id).children('.ratingSubline').html('Unbekannter Fehler!');
			}
			
			});
		});
	
	});
