function calc() {

	var tickets = $('#tickets');

	var available = $('#category option:selected').attr('available');



	if (available) {

		$('option', tickets).each(function() {

			if ($(this).val()*1 > available*1) {

				$(this).remove();

			}

		});

		var max_curr = $('option', tickets).last().val() || 0;

		if (max_curr < 20 && max_curr < available) {

			for (var i=max_curr; ++i<=available;) {

				$('<option value="'+i+'">'+i+'</option>').appendTo(tickets);

			}

		}

	} else {

		var max_curr = $('option', tickets).last().val() || 0;

		if (max_curr < 20) {

			for (var i=max_curr; ++i<=20;) {

				$('<option value="'+i+'">'+i+'</option>').appendTo(tickets);

			}

		}

	}



	var gift_certificate = ($('#giftCertificateValue').text() || 0) * 1;



	var delivery = 0;

 	if ($('#delivery_mail').attr('checked')) {

		delivery = 10;

  	} else

	if ($('#delivery_hotel').attr('checked')) {

		delivery = 5;

	}

	var category;
	if ($('#category').length > 0) {
		category = $('#category').val().replace(/^(\d+:)(\d+)$/,'$2');
	}


	var sum = tickets.val()*category+delivery-gift_certificate;

	if (sum < 0) {

		sum = 0;

	}

	$('#tempTotal').html($('#total').val(sum).val());

}



$(document).ready(function() {

	$('form.columnEvents').submit(function() {

		var select = $(this).find('select');

		if (select.count && select.val() == '0' || !select.val().match(/^[1-9]\d+$/)) {

			if (LANG == 'en') {

				alert('You have not selected a valid date or you have selected a Sold Out event.');

			} else {

				alert('Sie haben entweder keinen oder einen ausverkauften Termin ausgew�hlt.');

			}

			return false;

		}

	}).find('select').change(function() {

		var lang_link = (LANG == 'de') ? '/deutsch' : '';

		var a = $(this).closest('form').find('a.infolink');



		if ($(this).val().match(/^[1-9]\d+$/)) {

			if (!a.attr('hrefbak')) {

				a.attr('hrefbak', a.attr('href'));

			}

			a.attr('href',lang_link+'/eventinfo/'+$(this).val()+'/'+$(this).find('option:selected').text().replace(/\s/g,'-'));

		} else

		if (a.attr('hrefbak')) {

			a.attr('href', a.attr('hrefbak'));

		}

	}).change();



	// diese information links sind erlaubt

	var allowedInfoLinks = [

		'Mozart_Ensemble_Vienna',	'Mozart_Ensemble_Wien',

		'Vienna_Imperial_Orchestra',	'Wiener_Imperial_Orchester',

		'Vienna_Residence_Orchestra',	'Wiener_Residenz_Orchester',

		'Vienna_Walzer_Orchestra',	'Wiener_WalzerMozart_Orchester',

		'Vienna_Mozart_Orchestra',	'Wiener_Mozart_Orchester',

		'Vienna_Hofburg_Orchestra',	'Wiener_Hofburg_Orchester',

		'Salonorchester_Alt_Wien',

		'Vienna_Royal_Orchestra',	'Wiener_Royal_Orchester',

		'Vivaldi_Exclusive', 'Vivaldi_Exclusive_Konzerte'
	];



	// click on infolink

	$('a.infolink').click(function() {

		var href = $(this).attr('href');

		if (href.match(/\/eventinfo\/\d+/)) {

			return true;

		}

		for (var i=-1, len=allowedInfoLinks.length; ++i<len;) {

			if (href.match(new RegExp("event/"+allowedInfoLinks[i], 'i'))) {

				return true;

			}

		}

		if (LANG == 'de') {

			alert('Sie haben entweder keinen oder einen ausverkauften Termin ausgew�hlt.');

		} else {

			alert('You have not selected a valid date or you have selected a Sold Out event.');

		}

		return false;



	});



	$('#book input[type=radio][name=delivery], #book select[name=tickets], #book select[name=category]').click(calc).change(calc);

	if ($('form#book').length > 0 && !$('#book').hasClass('bookCoupon')) {

		calc();

		if ($.browser.msie) {

			setInterval(calc, 200);

		}

	}



	// popups: einfach ein link (<a>) mit target=popup erstellen

	$('a[target=popup]').click(function() {

		var now = new Date();

		window.open($(this).attr('href'), 'POPUP'+now.getTime(), 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0, resizable=0,width=400,height=450,left=100,top=100');

		return false;

	});

	// maps popups

	$('a[target=map]').click(function() {

		var now = new Date();

		window.open($(this).attr('href'), 'MAP'+now.getTime(), 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0, resizable=0,width=600,height=600,left=100,top=100');

		return false;

	});

	// seating popups

	$('a[target=seating]').click(function() {

		var now = new Date();

		window.open($(this).attr('href'), 'SEATING'+now.getTime(), 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0, resizable=0,width=630,height=690,left=100,top=100');

		return false;

	});



	// recommendations

	var recommendations = $('#recommendations div.recommendation');

	if (recommendations.length > 1 && (!$.browser.msie || !$.browser.version.match(/^6/))) {

		var recommend_interval;

		function recommend(back, force) {

			recommendations.filter(':visible').fadeOut((force ? 500 : 1000), function() {

				var next = back ? $(this).prev() : $(this).next();

				if (!next.length) {

					next = back ? $(this).siblings().last() : $(this).siblings().first();

				}

				next.fadeIn(1000);

			});

		}

		// back, forward buttons

		recommendations.each(function() {

			$(this).find('span.back, span.fwd').click(function() {

				recommend($(this).hasClass('back'), true);

				clearInterval(recommend_interval);

				recommend_interval = setInterval(recommend, 7000);

			});

		});

		recommend_interval = setInterval(recommend, 7000);

	}



	$('#faq li a.link').click(function() {

		var p = $(this).parents('li').find('p.desc');

		if (p.is(':hidden')) {

			p.get(0).style.display = 'block';

		} else {

			p.get(0).style.display = 'none';

		}

		return false;

	});



	// gutscheine

	var getCertValue = function() {

		var v = $('#giftCertificateValue');

		var c = $('#coupon');



		if (!c.val().length) {

			if (LANG == 'de') {

				alert('Sie haben keine Gutscheinnummer eingetragen.');

			} else {

				alert('You did not enter a gift coupon code.');

			}

			return;

		}

		$.ajax({

			url: '/checkgift.php',

			data: {	coupon: c.val() },

			beforeSend: function() {

				c.addClass('loading');

			},

			success: function(data) {

				if (data.amount) {

					c.removeClass('loading');

					v.text(data.amount);

				} else

				if (data.error) {

					if (LANG == 'de') {

						alert('Diese Gutscheinnummer ist nicht g�ltig.');

					} else {

						alert('This gift coupon code is not valid.');

					}

				}

				calc();

			},

			complete: function() {

				c.removeClass('loading');

				calc();

			}

		});

	};

	$('#coupon_check').click(getCertValue);

	$('#showGiftCertificateField > span').click(function() {

		$('div.giftCertificateField').toggle();

	});

	if (!$('#coupon').length || $('#coupon').val().length == 0) {

		$('div.giftCertificateField').hide();

	} else {
	
		if ($('#coupon')[0].tagName == 'input') {
			getCertValue();
		}

	}





	// IE6 CSS and PNG fixes

	if ($.browser.msie && $.browser.version.match(/^[65]/)) {

		$('ul.menu li:first-child').addClass('first');

		$(document).pngFix();



		$('#leftShadow, #rightShadow').each(function() {

			var bgImage = $(this).css('background-image');

			if (bgImage.match(/\.png.?\)$/)) {

				$(this).attr('writing-mode', 'tb-rl');

				$(this).css('background-image', 'none');

				bgImage = bgImage.replace(/^.*(http:.*\.png)"?\)$/,'$1');

				var bgColor = $(this).css('background-color');

				$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+bgImage+'",sizingMethod="scale")');

				$(this).css('background-color', bgColor)

			}

		});

		// all image buttons: replace png with iejpg/*.jpg

		$('input[type=image][src$=png]').each(function() {

			$(this).attr('src', $(this).attr('src').replace(/\/img\/(.*)(\.png)$/,'/img/iejpg/$1.jpg'));

		});

	}





	// IE select repair

	if ($.browser.xmsie) {

		var body = $('body');

		$('select').each(function() {

			var options = $(this).find('option');

			if (options.length > 1) {

				var span = $('<span style="position: absolute; left: -1000px; top: -1000px; display: inline-block; font-size: 12px;">'+options.last().text()+'</span>');

				span.appendTo(body);

				var w = span.width();



				if (w < $(this).width()) {

					$(this).selectbox({listboxMaxSize: 15});

				} else {

					$(this).selectbox({optionWidth: w*1.7, listboxMaxSize: 15});

				}

				span.remove();

			} else {

				$(this).selectbox();

			}

		});

	}

	if ($.browser.xmsie) {

		var lang_link = (LANG == 'de') ? '/deutsch' : '';

		var selects = $('form.columnEvents select');

		setInterval(function() {

			selects.each(function() {

				var a = $(this).closest('form').find('a.infolink');

				if ($(this).val().match(/^[1-9]\d+$/)) {

					if (!a.attr('hrefbak')) {

						a.attr('hrefbak', a.attr('href'));

					}

					a.attr('href',lang_link+'/eventinfo/'+$(this).val()+'/'+$(this).find('option:selected').text().replace(/\s/g,'-'));

				} else

				if (a.attr('hrefbak')) {

					a.attr('href', a.attr('hrefbak'));

				}

			});

		}, 333);

	}

});

