var loading = "<li><img src=\"http://v2.howhealthyismydrink.com/static/images/loader.gif\" class=\"loader\" alt=\"loading\" /> Please wait...</li>";
var loadingGray = "<li><img src=\"http://v2.howhealthyismydrink.com/static/images/loader-gray.gif\" class=\"loader\" alt=\"loading\" /> Please wait...</li>";
var currentUrl = '';
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function()
{
	var cssObj = { 'box-shadow' : '#666 0px 1px 1px', // Added when CSS3 is standard
		'-webkit-box-shadow' : '#666 0px 1px 1px', // Safari
		'-moz-box-shadow' : '#666 0px 1px 1px'}; // Firefox 3.5+
	$("#searchResults").css(cssObj);
	
	 $("#searchInput").blur(function(){
	 	$('#searchResults').fadeOut();
	 	currentUrl = '';
	 });
	
	$("a[rel^='prettyPhoto']").prettyPhoto();

	
	$("#searchInput").attr("autocomplete", "off");
	 
	$('input[title!=""]').hint();
	 
	 refreshIngredients();
});

if (document.images) {
    img1 = new Image();
    img1.src = "static/images/loader.gif";
    img2 = new Image();
    img2.src = "static/images/loader2.gif";
}

function measureLookup(e, id){
	var charCode;
	if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }
	if (charCode == 13) {
		changeMeasure(id);
		return false;
	}
	return false;
}

function lookup(e) {
	var charCode;
	if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }
	if (charCode == 13) {
		if(currentUrl == ''){
			addDrink();
			return false;
		}
		sendToSearch(currentUrl);
		currentUrl = '';
		return false;
	}
	if(charCode == 38){
		navigate('up');
		return false;
	}
	if(charCode == 40){
		navigate('down');
		return false;
	}
	for(var i = 0; i < $("#search ul li a").size(); i++) {
		$("#search ul li a").eq(i).data("number", i);
	}
	
	$("#search ul li a").hover(
		function () {
			currentSelection = $(this).data("number");
			setSelected(currentSelection);
		}, function() {
			$("#search ul li a").removeClass("highlight");
			currentUrl = '';
		}
	);
	
	var inputString = document.getElementById('searchInput').value;
	if(inputString.length == 0) {
		$('#searchResults').fadeOut();
	} else {
		$('#searchResults').html(loadingGray);
		$.get("lib/ajax/liveSearch.php", {s: ""+inputString+""}, function(data){
			$('#searchResults').fadeIn();
			$('#searchResults').html(data);
		});
	}
}

function navigate(direction) {
	// Check if any of the menu items is selected
	if($("#search ul li .highlight").size() == 0) {
		currentSelection = -1;
	}
	
	if(direction == 'up' && currentSelection != -1) {
		if(currentSelection != 0) {
			currentSelection--;
		}
	} else if (direction == 'down') {
		if(currentSelection != $("#search ul li").size() -1) {
			currentSelection++;
		}
	}
	setSelected(currentSelection);
}

function setSelected(menuitem) {
	$("#search ul li a").removeClass("highlight");
	$("#search ul li a").eq(menuitem).addClass("highlight");
	currentUrl = $("#search ul li a").eq(menuitem).html();
}

function sendToSearch(str){
	document.getElementById('searchInput').value = str;
	$('#searchResults').fadeOut();
	return false;
}

function clearBox(){
	document.getElementById('searchInput').value = '';
	$('#searchResults').fadeOut();
	return false;
}

function toggleChangeMeasure(id) {
	closeList(id);
	$("#cm"+id).slideToggle("fast",function(){
		$("#m"+id).select();
	});
}

function showList(id) {
	$.get("lib/ajax/getMeasures.php", {id: ""+id+""}, function(data){
		$("#li"+id).html(data);
		$("#li"+id).slideDown("slow");
		var $target = $paneTarget.find('li'+id);
		$paneTarget.stop().scrollTo( $target , 800 );

	});
}

function closeList(id) {
	$("#li"+id).slideUp("slow");
}

function sendToMeasure(str, id){
	$("#m"+id).val(str);
}

function addDrink() {
	var inputString = document.getElementById('searchInput').value;
	$('#searchResults').html(loadingGray);
	$.get("lib/ajax/addIngredient.php", {s: ""+inputString+""}, function(data){
		$("#msg").addClass('alert');
		$("#msg").html(data);
		$("#msg").fadeIn(500).fadeTo(2000, 1).fadeOut(500);
		$('#searchInput').focus();
		refreshIngredients();
		clearBox();
	});
}

function comingSoon() {
		$("#msg").addClass('alert');
		$("#msg").html("Feature coming soon...");
		$("#msg").fadeIn(500).fadeTo(2000, 1).fadeOut(500);
}

function refreshIngredients() {
	$('#ingredients').html(loading);
	$.get("lib/ajax/getIngredients.php", {s: ""+Math.random()+""}, function(data){
		$('#ingredients').fadeIn();
		$('#ingredients').html(data);
		calculate();
	});
}

function clearAll() {
	$('#ingredients').html(loading);
	$.get("lib/ajax/clearSessions.php", {s: ""+Math.random()+""}, function(data){
		$('#ingredients').fadeIn();
		$('#ingredients').html(data);
		refreshIngredients();
	});
}

function removeDrink(n) {
	$('#ingredients').html(loading);
	$.get("lib/ajax/removeDrink.php", {s: ""+n+""}, function(data){
		$('#ingredients').fadeIn();
		$('#ingredients').html(data);
		refreshIngredients();
	});
}

function removeDrinkFromList(n) {
	$('#drinkList').html(loading);
	$.get("lib/ajax/removeDrinkFromList.php", {s: ""+n+""}, function(data){
		$('#drinkList').fadeIn();
		$('#drinkList').html(data);
		updateList();
	});
}

function calculate() {
	$('#count').html(loading);
	$.get("lib/ajax/calculate.php", {s: ""+Math.random()+""}, function(data){
		$('#count').fadeIn();
		$('#count').html(data);
		showButtons();
	});
}

function changeMeasure(id){
	var m = document.getElementById('m'+id).value;
	$('#drinkList').html(loading);
	$.get("lib/ajax/changeMeasure.php", {m: ""+m+"", id: ""+id+""}, function(data){
		$("#msg").addClass('alert');
		$("#msg").html(data);
		$("#msg").fadeIn(500).fadeTo(2000, 1).fadeOut(500);
		refreshIngredients();
	});
}
/*
function addDrinkToList()
{
	$('#ingredients').html(loading);
	$.get("lib/ajax/addDrinkToList.php", {s: ""+Math.random()+""}, function(data){
		clearAll();
		setTimeout('updateList()',1500);
	});
}

function updateList(){
	$('#ingredients').html(loading);
	$.get("lib/ajax/getDrinkList.php", {s: ""+Math.random()+""}, function(data){
		//calculate();
	});
}*/

function showButtons(){
	var d = document.getElementById('nodrinks').innerHTML;
	if(d != 0){
		document.getElementById('meta').style.display = "block";
	}else{
		document.getElementById('meta').style.display = "none";
	}
	return false;
}
