$(document).ready(function(){

	(function() {
		var e = document.createElement('script');
		e.type = 'text/javascript';
		e.src = document.location.protocol + '//connect.facebook.net/pl_PL/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());


	$("#showVotes").click(function() 
	{
		showVotes();
		return false;
	});

	function showVotes()
	{
		$.ajax({ 
			type: "POST",
			url: "/showVotes",
			data: "id=1",
			dataType: "json",			
			beforeSend: function( json )
			{ 
				jQuery.blockUI({  
					message: '<h4>Proszę czekać...</h4>', 		   
					fadeIn: 400, 
					fadeOut: 400, 
					showOverlay: false, 
					centerY: false, 
					css: { 
						width: '150px',
						top: '10px', 
						left: '', 
						right: '10px', 
						border: 'none', 
						padding: '5px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '5px', 
						'-moz-border-radius': '5px', 
						opacity: '.6', 
						color: '#fff',
						cursor: 'wait'		
					} 
				});
			}, 

			success: function( json )
			{ 
				setTimeout( jQuery.unblockUI, 5 );
				$("#actio").css({ 'display': 'none' });
				$("#candidates").html( json.html );

				setTimeout(function() { nextStepShowVotes(json); }, 1000);
			}
		});
	}

	function nextStepShowVotes( json )
	{
		$("#votesBar1").attr('title',json.p1+'%');
		$("#votesBar1 span").css({ 'width': json.p1+'%' });
		$("#votesBar1").css({ 'display': 'block' });
		$("#votesBar1 span").html( '<strong>'+json.p1+'%</strong>' );
		$("#votesCount1").html( '<strong>'+json.votes1+'</strong> '+variant( "głosów", "głos", "głosy", json.votes1));
		
		$("#votesBar2").attr('title',json.p2+'%');
		$("#votesBar2 span").css({ 'width': json.p2+'%' });
		$("#votesBar2").css({ 'display': 'block' });
		$("#votesBar2 span").html( '<strong>'+json.p2+'%</strong>' );
		$("#votesCount2").html( '<strong>'+json.votes2+'</strong> '+variant( "głosów", "głos", "głosy", json.votes2));
		
		$("#votesBar3").attr('title',json.p3+'%');
		$("#votesBar3 span").css({ 'width': json.p3+'%' });
		$("#votesBar3").css({ 'display': 'block' });
		$("#votesBar3 span").html( '<strong>'+json.p3+'%</strong>' );
		$("#votesCount3").html( '<strong>'+json.votes3+'</strong> '+variant( "głosów", "głos", "głosy", json.votes3));
		
		$("#votesBar4").attr('title',json.p4+'%');
		$("#votesBar4 span").css({ 'width': json.p4+'%' });
		$("#votesBar4").css({ 'display': 'block' });
		$("#votesBar4 span").html( '<strong>'+json.p4+'%</strong>' );
		$("#votesCount4").html( '<strong>'+json.votes4+'</strong> '+variant( "głosów", "głos", "głosy", json.votes4));
		
		$("#votesBar5").attr('title',json.p5+'%');
		$("#votesBar5 span").css({ 'width': json.p5+'%' });
		$("#votesBar5").css({ 'display': 'block' });
		$("#votesBar5 span").html( '<strong>'+json.p5+'%</strong>' );
		$("#votesCount5").html( '<strong>'+json.votes5+'</strong> '+variant( "głosów", "głos", "głosy", json.votes5));
		
		$("#votesBar6").attr('title',json.p6+'%');
		$("#votesBar6 span").css({ 'width': json.p6+'%' });
		$("#votesBar6").css({ 'display': 'block' });
		$("#votesBar6 span").html( '<strong>'+json.p6+'%</strong>' );
		$("#votesCount6").html( '<strong>'+json.votes6+'</strong> '+variant( "głosów", "głos", "głosy", json.votes6));		
	}
		

	$(".abuse").click(function() 
	{
		var t = $(this);
		var idComment = t.attr('rel');
		
		
		$.ajax({ 
			type: "POST",
			url: "/addApplication",
			data: "idComment="+idComment,
			dataType: "json",			
			beforeSend: function( json )
			{ 
				jQuery.blockUI({  
					message: '<h4>Proszę czekać...</h4>', 		   
					fadeIn: 400, 
					fadeOut: 400, 
					showOverlay: false, 
					centerY: false, 
					css: { 
						width: '150px',
						top: '10px', 
						left: '', 
						right: '10px', 
						border: 'none', 
						padding: '5px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '5px', 
						'-moz-border-radius': '5px', 
						opacity: '.6', 
						color: '#fff',
						cursor: 'wait'		
					} 
				});
			}, 

			success: function( json )
			{ 
				$('#facebookPopup .header').html('Informacja');
				$('#facebookPopup p').html('&nbsp;');
				$('#facebookPopup p').first().html('Zgłoszenie zostało przyjęte.');

				jQuery.blockUI(
				{
					message: $('#facebookPopup'), 
					css: { top: '-150px', left: '-9999px', width:'450px', height:'206px', cursor: 'pointer' },
					overlayCSS:  {	cursor:	'default' }
				});
				
				setTimeout( jQuery.unblockUI, 3000 );
			}
		});
		return false;
	});

	$(".delete").click(function() 
	{
		var t = $(this);
		var idComment = t.attr('rel');
		
		$.ajax({ 
			type: "POST",
			url: "/delete",
			data: "idComment="+idComment,
			dataType: "json",			
			beforeSend: function( json )
			{ 
				jQuery.blockUI({  
					message: '<h4>Proszę czekać...</h4>', 		   
					fadeIn: 400, 
					fadeOut: 400, 
					showOverlay: false, 
					centerY: false, 
					css: { 
						width: '150px',
						top: '10px', 
						left: '', 
						right: '10px', 
						border: 'none', 
						padding: '5px', 
						backgroundColor: '#000', 
						'-webkit-border-radius': '5px', 
						'-moz-border-radius': '5px', 
						opacity: '.6', 
						color: '#fff',
						cursor: 'wait'		
					} 
				});
			}, 

			success: function( json )
			{ 
				setTimeout( jQuery.unblockUI, 5 );
				t.parent().hide();
			}
		});
		return false;
	});
	
	$(".vote").click(function() 
	{
		var t = $(this);
		var idCandidate = t.attr('rel');

		jQuery.blockUI(
		{
			message: $('#vote2'), 
			css: { top: '-150px', left: '-9999px', width:'450px', height:'206px', cursor: 'pointer' },
			overlayCSS:  {	cursor:	'default' }
		});
		
		$(".vote2").attr('rel',idCandidate);
		$('.vote').css({ 'height': '21px', 'margin': '7px 0 0 110px', 'background': 'url("/www/images/voteCheckbox.png") no-repeat scroll 0 -63px transparent' });
		t.css({ 'height': '55px', 'margin': '-27px 0 0 110px', 'background': 'url("/www/images/voteCheckbox.png") no-repeat scroll 0 1px transparent' });
	});

	$(".vote2").click(function() 
	{
		var t = $(this);
		
		if( $('#age').val() == 0 )
		{
			$('.required').css({ 'text-decoration': 'blink', 'font-weight': 'bold', 'color': 'red' });
/*			
			$('#facebookPopup .header').html('Informacja');
			$('#facebookPopup p').html('&nbsp;');
			$('#facebookPopup p').first().html('<span style="color:#FF0000">Musisz wybrać swój wiek.</span>');

			jQuery.blockUI(
			{
				message: $('#facebookPopup'), 
				css: { top: '-150px', left: '-9999px', width:'450px', height:'206px', cursor: 'pointer' },
				overlayCSS:  {	cursor:	'default' }
			});
			
			setTimeout( jQuery.unblockUI, 3000 );	
*/
		}
		else
		{
			var idCandidate = t.attr('rel');
			var age = $('#age').val();
			
			$.ajax({ 
				type: "POST",
				url: "/addVote",
				data: "idCandidate="+idCandidate+"&age="+age,
				dataType: "json",			
				beforeSend: function( json )
				{ 
					jQuery.blockUI({  
						message: '<h4>Proszę czekać...</h4>', 		   
						fadeIn: 400, 
						fadeOut: 400, 
						showOverlay: false, 
						centerY: false, 
						css: { 
							width: '150px',
							top: '10px', 
							left: '', 
							right: '10px', 
							border: 'none', 
							padding: '5px', 
							backgroundColor: '#000', 
							'-webkit-border-radius': '5px', 
							'-moz-border-radius': '5px', 
							opacity: '.6', 
							color: '#fff',
							cursor: 'wait'		
						} 
					});
				}, 
	
				success: function( json )
				{ 
					if( json.error == 1 ) 
					{
						$('#facebookPopup .header').html('Informacja');
						$('#facebookPopup p').html('&nbsp;');
						$('#facebookPopup p').first().html(json.msg);
		
						jQuery.blockUI(
						{
							message: $('#facebookPopup'), 
							css: { top: '-150px', left: '-9999px', width:'450px', height:'206px', cursor: 'pointer' },
							overlayCSS:  {	cursor:	'default' }
						});
						
						setTimeout( jQuery.unblockUI, 3000 );

						setTimeout( showVotes, 3000 );
					}
					else
					{
						showVotes();
					}
				}
			});
		}
		return false;
	});
	
	$("#vote2Close").click(function() 
	{
		setTimeout( jQuery.unblockUI, 5 );
	});
	
	if ( $('#voted').length > 0 )
	{
		showVotes();
	}
	if ( $('#logged').length > 0 )
	{
		setTimeout(function() { 
			$('.fbCommentBtn').hide();
			$('#comment').show("slide"); 
		}, 1000);
	}
});

function facebookLoggedIn()
{
	//facebookBlockUI();
	FB.getLoginStatus(function(response) {
		if (response.session) {
			 location.href = 'facebookConnected';
		} else {

			$('#facebookPopup .header').html('Informacja');
			$('#facebookPopup p').html('&nbsp;');
			$('#facebookPopup p').first().html('Jak chcesz komentować musisz się zalogować.');

			jQuery.blockUI(
			{
				message: $('#facebookPopup'), 
				css: { top: '-150px', left: '-9999px', width:'450px', height:'206px', cursor: 'pointer' },
				overlayCSS:  {	cursor:	'default' }
			});
			
			setTimeout( jQuery.unblockUI, 3000 );
		}
	});
	return false;
}

function facebookPopup() 
{
	$('#facebookPopup .header').html('Facebook Connect');
	$('#facebookPopup p').first().html('Trwa nawiązywanie połęczenia z Facebook Connect...');
	$('#facebookPopup p').last().html('Prosimy o cierpliwość, operacja może potrwać kilkanaście sekund.');
		
	jQuery.blockUI(
	{
		message: $('#facebookPopup'), 
		css: { top: '-103px', left: '-9999px', width:'0px', height:'0px', cursor: 'default' },
		overlayCSS:  {	cursor:	'default' }
	}); 
}

window.fbAsyncInit = function() {
	FB.init({appId: '133441000004363', status: true, cookie: true, xfbml: true})
	
	FB.getLoginStatus(function(response) {
		if (response.session) {
			FB.api(
			{
				method: 'fql.query',
				query: 'SELECT name, pic_square, url FROM profile WHERE id=' + FB.getSession().uid
			},
			function(response) {
				var user = response[0];
				$('#fbName').html(user.name).show('fast');
				$('#fbName').attr('href',user.url);
				$('#fbAvatar').attr("src",user.pic_square).show('fast');
			});


		}
	});	



};

function variant( word1, word2, word3, number)
{
	result = word1;
 	if(number > 1000)
	{
		result = word1;
	} 
	else
	{
		if( number == 1 && number < 20)
		result = word2;
		if( number > 1 && number < 5 )
		result = word3;
		if(( number > 4 && number < 20) || number == 0)
		result = word1;
		if( number > 20 )
		{
			r = number;
			r = r % 10;
			if( r == 2 || r == 3 || r == 4) { result = word3; }		
			r = number;
			r = r % 100;
			if( r > 10 ) { result = word1; }
		}
	}
	return result; 
}
