/*
 * @Author $Author: Abhijit $
 * @copyright 2009
*/
$(document).ready(function()
{
	if($("#opt").val()=='9'){
		$("#search11").keydown(function(e)
		{
			e1=e.keyCode;
			if(e1==38 || e1==40){
				if($("#search11").val()!='')
					load_data(e1);
				else
					$(".ac_results").hide();
			}
		});
		$("#search11").keyup(function(e)
		{
			e1=e.keyCode;
			if(e1!=38 && e1!=40){
				if($("#search11").val()!='')
					load_data(e1);
				else{
					$("#nextScrollPos").val(0); //reset value
					$("#prevScrollPos").val(0); //reset value
					$(".ac_results").hide();
					$("#locations").show();
				}
			}
		});
	}

	function load_data(e){
		var tot_url="/education/auto_search/auto_member_inst.php";
		//if(!$(".ac_results").is(':visible') && (e!=38 || e!=40 || e!=46 || e!=9)){
		if(((65>=e<=90) && e==32) || (e!=38 && e!=40)) {
			$.post(tot_url,{keyword:$("#search11").val(),cc:$("#cc").val(),cid:$("#cid").val()} ,function(data){
				var htmlcode = "";
				if(data.inst_count >= 1){
					if(data.inst_count==11){
						var show_inst = 10;
						var morelink_code = '<span style=\"margin-left:271px;\"><a href=\"/education/'+data.path+'search.php?pgid=2&k='+$("#search11").val()+'\">more</a></span>';
					}
					else {
						var show_inst = data.inst_count;
						var morelink_code = "";
					}
					for(var i=0;i<show_inst;i++){
						htmlcode += '<ul><li class=\"num'+(i+1)+'\"><input type=\"hidden\" id=\"nums'+(i+1)+'\" value=\"'+data.instname[i]+'\"><a id=\"link'+(i+1)+'\" href=\"/'+data.eduname+'/'+data.instlink[i]+'-nid-'+data.instid[i]+'.html\" style=\"color: #000\" title=\"Click here to know more\">'+data.instname[i]+'</a></li></ul>';
					}
					htmlcode = htmlcode+morelink_code;
					$(".ac_results").html(htmlcode);
					$(".ac_results").show();
					$("#locations").hide(); /*hide contactus dropdown menu due to ie6 problem*/
				}
				else if(data.inst_count == 0){
					$("#nextScrollPos").val(0); //reset value
					$("#prevScrollPos").val(0); //reset value
					$(".ac_results").html(htmlcode);
					$(".ac_results").hide();
					$("#locations").show();
				}

			},"json");
		}
		else if($(".ac_results").is(':visible') && (e==8 || e==46)){ //for backspace and delete button
			$.post(tot_url,{keyword:$("#search11").val(),cc:$("#cc").val(),cid:$("#cid").val()} ,function(data){
				var htmlcode = "";
				if(data.inst_count >= 1){
					if(data.inst_count==11){
						var show_inst = 10;
						var morelink_code = '<span style=\"margin-left:271px;\"><a href=\"/education/'+data.path+'search.php?pgid=2&k='+$("#search11").val()+'\">more</a></span>';
					}
					else {
						var show_inst = data.inst_count;
						var morelink_code = "";
					}
					for(var i=0;i<show_inst;i++){
						htmlcode += '<ul><li class=\"num'+(i+1)+'\"><input type=\"hidden\" id=\"nums'+(i+1)+'\" value=\"'+data.instname[i]+'\"><a id=\"link'+(i+1)+'\" href=\"/'+data.eduname+'/'+data.instlink[i]+'-nid-'+data.instid[i]+'.html\" style=\"color: #000\" title=\"Click here to know more\">'+data.instname[i]+'</a></li></ul>';
					}
					htmlcode = htmlcode+morelink_code;
					$(".ac_results").html(htmlcode);
					$(".ac_results").show();
					$("#locations").hide(); /*hide contactus dropdown menu due to ie6 problem*/
				}
				else if(data.inst_count == 0){
					$("#nextScrollPos").val(0); //reset value
					$("#prevScrollPos").val(0); //reset value
					$(".ac_results").html(htmlcode);
					$(".ac_results").hide();
					$("#locations").show();
				}

			},"json");
		}
		else if(e==38){
			//up key
			if($("#nextScrollPos").val()==1 && $("#prevScrollPos").val()!=parseInt($(".ac_results ul li").size())-1){
				var currentPos = $("#nextScrollPos").val();
				var nextPos = $(".ac_results ul li").size();
			}
			else if($("#prevScrollPos").val()==parseInt($(".ac_results ul li").size())-1){
				var currentPos = parseInt($("#prevScrollPos").val());
				var nextPos = parseInt($("#prevScrollPos").val())+1;
			}
			else if($("#scrollPos").val()==1 && $("#prevScrollPos").val()==0 && $("#nextScrollPos").val()==0){
				var currentPos = $(".ac_results ul li").size();
				var nextPos = parseInt($(".ac_results ul li").size())-1;
			}
			else if($("#prevScrollPos").val()==0){
				var currentPos = $(".ac_results ul li").size();
				var nextPos = parseInt($("#prevScrollPos").val())+1;
			}
			else{
				var currentPos = parseInt($("#prevScrollPos").val());
				var nextPos = parseInt($("#nextScrollPos").val())-1;
			}

			if(currentPos==0){
				//$("#search11").val($("#nums"+1).val());
				$(".num"+1).addClass('hoverstyle');
			}
			else{
				//$("#search11").val($("#nums"+currentPos).val());

				$(".num"+currentPos).addClass('hoverstyle');
				$(".num"+nextPos).removeClass('hoverstyle');

				$("#nextScrollPos").val(parseInt(currentPos)+1);
				$("#prevScrollPos").val(parseInt(currentPos)-1);
			}
		}
		else if(e==40){
			//down key
			if($("#nextScrollPos").val()!='0')
				var currentPos = $("#nextScrollPos").val();
			else
				var currentPos = $("#scrollPos").val();

			if(currentPos==parseInt($(".ac_results ul li").size())+1){
				currentPos = 1;
			}

			if(currentPos<=$(".ac_results ul li").size()){
				//$("#search11").val($("#nums"+currentPos).val());
				$(".num"+currentPos).addClass('hoverstyle');
				if(currentPos==$(".ac_results ul li").size() && $(".ac_results ul li").size()!=1)
					var nextPos = 1;
				else
					var nextPos = parseInt(currentPos)+1;

				$("#nextScrollPos").val(nextPos);
				$("#prevScrollPos").val(parseInt(currentPos)-1);
				if(currentPos>1){
					var prevPos = parseInt(currentPos)-1;
					$(".num"+prevPos).removeClass('hoverstyle');
				}
				else if(currentPos==1 && $(".ac_results ul li").size()!=1){
					$(".num"+$(".ac_results ul li").size()).removeClass('hoverstyle');
				}
			}
		}
	}
});

