function formatItem(row) {
	return row[0] + ',' + row[1];
}
// postcode suburb autocomplete
function selectItemSuburb(li) {
	if (li.extra) {
		$('#postcode').val(li.extra[0]);
		$('#state').val(li.extra[1]);
		$('#country_id').val(14);
	}
}
function selectItemPostcode(li) {
	if (li.extra) {
		$('#suburb').val(li.extra[0]);
		$('#state').val(li.extra[1]);
		$('#country_id').val(14);
	}
}

$(function() {

	$('a[href*=#]').live("click", function(e){

	//prevent the "normal" behaviour which would be a "hard" jump
		e.preventDefault();
		//Get the target
		var target = $(this).attr("href").substr(1);
		//perform animated scrolling
		$('html,body').animate(
		{
		//get top-position of target-element and set it as scroll target
		scrollTop: $("a[name = '"+target+"']").offset().top
		//scrolldelay: 2 seconds
		},1000,function()
		{
		//attach the hash (#jumptarget) to the pageurl
		location.hash = '#'+target;
		});
	});

	
	$("a.tooltip").click(function(e) {
		e.preventDefault();
	});
	
	$('.checkEmail').blur(function() {
		$.post('/order/check-email/', { email: $(this).val() }, function(d) {
			if (d=='emailIsTaken') {
				$('#emailIsTaken').slideDown(200);
				window.location = window.location.pathname + '#emailIsTaken';
				$('#checkout').attr('action', 'javascript:;');
				$('#purchaseButton').hide();
			}
			else {
				$('#emailIsTaken').slideUp(200);
				$('#checkout').attr('action', '/order/checkout/');
				$('#purchaseButton').show();
			}
		});
	});

	
	// Fancybox Image Lightbox - for product photos
	$('a.fancybox').fancybox({
		'titleShow'		: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	$('.payment_method').live('click', function(e) {
		var option = $(this);
		$('#directone_details').hide();
		$('#bank_details').hide();
		if (option.val() == 'directone') {
			$('#directone_details').show();
		} else {
			$('#bank_details').show();
		}
	});
	
	$('.delete').live('click', function(e) {
		e.preventDefault();
		var confirmText = 'Are you sure you want to delete this item?';
		if ($(this).attr('rel')) {
			confirmText = $(this).attr('rel');
		}
		if (!confirm(confirmText)){
			return false;
		} else {
			$.getJSON($(this).attr('href'), function(response){
				flashMessage([response]);
			});
			$(this).closest('tr').remove();
		}
	});
	
	$('#donationCheckBox').click(function(e) {
		checkbox = $(this);
		if (checkbox.is(':checked')) {
			$('input[name=donation]').val(1);
		} else {
			$('input[name=donation]').val(0);
		}
	});
	
	$('#ceremonySelect').change(function(e) {
		var select = $(this);
		$('input[name=ceremony_id]').val(select.val());
	});
	
	$('#suburb').autocomplete('/index/get-suburbs-or-postcodes/', { minChars:3, mustMatch:false, selectFirst:true, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItemSuburb, formatItem:formatItem });
	$('#postcode').autocomplete('/index/get-suburbs-or-postcodes/', { minChars:2, mustMatch:false, selectFirst:true, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItemPostcode, formatItem:formatItem });
	
});
