jQuery(document).ready(function() {
	var timespans = {
		1: 'monthly',
		3: 'quarterly',
		6: 'semi-annually',
		12: 'annually'
	};

	var update_price = function($form) {
		var $select = $form.find('select');
		var os = $form.find('input[name=os][type=hidden], input[name=os]:checked').val();
		var months = parseInt($select.val(), 10);
		var price = parseFloat($select.find('option:selected').data('price-' + os));
		$select.closest('ul.setup').find('li.total-price strong').html('&euro;' + price.toFixed(2));
		var savings = parseFloat($select.find('option:first').data('price-' + os)) * months - price;
		if (savings != 0)
			$select.closest('ul.setup').find('li.total-price span.save').html('You save '  + '&euro;' + savings.toFixed(2)).fadeIn();
		else
			$select.closest('ul.setup').find('span.save').fadeOut();
		var $form = $select.closest('form');
		$form.attr({action: $form.attr('action').replace(/billingcycle=\w+/, 'billingcycle=' + timespans[months].replace(/\W/, ''))});
	};

	jQuery('.slider-container .offers select').change(function(e) {
		update_price($(this).closest('form'));
	}).val('1');

	jQuery('.slider-container .offers input[name=os]:radio').change(function() {
		update_price($(this).closest('form'));
	});

	jQuery('.slider-container .offers li.os label').click(function() {
		var $label = $(this);
		var $form = $label.closest('form');
		$form.attr({action: $form.attr('action').replace(/pid=\w+/, 'pid=' + $(this).find('input.alt-radio').data('pid'))});
	});

	/* Pricing page */
	var offer_comparison_price = function($select) {
		var os = $select.val();
		var promotionType = $select.attr('promotion-type');
		var price = parseFloat($select.find('option:selected').attr('data-' + os));
		var pid = parseInt($select.find('option:selected').attr('data-' + os + '-pid'));
		var $button = $('.' + promotionType + '-button');
		$button.attr({href: $button.attr('href').replace(/pid=\w+/, 'pid=' + pid)});
	};

	jQuery('.offer-comparison select').change(function() {
		offer_comparison_price($(this));
	});
	
	/* Pricing page */
	var ssl_comparison_price = function($select) {
		var os = $select.val();
		var promotionType = $select.attr('promotion-type');
		var price = parseFloat($select.find('option:selected').attr('data-' + os));
		var pid = parseInt($select.find('option:selected').attr('data-' + os + '-pid'));
		var $button = $('.' + promotionType + '-button');
		$button.attr({href: $button.attr('href').replace(/pid=\w+/, 'pid=' + pid)});
	};

	jQuery('.ssl-comparison select').change(function() {
		offer_comparison_price($(this));
	});
	
});

