﻿function showSearchBox() {
    document.write("<form style=\"margin:0px\" method=\"POST\" name=\"frmSearch\" id=\"frmSearch\" action=\"\">");
    document.write("<div class=\"input2\"><!--搜索 -->");
    document.write("    <div class=\"input3\">");
    document.write("        <div style=\"width:23px; height:20px; float:left\"><img src=\"/public/images/fangdajing.jpg\" /></div>");
    document.write("        <div style=\"float:left\"><input type=\"text\" class=\"input1\" id=\"txtSearch\" name=\"txtSearch\" onkeyup=\"clickBtn()\" style=\"width:125px; height:20px;\" /></div>");
    document.write("        <div style=\" width:8px; height:20px; float:left\"><img src=\"/public/images/sousuo3.jpg\" /></div>");
    document.write("    </div>");
    document.write("</div>");
    document.write("<div style=\"width:159px; height:10px; padding-left:12px;\"></div>");
    document.write("<div class=\"input2\">");
    document.write("    <div class=\"input3\">");
    document.write("        <div class=\"input4\" style=\"width:125px;\">");
    document.write("            <div class=\"input4\" style=\"width:98px;\">");
    document.write("                <div class=\"searchBg\">");
    document.write("                   <div class=\"searchOption\">");
    document.write("                       <div id=\"divSearchOption\" class=\"searchOptionItem\">");
    document.write("                           <div class=\"searchSelect\" id=\"divSelectShow\">");
    document.write("                           </div>");
    document.write("                           <div style='display:none'>");
    document.write("                               <select id=\"selSearchType\" class=\"searchSelect\">");
    document.write("                                   <option value=\"0\">&nbsp;&nbsp;全站搜索</option>");
    document.write("                                   <option value=\"1\">&nbsp;&nbsp;搜索课程</option>");
    document.write("                                   <option value=\"2\">&nbsp;&nbsp;搜索杂志文章</option>");
    document.write("                                   <option value=\"3\">&nbsp;&nbsp;搜索专栏文章</option>");
    document.write("                                   <option value=\"4\">&nbsp;&nbsp;搜索视频</option>");
    document.write("                                   <option value=\"5\">&nbsp;&nbsp;搜索案例中心</option>");
    document.write("                               </select>");
    document.write("                           </div>");
    document.write("                       </div>");
    document.write("                   </div>");
    document.write("                   <div class=\"searchBtn\">");
    document.write("                       <div class=\"searchBtnItem\">");
    document.write("                           <img id=\"imgUpArrow\" src=\"/public/images/upbluearrow.png\" class=\"btnItem\" onclick=\"ChangeSelectType(0)\" onmouseover=\"ChangeImage(0, true)\"  onmouseout=\"ChangeImage(0, false)\"/>");
    document.write("                       </div>");
    document.write("                       <div class=\"searchBtnItem\">");
    document.write("                           <img id=\"imgDownArrow\" src=\"/public/images/downbluearrow.png\" class=\"btnItem\" onclick=\"ChangeSelectType(1)\" onmouseover=\"ChangeImage(1, true)\" onmouseout=\"ChangeImage(1, false)\"/>");
    document.write("                       </div>");
    document.write("                   </div>");
    document.write("                </div>");
    document.write("            </div>");
    document.write("        </div>");
    document.write("        <div class=\"input4\" style=\"width:34px;\"><input type=\"button\" name=\"btnSearch\" id=\"btnSearch\" onmouseover=\"this.style.cursor='hand'\" value=\" \" onclick=\"doSearch()\" style=\"background: url(/public/images/go.jpg) no-repeat; width: 34px; height: 23px; border:0;\"/></div>");
    document.write("    </div>");
    document.write("</div><!--搜索结束 -->");
    document.write("</form>");

    document.getElementById("selSearchType").value = "0";
    if (document.getElementById("hidSearchType") != null && document.getElementById("hidSearchType").value != "") {
        document.getElementById("selSearchType").value = document.getElementById("hidSearchType").value;
        jQuery("#divSelectShow").html(jQuery("#selSearchType").find("option:selected").text());
    }
    
    var color = jQuery("#hidSearchStyle").val();
    if(jQuery("#selSearchType").val() == "0"){
    	jQuery("#imgUpArrow").css("cursor", "default");
    	jQuery("#imgUpArrow").attr("src", "/public/images/disableup" + color + "arrow.png");
    }
    
    if(jQuery("#selSearchType").val() == "5"){
    	jQuery("#imgDownArrow").css("cursor", "default");
    	jQuery("#imgDownArrow").attr("src", "/public/images/disabledown" + color + "arrow.png");
    }

    if (document.getElementById("hidSearchKey") != null) {
        document.getElementById("txtSearch").value = document.getElementById("hidSearchKey").value;
    }  
}

function doSearch() {
    var searchForm = document.getElementById("frmSearch");
    var searchKey = document.getElementById("txtSearch");

    if (searchKey.value == "") {
    	alert("请填写要查询的关键字");
    } else {
        var urlString = encodeURIComponent(replaceHtml(searchKey.value));
        searchForm.action = "/search/index/type/" + document.getElementById("selSearchType").value + "/search/" + urlString;
        searchForm.submit();
    }
}

function ChangeSelectType(direct){
	var selectObj = jQuery("#selSearchType");

	if(direct == "0"){
		if(selectObj.val() != "0"){
			selectObj.val((Number(selectObj.val()) - 1));
		}
	}else{
		if(selectObj.val() != "5"){
			selectObj.val((Number(selectObj.val()) + 1));
		}
	}
	
	jQuery("#divSelectShow").html(jQuery("#selSearchType").find("option:selected").text());
	
	var color = jQuery("#hidSearchStyle").val();
	if(jQuery("#selSearchType").val() == "0"){
		jQuery("#imgUpArrow").css("cursor", "default");
    	jQuery("#imgUpArrow").attr("src", "/public/images/disableup" + color + "arrow.png");
    }else if(jQuery("#selSearchType").val() == "5"){
    	jQuery("#imgDownArrow").css("cursor", "default");
    	jQuery("#imgDownArrow").attr("src", "/public/images/disabledown" + color + "arrow.png");
    }else{
    	jQuery("#imgUpArrow").css("cursor", "pointer");
    	jQuery("#imgDownArrow").css("cursor", "pointer");
    	jQuery("#imgUpArrow").attr("src", "/public/images/up" + color + "arrow.png");
    	jQuery("#imgDownArrow").attr("src", "/public/images/down" + color + "arrow.png");
    }
}

function ChangeImage(direct, ishover){
	var color = jQuery("#hidSearchStyle").val();
	if(direct == 0 && ishover && jQuery("#selSearchType").val() != "0"){
		jQuery("#imgUpArrow").attr("src", "/public/images/hoverup" + color + "arrow.png");
	}else if(direct == 1 && ishover && jQuery("#selSearchType").val() != "5"){
		jQuery("#imgDownArrow").attr("src", "/public/images/hoverdown" + color + "arrow.png");
	}else if(direct == 0 && !ishover && jQuery("#selSearchType").val() != "0"){
		jQuery("#imgUpArrow").attr("src", "/public/images/up" + color + "arrow.png");
	}else if(direct == 1 && !ishover && jQuery("#selSearchType").val() != "5"){
		jQuery("#imgDownArrow").attr("src", "/public/images/down" + color + "arrow.png");
	}
}

function clickBtn() {
    if (event.keyCode == 13) {
        doSearch();
    }
}

function replaceHtml(replaceStr) {
    return replaceStr.replace(/<[a-zA-Z\/][^>]*>/g, "").replace(/[\\\.\"\'/]/g, "");
}

function cutString(cutStr, length){
    return cutString.substring(0, length) + "... ...";
}

showSearchBox();

jQuery().ready(function(){
	
	if(jQuery("#hidSearchStyle").val() == "orange"){
		jQuery("#divSelectShow").css("background", "url(/public/images/selectbg2.jpg) no-repeat");
		jQuery("#divSearchOption").css("background", "url(/public/images/selectbg2.jpg) no-repeat");
		jQuery(".searchBg").css("background", "url(/public/images/selectbg2.jpg) no-repeat");
		jQuery("#imgUpArrow").attr("src", "/public/images/uporangearrow.png");
		jQuery("#imgDownArrow").attr("src", "/public/images/downorangearrow.png");
		jQuery("#btnSearch").attr("style", "background: url(/public/images/zazhi/go.jpg) no-repeat; width: 34px; height: 23px; border:0;");
	}
});
