$(document).ready(function() {

	$('.start_voting li[id^="vote"]').each(function() {		
		$(this).click(function() {					
			var p = $(this).attr('id').match(/^vote(\d+)/gi);		
			if( p != null ) {				
				send_vote(RegExp.$1);				
			}
		});
	});
	

	$('li[id^="vote"]').each(function() {				
		$(this).mouseover(function(e) {
			var height = $('#popupcontent').height();
			var width = $('#popupcontent').width();
			  //calculating offset for displaying popup message
			
			  var leftVal=e.pageX+7;
			  var topVal=e.pageY-80+(height/2)+"px";
			  //show the popup message and hide with fading effect
			  			  				
			if( $(this).attr('id').match(/^vote(\d+)/gi) ) {
				var pp = RegExp.$1;
				//$('#test').html( '='+PP_DATA.data[pp].fullname );
			
			  $('#popupcontent').html( PP_DATA.data[pp].fullname+'<br />'+PP_DATA.data[pp].discipline+', '+PP_DATA.data[pp].club+'<br />'+PP_DATA.data[pp].description );
			  $('#popupcontent').css({left:leftVal,top:topVal}).show();
			}
			  //$('#popupcontent').show();
		});
		
		$(this).mouseout(function(e) {
			$('#popupcontent').hide();
		});
		
	});
	
	
	/* ajax send vote */
	function send_vote(pp_id) {
		$.ajax ({					
			type: "POST",
			url: "glosowanie,vote",
			//url: "index.php/glosowanie/vote",
			data: "pp_id=" + pp_id,			
			dataType: 'html',		
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{				
				$('#left_container').html(XMLHttpRequest.responseText);
				//alert('Bład głosowania, spróbuj jeszcze raz');
				$("#ajax_response").append('Error object :'+XMLHttpRequest.responseText+'<br />Status:'+textStatus+'<br />Error:'+ errorThrown);
				location.reload();
			},
			success: function(data, status)
			{	
				$('#popupcontent').hide();
				$('#voting_box').html(data);
				$('#popupcontent').hide();
				location.reload();
			}			
		}); 		
		return false;
	} // end function


});

