function rozmiescIkony() {
	$('.fotolink>.o_bezposrednia').css({
		left: '63px',
		top: '1px'
	});
	
	$('.fotolink .oferty_ikonki_inner').each(function(i, it) {
		$(it).find('img').each(function(index, item) {
			switch(index) {
				case 0:
					$(this).css({
						top: '1px',
						left: '1px'
					});
					break;
				case 1:
					$(this).css({
						top: '65px',
						left: '1px'
					});
					break;
				case 2:
					if ($(this).parent().parent().parent().find('.o_bezposrednia').length > 0) {
						$(this).css({
							top: '65px',
							left: '102px'
						});
					} else {
						$(this).css({
							top: '0px',
							left: '102px'
						});
					}
					break;
				case 3:
					if ($(this).parent().parent().parent().find('.o_bezposrednia').length == 0) {
						$(this).css({
							top: '65px',
							left: '102px'
						});
					}
					break;
			}
		});
	});
	
	$('.lightbox_link .oferty_ikonki_inner').each(function(i, it) {
		$(it).find('img').each(function(index, item) {
			switch(index) {
				case 0:
					$(this).css({
						top: '15px',
						left: '1px'
					});
					break;
				case 1:
					$(this).css({
						top: '280px',
						left: '1px'
					});
					break;
				case 2:
					if ($(this).parent().parent().parent().find('.o_bezposrednia_duze').length > 0) {
						$(this).css({
							top: '280px',
							left: '537px'
						});
					} else {
						$(this).css({
							top: '15px',
							left: '537px'
						});
					}
					break;
				case 3:
					if ($(this).parent().parent().parent().find('.o_bezposrednia_duze').length == 0) {
						$(this).css({
							top: '280px',
							left: '537px'
						});
					}
					break;
			}
		});
	});
}

$(document).ready(function() {
	var main = '/';
	
	$('a.lightbox_link').lightBox({
		imageLoading: main+'css/images/lightbox-ico-loading.gif',
		imageBtnClose: main+'css/images/lightbox-btn-close.gif',
		imageBtnPrev: main+'css/images/lightbox-btn-prev.gif',
		imageBtnNext: main+'css/images/lightbox-btn-next.gif',
		txtImage: 'Obrazek',
		txtOf: 'z'
	});
	
	$('select[name="typ_nieruchomosci"]').change(function() {
		if ($(this).val() == '0')
			$('#adv_search_btn').hide();
		else $('#adv_search_btn').show();
	});
	
	$('.strona').click(function() {
		/*
		if (!$(this).hasClass('active')) {
			var page = $(this).attr('class').split(' ');
			page = page[page.length-1];
			page = page.substr(1);
			var akcja = $('#typ_oferty').val();
			var row = $('#kategoria').val();
			if (akcja != undefined && row != undefined)
				var url = main+'index.php?module='+$('#stronnicowanie_type').val()+'&ajax=1&akcja=getpage&page='+page+'&akcja_='+akcja+'&row_='+row;
			else
				if (akcja != undefined)
					var url = main+'index.php?module='+$('#stronnicowanie_type').val()+'&ajax=1&akcja=getpage&row='+page+'&klucz='+akcja;
				else
					var url = main+'index.php?module='+$('#stronnicowanie_type').val()+'&ajax=1&akcja=getpage&row='+page;
			$.ajax({
				url: url,
				type: 'post',
				beforeSend: function(xhr, c) {
					$('.ogloszenia .ogloszenie').remove();
					$('.ogloszenia').css('text-align', 'center');
					$('.ogloszenia').append('<img src="images/loading_transparent.gif" alt="Ładowanie" id="loadingimg" style="margin-top: 10px; background: #E8E8CF;" />')
				},
				success: function(response) {
					$('.ogloszenia #loadingimg').remove();
					$('.ogloszenia').css('text-align', 'left');
					$('.ogloszenia').append(response);
					$('.strona.active').removeClass('active');
					$('.s'+page).addClass('active');
					rozmiescIkony();
				}
			});
		}
		*/
	});
	
	$('.sort a').click(function() {
		var sort = $(this).attr('id').split('_');
		sort = sort[1];
		if ($('input#kategoria').length > 0)
			var kategoria = $('input#kategoria').val();
		else
			if($('input#typ_oferty').length > 0)
				var kategoria = $('input#typ_oferty').val();
			else
				var kategoria = null;
		$.ajax({
			url: main+'index.php?module='+$('#stronnicowanie_type').val()+'&ajax=1&akcja=order&row='+sort+'&klucz='+kategoria,
			type: 'post',
			success: function(response) {
				if ($('#stronnicowanie_type').val() == 'wyszukiwarka')
					location.href = location.href;
				else
					location.reload();
			}
		});
	});
	
	$('#adv_search_btn').click(function() {
		var $this = $(this);
		$.ajax({
			url: main+'index.php?module='+$('#stronnicowanie_type').val()+'&ajax=1&akcja=get_adv_search&row='+$('select[name="typ_nieruchomosci"]').val(),
			type: 'post',
			success: function(response) {
				if($('select[name="typ_nieruchomosci"]').val() == 0) {
					$('#is_adv_search').val(0);
					//$this.show();
					$('.adv_search').empty();
					$('input[name="search_go"]').css('margin-left', '20px');
				} else {
					$('#is_adv_search').val(1);
					$('.adv_search').empty().append(response);
					//$this.hide();
					$('input[name="search_go"]').css('margin-left', '20px');
				}
			}
		});
	});
	
	$('.menu li.with_sub a').mouseenter(function() {
		$(this).parent().find('ul').slideDown('fast').show();
		$(this).parent().hover(function() {}, function() {
			$(this).parent().find('ul').slideUp('fast');
		});
	});
	
	$('#arrow_left').click(function() { 
		if (!$('.miniaturki .inner img:eq(0)').hasClass('active')) {
			$('.miniaturki .inner .active')
				.removeClass('active')
				.prev()
				.addClass('active');
			if ($('.miniaturki .inner .active').prev().hasClass('hidden')) {
				$('.miniaturki .inner .active').prev().removeClass('hidden');
				$('.miniaturki .inner img:not(.hidden):last').addClass('hidden');
			}
			$('.miniaturki .inner .active')
				.removeClass('hidden')
				.next()
				.removeClass('hidden');
			if ($('.miniaturki .inner img:not(.hidden)').length > 5) {
				$('.miniaturki .inner img:not(.hidden):last').addClass('hidden');
			}
			$('.miniaturki .inner .active').click();
		}
	});
	$('#arrow_right').click(function() {
		if (!$('.miniaturki .inner img:last').hasClass('active')) {
			$('.miniaturki .inner .active')
				.removeClass('active')
				.next()
				.addClass('active');
			if ($('.miniaturki .inner .active').next().hasClass('hidden')) {
				$('.miniaturki .inner .active').next().removeClass('hidden');
				$('.miniaturki .inner img:not(.hidden):first').addClass('hidden');
			}
			$('.miniaturki .inner .active')
				.removeClass('hidden')
				.prev()
				.removeClass('hidden');
			if ($('.miniaturki .inner img:not(.hidden)').length > 5) {
				$('.miniaturki .inner img:not(.hidden):first').addClass('hidden');
			}
			$('.miniaturki .inner .active').click();
		}
	});
	$('.miniaturki .inner img').click(function() {
		if ($(this).hasClass('active')) {
		} else {
			$('.miniaturki .inner img.active').removeClass('active');
			$(this).addClass('active');
		}
		var num = $(this).attr('id').toString().substr(2);
		$('.oferta_galeria a img:first').addClass('hidden');
		$('.oferta_galeria a img').parent().addClass('hidden');
		$('.oferta_galeria a img#itt'+num).removeClass('hidden');
		$('.oferta_galeria a img#itt'+num).parent().removeClass('hidden');
		$('.oferta_galeria a img#itt'+num).next().removeClass('hidden');
	});
	
	$('.oglo_z .podobne .miniatury a:eq(0)').click(function() { 
		if (!$('.oglo_z .podobne .miniatury img:eq(0)').hasClass('active')) {
			$('.oglo_z .podobne .miniatury .active')
				.removeClass('active')
				.prev()
				.addClass('active');
//			$('.oglo_z .podobne .miniatury img').addClass('hidden');
			if ($('.oglo_z .foto.miniatury .active').prev().hasClass('hidden')) {
				$('.oglo_z .foto.miniatury .active').prev().removeClass('hidden');
				$('.oglo_z .foto.miniatury img:not(.hidden):last').addClass('hidden');
			}
			$('.oglo_z .podobne .miniatury .active')
				.removeClass('hidden')
				.next()
				.removeClass('hidden');
		}
	});
	$('.oglo_z .podobne .miniatury a:eq(1)').click(function() {
		if (!$('.oglo_z .podobne .miniatury img:last').hasClass('active')) {
			$('.oglo_z .podobne .miniatury .active')
				.removeClass('active')
				.next()
				.addClass('active');
			if ($('.oglo_z .foto.miniatury .active').next().hasClass('hidden')) {
				$('.oglo_z .foto.miniatury .active').next().removeClass('hidden');
				$('.oglo_z .foto.miniatury img:not(.hidden):first').addClass('hidden');
			}
//			$('.oglo_z .podobne .miniatury img').addClass('hidden');
			$('.oglo_z .podobne .miniatury .active')
				.removeClass('hidden')
				.prev()
				.removeClass('hidden');
		}
	});
	
	$('#markiza').marquee('markiza')
		.mouseover(function () {
			$(this).trigger('stop');
		})
		.mouseout(function () {
			$(this).trigger('start');
		})
		.mousemove(function (event) {
			if ($(this).data('drag') == true) {
				this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
			}
		});
	$('#polec_link').click(function() {
		$('.polec').toggle();
	});
	$('#cancel_polec').click(function() {
		$('.polec').hide();
	});
	
	$('#drukuj_link').click(function() {
		var myWindow = window.open(
			'/index.php?module=drukujogloszenie&row='+$('input[name="row"]').val()+'&klucz='+$('input[name="klucz"]').val()+'&ajax=1', 
			'myWindow', 
			'width=620,height=600'
		);
	});
	
	$('.print_porownywarka').click(function() {
		var myWindow = window.open(
			'/index.php?module=porownywarka&akcja=drukuj&ajax=1', 
			'myWindow', 
			'width=700,height=600'
		);
	});
	
	if ($('input[name="cena_transakcyjna"]').val() != '' && $('input[name="cena_transakcyjna"]').val() != '0.00') {
		$('input[name="oblicz_cene"]').click();
	}
	
    
    if ($('#zapytaj_agenta_error, zapytaj_agenta_info').length > 0) {
        var targetOffset = $('#zapytaj_agenta').offset().top;
        $('html,body').animate({
            scrollTop: targetOffset
        }, 1000);
    }
	
	$('#podobne_right').click(function() {
		if ($('.podobne .miniatury .ogloszenie:not(.hidden):last').next().hasClass('hidden')) {
			$('.podobne .miniatury .ogloszenie:not(.hidden):last').next().removeClass('hidden');
			$('.podobne .miniatury .ogloszenie:not(.hidden):first').addClass('hidden');
		}
	});
	$('#podobne_left').click(function() {
		if ($('.podobne .miniatury .ogloszenie:not(.hidden):first').prev().hasClass('hidden')) {
			$('.podobne .miniatury .ogloszenie:not(.hidden):first').prev().removeClass('hidden');
			$('.podobne .miniatury .ogloszenie:not(.hidden):last').addClass('hidden');
		}
	});
	
	if ($('#lat_lng').length > 0 && $('#lat_lng').val() != '') {
		var myOptions = {
	        zoom: 13,
	        mapTypeId: google.maps.MapTypeId.ROADMAP,
			disableDefaultUI: true,
			draggable: false,
			scrollwheel: false
	    };
		
	    var map = new google.maps.Map(document.getElementById("mala_mapka"), myOptions);
		var geocoder = new google.maps.Geocoder();
		
		var inProcess = false;
	
//		function onZoomChanged() {
//		    if (inProcess) return;
//		
//		    if (map.getZoom() > 14) {
//		        inProcess = true;
//		        map.setZoom(14);
//		        inProcess = false;
//		        return
//		    }
//		    else if (map.getZoom() < 12) {
//		        inProcess = true;
//		        map.setZoom(12);
//		        inProcess = false;
//		        return;
//		    }
//		}
//		
//		google.maps.event.addListener(map, 'zoom_changed', onZoomChanged);
		
		var latlngStr = $('#lat_lng').val().split(",");
	    var lat = parseFloat(latlngStr[0]);
	    var lng = parseFloat(latlngStr[1]);
	    var latlng = new google.maps.LatLng(lat, lng);
		
		var marker = new google.maps.Marker({
	        map: map,
	        position: latlng
	    });		
		map.setCenter(latlng);
	}
	
//	$('.menu li.with_sub a').mouseenter(function() {
//		$(this).parent().find('ul').slideDown('fast').show();
//		$(this).parent().hover(function() {}, function() {
//			$(this).parent().find('ul').slideUp('fast');
//		});
//	});
	
	$('#o_firmie_footer, #sprawdz_nas_footer, #vademecum_footer, #kontakt_footer').mouseenter(function() {
		$(this).parent().find('div').show();
		$(this).parent().hover(function() {}, function() {
			$(this).parent().find('div').hide();
		});
	});
	
	$('#zapytaj_agenta .txt').focus(function() {
		if (this.value == this.defaultValue)
			this.value = '';
	});
	
	$('#zapytaj_agenta .txt').blur(function() {
		if (this.value == '')
			this.value = this.defaultValue;
	});
	
		
	if(window.sidebar || window.external) {
		$('#ustaw_startowa').remove();
	} else {
		$("#dodaj_do_ulubionych").remove();
		$('#ustaw_startowa').remove();
	}
	

	$("#dodaj_do_ulubionych").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return true; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
		}
	});
	
	$('#przeslij_do_znajomego_btn').click(function() {
		$('#wyslij_do_znajomego').show();
	});
	
	$('#anuluj_polec_btn').click(function() {
		$('#wyslij_do_znajomego').hide();
	});
	
	$('#wyslij_wiadomosc_btn').click(function() {
		$('#wyslij_wiadomosc').show();
	});
	
	$('#anuluj_wiadomosc_btn').click(function() {
		$('#wyslij_wiadomosc').hide();
	});
	
	rozmiescIkony();
});
