var type='song';
  function getType(){
	return type;
  }
function setDefKeyword(){
	if($('#inputsearch').val() == null || $('#inputsearch').val().length <1){
		$('#inputsearch').val(SEARCH_DEFAULT_KEYWORD);
	}
}
function setDefaultType()
{
	var typeval=$('#type').val();
	if(typeval=="singer")
	{
		$("#singer").addClass("current");
	} else if(typeval=="album")
	{
		$("#album").addClass("current");
	} else if(typeval=="lyric")
	{
		$("#lyric").addClass("current");
	} else {
			$("#song").addClass("current");
			$("#type").val("song");
	}
	
	type= $('#type').val();
}

function doSearch(){
	var searchValue = document.getElementById('inputsearch').value;
	if(!searchValue || searchValue.length < 1) { alert('请输入搜索条件！！！'); return false;}
	setSearchType(type);
	document.sform.submit();
}

$(function(){
	setDefaultType();
	setDefKeyword();
	$('.stype').click(function(){
		setSearchType($(this).attr('id'));
	})
	
	$('#inputsearch').focus(                
          function(evt){
        	if($('#inputsearch').val() == SEARCH_DEFAULT_KEYWORD) {
        		$('#inputsearch').val('');
        	}
       		$('#inputsearch').click(); 	
			$('#inputsearch').keydown(function(evt) {
				if(evt.keyCode==13){
					doSearch();
				}
			});	
       }).blur(function(evt){
        	if($('#inputsearch').val() == '') {
        		$('#inputsearch').val(SEARCH_DEFAULT_KEYWORD);
        	}
		   $('#inputsearch').unbind('keydown');	
		   });
	
	function log(event, data, formatted) {
				$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
			}	
			function formatItem(row) {
				return row[0] + " (<strong>id: " + row[1] + "</strong>)";
			}
			function formatResult(row) {
				return row[0].replace(/(<.+?>)/gi, '');
			} 
			var path=$("#path").val();
			if(path || path.length<1) {path="";}
            $("#inputsearch").autocomplete(path+"/autocomplete.html",{
            	extraParams:{word:function(){return $('#inputsearch').val();},type:function(){return getType();},flag:SEARCH_FLAG},                	   
   				multiple: true,
   				selectFirst: false,
   				autoFill: false,
   				scroll:false,
   				width:214,	   					   				
   				scrollHeight:200,				   					   				
   				max:20,     
   				multipleSeparator: '',	   				    
   				dataType: 'json',     

   				parse: function(data) {
     			var rows = [];   
     			if(data != null){
     				for(var i=0; i<data.length; i++){   
     	      			rows[rows.length] = {    
     	        			data:data[i],
     	        			value:data[i],    
     	        			result:data[i]    
     	    			}; 
     			}	   
  			}   
				return rows;   
 		},   
			formatItem: function(row, i, n) {   
  			return row;         
			}   
      }); 
            $("#searchbtn").click(function  cForm(){
            	var word = document.sform.word.value;
            	if(word==undefined||word==null)
            	{word="";}
            	if(word.length==0 || word=='请输入你要搜索的内容！'){
            		document.sform.word.value="请输入你要搜索的内容！";
            		return false;
            	}
            	else{
            		return true;
                }
            });
                    
	
});
function wordFocus() {
	//var word = trim(document.search1.word.value);
	var word = document.sform.word.value;
	if(word==undefined||word==null)
	{word="";}
	if(word == "请输入你要搜索的内容！") {
		document.sform.word.value = "";
	}

}

function searchKeyWord(val){
	document.getElementById("inputsearch").value=val;
	//document.getElementById("radioAll").checked="checked";
	//var searchValue = document.getElementById('searchword').value;
	//alert("setsearchtype:==========="+" "+type);
	setSearchType(type);
	document.sform.submit();
	//window.open("http://218.200.227.218:8080/publish/SearchFz?type="+type+"&word="+searchValue,"searchwindow","height=600, width=1000, top=200, left=300, toolbar=no, menubar=no, scrollbars=yes, resizable=yes");	
}

function setSearchType(type_){
	/*var pings = document.getElementsByName("ping");
	for(aa=0;aa<pings.length;aa++){
		$(pings[aa]).removeClass('buttonstyle2');
		pings[aa].className="buttonstyle1";
	}*/
	resetStyle();
    document.getElementById(type_).className = "current";
	document.getElementById("type").value = type_;
	type = type_;
	$('#inputsearch').flushCache();
}

function resetStyle() {
	$('#song').removeClass('current');
	$('#singer').removeClass('current');
	$('#album').removeClass('current');
	$('#lyric').removeClass('current');
}

