// Google Analytics
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

function trackEvent(target, action, opt_label, opt_value) {
	if (pageTracker && !pageTracker.cb) {
		setTimeout(function() {
			trackEvent(target, action, opt_label, opt_value);
		}, 200);
		return;
	}
	pageTracker._trackEvent(target, action, opt_label, opt_value);
}

function recordOutboundLink(link, category, action) {
	try {
		var pageTracker = _gat._getTracker("UA-15605926-2");
		trackEvent(category, action);
		setTimeout('document.location = "' + link.href + '"', 100);
	} catch(err) {}
}

// Everything Else
function fillSelect(date, target, price, wordy) {
	if ($(date).value) {
		u = '/shows/selector/'+$(date).get('value');
		if (wordy) u += '/1';
		$(target).empty();
		var option = new Element('option').set('text', 'Loading...').inject(target);
		var req_1 = new Request.HTML({
			url: u,
			method: 'get',
			update: $(target+'Wrapper'),
			onComplete: function() {
				if (price) {
					// Show the price of the selected show
					fillPrices($('ShowId').value);
					$('ShowId').addEvent('change', function() {
						fillPrices(this.value);
					});
				}
			}
		}).send();
	}
}

function fillPrices(id) {
	ticket_types = ['Adults', 'Children', 'Seniors', 'Ashevillans'];
	if (id) {
		ticket_types.each(function(item, index){
		    $('ticket'+item+'Cost').set('text', 'Loading...');
		});
		var req_2 = new Request({
			method: 'get',
			url: '/shows/prices/'+id,
			data: { 'do' : '1' },
			onComplete: function(response) {
				responses = response.split(',');
				responses.each(function(item, index){
					if (item && item > 0) {
						$('ticket'+ticket_types[index]+'Wrapper').setStyle('display', '');
						$('ticket'+ticket_types[index]+'Cost').set('text', '$'+formatMoney(item));
					} else {
						$('ticket'+ticket_types[index]).setProperty('checked','');
						$('ticket'+ticket_types[index]+'Count').value = 0;
						$('ticket'+ticket_types[index]+'Count').setStyle('visibility', 'hidden');
						$('ticket'+ticket_types[index]+'Wrapper').setStyle('display', 'none');
					}
				});
			}
		}).send();
	}
}

function formatMoney(amount) {
	var i = parseFloat(amount);
	if (isNaN(i)) { i = 0.00; }
	var minus = '';
	if (i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if (s.indexOf('.') < 0) { s += '.00'; }
	if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function toggle_count(n) {
	if ($('ticket'+n).checked == true) {
		$('ticket'+n+'Count').value = 1;
		$('ticket'+n+'CountWrapper').setStyle('visibility', 'visible');
	} else {
		$('ticket'+n+'Count').value = 0;
		$('ticket'+n+'CountWrapper').setStyle('visibility', 'hidden');
	}
}

function spm_trap(n) {
	locationstring="mailto:"+n+"@lazoomtours.com?subject=LaZoom Tours";
	window.location=locationstring;
}