

//////////////////////////////////
// My Cellar Add Wine Functions //
//////////////////////////////////

//define global variables
note_number = "";
note_text = "";
note_height = "";

function show_note_edit(wine_id)
{
	//figure out ids
	note_id = "#"+wine_id + "_notes";
	note_wrapper = "#"+wine_id + "_wrapper";

	// check if a note is already open
	if (note_number != "")
	{
		if (note_number != wine_id)
		{
			if (confirm("You are still editing another note. If you continue changes to that note will not be saved."))
			{
				hide_note();
				add_note(wine_id);
			}
		}
	} else
	{
		add_note(wine_id);
	}
}

function hide_note()
{
	//figure out ids
	note_id = "#"+note_number + "_notes";
	note_wrapper = "#"+note_number + "_wrapper";


	// hide notes
	$(note_wrapper).slideUp("fast", function() {
			
			
			$(note_id).slideDown("fast");// clear existing note text
			$(note_id).html("");
			$(note_id).html(note_text);
			$(note_wrapper).slideDown("fast");	// empty global variables
	note_number = "";
	note_text = "";

		});

	


}

function add_note(wine_id) 
{
	// save_globals
	note_number = wine_id;

	//figure out ids
	note_id = "#"+wine_id + "_notes";
	note_wrapper = "#"+wine_id + "_wrapper";

	
	// get existing notes
	notes = $(note_id).html();
	note_text = notes;

	// create edit box html
	edit_box = "<span style='display: block; float: left; width: 15px;' id='"+wine_id+"_error'></span><textarea class='my_cellar_editbox' style='height: 90px; width: 600px;' id='"+note_id+"_edit'>"+notes+"</textarea><br /><input type='button' value='Cancel' onclick=\"javascript: hide_note()\"'/><input type='button' value='Save Changes' onclick=\"javascript: save_notes('"+wine_id+"')\"'/><br style='clear: both;' />";

	//note_height = $(note_wrapper).height();	
	//$(note_wrapper).animate({ height: '120px'}, 'slow');

	$(note_wrapper).slideUp("fast", function() { $(note_id).html(edit_box); $(note_wrapper).slideDown("fast"); });
}


function save_notes(note_id)
{
	// get note_text
	note = $('.my_cellar_editbox').val();
	// get wine_id
	wine_id = note_id;
	// get message holder
	note_wrapper = "#"+note_id + "_error";


	//show loading image
	$(note_wrapper).html('<img src="'+base_url+'/skins/'+skin+'/js/ajax-loader.gif" height="16" width="16" /><br/>');	

	// build query string
	query = "wine_id="+wine_id;
	query += "&note="+note;
	query += "&function=add_note";


	//submit ajax query
	$.ajax({
	  url: base_url+'/sources/ajax/mycellar.php',
	  data: query,
	  dataType: 'json',
	  type: 'post',
	  success: function (j) 
			{
				if (j.ok) {	
					// update global variable
					note_text = j.msg;
					//run hide function
					hide_note();

					

				} else {	
					alert("Error:"+j.msg);
					
				}
			}
	});
}


function adjust_qty(qty_id)
{

	// get wine_id
	wine_id = qty_id;
	// get qty holder
	qty_holder = "#"+wine_id + "_new_qty";
	// get qty button
	qty_button = "#"+wine_id + "_qty_button";
	// get qty display
	qty_display = "#"+wine_id + "_quantity";

	//show loading image
	$(qty_button).attr('src', base_url+'/skins/'+skin+'/js/ajax-loader.gif');	

	// build query string
	query = "wine_id="+wine_id;
	query += "&new_qty="+$(qty_holder).val();
	query += "&function=adjust_qty";


	//submit ajax query
	$.ajax({
	  url: base_url+'/sources/ajax/mycellar.php',
	  data: query,
	  dataType: 'json',
	  type: 'post',
	  success: function (j) 
			{
				if (j.ok) {	

					// update display
					$(qty_display).html(j.msg);

					// remove loading image
					$(qty_button).attr('src', base_url+'/skins/'+skin+'/images/icons/disk.png');	

				} else {	
					alert("Error:"+j.msg);
					
				}
			}
	});
}

function delete_wine(wine_id)
{

	// confirm delete
	if (confirm("Are you sure you what to delete this wine from your cellar? Deleting this wine will permanantly delete any notes you have written and any quantity counts."))
		{
		// get delete button
		del_button = "#"+wine_id + "_delete_button";

		// get wine_row
		wine_row = "#"+wine_id + "_row";

		//show loading image
		$(del_button).attr('src', base_url+'/skins/'+skin+'/js/ajax-loader.gif');	

		// build query string
		query = "wine_id="+wine_id;
		query += "&function=remove";


		//submit ajax query
		$.ajax({
		  url: base_url+'/sources/ajax/mycellar.php',
		  data: query,
		  dataType: 'json',
		  type: 'post',
		  success: function (j) 
				{
					if (j.ok) {	

						// Remove out wine row
						//$(wine_row).fadeOut("slow", function() {

								$("#my_cellar").html(j.html);
								$("#my_cellar").slideDown();

						//});

					} else {	
						alert("Error:"+j.msg);
					
					}
				}
		});
	}
}
