jQuery.noConflict();

function del_cookie(name) {
	document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

jQuery(document).ready(function($) {

	$('#workshop_prices').html('');

	var workshopPrices = $('#workshop_prices');
	var template = '<div id="###DIVID###" class="workshop_price"><div class="price">###PRICE###</div><div class="name">###COURSENAME###</div><input type="hidden" name="orig_price_###DIVID###" class="orig_price_value" value="###ORIG_PRICE###"/><input type="hidden" name="###NAME###" value="###PRICE###" class="submit_price" /><input type="hidden" class="forchildren" value="###FORCHILDREN###" /></div>';
	
	var sum_price_field = $('#workshop_prices_sum .price');
	var rabatt_early_until;
	var date_now;
	if ($('#rabatt_early_until').length > 0) {
		var tmpArr = $('#rabatt_early_until').attr('value').split(",");
		rabatt_early_until = new Date (tmpArr[0], tmpArr[1], tmpArr[2]);
		
		var tmpArr = $('#date_now').attr('value').split(",");;
		
		date_now = new Date (tmpArr[0], tmpArr[1], tmpArr[2]);
	}
	var lang_uid=$('#sys_language_uid').attr('value');
	
	var rabatt_menge = $('#uid6_2');
	var rabatt_pass = $('#uid6_1');
	var rabatt_student = $('#uid6_0');
	var rabatt_early = $('#uid6_3');
	
	if (lang_uid==1) {
		rabatt_menge = $('#uid28_2');
		rabatt_pass = $('#uid28_1');
		rabatt_student = $('#uid28_0');
		rabatt_early = $('#uid28_3');
	}
	
	var count_courses = 0;
	
	function updatePrices() {
		var sum_price = 0;
		//var orig_prices = $('.orig_price_value');
		var orig_prices_container = $('#workshop_prices .workshop_price');
		
		if (orig_prices_container.length > 1) {
			if (! rabatt_menge.attr('checked')) {
				//rabatt_menge.click();
				rabatt_menge.attr('checked', true);
			}
		}
		else {
			if (rabatt_menge.attr('checked')) {
				//rabatt_menge.click();
				rabatt_menge.attr('checked', false);
			}
		}
		if (rabatt_early_until > date_now) {
			if (! rabatt_early.attr('checked')) {
				//rabatt_early.click();
				rabatt_early.attr('checked', true);
			}
		}
		if (rabatt_early_until < date_now) {
			if (rabatt_early.attr('checked')) {
				//rabatt_early.click();
				rabatt_early.attr('checked', false);
			}
		}
		
		orig_prices_container.each(function () {
			var price = parseInt($('.orig_price_value', this)[0].value);
			
			//Mengenrabbatt erst ab dem zweiten Kurs:
			if (sum_price > 0)
				price = rabatt_menge.attr('checked') ? price - 10 : price;
			//Kinderrabatt nur für Erwachsenenkurse:
			var forchildren = $('.forchildren', this)[0];
			if ($('.forchildren', this)[0].value != 1)
				price = rabatt_student.attr('checked') ? price - 10 : price;
				
			price = rabatt_early.attr('checked') ? price - 10 : price;
			if ($('.forchildren', this)[0].value != 1)
				price = rabatt_pass.attr('checked') ? 25 : price;
			else
				price = rabatt_pass.attr('checked') ? 10 : price;
			
			var makeId = '#' + $('.orig_price_value', this)[0].name.replace(/orig_price_/, '');
			$(makeId +' .price').html(price + ',- €');
			$('.submit_price', this).attr('value', price);
			sum_price += price;
		});
		
		sum_price_field.html(sum_price + ',- €');
		$(sum_price_field).next().attr('value', sum_price);
	}
	
	$('input.powermail_uid6').click(function() {
		updatePrices();
	});
	
	$('input.powermail_uid28').click(function() {
		updatePrices();
	});
	
	function name2id(name) {
		return name.replace(/[\[\]]/g, '_');
	}
	
	function createWorkshopPrice(divid, name, price, forchildren) {
		return template
				.replace(/###DIVID###/g, name2id(divid))
				.replace(/###COURSENAME###/, name)
				.replace(/###PRICE###/, price)
				.replace(/###ORIG_PRICE###/, price)
				.replace(/###NAME###/, divid.replace(/uid1/, 'uid1000'))
				.replace(/###FORCHILDREN###/, forchildren);
	}
	
	$('.registration_workshop .workshop_description').each( function () {
		var description = this;
		$('.register_input input[type=checkbox]', description).each(function () {
			var input = this;
			var clickfunc = function () {
				if (input.checked) {
					$(description).addClass("selected");
					var forchildren = ($('.special', description).text().search(/Kinderkurs|for children/i) != -1) ? 1 : 0;
						
					$(createWorkshopPrice(input.name, input.value, $('.price', description).html(), forchildren)).appendTo(workshopPrices);
					updatePrices();
				}
				else {
					$(description).removeClass("selected");
					$('#' + name2id(input.name)).remove();
					updatePrices();
				}
				return true;
			};
			$(input).click(clickfunc);
			clickfunc();
		}).change();
	});
	
	var tabContainers = $('.tx-powermail-pi1_fieldset');
	var headContainers = $('.registration_step');
	var navigationContainers = $('.powermail_navigation_step');
	
	if (! (jQuery.browser.msie && jQuery.browser.version == '6.0')) {
	
		tabContainers.hide();
		navigationContainers.hide();
		
		$('.tabnavigation a').click(function (event) {
			//alert(this.id.replace(/_link/, '_container'));
			tabContainers.hide().filter(this.hash).fadeIn();
			
			var tmpArr = this.hash.split('_');
			var no = tmpArr[tmpArr.length-1];
			
			headContainers
				.removeClass('register_step_act')
				.filter('.head_step_' + no)
				.addClass('register_step_act');
			
			navigationContainers
				.hide()
				.filter('.navigation_step_' + no)
				.show();
			
			$('.tabnavigation a').removeClass('selected');
			$(this).addClass('selected');
			
			return true;
		});
	
		if (tabContainers.length > 0) {
			tabContainers.filter(':first').show();
			if (lang_uid==1)
				navigationContainers.filter('.navigation_step_4').show();
			else
				navigationContainers.filter('.navigation_step_1').show();
			headContainers.filter(':first').addClass('register_step_act');
			
			//FIXME: quick fix to delete phantom courses that won't show in frontend!
			del_cookie('fe_typo_user');
		}
	}
	else {
		navigationContainers.filter(':parent').hide();
		headContainers.filter(':parent').hide();
	}
	$('.powermail_submit_link').click(function () {
		tabContainers.hide().filter(this.hash).fadeIn();
		$('.powermail_navigation').hide();
		document.forms[0].submit();
		return false;
	});
	
});

