﻿function change_options(radio, checkbox) {
	var
		type = '',
		radio_dom = radio ? radio.get(0) : false,
		checkbox_dom = checkbox ? checkbox.get(0) : false,
		result = 0,
		counter = 0;

	// for each price type
	if (radio_dom) {
		$(".price-type").each(function() {
			var
				self = $(this),
				input = self.find(':radio');
			if (radio_dom == input.get(0)) {
				selected = true;
				self.addClass('selected-type');
			} else {
				self.removeClass('selected-type');
			}
		});
	} else {
		radio = $(".price-type :radio[name=type_price]:checked");
	}
	type = radio.val();
	result += parseFloat(radio.attr('rel'));

	// for each coding variant
	$(".coding_variant").each(function() {
		var
			self = $(this),
			input = self.find(':checkbox'),
			current = input.val(),
			shows = $('#shows_' + type + '_' + current).val(),
			price = 0,
			shows;

		if (type != 'free') {
			price = parseFloat($('#price_html_' + type + '_' + current).val());
		}
		if (shows == 'yes' && input.attr('checked')) {
			result += parseFloat(price);
		}
		if (checkbox_dom) {
			if (checkbox_dom == input.get(0)) {
				if (checkbox.attr('checked')) {
					self.addClass('selected-type');
				} else {
					self.removeClass('selected-type');
				}
			}
		} else {
			$('#price_span_' + current).html(price ? '$'+price : 'FREE');

			if (shows == 'yes') {
				self.removeClass('hidden');
				++counter;
			} else {
				self.addClass('hidden');
			}
			/*
			if (type != 'free') {
				$('#price_addon_' + current).removeClass('hidden');
			} else {
				$('#price_addon_' + current).addClass('hidden');
			}*/
		}
	});
	if (!checkbox_dom) {
		if (counter) {
			$('#empty_addon').addClass('hidden');
		} else {
			$('#empty_addon').removeClass('hidden');
		}
	}
	$('#total_price').text(result);
}

$(document).ready(function() {
	$('.price-type :radio').change(function() {
		change_options($(this), false);
	}).click(function(e) {
		e.stopImmediatePropagation();
	});
	$('.price-type').click(function(e) {
		e.stopImmediatePropagation();
		$(this).find(':radio').attr('checked', true).change();
	});
	$('.coding_variant input:checkbox').change(function() {
		change_options(false, $(this));
	}).click(function(e) {
		e.stopImmediatePropagation();
	});
	$('.coding_variant').click(function(e) {
		e.stopImmediatePropagation();
		var input = $(this).find(':checkbox');
		input.attr('checked', !input.attr('checked')).change();
	});
	$('.price-type :radio:first').change();
	$('#screenshots_slider').easySlider({
		auto: false, 
		continuous: true,
		controlsShow: false,
		nextId: "pager_next",
		prevId: "pager_prev"
	});
});

function showInvoice(cart_id) {
	w_width = 710;
	w_height = 450;
	w_left = screen.width / 2 - w_width / 2;
	w_top = screen.height / 2 - w_height / 2;
	
	window.open('./docs/invoice.php?cart_id=' + cart_id, '', 'width=' + w_width + ',height=' + w_height + ',left=' + w_left + ',top=' + w_top + ',scrollbars=yes,menubar=yes');
}

function deleteProduct() {
	return confirm('Do you really want to delete the product from the shopping cart?');
}

function paymentSubmit()
{
	if (!$('#agree:checked').length) {
		window.alert('Please check Terms Of Use to continue.');
		return false;
	}
    return true;
}
