$(document).ready(function(){
	$(".tasting_header_click").toggle(
		function() {
			$(this).parent().next(".tasting_notes").slideDown('slow');
		},
		function(){
			$(this).parent().next(".tasting_notes").slideUp('fast');
		}
	);
	$(".confirm_delete").click(function () {
		var answer = confirm('Are you sure you wish to delete this bottle. This cannot be undone.');
		return answer;
	});
	$(".confirm_delete_description").click(function () {
		var answer = confirm('Are you sure you wish to delete this tasting note. This cannot be undone.');
		return answer;
	});
/* login form validation */
	$('.login_form').validate({
		rules: {
			'data[User][email]': {
				email: true,
				required: true
			},
			'data[User][password]': {
				required: true
			}
		}
	});
	$('.new_user_form').validate({
		rules: {
			'data[User][email]': {
				email: true,
				required: true
			},
			'data[User][password]': {
				required: true, 
			}
		/*	,
			'data[User][password_confirmation]': {
				equalTo: 'data[User][password]'
			} */
		}
	});
	$('#BottleAddForm').validate({
		rules: {
			'data[Bottle][vintage]': {
				rangelength: [4, 4],
				number: true
			},
			'data[Bottle][number_purchased]': {
				number: true
			},
			'data[Bottle][year_purchased]': {
				rangelength: [4, 4],
				number: true
			},
			'data[Bottle][try_next]': {
				rangelength: [4, 4],
				number: true
			},
			'data[Bottle][number_left]': {
				number: true
			},
			'data[Bottle][purchase_price]': {
				number: true
			},
			'data[Bottle][prime_year]': {
				rangelength: [4, 4],
				number: true
			},
			'data[Bottle][barcode]': {
				number: true
			}
		},
		messages: {
			'data[Bottle][vintage]': 'This must be a 4 character long year (yyyy)',
			'data[Bottle][year_purchased]': 'This must be a 4 character long year (yyyy)',
			'data[Bottle][try_next]': 'This must be a 4 character long year (yyyy)',
			'data[Bottle][purchase_price]': 'This just needs to be a number, no \'$\' required',
			'data[Bottle][prime_year]': 'This must be a 4 character long year (yyyy)',
			'data[Bottle][barcode]': 'This field must be a number with no spaces'
		}
	});
	$('#UserTellFriendForm').validate({
		rules: {
			'data[User][your_email]': {
				email: true,
				required: true
			},
			'data[User][friend_email]': {
				email: true,
				required: true
			},
			'data[User][your_name]': {
				required: true
			},
			'data[User][friend_name]': {
				required: true
			}
		}
	});
/* end login form validation */
	
/* Ajax Autocompletes */
	$('#WineryWineryName').autocomplete('../Wineries/ajax_auto_complete');
	$('#RegionRegionName').autocomplete('../Regions/ajax_auto_complete');
	$('#BottleVariety').autocomplete('../Varieties/ajax_auto_complete');
	$('#UserCountry').autocomplete('../Countries/ajax_autocomplete');
	$('#CountryName').autocomplete('../Countries/ajax_autocomplete');
/* End Ajax Autocompletes */
	
/* Tool tips */
	$('.confirm_delete').qtip({
		content: 'Permanently delete a bottle',
		show: 'mouseover',
		hide: 'mouseout',
		style: {
			name: 'red',
			tip: 'topLeft'
		}
	});
	$('.archive_bottle').qtip({
		content: 'Move this bottle to your archive',
		show: 'mouseover',
		hide: 'mouseout',
		style: {
			name: 'cream',
			tip: 'topLeft'
		}
	});
	$('.unarchive_bottle').qtip({
		content: 'Move this bottle back to your cellar',
		show: 'mouseover',
		hide: 'mouseout',
		style: {
			name: 'cream',
			tip: 'topLeft'
		}
	});
	$('.bottle_info').qtip({
		content: 'View, edit and add tasting notes for this bottle',
		show: 'mouseover',
		hide: 'mouseout',
		style: {
			name: 'green',
			tip: 'topLeft'
		}
	});
	$('.barcode_info').qtip({
		content: 'We have added this field for future use. It doesn\'t do anything for now but will be useful in the future.',
		show: 'mouseover',
		hide: 'mouseout',
		style: {
			name: 'green',
			tip: 'topLeft'
		},
		width: '250px'
	});
/* End Tool tips */

/* accordian */
	$('#about').accordion({
		autoHeight: false
	});
	$('.tasting').accordion({
		active: false,
		collapsible: true,
		autoHeight: false,
	});
/* end accordian */

/* check details added for bottle (haven't accadentally pushed enter */	
	$('.submit').click(function(){
		var winery = $('#WineryWineryName').val();
		var label = $('#BottleLabel').val();
		var variety = $('#BottleVariety').val();
		if ((winery == '') && (label == '') && (variety == '')) {
			var answer = confirm('You don\'t seem to have much detail entered here. Are you sure you want to continue?');
			return answer;
		}
	});
/* end check */	

/* tell friend dialog box */
	var dialog = $('#tell_friend')
		.dialog({
			autoOpen: false,
			title: 'Tell a friend about Sommelier.net.au',
			width: 300,
			modal: true,
			hide: 'explode'
		});
 	$('#open_tell_friend').click(function() {
		dialog.dialog('open');
	});
	$('#UserTellFriendForm').submit(function() {
		if ($('#UserTellFriendForm').valid()){
			$.post('/users/tell_friend/', $('#UserTellFriendForm').serialize(),
				dialog.dialog('close')
			);
			return false;
		}
		else{
			return false;
		}
	});
	
/* end tell friend dialog box */

/* Stars */

	$('.rating').rating({
		split: 2
	});
	$('.rating_single').rating({
		split: 2,
		readOnly: true
	});

/* Stars End */

}); // end ready
