jQuery.noConflict();
     
jQuery(document).ready(function($){
	var $variantId = $('<input type="hidden" name="vid" value="0">').appendTo($('.product-add-form'));
	var $price = $('#var-price');
	var $special = $('#var-special');
	var $priceDiff = $('#price-diff');
	var $qty = $('#items-qty');
	
	function update(price, special, id, qty) {
		$variantId.attr('value', id);
		$qty.text(qty).parent().show();
		
		if (parseFloat(special) != parseFloat(price) && parseFloat(special) > 0) {
			$price.text(price).parent().show();
			$special.text(special);
			var priceDiff = parseFloat((price - special).toPrecision(5));
			$priceDiff.text(priceDiff).parent().show();
		} else {
			$price.parent().hide();
			$special.text(price);
			$priceDiff.parent().hide();
		}
	}
	
	$('.variant').each(function() {
		var $this = $(this);
		
		var $label = $this.find('label');
		var $radio = $this.find('input:radio');
		var $hidden = $this.find('input:hidden');
		
		var params = {
			'name':		$label.text(),
			'id':		$radio.attr('value'),
			'price':	$hidden.attr('value').split(',')[0],
			'special':	$hidden.attr('value').split(',')[1],
			'qty':		$hidden.attr('value').split(',')[2],
			'disabled':	$radio.attr('disabled'),
			'selected': $radio.attr('checked')
		}
		
		$label.remove();
		$radio.remove();
		$hidden.remove();
		
		if (params['selected']) {
			$this.parent().find('.selected').removeClass('selected');
			$this.addClass('selected');
			update(params['price'], params['special'], params['id'], params['qty']);
		}
		
		$this.html(params['name']);
		if (params['disabled']) {
			$this.addClass('disabled');
			var $tip = $('<span>').addClass('qty-tip').text('Produkt niedostępny w rozmiarze ' + params['name']).appendTo($this);
			$this.mouseover(function() {
				$tip.show()
			});
			$this.mouseout(function() {
				$tip.hide();
			});
		} else {
			$this.click(function() {
				$this.parent().find('.selected').removeClass('selected');
				$this.addClass('selected');
				update(params['price'], params['special'], params['id'], params['qty']);
			});			
			$this.mouseover(function() {
				$this.addClass('hovered');
			});			
			$this.mouseout(function() {
				$this.removeClass('hovered');
			});			
		}
	});	       
});

