var cse, cate = "", order = "", range = "", time = 0, page = 1;
function display(data){
	if (data.length==0) {
		$(".pagelink").html('');
		$("#results").html('0');
		$(".search-list").html('<p class="noresults">很抱歉，没有找到相关的内容，请换个关键词试试！</p>');
		return;
	}
	var html = "";
	for(var i=0; i<data.length; i++){
		var article = data[i];
		html += "<li><div class='book'>";
		html += "<h5><a href='" + article.linkUrl + "' target='_blank'>" + article.title + "</a></h5>";
		if (article.image!='') {
			html += "<a class='cover' href='" + article.linkUrl + "' target='_blank'><img src='" + article.image + "' width='106'></a>";
		}
		html += "<p class='author'>";
		html += "<span>栏目：";
		for(var j=0; j<article.breadCrumb_title.length; j++){
			if (j>0) {
				html += "&nbsp;>&nbsp;";
			}
			html += "<a href='" + article.breadCrumb_url[j] + "' target='_blank'>" + article.breadCrumb_title[j] + "</a>";
		}
		html += "</span>";
		if (article.author_nickname!='') {
			html += "<span>文：<a href='" + article.author_url + "' target='_blank'>" + article.author_nickname + "</a></span>";
		}
		html += "</p>";
		html += "<p class='disc'>" + article.abstract + "</p>";
		html += "</div></li>";
	}
	$(".search-list").html(html);
	cse.getSearchInfo(key,getPage);
}
function getPage(data){
	$("#results").html(data.totalNum);
	var pageCount = Math.ceil(data.totalNum / 10);
	if (pageCount > 1) {
		var html = "";
		var curpage = 1;
		if(data.curPage!='') curpage = parseInt(data.curPage) + 1;
		var pagestart = (curpage > 5) ? (curpage - 5) : 1;
		var pageend = ((curpage + 4) > pageCount) ? pageCount : (curpage + 4);
		if (curpage > 1) {
			html += "<a href='javascript:' onclick='setPage(" + (curpage - 1) + ")'><b>&lt;</b> 上一页</a>";
		}
		for (i = pagestart; i <= pageend; i++) {
			if (i == curpage) {
				html += "<strong>" + i.toString() + "</strong>";
			} else {
				html += "<a href='javascript:' onclick='setPage(" + i + ")'>" + i.toString() + "</a>";
			}
		}
		if (curpage < pageCount) {
			html += "<a href='javascript:' onclick='setPage(" + (curpage + 1) + ")'>下一页 <b>&gt;</b></a>";
		}
		$(".pagelink").html(html);
	} else {
		$(".pagelink").html('');
	}
}
function filterCate(o,c){
	page = 1, cate = c;
	$(".j-filter-cate").removeClass('crt');
	$(o.parentNode).addClass('crt');
	GetResult()
}
function setPage(p){
	page = p;
	GetResult()
}
function init(){
	cse = new BCse.Search(sid);
	cse.setResultType(6);
	GetResult()
}
function GetResult() {
	if (cate=="") {
		cse.closeFieldFilter()
	} else {
		cse.openFieldFilter({'breadCrumb_title':cate})
	}
	if (range=="title") {
		cse.openTitleFilter()
	} else {
		cse.closeTitleFilter()
	}
	if (time==0) {
		cse.closeTimeFilter()
	} else {
		cse.openTimeFilter(time)
	}
	switch(order){
		case "time":
			cse.openTimeOrder()
			break;
		case "views":
			cse.openFieldOrder({"replyCount":0})
			break;
		case "favs":
			cse.openFieldOrder({"price_new":0})
			break;
		case "tops":
			cse.openFieldOrder({"price_old":0})
			break;
		default:
			cse.closeOrder()
			break;
	}
	cse.getResult(key, display, page)
}
function loadScript(){
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.charset = "utf-8";
	script.src = "http://zhannei.baidu.com/api/customsearch/apiaccept?sid=" + sid + "&v=2.0&callback=init";
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(script,s);
}
$(function(){
	loadScript(), $(".by-range").bind('click', function() {
		$(".adv-by-time").hide(), $(".adv-by-order").hide(), $(".adv-by-range").toggle()
	}), $(".adv-by-range a").bind('click', function() {
		$(".by-range").html("范围："+$(this).html()+"<span></span>"), page = 1, range = $(this).attr('data-val'), $(".adv-by-range").hide(), GetResult()
	}), $(".by-time").bind('click', function() {
		$(".adv-by-range").hide(), $(".adv-by-order").hide(), $(".adv-by-time").toggle()
	}), $(".adv-by-time a").bind('click', function() {
		$(".by-time").html("时间："+$(this).html()+"<span></span>"), page = 1, time = parseInt($(this).attr('data-val')), $(".adv-by-time").hide(), GetResult()
	}), $(".by-order").bind('click', function() {
		$(".adv-by-range").hide(), $(".adv-by-time").hide(), $(".adv-by-order").toggle()
	}), $(".adv-by-order a").bind('click', function() {
		$(".by-order").html("排序："+$(this).html()+"<span></span>"), page = 1, order = $(this).attr('data-val'), $(".adv-by-order").hide(), GetResult()
	})
})