var fInitCartDependencies;
$(document).ready(function() {

	$('.search .field-text input, .newsletter .field-text input, .login .field-text input').JTextField();

	$('.cart .quantity .field-text input:not(.weight), .cart-summary .quantity input:not(.weight)').JTextField({
		bResetOnEmpty: false,
		sType: JTextField.TYPE_INT,
		iMin: 0, // minimalna wartosc
		iRound: 1 // wielkosc zaokraglenia
	});
	$('.cart .quantity .field-text input.weight, .cart-summary .quantity input.weight').JTextField({
		bResetOnEmpty: false,
		sType: JTextField.TYPE_INT,
		iMin: 50, // minimalna wartosc
		iRound: 50, // wielkosc zaokraglenia
		bZero: true
	});

	$('.product-details .add-to-cart dt.custom input:not(.weight)').JTextField({
		bValueAsLabel: true,
		sType: JTextField.TYPE_INT,
		iMin: 0, // minimalna wartosc
		iRound: 1 // wielkosc zaokraglenia
	});
	$('.product-details .add-to-cart dt.custom input.weight').JTextField({
		bValueAsLabel: true,
		sType: JTextField.TYPE_INT,
		iMin: 50, // minimalna wartosc
		iRound: 50 // wielkosc zaokraglenia
	});
	$('.product-details .add-to-cart dd.custom input').mousedown(function(eEvent) {
		if (!$('.product-details .add-to-cart dt.custom input').get(0).Check()) {
			this.bDontSubmit = true;
			eEvent.preventDefault();
			eEvent.stopImmediatePropagation();
			return false;
		}
		this.bDontSubmit = false;
		return true;
	}).click(function(eEvent) {
		return !this.bDontSubmit;
	});

	$('.carousel').JProductCarousel({
		nBlurOpacity: 1, // stopien przezroczystosci gdy kursor poza obszarem
		nFocusOpacity: .6, // stopien przezroczystosci gdy kursor w obszarze
		iSlideSwitchInterval: 7000 // liczba milisekund przed automatycznym przelaczeniem sie slajdu
	});

	$('.suggestions, .layout-right-column .promotions ul, .products-list-two-columns').JProductSuggestion({
		nBlurOpacity: 1, // stopien przezroczystosci gdy kursor poza obszarem
		nFocusOpacity: .6 // stopien przezroczystosci gdy kursor w obszarze
	});

	$('.product-details .gallery').JGallery({
		iSlideSwitchInterval: 5000 // liczba milisekund przed automatycznym przelaczeniem sie slajdu
	});

	$('.product-description').tabs({
		fx: {
			height: 'toggle',
			opacity: 'toggle',
			duration: 150
		}
	});

	$('.rating').JBeatifulRating();

	$('#cart-form').JCartForm({
		sCurrency: 'zł',
		recalculate_url: 'koszyk.html',
		bReloadEverything: true
	});
	$('#cart-form input[name="recalculate"]:eq(0)').triggerHandler('click');

	$('input.toggle').JToggler();
	
	$('#search-query').autocomplete({
		serviceUrl: 'search.php',
		minChars:2,
		width:160
	});

	$('.message').delay(3000).slideUp(250, function() { // liczba milisekund po ktorych wiadomosc ma zniknac i czas animacji
		$(this).remove();
	});

	fInitCartDependencies = function() {
		// Przesylka zagraniczna
		$('input[name="shipping"][value="3"]').click(function() {
			$('input[name="payment"][value="2"]').attr('disabled', 'disabled').closest('label').css('display', 'block');
			$('input[name="payment"][value="3"]').attr('disabled', 'disabled').closest('label').css('display', 'none');
			setTimeout(function() {
				if ($('input[name="payment"][value="2"]').is(':checked') || $('input[name="payment"][value="3"]').is(':checked')) {
					$('input[name="payment"][value="1"]').click().closest('form').get(0)._CalculateOrderPrice();
				}
			}, 5);
		});
		// Poczta polska - priorytet
		$('input[name="shipping"][value="1"]').click(function() {
			$('input[name="payment"][value="2"]').attr('disabled', '').closest('label').css('display', 'block');
			$('input[name="payment"][value="3"]').attr('disabled', 'disabled').closest('label').css('display', 'none');
			setTimeout(function() {
				if ($('input[name="payment"][value="3"]').is(':checked')) {
					$('input[name="payment"][value="2"]').click().closest('form').get(0)._CalculateOrderPrice();
				}
			}, 5);
		});
		// Kurier
		$('input[name="shipping"][value="2"]').click(function() {
			$('input[name="payment"][value="3"]').attr('disabled', '').closest('label').css('display', 'block');
			$('input[name="payment"][value="2"]').attr('disabled', 'disabled').closest('label').css('display', 'none');
			setTimeout(function() {
				if ($('input[name="payment"][value="2"]').is(':checked')) {
					$('input[name="payment"][value="3"]').click().closest('form').get(0)._CalculateOrderPrice();
				}
			}, 5);
		});
		
		$('input[name="shipping"]:checked').triggerHandler('click');
		
	};
	fInitCartDependencies();
	
	// Pokazywanie sekcji ukrytych
	
	$('a[rel="show"]').click(function(eEvent) {
		$($(this).attr('href')).show();
		$(this).remove();
		return false;
	});

	//mielenie
	$('form.add-to-cart select').change(function(eEvent) {
		var aMatch = $(this).find('option:selected').text().match(/\([+-]?(\d+[.,]\d{2})[^\)]*\)/);
		var fGrindPrice = 0;
			if (aMatch != undefined){
			fGrindPrice = isNaN(parseFloat(aMatch[1])) ? 0 : parseFloat(aMatch[1]);
			}
		$(this).closest('form').find('dl dd:not(.custom) .price').each(function() {
			if (this.fBasePrice == undefined) this.fBasePrice = parseFloat($(this).text());
			if (this.sCurrency == undefined) this.sCurrency = $(this).text().substr($(this).text().length - 2);
			$(this).text(this.fBasePrice + fGrindPrice + this.sCurrency);
			});
	}).triggerHandler('change'); 

});