function popupWindow(url) {
  //popup_window = open(url, 'facebook', 'resizable=no,width=600,height=400,left=' + (screen.width-600)/2 + ',top=' + (screen.height-480)/2);
  popup_window = open(url, 'facebook', 'resizable=no,width=520,height=360,left=' + (screen.width-520)/2 + ',top=' + (screen.height-380)/2);
}

function readCookie(name) {
	var cookies = document.cookie.split('; ');

	for(i in cookies) {
		var cookie = cookies[i].split('=', 2);

		if(cookie[0] == name) return cookie[1];
	}
}

$(document).ready(function() {
  $('.facebook').click(function() {
  	popupWindow($(this).attr('href') + '?display=popup');

    return false;
  });

  $('.vote').click(function() {
  	var vote = $(this);

		allow = function() {
			vote.addClass('ajax');

			$.post('/ajax.php', {vote: vote.attr('href').substr(1), article: document.location.pathname.match(/[0-9]+$/)}, function(rank) {
				vote.removeClass('ajax');

				if(rank) {
					$('#rank img').fadeOut('normal', function() {
						$(this).attr('src', $(this).attr('src').replace(/[^/]+$/, rank + '.gif'));	
						$(this).fadeIn('normal');
					});
				}
			});
		}

		cancel = function() {
			alert('Hodnotit článek mohou pouze přihlášení uživatelé');
		}

  	if(readCookie('user') == undefined) {
  		$('.facebook').click();
		}
		else allow();

    return false;
  });

  $('.comment input[type="submit"]').click(function() {
		allow = function() {
			$('#side form').submit();
		}

		cancel = function() {
			alert('Komentovat článek mohou pouze přihlášení uživatelé');
		}

  	if(readCookie('user') == undefined) {
  		$('.facebook').click();
		}
		else allow();

    return false;
  });
});
