$(document).ready(function(){

	$(".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, 
			}
		}
	});
	$('#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'
		}
	});
/* end login form validation */
	
/* Ajax Autocompletes */
	$('#WineryWineryName').autocomplete({
		source: '/Wineries/ajax_auto_complete',
		select: function(event, ui){
			$('#WineryWineryName').attr('value', ui.item.id);
			$('#BottleWineryId').attr('value', ui.item.id);
		}
	});
	$('#RegionRegionName').autocomplete({
		source: '/Regions/ajax_auto_complete',
		select: function(event, ui){
			$('#RegionRegionName').attr('value', ui.item.id);
			$('#BottleRegionId').attr('value', ui.item.id);
		}
	});
	$('#BottleVariety').autocomplete({
		source: '/Varieties/ajax_auto_complete',
		select: function(event, ui){
			$('#BottleVariety').attr('value', ui.item.id);
			$('#BottleVarietyId').attr('value', ui.item.id);
		}
	});
/* 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 */

/***********************

	Redundant?

****************************/


/* 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 */	

/* Stars */

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

/* user homepage buttons */
	$(".button").button();
	$(".button_home").button();
	$(".add_button").button();
	$(".cellar_button").button();
	$(".archive_button").button();
	$(".user_update_button").button();
	$(".wishlist_button").button();
	$(".home_values").button({
		enable: false
	});
	
/* end user homepage buttons */

/***********************

	Not sure what this does

****************************

	$('.edit_left').click(function(event){
		event.preventDefault();
		var blah = $('h3').find().prevObject;
		var second_last = parseInt(blah.length) - 2;
		$('.tasting').accordion({'active': second_last});
	}); */

	$('.bottle_main, .tasting_note_main').live('click', function(){
		$(this).next('.bottle_details, .tasting_note_details').toggle('slow');
	});
	
/* display bottle details and tasting notes */

/* Variety Search */
	$('.variety_search').keyup(function(input){
		console.log($('.variety_search').val());
		if(input.keyCode == 13){
			$(this).closest('form').submit();
		}
	});
	
/* add tasting note */
	$('.add_tasting_note').click(function(){
		$('.tasting_note_dialog').dialog({
			modal : true,
			buttons: {
				'Add tasting note': function(){
					$.post('/descriptions/add', $('#DescriptionAddForm').serialize(), function(){
						$.post('/descriptions/new_description/' + $('.tasting_note_dialog').attr('value'), function(data){
							var toHighlight = $('.add_tasting_note').after(data);
							$(toHighlight).next().children().effect('highlight', {color : '#fff'}, 2000);
						});
					});
					$(this).dialog('close');
				},
				'cancel' : function(){
					$(this).dialog('close');
				}
			},
			title : 'Add new tasting note',
			width: 600,
			height: 500,
			position: 'top'
		});
		
		// add content to dialog
		$.post('/descriptions/add', {id : $('.tasting_note_dialog').attr('value')}, function(data){
			$('.tasting_note_dialog').html(data);
		})
	});
	
}); // end ready
