/*
MÖGLICHE KOMBINATIONEN STANDARD:

CINEMA --> 25,00 (danach 30,00)
SPORT --> 25,00 (danach 30,00)
FUSSBALL --> 17,50 (danach 21,00)

CINEMA + SPORT --> 37,50 (danach 45,00)
CINEMA + FUSSBALL --> 30,00 (danach 36,00)
FUSSBALL + SPORT --> 30,00 (danach 36,00)

CINEMA + SPORT + FUSSBALL --> 42,50 (danach 51,00)
*/

(function ($) {

	$.fn.SkyCalculator = function (options) {

		// user variables
		var defaults = {
			'selector': {
				'checkbox': 'input[type=checkbox].package',
				'result': '#sky-result',
				'resultSmall' : '#sky-result-small',
				'package': 'input[type=checkbox].package:checked',
				'packageList': '#package-list',
				'cta': '#cta-order'
			},
			'shop_link': 'https://www.sky.at/bestellung/abo-konfigurieren/pakete-konfigurieren-magenta/einrichten?preselectedPackComb={{ PACKAGES }}&wkz=AWESP28'

		};

		// combines default settings and user options
		this.settings = $.extend({}, defaults, options);

		// define private variable to use anywhere
		var _self = this;
		var packageList;
		var cta;

		// initiate function
		function init() {
			packageList = get('packageList');
			cta = get('cta');
			
			initCheckboxes();
			updateResult();
			updateList();

			get('checkbox').on('change', handleCheckboxes);
		}

		function initCheckboxes() {
			get('checkbox').each(function() {
				if (this.checked) $(this).parent().addClass('checked');
			})
		}

		function handleCheckboxes() {
			$(this).parent().toggleClass('checked');
			updateResult();
			updateList();
		}

		function updateResult() {
			var result = 0.00;
			var resultSmall = 0.00;

			// packages
			if (get('package').length == 1 && get('package').data('name') == 'Sky Cinema') {
				result = 25.00;
				resultSmall = 30.00;
			} else if (get('package').length == 1 && get('package').data('name') == 'Sky Sport') {
				result = 25.00;
				resultSmall = 30.00;
			} else if (get('package').length == 1 && get('package').data('name') == 'Sky Bundesliga') {
				result = 17.50;
				resultSmall = 21.00;
			} else if (get('package').length < 3 && get('package').data('name') == 'Sky Cinema' && get('package').eq(1).data('name') == 'Sky Sport') {
				result = 37.50;
				resultSmall = 45.00;
			} else if (get('package').length > 1 && get('package').data('name') == 'Sky Cinema' && get('package').eq(1).data('name') == 'Sky Bundesliga') {
				result = 30.00;
				resultSmall = 36.00;
			} else if (get('package').length > 1 && get('package').data('name') == 'Sky Sport' && get('package').eq(1).data('name') == 'Sky Bundesliga') {
				result = 30.00;
				resultSmall = 36.00;
			} else if (get('package').length == 3) {
				result = 42.50;
				resultSmall = 51.00;
			} else {
				result = 0.00;
				resultSmall = 0.00;
			}


			// round result
			result = result.toFixed(2);
			resultSmall = resultSmall.toFixed(2);
			result = result.toString().replace('.', ',');
			resultSmall = resultSmall.toString().replace('.',',');
			
			// show result
			get('result').html(result);
			get('resultSmall').html(resultSmall);
		}

		function updateList() {
			var url = _self.settings.shop_link;
			var packages = [];
			// packages
			packageList.find('.feature-positive').remove();
			get('package').each(function() {
				var id = $(this).attr('id');
				var option = $('<li>', {
					'class': 'feature-positive',
					'data-id': id
				}).text($(this).data('name'));
				packageList.find('.empty-message').before(option);
				packages.push($(this).data('param'));
			});
			if(packageList.find('.feature-positive').length > 0) {
				packageList.removeClass('empty');
				cta.attr('href', url.replace(/\{\{ PACKAGES \}\}/g, packages.join('|')));
				cta.removeClass('btn-disabled');
			}
			else {
				packageList.addClass('empty');
				cta.addClass('btn-disabled');
			}
		}


		function get(index, param) {
			if (typeof param != 'undefined') return _self.find(_self.settings.selector[index] + param);
			return _self.find(_self.settings.selector[index]);
		}

		// initiate plugin
		init();

		// return the jquery object
		return this;

	};

}(jQuery));
	
$(function() {
	$('body').SkyCalculator({});
});