// St-Germain Javascript utility functions

$(function () {

	// 	Home page flash insertion
	$("#home_flash").flash( {   
			swf: '/_flash/can-can3C.swf',   
			width: 533,
			height: 200
	});
	
	//		Home page rollovers
	$("#home-lebar").hover( 
		function() {	$(this).attr("src", $(this).attr("src").replace("_off.gif","_on.gif"))	},
		function() {	$(this).attr("src", $(this).attr("src").replace("_on.gif","_off.gif"))	}
	)
	$("#home-cancan").hover( 
		function() {	$(this).attr("src", $(this).attr("src").replace("_off.gif","_on.gif"))	},
		function() {	$(this).attr("src", $(this).attr("src").replace("_on.gif","_off.gif"))	}
	)

	// Validate the signup form
	$("#lebar").validate({
	
		rules: {
			firstname: { required: true }
		}
	
	
	})
	
		/*															!Classic Cocktails */
	/**********************************
	**
	**		CLASSIC COCKTAILS
	**
	*********************************
	//			Get cocktails
	//				Classic = category_id of 1
	//
	if ($("#classic").length > 0) {
		drinks_per_page = 5
		current_page = 1
		
		$.getJSON("/cocktails/select_cocktails_from_category.php", {category_id:1, limit:drinks_per_page, page:current_page}, function(json) {
			
			//		Get List of cocktails
			//
			list_html = ""
			$.each(json, function(i,val) {
				list_html += '<div class="cocktail_name">' + json[i].name + "\n"
				list_html += '	<div class="cocktail_id">' + json[i].id + '</div>\n'
				list_html += '</div>\n'
			})
			$("#classic_list").html(list_html)
			
			
			// 		Get full data for each cocktail
			//
			$.each(json, function(i,val) {
				$("#classic_cocktail").append('<div id="cocktail_' + json[i].id + '" class="main_cocktail"></div>\n')
				
				getFullCocktailInfo(json[i].id)		// works
				//getRecipeDataAndDisplay(json[i].id)		// testing
				
			})	
			
			$(".cocktail_name:first").trigger("click")		
		})
		
		// 		Start highlight
		//			Can't use "hover" because these elements are created after document loads
		//
		$(".cocktail_name[class!=cocktail_name current]").live("mouseover", function() { $(this).addClass("on"); })
		$(".cocktail_name").live("mouseout", function() { $(this).removeClass("on") })
		
		
		//			User clicks on cocktail name on side list
		//
		$(".cocktail_name").live("click", function() {

			$(".main_cocktail").hide()
			cocktail_id = $(this).find(".cocktail_id").text()
			$("#cocktail_"+cocktail_id).show()
			
			$(".cocktail_name").removeClass("current")
			$(this).addClass("current")
			$(this).removeClass("on")
			
			//alert("click: " + cocktail_id)
		})
		
		$("#recipe_page").hover(
			function() { 
				src = $(this).attr("src")
				src = src.replace("_off","_on")
				src = $(this).attr("src",src)
			},
			function() { 
				src = $(this).attr("src")
				src = src.replace("_on","_off")
				src = $(this).attr("src",src)
			}
		)
		
		$("#recipe_page").click(function() {
			$(".cocktail_name").fadeOut("fast")
			
			
			current_page = (current_page==4) ? 1 : current_page + 1
			next_page = (current_page==4) ? 1 : current_page + 1
			
			getUpdatedCocktailList()
			
			src = $(this).attr("src")
			src = src.replace(current_page,next_page)
			src = $(this).attr("src",src)
		})
		
		function getUpdatedCocktailList() {
		
			$.getJSON("/cocktails/select_cocktails_from_category.php", {category_id:1, limit:drinks_per_page, page:current_page}, function(json) {
			
				//		Get List of cocktails
				//
				list_html = ""
				$.each(json, function(i,val) {
					list_html += '<div class="cocktail_name">' + json[i].name + "\n"
					list_html += '	<div class="cocktail_id">' + json[i].id + '</div>\n'
					list_html += '</div>\n'
				})
				$("#classic_list").html(list_html)
				$(".cocktail_name").fadeIn("fast")
				
				$(".cocktail_name:first").trigger("click")	
				
			})
		
		}
	}
	
	*/
	
	
	/*															!Valentine's Cocktails */
	/**********************************
	**
	**		VALENTINES COCKTAILS
	**
	**********************************/
	//
	//			Get cocktails
	//				Valentines = category_id of 2
	//
	if ($("#valentines").length > 0) {
		$.getJSON("/cocktails/select_cocktails_from_category.php", {category_id:2}, function(json) {
			
			//		Get List of cocktails
			//
			list_html = ""
			$.each(json, function(i,val) {
				list_html += '<div class="cocktail_name">' + json[i].name
				list_html += '	<div class="cocktail_id">' + json[i].id + '</div>'
				list_html += '	</div>\n'
			})
			$("#valentines_list").html(list_html)
			
			// 		Get full data for each cocktail
			//
			$.each(json, function(i,val) {
				$("#valentines_cocktail").append('<div id="cocktail_' + json[i].id + '" class="main_cocktail"></div>\n')
				
				// works
				getFullCocktailInfo(json[i].id)
				
				// testing
				//getRecipeDataAndDisplay(json[i].id)
				
			})		
			
			$(".cocktail_name:first").trigger("click")		
		})
		
		// 		Start highlight
		//			Can't use "hover" because these elements are created after document loads
		//
		$(".cocktail_name").live("mouseover", function() { $(this).addClass("on"); })
		$(".cocktail_name").live("mouseout", function() { $(this).removeClass("on") })
		
		
		//			User clicks on cocktail name on side list
		//
		$(".cocktail_name").live("click", function() {
			$(".main_cocktail").hide()
			cocktail_id = $(this).find(".cocktail_id").text()
			$("#cocktail_"+cocktail_id).show()
		})
	}
	
	
/* Can-Can winner recipes 	 */
	$("#winner1").click(function() {
		$("#test").load("can-can-winner-1.php", {}, function() {
			$("#test").fadeIn( function() {
				$("#recipe_popup").prepend('<img src="../_images/buttons/close.png" alt="close" width="16" height="16" class="close"/>')
			})
		})
	})
	$("#winner2").click(function() {
		$("#test").load("can-can-winner-2.php", {}, function() {
			$("#test").fadeIn( function() {
				$("#recipe_popup").prepend('<img src="../_images/buttons/close.png" alt="close" width="16" height="16" class="close"/>')
			})
		})
	})
	$("#winner3").click(function() {
		$("#test").load("can-can-winner-3.php", {}, function() {
			$("#test").fadeIn( function() {
				$("#recipe_popup").prepend('<img src="../_images/buttons/close.png" alt="close" width="16" height="16" class="close"/>')
			})
		})
	})

	$("#test").live("click", function() {
//		$(this).fadeOut()
	})

	
	/*															!Where to find St-Germain */
	/**********************************
	**
	**		WHERE TO FIND ST-GERMAIN
	**
	**********************************/
	if($("#find").length>0) {
		$(".find_content select[name!=dist]").css("width","135px")
	
		if (isIE()) {
			$("#float").css("width","900px")
		}	
	}
	
	$(".submit").click(function() {
		$('<img src="/_images/ajax_loader_main.gif" alt="ajax_loader_main" width="15" height="15" class="ajax_loader"/>').insertAfter(this)
		$(this).hide()
		
		var zip = $(".find_content input[name=zip]").val()
		var dist = $(".find_content select[name=dist]").val()
		var units = $(".find_content input[name=units]").val()
		var state = $(".find_content select[name=state]").val()
		var city = $(".find_content select[name=city]").val()
		
		$("#test").load("find/locate_ajax.php", { zip:zip, dist:dist, units:units, state:state, city:city}, function() {
			$("img[src*=ajax_loader]").remove()
			
			$("#test").fadeIn( function() {
				if ($(".locator_results").length == 0) {
					error_ms = $(this).html()
					if	(error_ms =="") {
						$(this).html('<div class="locator_results"><i>Zoot alors!</i><p>We need more information for a proper search. Please try again.</p></div>')
					} else {
						$(this).html('<div class="locator_results"><i>Quel dommage!</i><p>' + error_ms + '</p></div>')
					}
				}
				$(".locator_results").prepend('<img src="../_images/buttons/close.png" alt="close" width="16" height="16" class="close"/>')
				$(".submit").show()
			})
		})

	})
	
	//		Deal with Locator page links, which try to refresh the page
	//
	$("input.submitLinkCurrentPage").live("click", function(e) {
		e.preventDefault()
	})

	$("input.submitLink").live("click", function(e) {
		e.preventDefault()
		// alert("next")
//		$(this).addClass("input_on")
		
		var zip = $(".find_content input[name=zip]").val()
		var dist = $(".find_content select[name=dist]").val()
		var units = $(".find_content input[name=units]").val()
		var state = $(".find_content select[name=state]").val()
		var city = $(".find_content select[name=city]").val()
		var page = $(this).parents("form").find("input[name=page]").val()
		
		$("#test").load("find/locate_ajax.php", { zip:zip, dist:dist, units:units, state:state, city:city, page:page}, function() {
			$(".locator_results").prepend('<img src="../_images/buttons/close.png" alt="close" width="16" height="16" class="close"/>')
			$(this).removeClass("input_on")
		})
	})

	$(".close").live("click", function() {
		$(this).parent().fadeOut()
		$(this).parents("#test").fadeOut()
	})
	
	
	/*															!CanCan Winners */
	/**********************************
	**
	**		CAN-CAN WINNERS
	**
	**********************************/
	var cancan 				= new Array()
	var cancan_contests 	= new Array()
	var cancan_contest_ids 	= new Array()
	var cancan_cocktail_ids	= new Array()
	var cancan_pros 		= new Array()
	var cancan_footnotes	= new Array()
	var cancan_winning_cocktails
	var cancan_winning_recipes
	
	var machine_click_allowed = false
	var recipes_loading = true

	if ($("#cancan_right").length > 0) {

		var contest_ids 	= new Array()
		var cocktail_ids	= new Array()
		
		$.getJSON("/cancan/select_all_contests_json.php", getContestInformation)
	}
	
	
	
	//		User chooses a different contest to look at
	//
	$("#contest_list").change( function() {
		$("#current_contest").html(cancan_contests[$(this).val()]) 
		$("#current_contest_id").html($(this).val())
		displayWinners(contest_id=$(this).val())
		$(".winner:first").trigger("click")
	})
	
	$(".winner").live("click", function() {
		//alert("click")
		if (machine_click_allowed || !recipes_loading) {
		
			// update text
			$(".winner").removeClass("on")
			$(this).addClass("on")
		
			// reset all images
			$.each( $(".place img"), function() {
				src = $(this).attr("src")
				src = src.replace("_on","_off")
				$(this).attr("src",src)
			})
			
			// update cuurent place image
			src = $(this).find(".place img").attr("src")
			src = src.replace("off","on")
			$(this).find(".place img").attr("src",src)
		
			getRecipeDataAndDisplay($(this).find(".cocktail_id").text())
			
			user_click_allowed = true
		}
	})
	
	$("#prev").click( function() {

		if ($("#contest_list option:selected").length == 0) {
			$("#contest_list option:eq(0)").next().attr("selected","selected")
		} else {
			$("#contest_list option:selected").next().attr("selected","selected")
		}
		updateContest()
		
		if (!$("#contest_list option:selected").next().val()){
			$(this).fadeOut()
		}
		
		if ($("#next:hidden")){
			$("#next").fadeIn()
		}
	})
	$("#next").click( function() {

		$("#contest_list option:selected").prev().attr("selected","selected")
		updateContest()
		
		if (!$("#contest_list option:selected").prev().val()){
			$(this).fadeOut()
		}
		if ($("#prev:hidden")){
			$("#prev").fadeIn()
		}
	})
	
	
	/*															!Cocktail Search */
	/**********************************
	**
	**		COCKTAIL SEARCH
	**
	**********************************/
	$("#search_submit").click(function(e) {
		e.preventDefault();
		search_term = $("input[name=cocktail_search]").val()
		searchForCocktails(search_term)
	})
	
	$("#alcohol_search_submit").click(function(e) {
		e.preventDefault();
		search_term = $("select[name=mixers]").val()
		searchForCocktails(search_term)
	})

	$(".cocktail_result_item").live("click", function(e){
		e.preventDefault();
		a = $(this).attr("id")
		boom = a.split("_")
		cocktail_id = boom[boom.length-1]

		$("#recipe").fadeOut("fast", function() {
			$("#ajax_loader").fadeIn("fast")
		})
		
		getRecipeDataAndDisplay(cocktail_id)
	})
	
	

	/**********************************
	**
	**		FUNCTIONS
	**
	**********************************/

	
	
	function getContestInformation(json) {
		cancan = json
		
		//		Populate dropdown and display contest name
		//
		var option_html = ""
		var is_first = true
		$.each(cancan, function(i, contest) {
		
			//		Create contest lists
			//
			var already_in_list = false
			// loop over contest_ids and see if we've already added this one
			$.each(contest_ids, function(j,contest_id) {
				if (contest.contest_id == contest_id) {
					already_in_list = true
					return false
				}
			})
			
			if (!already_in_list) {
				option_html += '<option value="' + contest.contest_id + '">' + contest.name + '</option>\n'
				if (is_first) { 
					$("#current_contest").html(contest.name) 
					$("#current_contest_id").html(contest.contest_id)
					is_first=false; 
				}
				contest_ids.push(contest.contest_id)
				cancan_contests[contest.contest_id] = contest.name
			}
			
			//		Create cocktail list
			//
			cocktail_ids.push(json[i].cocktail_id)
		})
		
		$("#contest_list").html(option_html)
		cocktail_ids = cleanArray(cocktail_ids)
		contest_ids = cleanArray(contest_ids)

		getProsFromDrinkList(cocktail_ids)		//	set cancan_pros
		getCanCanRecipesAndFootnotes(cocktail_ids[0])

	
	}
	

	
	
	
	function getProsFromDrinkList(cocktail_ids)	{
		
		cocktail_ids_string = cocktail_ids.join(",")
		
		$.ajax({		
			url: "/cancan/select_pros_from_drink_list.php",
			dataType: "json",
			data: {cocktail_ids_string:cocktail_ids_string},
			error: function(xhr,error) {
				alert("error: " + error)
				$("#log").html("readyState: "+xhr.readyState+"<br>status: "+xhr.status + "<br>responseText: "+xhr.responseText);
			},
			parseError: function() {
				alert("parserror")
			},
			success: function(pro_data) {
				$.each(pro_data, function(i,val){
					text = pro_data[i].cocktail_id + "=" + pro_data[i].pro_name		
					keyValuePair = text.split('=');		
					cancan_pros[ keyValuePair[0] ] = new Array()
					cancan_pros[ keyValuePair[0]]['name'] = keyValuePair[1]
					cancan_pros[ keyValuePair[0]]['bar'] = 	pro_data[i].bar
					cancan_pros[ keyValuePair[0]]['address'] = 	pro_data[i].address
					cancan_pros[ keyValuePair[0]]['city'] = pro_data[i].city
					cancan_pros[ keyValuePair[0]]['state'] = pro_data[i].state
					cancan_pros[ keyValuePair[0]]['country'] = pro_data[i].country
				})
				displayWinners($("#current_contest_id").text())	
			}
		})
	}
	
	function getCanCanRecipesAndFootnotes(cocktail_ids_arg){

		//		Get all recipes of winning cocktails
		//
		var recurse = false
		if (cocktail_ids_arg.length > 0) {
			cocktail_ids_string = cocktail_ids_arg.join(",")
			recipes_loading = true
		} else {
			cocktail_ids_string = cocktail_ids_arg
			recurse = true
		}	

		$.getJSON("/cancan/select_recipes_from_drink_list.php", {cocktail_ids_string:cocktail_ids_string}, function(recipe_data) {
			
			cancan_winning_recipes = recipe_data
			
			if (recurse) {
				machine_click_allowed = true
				$(".winner:first").trigger("click")
				machine_click_allowed = false
				  
				getCanCanRecipesAndFootnotes(cocktail_ids)
			} else {
				recipes_loading = false
				$('#winners').animate({ opacity: 1 }, 500, function() {});
				$(".winner").css("cursor","pointer")
				$(".winner:first").trigger("click")
			}
			
			$.getJSON("/cancan/select_footnotes_from_drink_list.php", {cocktail_ids_string:cocktail_ids_string}, function(footnotes_data) {		
				if (footnotes_data != null) {
					$.each(footnotes_data, function(i,val) {
						text = footnotes_data[i].cocktail_id + "=" + footnotes_data[i].footnote
						keyValuePair = text.split('=');		
						if (typeof cancan_footnotes[ keyValuePair[0] ] != "array") {
							cancan_footnotes[ keyValuePair[0] ] = new Array()
						}
						cancan_footnotes[ keyValuePair[0] ].push(keyValuePair[1])
					})
				}	
			})	
		})	
	}	
	
		
	
	function updateContest () {
		$("#current_contest_id").html($("#contest_list").val())	
		$("#current_contest").html($("#contest_list :selected").text()) 
		displayWinners($("#current_contest_id").text() )
		$(".winner:first").trigger("click")
	}
	
	function displayWinners(contest_id){
		
		//		clear out winner's circle
		$("#winners").html("")
		winner_html = ''
		
		//		Build winner HTML
		$.each(cancan, function(i,val){
			if (cancan[i].contest_id == contest_id){
				winner_html += '<div class="winner">'
				
				//		see if place is Honorable Mention or not
				if (/\d/.test(cancan[i].place )) {
					winner_html += '	<div class="place"><img src="/_images/cancan/' + cancan[i].place + '_off.gif"></div>'
				} else {
					winner_html += '	<div class="place"><img src="/_images/cancan/hon_off.gif"></div>'
				}
				winner_html += '	<div class="drink_name">' + cancan[i].cocktail_name + "</div>"	
				winner_html += '	<div class="pro_name">by ' + formatName(cancan_pros[cancan[i].cocktail_id]['name']) + "</div>"			
				winner_html += '	<div class="cocktail_id">' + cancan[i].cocktail_id + "</div>"
				winner_html += '</div>\n'
			}
		})
		
		$("#winners").append(winner_html)	
		
		if (recipes_loading){
			$('#winners').animate({ opacity: 0.3 }, 100, function() {});
		}
	}
	
		
	function formatName(name) {
		if (name != null) {
			name_array = name.split(" ")
			for (i=0; i<name_array.length; i++) {
				name_array[i] = name_array[i].toLowerCase()
				name_array[i] = name_array[i].slice(0,1).toUpperCase() + name_array[i].slice(1);
			}
			
			return name_array.join(" ")
		} else {
			return false
		}
	}
	
	
	function getFullCocktailInfo(cocktail_id){
		
		// 		Display Recipe
		//
		$.getJSON("/cocktails/select_single_cocktail.php", { cocktail_id:cocktail_id } , function(data) {
			cocktail_data = data[0]
			
			cocktail_html = '<img class="drink"><table cellpadding="0" cellspacing="0" border="0">\n'
			cocktail_html += '<tr valign="top">\n<td colspan="2"><img class="title"></td>\n</tr>\n'
			cocktail_html += '<tr valign="top"><td>'
			
			recipe_html = '<table cellpadding="0" cellspacing="0" border="0" class="recipe">\n'
			recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_top"></td></tr>\n'
			$.each(data, function(i, val) {
			
				quantity = cleanUpQuantity(data[i].how_much)
				
				recipe_html += "	<tr>\n"
				recipe_html += '		<td class="quantity">'+ quantity + "</td>\n"
				recipe_html += '		<td class="ingredient">'+data[i].ingredient + "</td>\n"
				recipe_html += "	</tr>\n"
			})
			recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_bottom"></td></tr>\n'
			recipe_html += '<tr><td colspan="2" class="no_border">\n'
			recipe_html += '	<div class="method"><span class="reciphead">Method:</span> ' + cocktail_data.method + '</div>\n'
			if (cocktail_data.variation != null) {
				recipe_html += '	<div class="variation"><span class="reciphead">Variation:</span> ' + cocktail_data.variation + '</div>\n'
			}
			recipe_html += '</td></tr>\n'			
			recipe_html += "</table>\n"
			cocktail_html += recipe_html + "</td></tr></table>"
			
			$("#cocktail_"+cocktail_id).append(cocktail_html)
			
			displayTitleImageFilename(cocktail_id,"recipes")
			//displayDrinkImageFilename(cocktail_id)

		})
		
		// 		Display Footnotes
		//
		$.getJSON("/cocktails/select_cocktail_footnotes.php", { cocktail_id:cocktail_id } , function(data) {
			footnote_html = ""
			$.each(data, function(i, val) {
				footnote_html += data[i].footnote + '<br>'
			})
			if (footnote_html.length > 0) {
				footnote_html = '<div class="footnotes">' + footnote_html  + '</div>\n'
				$("#cocktail_"+cocktail_id + " .recipe td:last").append(footnote_html)
			}
		})
	}
	
	function displayTitleImageFilename(cocktail_id, directory) {
		$.getJSON("/cocktails/get_image_filenames_from_directory.php", {images_directory:"/_images/"+directory+"/names", cocktail_id:cocktail_id}, function(imgJSON) {
			$("#cocktail_"+cocktail_id + " img.title").attr("src","/_images/"+directory+"/names" + imgJSON)
			$("#cocktail_"+cocktail_id + " img.title").show()
			$("#cocktail_"+cocktail_id + " img.title").parents("td").css("border",0)
		})
	}
	
	function displayDrinkImageFilename(cocktail_id) {
		$.getJSON("/cocktails/get_image_filenames_from_directory.php", {images_directory:"/_images/valentines/drinks", cocktail_id:cocktail_id}, function(imgJSON) {
			$("#cocktail_"+cocktail_id + " img.drink").attr("src","/_images/valentines/drinks/" + imgJSON)
			$("#cocktail_"+cocktail_id + " img.drink").show()
			$("#cocktail_"+cocktail_id + " img.drink").css("margin-left","20px")
			$("#cocktail_"+cocktail_id + " img.drink").parents("td").css("border",0)
		})
	}
	
	function isIE() {
	  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	}
	
	function cleanArray(actual){
 		var newArray = new Array();
  		for(var i = 0; i<actual.length; i++){
      		if (actual[i]){
        		newArray.push(actual[i]);
    		}
  		}
  		return newArray;
	}
	
	function cleanUpQuantity(quantity){
		updated_quantity = quantity.replace("1/2", "<span class='fraction'>&frac12;</span>")
		updated_quantity = updated_quantity.replace("1/4", "<span class='fraction'>&frac14;</span>")
		updated_quantity = updated_quantity.replace("3/4", "<span class='fraction'>&frac34;")
		
		updated_quantity = updated_quantity.replace(".25", "<span class='fraction'>&frac14;</span>")
		updated_quantity = updated_quantity.replace(".75", "<span class='fraction'>&frac34;</span>")
		updated_quantity = updated_quantity.replace(".5", "<span class='fraction'>&frac12;</span>")
		
		updated_quantity = updated_quantity.replace("1 oz", "1 part")
		updated_quantity = updated_quantity.replace("oz.", "parts")
		updated_quantity = updated_quantity.replace("oz", "parts")
		updated_quantity = updated_quantity.replace("ounces", "parts")
		updated_quantity = updated_quantity.replace("ounce", "part")
		
		return updated_quantity
	
	}
	
	function searchForCocktails(search_term) {
		//$("#results").load("/cocktails/select_cocktails_from_search.php",{ search_term:search_term })	// for testing
		
		$.ajax({		
			url: "/cocktails/select_cocktails_from_search.php",
			dataType: "json",
			data: { search_term:search_term },
			success: function(cocktail_json) {
				result_html = ""
				$.each(cocktail_json, function(i,val){
					result_html += '<a href="" id="cocktail_search_' + cocktail_json[i].id + '" class="cocktail_result_item">' + cocktail_json[i].name + '</a><br>\n'
				})
				
				$("#results").html(result_html)

			}
		})
	}
	
	// 	Set cocktail object to hold all information about a particular cocktail
	//		Used for recipe displays
	//
	cocktail = new Object()
		
	function getRecipeDataAndDisplay(cocktail_id) {
	
		// want to reset this before every recipe grab
		cocktail.recipes = new Array()
		cocktail.footnotes = new Array()
		cocktail.mixologist = ""
		cocktail.mixologist_info = ""
		
		/***
		**		Get data
		***/
		// 	See if recipe already exists in memory
		//
		if (cancan_winning_recipes != undefined && cancan_winning_recipes.length > 0) {
			$.each(cancan_winning_recipes, function(i, val) {
				if (cancan_winning_recipes[i].id == cocktail_id) {
				
					cocktail.name = cancan_winning_recipes[i].name
					cocktail.method = cancan_winning_recipes[i].method
					if (cancan_winning_recipes[i].variation != null) {
						cocktail.variation = cancan_winning_recipes[i].variation
					}				
					if (cancan_footnotes[cocktail_id] != undefined){
						cocktail.footnotes[0] = cancan_footnotes[cocktail_id]
					}
					
					cocktail.mixologist = formatName(cancan_pros[cocktail_id]['name'])
					cocktail.recipes[i] = new Array()	
					cocktail.recipes[i]['quantity'] = cancan_winning_recipes[i].how_much
					cocktail.recipes[i]['ingredient'] = cancan_winning_recipes[i].ingredient
					cocktail.mixologist_info = ""
					if (cancan_pros[cocktail_id]['bar'] != null && cancan_pros[cocktail_id]['bar'] != "") { 
						cocktail.mixologist_info += cancan_pros[cocktail_id]['bar'] + "<br>"
					}
					if (cancan_pros[cocktail_id]['address'] != null) { 
						cocktail.mixologist_info += formatName(cancan_pros[cocktail_id]['address']) + "<br>"
					}
					if (cancan_pros[cocktail_id]['city'] != null) { 
						cocktail.mixologist_info += formatName(cancan_pros[cocktail_id]['city']) + ", " + cancan_pros[cocktail_id]['state'] + "<br>"
					}
					cocktail.mixologist_info += formatName(cancan_pros[cocktail_id]['country'])
				}
			})
			
			$("#recipe_box img[src*=ajax]").remove()
			
			displayRecipe(cocktail)
		}
		else
		{
			// Get data from DB
			//
			//alert("cocktail_id: " + cocktail_id)
			$.getJSON("/cocktails/select_single_cocktail_2.php", { cocktail_id:cocktail_id } , function(data) {
				cocktail_data = data[0]
				
				// 	Name, Method, and Variation
				//
				cocktail.name = cocktail_data.name
				cocktail.method = cocktail_data.method
				if (cocktail_data.variation != null) {
					cocktail.variation = cocktail_data.variation
				}
				
				// 	Mixologist
				//
				$.each(data, function(i, val){
					if (data[i].mixologist != null) {
						cocktail.mixologist = formatName(data[data.length-1].mixologist)
						return false		// break out of loop
					}
				}) 

				// Ingredients
				//
				$.each(data, function(i, val) {	
					if (data[i].how_much != undefined) {	
						cocktail.recipes[i] = new Array()			
						cocktail.recipes[i]['quantity']  =  data[i].how_much
						cocktail.recipes[i]['ingredient'] = data[i].ingredient
					}
				})

				//	Footnotes
				//
				$.getJSON("/cancan/select_footnotes_from_drink_list.php", {cocktail_ids_string:cocktail_id}, function(footnotes) {
					if (footnotes != null) {
						$.each(footnotes, function(i,val) {
							cocktail.footnotes[i] = footnotes[i].footnote
						})
					}
					
					//	Do the actual display
					//
					displayRecipe(cocktail)
				})
			})	
		}
	}
	
	function displayRecipe(cocktail){
		
		// Name
		//
		$("#cocktail_name").html(cocktail.name)
		
		// Byline
		//
		if (cocktail.mixologist != undefined && cocktail.mixologist != "") {
			$("#byline").html("by " + cocktail.mixologist)
		} else {
			$("#byline").html("")
		}
			
		// Recipe
		//
		recipe_html = '<table cellpadding="0" cellspacing="0" border="0" class="recipe">\n'
		recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_top"></td></tr>\n'
		for (var i in cocktail.recipes) {
			recipe_html += "	<tr>\n"
			recipe_html += '		<td class="quantity">'+ cleanUpQuantity(cocktail.recipes[i].quantity) + "</td>\n"
			recipe_html += '		<td class="ingredient">'+cocktail.recipes[i].ingredient + "</td>\n"
			recipe_html += "	</tr>\n"
		}
		recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_bottom"></td></tr>\n'
		recipe_html += '<tr><td colspan="2" class="no_border">\n'
		recipe_html += '</td></tr></table>\n'	
		$("#ingredients").html(recipe_html)
		
		// Method
		//
		$("#method").html('<span class="recipe_hd">Method:</span> ' + cocktail.method)
		
		//	Footnotes
		//
		footnote_html = ""
		for(i=0; i<cocktail.footnotes.length; i++) {
			footnote_html += cocktail.footnotes[i] + "<br>"
		}
		$("#footnotes").html(footnote_html)
		
		
		// 	Mixologist Address
		//
		if (cocktail.mixologist_info != null) {
			$("#mixologist").html('<div class="recipe_hd">Mixologist:</div>' + cocktail.mixologist + "<br>" + cocktail.mixologist_info)
		}
		
		$("#ajax_loader").fadeOut("fast", function() {
			$("#recipe").fadeIn("fast")
		})
	}
})


/*
	function displayRecipe(cocktail_id) 
	{
		
		$("#recipe_box img[src*=ajax]").remove()
		//$("#cocktail_name").html(cocktail_id)


		if ($("#byline").length > 0) {
			$("#byline").html("by " + formatName(cancan_pros[cocktail_id]['name']))
			mix_html = '<div class="recipe_hd">Mixologist:</div> '
			mix_html += formatName(cancan_pros[cocktail_id]['name']) + "<br>"
			
			if (cancan_pros[cocktail_id]['bar'] != null && cancan_pros[cocktail_id]['bar'] != "") { 
				mix_html += cancan_pros[cocktail_id]['bar'] + "<br>"
			}
			if (cancan_pros[cocktail_id]['address'] != null) { 
				mix_html += formatName(cancan_pros[cocktail_id]['address']) + "<br>"
			}
			if (cancan_pros[cocktail_id]['city'] != null) { 
				mix_html += formatName(cancan_pros[cocktail_id]['city']) + ", " + cancan_pros[cocktail_id]['state'] + "<br>"
			}
			mix_html += formatName(cancan_pros[cocktail_id]['country'])
			$("#mixologist").html(mix_html)
		}
		

		recipe_html = '<table cellpadding="0" cellspacing="0" border="0" class="recipe">\n'
		recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_top"></td></tr>\n'
			
		$.each(cancan_winning_recipes, function(i, val) {
			if (cancan_winning_recipes[i].id == cocktail_id) {
				$("#cocktail_name").html(cancan_winning_recipes[i].name)
				
				$("#method").html('<span class="recipe_hd">Method:</span> ' + cancan_winning_recipes[i].method)
				//$("#variation").html('<span class="recipe_hd">Variation:</span> ' + cancan_winning_recipes[i].variation)
				
				if (cancan_footnotes[cocktail_id] != undefined){
					$("#footnotes").html(cancan_footnotes[cocktail_id])
				} else {
					$("#footnotes").html('')
				}			
				
				quantity = cleanUpQuantity(cancan_winning_recipes[i].how_much)

				recipe_html += "	<tr>\n"
				recipe_html += '		<td class="quantity">'+ quantity + "</td>\n"
				recipe_html += '		<td class="ingredient">'+cancan_winning_recipes[i].ingredient + "</td>\n"
				recipe_html += "	</tr>\n"	
			}
		})
		
		recipe_html += '<tr><td colspan="2"><img src="/_images/clear.gif" class="recipe_bottom"></td></tr>\n'
		recipe_html += '<tr><td colspan="2" class="no_border">\n'
		recipe_html += '</td></tr></table>\n'	
		
		$("#ingredients").html(recipe_html)
	}
	*/

	/*
	function getRecipeFootnotesFromDrinkList(cocktail_ids_arg)	{
	
		//alert("getRecipeFootnotesFromDrinkList")
	
		// 	Allow for single cocktail id
		//
		if (cocktail_ids_arg.indexOf(",") > -1) {
			cocktail_ids_string = cocktail_ids_arg.join(",")
		} else {
			cocktail_ids_string = cocktail_ids_arg
		}	
		
		// Calling the alert below makes everything work. Comment it out, and footnotes disappear.
		// UPDATE: waiting several seconds is what works
		alert("cocktail_ids_string: " + cocktail_ids_string)
		alert_ms = ""
		$.getJSON("/cancan/select_footnotes_from_drink_list.php", {cocktail_ids_string:cocktail_ids_string}, function(footnotes_data) {

			alert("in footnotes JSON")
			
			if (footnotes_data != null) {
			
				alert("in footnotes JSON: " + footnotes_data.length)
				
				$.each(footnotes_data, function(i,val) {
					//alert("in loop")
					text = footnotes_data[i].cocktail_id + "=" + footnotes_data[i].footnote
					keyValuePair = text.split('=');		
					if (typeof cancan_footnotes[ keyValuePair[0] ] != "array") {
						cancan_footnotes[ keyValuePair[0] ] = new Array()
					}
					cancan_footnotes[ keyValuePair[0] ].push(keyValuePair[1])
					
					
					
					// for single-cocktail refactor
					//cocktail.footnotes[keyValuePair[0]] = keyValuePair[1]
					
					alert_ms += "cancan_footnotes[" + keyValuePair[0] + "] = " + cancan_footnotes[keyValuePair[0]] + "\n\n"
				})
				//alert("outside of footnotes loop")
				alert(alert_ms)
				//alert("cancan_footnotes[137] = " + cancan_footnotes[137])
			}
			
		})
	
	}
		*/