﻿var aArray=Array();
aArray["0.100"]="大米1";
aArray["100"]="大米2";
aArray["200"]="大米3";
aArray["300"]="大米4";
aArray["400"]="大米5";
aArray["500"]="大米6";
			
			
			
$(document).ready(function(){
	$("#div_input_word").html('<ul id="show_ul"></ul>');
	$("#input_word").keyup(submitForm);
	
    var options = { 
            //target:        '#output',   // target element(s) to be updated with server response 
            //beforeSubmit:  showRequest,  // pre-submit callback 
            success:       showResponse  // post-submit callback 
            // other available options: 
            //url:       url         // override for form's 'action' attribute 
            //type:      type        // 'get' or 'post', override for form's 'method' attribute 
            //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
            //clearForm: true        // clear all form fields after successful submit 
            //resetForm: true        // reset the form after successful submit 
            // $.ajax options can be used here too, for example: 
            //timeout:   3000 
    };
    
    $('#search_form').ajaxForm(options);
	
});


function submitForm(){
	$('#search_form').submit();
}

function ShowNo(){				//隐藏一个层
	$("#div_input_word").css("display","none");
	$("#show_ul").html("");
}
function showFloat(){			//显示一个层

	

	if($("#input_word").val()=="")
	{
		$("#div_input_word").css("display","none");
	}
	else
	{
		var range = getRange();
		document.getElementById("div_input_word").style.display="";
		$("#show_ul").html("");
		for(var key in aArray){
			var url='<a href="'+aArray[key].url+'" target="_blank">'+aArray[key].name+'</a>';
			$("#show_ul").append("<li id='show_li'>"+url+"</li>");
		}
		$("#show_ul").append("<div class='btn'></div>");
		$(".btn").append("<input id='food_search_button_2' type='submit' class='button food_search_button' value='搜索食物' style='font-size:12px;'/>");
		$(".btn").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		$(".btn").append("<input id='dish_search_button_2' type='submit' class='button dish_search_button' value='搜索菜肴' style='font-size:12px;'/>");
		$("#food_search_button_2").click(food_search);
		$("#dish_search_button_2").click(dish_search);
	}
}
function getRange(){			//得到屏幕的大小
	 var top     = document.body.scrollTop;
	 var left    = document.body.scrollLeft;
	 var height  = document.body.clientHeight;
	 var width   = document.body.clientWidth;
	 if (top==0 && left==0 && height==0 && width==0){
	 	top     = document.documentElement.scrollTop;
		left    = document.documentElement.scrollLeft;
		height  = document.documentElement.clientHeight;
		width   = document.documentElement.clientWidth;
	 }
	 return  {top:top  ,left:left ,height:height ,width:width } ;
}















function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
	//$('#output').html()='';
	document.getElementById("action").disabled = true;
    return true; 
}

function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 	//$('#form_dingdan').clearForm(); 
 
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    //    '\n\nThe output div should have already been updated with the responseText.'); 

    //alert(statusText);
	//alert(responseText);
	var dataJSON = responseText;
	//alert(dataJSON);
	var data = $.parseJSON(dataJSON);
	aArray = data;
	showFloat();
}







$(document).ready(function(){
	$("#food_search_button_1").click(food_search);
	$("#dish_search_button_1").click(dish_search);
});


function food_search(){
	input_word = $('#input_word').val();
	if(!input_word){
		alert('请输入食物名称进行搜索!');
		return;
	}
	search_url = '/foods/search_food/'+input_word;
	window.location.href = search_url;
	//alert(1+search_url);
}

function dish_search(){
	input_word = $('#input_word').val();
	if(!input_word){
		alert('请输入菜肴名称进行搜索!');
		return;
	}
	search_url = '/foods/search_dish/'+input_word;
	window.location.href = search_url;
	//alert(2+search_url);
}




