$(document).ready(function() {
	
	$('input.innerLabel, textarea.innerLabel').each(function() {
		$(this).attr('rel', $(this).val()).addClass('empty');
	}).focus(function() {
		if ($(this).val() == $(this).attr('rel'))
			$(this).val('').removeClass('empty');
	}).blur(function() {
		if ($(this).val() == '')
			$(this).val($(this).attr('rel')).addClass('empty');
	});
	
	$('#articleTopics a, div.topicList a, #selectTopic li a').append('<span></span>');

	if ($('#articleComments').size() > 0) {
		if (window.location.hash.toLowerCase().indexOf('comment') < 0) {
			$('#articleComments').removeClass('visible').addClass('hidden');
			$('#commentsHolder').hide();
			$('#articleComments div.comment').append('<div class="arrow"></div>');
			$('#loginToComment.hidden').hide();
			$('#articleComments').click(function() {
				if ($(this).hasClass('hidden')) {
					$('#commentsHolder').slideDown(300, function() { $.scrollTo($('#articleComments'), 500); });
					$(this).removeClass('hidden').addClass('visible');
					$('#articleComments > h2').text('Komentarze:');
					$('#loginToComment').delay(300).fadeIn(300);
				}
			});
			var str = $('#articleComments > h2').html();
			str = str.replace(':', '');
			$('#articleComments > h2').html(str);
		}
	}
	
	$('#newsBoxIn').scrollable({
		vertical: true,
		speed: 400
	}).navigator({
		indexed: true
	});
	
	$('article h1.right').parent().next('p').css('margin-top', '10px');
	
	$('a.emailAddress').each(function() {
		var adres = $(this).text();
		adres = adres.replace('()', '@');
		adres = adres.replace(/--/g, '.');
		if ($(this).attr('href')) {
			$(this).attr('href','mailto:' + adres);
		}
		$(this).text(adres);
	});
	
	$('#overlay.hidden').css('top', '-' + ($('#overlay').height() + 40) + 'px');
	
	$('a[rel=login]').click(function() {
		$('#overlay').show().animate({
			top: '-10px'
		}, 300);
		$('#overlay input[type=text]:first').focus();
		return false;
	});

	$('#overlayClose').click(function() {
		$('#overlay').animate({
			top: '-' + ($('#overlay').height() + 40) + 'px'
		}, 300);
		return false;
	});
	
	$('a.external').attr('target', '_blank');
	
	$('#logo').after('<span id="markNet"></span>');
	
	$('#loginBox').submit(function() {
		$.post("/logowanie_ajax.php", {
			login_user: $('#login_user').val(),
			login_haslo: $('#login_haslo').val()
		}, function(data) {
			if (data != '11') {
				if (data == '00' || data == '01') {
					errorInfo = 'Podana nazwa użytkownika jest nieprawidłowa';
					$('#login_user').focus();
				} else if (data == '10') {
					errorInfo = 'Podane hasło jest nieprawidłowe';
					$('#login_haslo').focus();
				}
				$('#loginBox p.errorInfo').text(errorInfo).slideDown('200');
			} else {
				$('#loginBox p.errorInfo').text('').slideUp('200');
				$('#overlayClose').click();
				$('#loginToComment').hide();
				$('#commentArticle').slideDown(200).find('textarea').focus();
				$('#userLogout').show();
				$.scrollTo({ top: '+=300px', left: '0' }, 700);
				$.get('/ajax_get_session_info.php', function(data) {
					var dl = data.length;
					var username = data.substr(1, (dl-1));
					$('#commentArticle h2 strong').text(username);
					if (data.substr(0, 1) == 1) {
						$('#commentArticle span.genderC').text('y');
					}
				});
				$.get('/ajax_get_session_userid.php', function(data) {
					$('#kom-user_id').val(data);
				});
			}
		});
		return false;
	});
	
	if ($.browser.msie && parseInt($.browser.version) < 9) {
		$('#imgCopyright').hide();
	}
	
	$('#facebookBox').hover(function() {
		$(this).stop().animate({ left: '-1px' }, 250);
	}, function() {
		$(this).stop().animate({ left: '-216px' }, 250);
	});
	
});
