$(document).ready(function() {
	
	$('#adsense_left .inner').mouseover(function(){if(!$('#adsense_left').attr('anim') || $('#adsense_left').attr('anim')==0) $('#adsense_left').attr('anim', '0').animate({left : '0px'}, 1000, function(){$('#adsense_left').attr('anim', '1')})});
	$('#adsense_left .inner').mouseout(function(){if($('#adsense_left').attr('anim')==1) $('#adsense_left').animate({left : '-200px'}, 1000, function(){$('#adsense_left').attr('anim', '0')})});
	
	
	$('.image a img').after('<span id="lens_p"></span>');
	$('#image_full img').after('<a id="lens_m"></span>');
	$('#lens_m').attr('href', $('#image_full_back').attr('href'));
	
	$('.image a').first().mouseover(function(){$('.image .pin').css('visibility', 'visible')});
	$('.image a').first().mouseout(function(){$('.image .pin').css('visibility', 'hidden')});
	
	$('.image .pin span').mouseover(function(){$('.image .pin').css('visibility', 'visible');$(this).css('background-image', 'url(/img/pinbg.png)')});
	$('.image .pin span').mouseout(function(){$(this).css('background-image', 'url(/img/pinbg1.png)')});
	
	$('textarea,input').focus(function(){$(this).css('border', '2px solid #BBDAFD')});
	$('textarea,input').blur(function(){$(this).css('border', '1px solid #666')});
	
	
	$('.dk_off textarea').html('Napisz komentarz do tego zdjęcia...').focus(function(){$('.dk_off').removeClass('dk_off');$(this).empty()});
	$('.dk_off a').click(function(){$('.dk_div').addClass('dk_off');$('.dk_div .error').remove();return false;});
	
	$('.dk_div button').after('<input type="hidden" name="type" value="ajax" />')
	.click(function(){
		var form = $($(this).attr('form'));
		
		$.post(form.attr('action'), form.serialize(),
			function(data){
				$('.dk_div button').attr('disabled', '');
				$('.dk_div .error').remove();
				
				if (data.error) {
					$(".dk_div textarea").val(data.error.komentarz.val);
					$(".dk_div input[name='imie']").val(data.error.imie.val);
					$(".dk_div input[name='dzien']").val('');
					
					
					if (data.error.komentarz.err) {
						$(".dk_div textarea").after('<div class="error">'+data.error.komentarz.err+'</div>');
					}
					if (data.error.imie.err) {
						$(".dk_div input[name='imie']").after('<div class="error">'+data.error.imie.err+'</div>');
					}
					if (data.error.dzien && data.error.dzien.err) {
						$(".dk_div input[name='dzien']").after('<div class="error">'+data.error.dzien.err+'</div>');
					}
				} else if (data.fail) {
					$(".dk_div td").last().before('<div class="error">Dodawanie komentarza nie powiodło się. Odśwież stronę i spróbuj ponownie.</div>');
				} else if (data.ok) {
					$(".dk_div textarea").val('');
					$(".dk_div input[name='imie']").val('');
					$(".dk_div input[name='dzien']").val('');
					
					$('.dk_div').addClass('dk_off');
					
					$('#komentarze + i.stat').remove();
					
					$('#komentarze').before('<div class="comm_thx">Komentarz został dodany. Dziękujemy!</div>').prev().delay(5000).fadeOut(1000);
					$('#komentarze').after('<div class="comm_f"><div class="cont"><a href="'+site_urli+'komentarze/uzytkownik/'+data.ok.imie_url+'.html">'+data.ok.imie+'</a> '+data.ok.tresc+' <span class="stat">przed chwilą</span></div></div>')
					
				}
				
				
				$(".dk_div input[name='hc_token']").val(data.captcha);
			}, 'json'
		);
		$(this).attr('disabled', 'disabled').blur();
		
		return false;
	});
	
	
	
	
	
	
	$('#image_full').css('overflow', 'hidden').width('950px').height('500px');
	
	
	$("#image_full img").one("load",function(){
		$('#image_full').width(fs_width(950)).height(fs_height(500))
		.append('<a href="#" id="image_full_screen">Pełny ekran</a>')
		.before('<div id="image_full_info">Kliknij i przeciągnij zdjęcie, aby je przesunąć.</div>');
	
		$('#image_full_info').width(fs_width(950));
		
		$('#image_full_screen').click(function(){
			if ($(this).data('tryb') == 1) {
				$('#image_full_info').after($('#image_full'));
				$('#image_full').css('margin', '2px 0 10px 0').width(fs_width(950)).height(fs_height(500));
				
				$('#image_full_screen_div').remove();
				
				$(this).data('tryb', 2);
				$(this).html('Pełny ekran');
				$('#sugester_widget').show();
			} else {
				$('body').append('<div id="image_full_screen_div"></div>');
				$('#image_full_screen_div').append($('#image_full'));
				$('#image_full').css('margin', '15px auto 0 auto').width(fs_width($(window).width() - 35)).height(fs_height($(window).height() - 35));
				
				if (parseInt($('#image_full img').css('left')) < -$('#image_full img').width() + parseInt($('#image_full').css('width'))) {
					$('#image_full img').css('left', (-$('#image_full img').width() + parseInt($('#image_full').css('width')) + 'px'))
				}
				
				if (parseInt($('#image_full img').css('top')) < -$('#image_full img').height() + parseInt($('#image_full').css('height'))) {
					$('#image_full img').css('top', (-$('#image_full img').height() + parseInt($('#image_full').css('height')) + 'px'))
				}
				
				$(this).data('tryb', 1);
				$(this).html('Zmniejsz');
				$('#sugester_widget').hide();
			}
			
			return false;
		});
	})
	.each(function(){
	if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
	$(this).trigger("load");
	}); 
	
	
	
	
	
	$('#image_full img').mousedown(function(e){
		$(this).data('s_move', 1);
		$(this).data('s_move_x', e.pageX);
		$(this).data('s_move_y', e.pageY);
		
		$(this).data('s_move_top', parseInt($('#image_full img').css('top')));
		$(this).data('s_move_left', parseInt($('#image_full img').css('left')));
		
		return false;
	}).mouseup(function(){
		$(this).data('s_move', 2);
	}).mousemove(function(e){
		if ($(this).data('s_move') == 1) {
			var top = (parseInt($(this).data('s_move_top')) + e.pageY - $(this).data('s_move_y'));
			var left = (parseInt($(this).data('s_move_left')) + e.pageX - $(this).data('s_move_x'));
			
			if (top <= 0 &&  top >= -$(this).height() + parseInt($('#image_full').css('height'))) {
				$(this).css('top', top + 'px');
			}
			
			if (left <= 0 && left >= -$(this).width() + parseInt($('#image_full').css('width'))) {
				$(this).css('left', left + 'px');
			}
			
			return false;
		} 
	});
	
	
	$('#image_relation a').first().append('<span class="np">następne</span>').next().append('<span class="np">poprzednie</span>');
	
});

function fs_width(d) {
	return $('#image_full img').width()<d?$('#image_full img').width()+'px':d+'px';
}

function fs_height(d) {
	return $('#image_full img').height()<d?$('#image_full img').height()+'px':d+'px';
}

