$(document).ready(function() {
	/*
	$('.search_result_div').hover(function() {
	  $(this).addClass('search_result_hover');
	  //$(this).$("p").addClass('search_result_head_hover');
	}, function() {
	  $(this).removeClass('search_result_hover');
	});

	*/
	
/*	$('.hover_img_container').bind('click', function() {
		if(typeof(loc)=='undefined')
			loc='/';
		var url=loc+'ajax_functions/log_action.php';
		var company=$('a',this).attr('href');
		
		$.get(url, {action: 'shop_clicked', parameters:company});
		});*/
	
	$("#no-pf tr:even").addClass("trbg");
	
	var container_position=$('#container').position();
	//for Chrome & Safari
	var leftPos_Chrome = parseFloat($('#container').css('left')); //alert(isNaN(leftPos_Chrome)+' '+leftPos_Chrome+' '+container_position.left+' '+$('#container').css('left'))
	$('#rate_layer').hide();
	var max_comment_length=30;
	$('.hover_img_container').mousemove( 	
			function(e) {
				var pos_x=e.pageX-(container_position.left ? container_position.left : leftPos_Chrome) + 30;
				var pos_y=null;
				//flip the layer up/bottom depending from the distance until wthhe window bottom
				var doc_height=typeof(window.innerHeight)=='number' ? window.innerHeight : document.documentElement.clientHeight;
				var scrolled_top=typeof(window.pageYOffset)=='number' ? window.pageYOffset : document.documentElement.scrollTop;
				var diff=doc_height-e.pageY + scrolled_top; 
				var rate_layer_height=parseInt($('#rate_layer').css('height'));
				var flip={top:270, bottom:20}
				if(diff < rate_layer_height) {
					//flip to top
					pos_y=e.pageY-container_position.top - flip.top;					
				} else {
					//flip to bottom
					pos_y=e.pageY-container_position.top - flip.bottom;					
				}
				$('#rate_layer').css({position:'absolute',top: pos_y,left:pos_x});
				//fill with content of the selected shop
				if($('#rated_company_name').html()) //if done, return
					return;				
				//1. description
				var shop_id=this.id; 
				var selected_shop_desc=$('#company_desc_'+shop_id).text();
				//remove ',' and break
				selected_shop_desc=selected_shop_desc.replace(',','<br/>');
				$('#rated_company_name').html(selected_shop_desc);
				//2. first 5 ratings: find the hidden table and copy the content (trs)
				var selected_shop_ratings=$('#rating_table_'+shop_id);
				var trs=$('tr',selected_shop_ratings);
				if(trs.length<1) {
					return;
				}
				$('#rate_layer').show();//show only from this point, since is known that the data exists
				for(var i=1; i<=5; i++) {//begin with 1, skip th
					//leave only first max_comment_length symbols
					var new_tr=$(trs[i]).clone();
					var txt=$('.rating_comment',new_tr).text();
					if(txt.length>max_comment_length)
						txt=txt.substring(0, max_comment_length)+'...';
					$('.rating_comment',new_tr).text(txt);
					//jquery fnc doesnt work here for IE
					tr=document.createElement('TR');
					document.getElementById('single_ratings_table').appendChild(tr);
					$(tr).html($(new_tr).html());
				}; 
				//3. total table: find the hidden table and copy content
				var selected_shop_totals=$('#rating_table_total_'+shop_id);
				var trs=$('tr',selected_shop_totals);
				$(trs).each(function(){
					var tr_content=$(this).html();
					tr=document.createElement('TR');
					document.getElementById('total_ratings_table').appendChild(tr);
					$(tr).html(tr_content);
				});
				
			}
	);
	$('.rating_stars_holder').mouseout(	
			function() {
				$('#rate_layer').hide();
				//remove all content
				$('#rated_company_name').html('');
				$('#single_ratings_table').html('');
				$('#total_ratings_table').html('');
			});

});

function show_rating_table(elt,id) {
	var table_id='rating_table_'+id;
	$('#'+table_id).parent().toggle();
	if($('#'+table_id).parent().css('display')=='none') {
		elt.innerHTML='Bewertungen anzeigen';
	} else {
		elt.innerHTML='Bewertungen ausblenden';
	}

}