$.fn.delay = function(time, callback){
	jQuery.fx.step.delay = function(){};
	return this.animate({delay:1}, time, callback);
}

$(document).ready(function() {
	
	/*
	 * ZMIANA ROZMIARU TEKSTU
	 */
	$('#size-adjuster a').click(function() {
		$('#content').removeClass('font-s').removeClass('font-m').removeClass('font-l').addClass('font-' + $(this).attr('href').substr(1));
		return false;
	});
	
	/*
	 * EFEKT FADE GLOWNEGO MENU
	 */
	$('#horizontal-navigation a').each(function() {
		$(this).wrapInner($('<span/>').css({
			display: 'inline',
			float: 'left',
			background: 'url(\'' + $(this).find('img:eq(0)').attr('src') + '\') 0 0',
			cursor: 'pointer'
		}));
		$(this).find('img').css({
			top: -43,
			opacity: 0
		});
		$(this).click(function() {
			location.href = $(this).closest('a').attr('href');
		}).hover(function() {
			$(this).find('img').stop(true, true).animate({
				opacity: 1
			}, 50);
		}, function() {
			$(this).find('img').stop(true, true).animate({
				opacity: 0
			}, 300);
		});
	});
	
	/*
	 * SLICZNE SELECTY
	 */
	$('select:not(.field-multiselect select, .field-pageselect select)').beautifulSelect();
	
	
	/*
	 * LOGARYTMICZNY SLIDER CENOWY
	 */
	var fLogScale = function(nMin, nMax, nValue) {
		if (nMin < 1) {
			nMin = 1;
		}
		var nMinValue = Math.log(nMin + 1000);
		var nMaxValue = Math.log(nMax + 1000);
		var nScale = (nMaxValue - nMinValue) / 1000;
		return Math.round(Math.exp(nMinValue + nScale * nValue) - 1000);
	};
	var fInvLogScale = function(nMin, nMax, nValue) {
		if (nMin < 1) {
			nMin = 1;
		}
		var nMinValue = Math.log(nMin + 1000);
		var nMaxValue = Math.log(nMax + 1000);
		var nScale = (nMaxValue - nMinValue) / 1000;
		return (Math.log(nValue + 1000) - nMinValue) / nScale;
	};
	$(".field-slider").each(function() {
		var nMin = parseFloat($(this).find('.range-min').val());
		var nMax = parseFloat($(this).find('.range-max').val());
		var nValueA = parseFloat($(this).find('.min').val());
		var nValueB = parseFloat($(this).find('.max').val());
		$(this).find('div').slider({
			range: true,
			min: 0,
			max: 1000,
			values: [fInvLogScale(nMin, nMax, nValueA), fInvLogScale(nMin, nMax, nValueB)],
			slide: function(event, ui) {
				$(this).closest('.field-slider').find('.min').val(fLogScale(nMin, nMax, ui.values[0]));
				$(this).closest('.field-slider').find('.min-label span').text(fLogScale(nMin, nMax, ui.values[0]));
				$(this).closest('.field-slider').find('.max').val(fLogScale(nMin, nMax, ui.values[1]));
				$(this).closest('.field-slider').find('.max-label span').text(fLogScale(nMin, nMax, ui.values[1]));
			}
		});
		$(this).find('.min-label span').text(parseFloat($(this).find('.min').val()));
		$(this).find('.max-label span').text(parseFloat($(this).find('.max').val()));
	});
	
	
	/*
	 * KALENDARZ
	 */
	$.datepicker.regional['pl'] = {
		closeText: 'Zamknij',
		prevText: '&#x3c;Poprzedni',
		nextText: 'Następny&#x3e;',
		currentText: 'Dziś',
		monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
		'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
		monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
		'Lip','Sie','Wrz','Pa','Lis','Gru'],
		dayNames: ['Niedziela','Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
		dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
		dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
		dateFormat: 'yy-m-d', firstDay: 1,
		isRTL: false
	};
	$.datepicker.setDefaults($.datepicker.regional['pl']);
	$('.search .field-date').each(function() {
		var jField = $('<input type="hidden"/>');
		$(this).append(jField);
		var jDay = $(this).find('select:eq(0)');
		var jMonth = $(this).find('select:eq(1)');
		jField.datepicker({
			minDate: '0',
			maxDate: '+1Y',
			showOn: 'button',
			buttonImage: '/_images/buttons/calendar.png',
			onSelect: function(sDateText, cInstance) {
				var aDate = sDateText.split('-');
				jDay.val(parseInt(aDate[2])).change();
				jMonth.val(parseInt(aDate[1])).change();
			}
		});
	});
	
	
	/*
	 * POKAZYWAJKI
	 */
	var jTmp;
	$('a.toggle-block').each(function() {
		if ($($(this).attr('href')).length) {
			$($(this).attr('href')).css('display', 'none');
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideToggle(200);
				return false;
			});
		}
	});
	$('a.show-block').each(function() {
		if ($($(this).attr('href')).length) {
			$($(this).attr('href')).css('display', 'none');
			$('a.hide-block[href="' + $(this).attr('href') + '"]').css('display', 'none');
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideDown(200);
				$('a.hide-block[href="' + $(this).attr('href') + '"]').css('display', 'block');
				$(this).css('display', 'none');
				if ($(this).attr('href') == '#advanced-search') {
					jTmp = $(this).closest('form').find('.simple-search-submit').remove();
				}
				return false;
			});
		}
	});
	$('a.hide-block').each(function() {
		if ($($(this).attr('href')).length) {
			$(this).click(function() {
				$($(this).attr('href')).stop(true, true).slideUp(200);
				$('a.show-block[href="' + $(this).attr('href') + '"]').css('display', 'block');
				$(this).css('display', 'none');
				if ($(this).attr('href') == '#advanced-search') {
					jTmp = $(this).closest('form').find('.more:first').before(jTmp);
				}
				return false;
			});
		}
	});
	
	
	/*
	 * KARUZELE OFERT
	 */
	$('.ideas').chunker({
		chunkSize: 4,
		sliceSize: 2,
		autoDelay: 12
	});
	
	$('.offers-short').chunker({
		chunkSize: 4,
		sliceSize: 1,
		autoDelay: 6
	});
	
	
	/*
	 * PLYNNE POKAZANIE OCEN
	 */
	$('.offer .rating-indicator span, .reviews-mainsite .recently-added .rating-indicator span').each(function(i) {
		var iWidth = $(this).css('width');
		$(this).css('width', 0).delay(i * 100).animate({
			width: iWidth
		}, 350);
	});
	
	
	/*
	 * KARUZELE MINIATUR
	 */
	$('.mini-gallery').thumbCarousel({
		item_width: 55,
		place_width: 220
	});
	
	
	/*
	 * KARUZELE NEWSOW
	 */
	$('.news-flash').newsCarousel({
		item_width: 61,
		place_width: 366,
		auto_rotation_delay: 4000
	});
	
	
	/*
	 * ZAKLADKI
	 */
	$('.tabs').tabs();
	$('.ui-tabs-panel').each(function() {
		$('a[href="#' + $(this).attr('id') + '"]').each(function() {
			if ($(this).closest('.ui-tabs').length) {
				return;
			}
			$(this).click(function() {
				$('.ui-tabs a[href="' + $(this).attr('href') + '"]').click();
				return true;
			});
		});
	});
	
	
	/*
	 * WYSWIETLANIE SUMARYCZNEJ OPINII
	 */
	$('.reviews .summary').ratingChart();
	
	
	/*
	 * AKORDEON OFERT
	 */
	$('.offer-accordion ul').accordion({
		autoHeight: false
	});
	
	
	/*
	 * POWTARZALNE POLE TYPU FILE
	 */
	$('.field-file').each(function() {
		var sFieldName = $(this).find('input').attr('name');
		$(this).find('.add a').click(function() {
			var jFields = $(this).closest('.fields').find('.field input');
			for (var i = 0; i < jFields.length; i++) {
				jFields.eq(i).attr('name', sFieldName + '[' + i + ']');
			}
			var jField = $('<div class="field"><input type="file" name="' + sFieldName + '[' + i + ']"/> <a href="#">usuń</a></div>').css('display', 'none');
			$(this).closest('.add').before(jField);
			jField.slideDown(100);
			return false;
		});
		$(this).find('.field a').live('click', function() {
			$(this).closest('.field').slideUp(100, function() {
				var jFields = $(this).closest('.fields');
				$(this).remove();
				jFields = jFields.find('.field input');
				for (var i = 0; i < jFields.length; i++) {
					jFields.eq(i).attr('name', sFieldName + '[' + i + ']');
				}
			});
			return false;
		});
	});
	
	
	/*
	 * LISTA TEMATOW - ZDJECIA
	 */
	$('.themes li > a').each(function(i) {
		var sBg = $(this).attr('style');
		if (!sBg || !sBg.length) {
			return;
		}
		$(this).mouseover(function() {
			$(this).closest('.themes').attr('style', sBg);
		});
		$(this).attr('style', '');
		$(this).css('background', 'transparent').attr('style', ';');

	});
	
	
	/*
	 * AJAXOWY PRZEWODNIK
	 */
	$('#guide-links').find('a').click(function() {
		$('#guide-body').fadeOut(150).load($(this).attr('href').replace(/\#guide-body/, ''), function() {
			$('#guide-body').fadeIn(150);
		});
		return false;
	});
	
});