jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') {
		options = options || {path:'/'};
		if (value === null) {
			value = '';
			options = $.extend({}, options);
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString();
		}
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		} else {
			var cookieValue = null;
			if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

$(function(){
	$(".boxy").boxy();
});
  $(window).scroll(function(){
		   var sc=$(window).scrollTop();
		   var rwidth=$(window).width()
      	  if(sc>0){
				$(".go-top-btn").css("visibility","visible");
				}else{
			$(".go-top-btn").css("visibility","hidden");
				}
		  })

	  $(".go-top-btn").click(function(){
		  var sc=$(window).scrollTop();
		 $('body,html').animate({scrollTop:0},1000);
		  })

$(document).ready(function(){
	$("#kw").autocomplete(tp_search_url, {
		minChars: 1,
		delay: 100,
		useDelimiter: true,
		selectFirst: false,
		autoFill: false,
		matchSubset: false, // 不缓存，如果服务器压力过大，可以打开缓存
		remoteDataType:'json',
		sortResults:false,
		filterResults:false,
		onItemSelect:function (data){window.location=data['data'];}
	});
});


function qrsearch(){
	if($("#kw").val()=='请输入小说名...'||$("#kw").val()==''){
		$("#kw").val('');
		$("#kw").focus();
		return false;

	}else{
		return true
	//document.location = '/search/?kw='+ encodeURIComponent($("#kw").val())+"";
	}
}



function clickjs(tit_id,box_id,cur){
	var g_tags=$(tit_id),
		g_conts=$(box_id),
		g_current=cur;
	g_tags.click(function(){
		var g_index=g_tags.index(this);
		$(this).addClass(g_current).siblings().removeClass(g_current);
		g_conts.eq(g_index).show().siblings().hide();
	})
}



function clicktabs(tit_id,box_id,cur){
	var g_tags=$(tit_id),
		g_conts=$(box_id),
		g_current=cur;
	g_tags.mouseover(function(){
		var g_index=g_tags.index(this);
		$(this).addClass(g_current).siblings().removeClass(g_current);
		g_conts.eq(g_index).show().siblings().hide();
	})
}

function lazyload(option){
		var settings={
		  defObj:null,
		  defHeight:0
		};
		settings=$.extend(settings,option||{});
		var defHeight=settings.defHeight,
			defObj=(typeof settings.defObj=="object")?settings.defObj.find("img"):$(settings.defObj).find("img");
		var pageTop=function(){
		  return document.documentElement.clientHeight+Math.max(document.documentElement.scrollTop,document.body.scrollTop)-settings.defHeight;
		};
		var imgLoad=function(){
		  defObj.each(function(i){
		   if ($(this).offset().top<=pageTop()){
			var src2=$(this).attr("a");
			if (src2){
					$(this).attr('src',src2);
					$(this).removeAttr("a");
			}
		   }
		  });
		};
		imgLoad();
		$(window).bind("scroll", function(){
			imgLoad();
		});

}




//Add Site Favorites
$("#fav").click(function() {
	var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL',
		url =location.href,
		sitename =document.title;
	if (document.all && window.external){
	  window.external.addFavorite(url,sitename);
	  }else if (window.sidebar){
	 window.sidebar.addPanel(sitename,url, "");
   }else {
		   alert('您可以尝试通过快捷键' + ctrl + ' + D 加入到收藏夹!');
   }
   return false;
});

function setHomepage(sURL) {
	if($.browser.msie){
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(sURL);
	} else {
		alert('非 IE 浏览器请手动将本站设为首页');
		return false;
	}
}


Date.prototype.format = function(format){
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(),    //day
	"h+" : this.getHours(),   //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
	"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	(this.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))
	format = format.replace(RegExp.$1,
	RegExp.$1.length==1 ? o[k] :
	("00"+ o[k]).substr((""+ o[k]).length));
	return format;
}

hsbooks_length = 100;
function getmybook(){
    var myhsbooks = orderbook(gethsbooks()),
    hscontent = new Array();
    for (var i in myhsbooks) {
			hscontent.push('<a href="' + myhsbooks[i][3] + '"><span>' + myhsbooks[i][2] + '</span>' + myhsbooks[i][0] + '</a>');
    }
    hscontent = hscontent.join('');
    if (hscontent) {
        $("#recorconid").html('<div class="recordlist"><p>' + hscontent + '</p></div><div id="clearrecord">清除历史记录</div>');
    }
}

function orderbook(h) {
    var newbooks = new Array();
    for (var i in h) {
        var obook = h[i].split("||");
        newbooks[i] = obook;
    }
    return newbooks;
/**
    var newbooks = new Array();
    for (var i in h) {
        var obook = h[i].split("||");
        newbooks[i] = obook;
    }
    return newbooks.sort(function(a, b) {
        return b[5] - a[5];
    })
*/
}
function gethsbooks() {
    var c = $.cookie('bvalues');
    if(!c) return new Array();
    var t = c.split("|||");
    return t;
}
function getlastbook(){
    var b_tits=b_tit,
		b_subtits=b_subtit,
		b_urls=b_url,
		b_suburls=b_suburl,
		f_vars= Number(b_id),
		d = new Date(),
		f_times=d.format('yyyy-MM-dd'),
		lastbook = [b_tits,b_urls,b_subtits,b_suburls,f_vars,f_times];
    return lastbook;
}



// 删除一本收藏的小说
$("#favconid code").live('click',function(e){
    var tp = $(this),
    txt = tp.parent().find('em').text();
    var myhsbooks = order_book(get_hsbooks()),
			arr = new Array();
    for (var i in myhsbooks) {
		if (txt != myhsbooks[i][0]) {
			arr.push(myhsbooks[i].join("||"));
		}
    }
	$.cookie('gv', arr.join("|||"), {expires: 365});
    tp.parent().remove();
    if($("#favconid").text()=='') {
		$("#favconid").html('<i>暂无收藏记录...</i>');
	}
	 return false;
});

function get_hsbooks() {
    var c = $.cookie('gv');
    if(!c) return new Array();
    var t = c.split("|||");
    return t;
}
function get_lastbook(){
	 var b_tits=b_tit,
		b_urls=b_url,
		b_isicos=b_isico,
		 lastbook = [b_tits,b_urls,b_isicos];
    return lastbook;
}
function seths_cookie() {
    var lastbook = get_lastbook(),
		hsbooks = get_hsbooks(),
		rp = false;
    for (var i in hsbooks) {
        var ohsbook = hsbooks[i].split("||");
        if (ohsbook.slice(1, 2) == lastbook[1]) {
            hsbooks[i] = ohsbook.join("||");
            rp = true;
			alert('你已收藏过了');
            break;
        }
    }
    if (!rp) {
        hsbooks.unshift(lastbook.join("||"));
        if(hsbooks.length > hsbooks_length) hsbooks.pop();
		alert('已成功收藏!');
    }

    var hscookie = hsbooks.join("|||");
    $.cookie('gv', hscookie, {expires: 365});

}

$("#favbooks,.sccs").click(function(){
	seths_cookie();
});

function order_book(h) {
    var newbooks = new Array();
    for (var i in h) {
        var obook = h[i].split("||");
        newbooks[i] = obook;
    }
    return newbooks;
}

function get_mybook(){
    var myhsbooks = order_book(get_hsbooks()),
    hscontent = new Array();
    for (var i in myhsbooks) {
			hscontent.push('<a href="' + myhsbooks[i][1] + '"><span>' + myhsbooks[i][2] + '</span><code></code><em>' + myhsbooks[i][0] + '</em></a>');
    }
    hscontent = hscontent.join('');
    if (hscontent) {
        $("#favconid").html('<div class="recordlist"><p>' + hscontent + '</p></div>');
    }
}
getmybook();


$(function() {
	$("#readid").hover(
		function() {
			$(this).addClass('readrecord_cur');
		},
		function() {
			$(this).removeClass('readrecord_cur');
		});
	$("#favid").hover(
		function() {
			$(this).addClass('favboxs_cur');
			get_mybook();
		},
		function() {
			$(this).removeClass('favboxs_cur');
		}
	);

	$("#clearrecord").click(function() {
		$("#recorconid").html('<i>暂无阅读记录...</i>');
		$.cookie('bvalues', '');
	});
});

/*反馈*/
$(function(){
	function feedbacktabs(tit_id,box_id,cur){
		var g_tags=$(tit_id),
			g_conts=$(box_id),
			g_current=cur;
		g_tags.click(function(){
			var li_id=$(this).attr('id');
			if (li_id == 1) {
				$("#message").val('提示：对我们的产品有什么意见和建议，来这里反馈吧~');
				$("#classid").val('1');
			}
			if (li_id == 2) {
					$("#message").val('提示：如果在本站有没找到的小说名称，请在此输入相关信息~');
					$("#classid").val('2');
			}
			if (li_id == 3) {
				$("#message").val('提示：欢迎各大小说站与我们进行资源合作~\n为方便尽快与你联系，请写明：1：合作网站\n 2：联系方式');
				$("#classid").val('3');
			}
			if (li_id == 4) {
				$("#message").val('提示：本站现征集优秀小说站友情连接~');
				$("#classid").val('4');
			}

			var g_index=g_tags.index(this);
			$(this).addClass(g_current).siblings().removeClass(g_current);
			g_conts.eq(g_index).show().siblings().hide();
		})
	}
	feedbacktabs("#feedbacktag li","#feedbackcon>ul","on");
});

$(document).ready(function() {
	$('#message').focus(function() {
		if ($("#message").val().indexOf('提示') >= 0) {
			$("#message").val("");
		}
	});

	$('#contact').focus(function() {
		if ($(this).val() == 'QQ/邮箱/电话') {
			$(this).val("");
		}
	});
	$('#contact').blur(function() {
		if ($(this).val() == "") {
			$(this).val('QQ/邮箱/电话');
		}
	});
});


function f_submit() {
	$("#feedback_hit").hide();
	var data = {
		message:$('#message').val(),
		contact:$('#contact').val(),
		classid:$('#classid').val(),
		verifycode:$('#verifycode').val(),
		curl:document.URL,
		op:'f_submit'
	};
	 $.ajax({
		type: "POST",
		url: tp_feedback,
		data:data,
		success: function(result) {
			var obj = jQuery.parseJSON(result);
			if (obj.ok) {
				$("#feedbackform").hide();
				$("#feedback_success").show();
			} else {
				if (obj.hasOwnProperty('verifycode')) {
					$("#feedback_hit").text(obj.verifycode);
				}
				if (obj.hasOwnProperty('body')) {
					$("#feedback_hit").text(obj.body);
				}
				if (obj.hasOwnProperty('error')) {
					$("#feedback_hit").text(obj.error);
				}
				$("#feedback_hit").show();
			}
		}
	});
}

function feedback_return() {
	$("#feedbackform").show();
	$("#feedback_success").hide();
}

var fixvar = false;
function returntop() {
	var recimgs = 1200,
		scrollheight = $(document).scrollTop() + $(window).height();
	if (scrollheight > recimgs) {
		if (!fixvar) {
			fixvar=true;$("body").append('<div id="fixedwrap"><span class="fixicos"></span><div class="clearfix fixtopbox"><div class="fixtopa"><h6>分类</h6>' + tp_catalog + '</div><a href="' + tp_recbigbox + '" class="gonows" target="_self">我最近看的</a></div><a href="javascript:void(0);" target="_self" class="fixbottoma">TOP</a></div>');
			$("#fixedwrap").css("left",(($(window).width()-980)/2)-100);
		} else {
			$("#fixedwrap").show();
		}

	}else{
		$("#fixedwrap").hide();
	}
}
$(window).bind('scroll', returntop);