$(function() {
 	$(document).ready(function() {  
		$("form").each(function() {
		var form = $(this);
		form.submit(function() {
			if(form.serialize().length>1000) {
				form.attr('method','post');
			}
			return true;
		});
 		});
 	});
});

function SetCookie(name,value)
{
	var Days = 365;
	var exp  = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";path=\/;expires=" + exp.toGMTString();
}
function getCookie(name)
{
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function openc(obj, inw)
{
	var nw=arguments[1]?arguments[1]:0;
	var cnw = getCookie('nw');
	if(nw=='0' || cnw==null) {
		obj.target="_blank";
	}
	return true;
}
function getSel()
{
	var txt = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		txt=txt.toString();
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		txt=txt.toString();
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
	}
	if(txt.length==0) return;
	document.f.q.value =  txt;
	document.bsf.q.value =  txt;
}

function addBookmark(title,url,sc_b) {
if (window.sidebar) { 
window.sidebar.addPanel(title, url,""); 
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
alert(sc_b);
} else {
alert(sc_b);
}
}

function addOpenSearch(sc_a,sc_b)
{
 if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
 window.external.AddSearchProvider("http://www.shendusou.com/scripts/search_provider.xml");
 } else {
	addBookmark(sc_a,"http://www.shendusou.com",sc_b);
 }
}
function email(value) {
//Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
return  /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}
function parseJSON(v, safe) {
	if (safe === undefined) safe=true;
	if (safe && !/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(v))
		return undefined;
	return eval('('+v+')');
}

function saveDoc(uid,code,sc_a,sc_b,sc_c) {
	$('#msg_'+code).html(sc_a);
	var title = jQuery.trim($('#title_'+code).text());
	var params = {uid:uid,code:code,title:title};
	$.post('/login?act=saveDoc',params, function(data) {
			var ret = parseJSON(data);
			if (ret!=undefined && ret.status!=undefined && ret.status==1) {
			$('#msg_'+code).html(sc_b);
			} else {
			var error = sc_c;
			if (ret!=undefined && ret.error!=undefined) { 
			error = ret.error;
			}
			$('#msg_'+code).html(error);
			}
			});
}

function printObjectTree(Obj){  
	for(attr in Obj){  
		var value=Obj[attr];  
		var type=typeof(value);  

		if(type =='object'){  
			printObjectTree(value);  
		}else{  
			var result=attr+':'+ value;  
		}  
	}  

}

function strToDate(str){    
    var year = str.substr(0,4);  
    var month = str.substr(4,2);  
    var day = str.substr(6,2);
	var temDate = year+"/";
	if (month!="00") temDate=temDate+month+"/";
	if (day!="00") temDate=temDate+day;
	return temDate;
 }

function initResize(nsize){}

function closeDiv(){
	$("#preview").hide();
	$("#pimage").hide();
	$("#prange").hide();
}

function pclosetime(){
	window.clearTimeout(setTimeout("0")-1);
}
function psettime(){
	setTimeout('closeDiv()', 3000); 
}

function preview(obj,uid,tab,sc_b,sc_c,sc_d,sc_e,c){
	var html="";
	if ($("#puid").attr("value")==uid) {
		return;
	} else{
		 if (c!="custom") {getUserData(obj);}
		$("#puid").attr("value",uid);
	}
	$.post('/preview?tab='+tab+'&q=cview:'+encodeURIComponent(uid), function(data) {
			var ret = parseJSON(data);
			if (ret!=undefined) {
				$("#ptitle").html(ret['title']);
				if (ret['author']!=null)html+="[<em>"+sc_b+"</em>]"+ret['author']+"<br />"
				if (ret['class_name']!=null)html+="[<em>"+sc_c+"</em>]"+ret['class_name']+"<br />"
				if (ret['ext_claim']!=null)html+="[<em>"+sc_d+"</em>]"+ret['ext_claim']+"<br />"
				if (ret['content_time']>"19700000000000" && ret['content_time']<ret['nowdate']) html+="[<em>"+sc_e+"</em>]"+strToDate(ret['content_time']);
				 $("#pmeta").html(html);
				html="<em>";
				if(ret['abstract']!=null)html+="<br>"+ret['abstract']+"<br><br>";
				if(ret['index_body']!=null)html+=ret['index_body'];
				html+="</em>";
				$("#content").scrollTop(0);
				$("#psnippet").html(html);
			} else {	
				$("#ptitle").html('ERROR！');
				$("#pmeta").html('');
				$("#psnippet").html('');
			}
	});
}

function showDiv(obj,tab,uid,mod,sc_a,sc_b,sc_c,sc_d,sc_e,c){
	 pclosetime();
	 var ww=690;
	 if (mod==1)  ww=840;
 	 var range=getPageSize();
	 var top=range[3]+getPageScroll()-520;
	 if (getPosition(obj).y<top) top=getPosition(obj).y-10;	
	
	 $("#preview").css('min-width','200px');
	 $("#preview").css('background','#94b7db');
	 if (c!="custom") {
		$("#preview").css('top',top);
		$("#preview").css('left',(getPosition(obj).x+520));
		$("#preview").css('height','500px'); 
		$("#preview").css('width',(range[2]-ww)+'px');
		$("#pfram").css('height','486px');
	 }else{
		 $("#preview").css('top',top+30);
		 $("#preview").css('left','0px');
		 $("#preview").css('height','200px'); 
		 $("#preview").css('width','550px');
		 $("#pfram").css('height','186px');
	 }
	 $("#pfram").css('border','2px black solid');
	 $("#pfram").css('margin','5px 5px 5px 5px');
	 $("#pfram").css('background','#d7e2ee');
	 $("#pclose").css('margin','1px 2px');
	 $("#pclose").css('border-bottom','1px solid #CCC');
	 $("#pclose").html("<a style='background:#94b7db;color:white' href='javascript:;' onclick='closeDiv()'>"+sc_a+"</a>");

	 var hight=$("#pfram").height()-$("#pclose").height()-21;
	 $("#content").css('height',hight+'px')
	 preview(obj,uid,tab,sc_b,sc_c,sc_d,sc_e,c);
	 $("#preview").show();

	$("#pimage").css('left',getPosition(obj).x+490);
	$("#pimage").css('top',getPosition(obj).y-4);
	$("#pimage").show();
	$("#prange").css('left',getPosition(obj).x-4);
	$("#prange").css('top',getPosition(obj).y-4);
	$("#prange").show();
}
function getPosition(el){
	for (var lx=0,ly=0;el!=null;lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll)
	return yScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
} 

function getUserData(obj,n_url,tp){
	if (tp==undefined)tp="";
	var value=parseInt(Math.random()*9999+1);
	if(getCookie("shendusou")==null) SetCookie("shendusou",value);
	var url="";
	if (obj.href==undefined){
		if (n_url==undefined ||n_url==""){
			url=window.location;
		}else{
			url=n_url;
		}
		var text="深度搜";
	}else{
		url=obj.href;
		var text=$.trim($(obj).html());
	}
	url=encodeURI(url);
	var params = {url:url,text:text,tp:tp};
	$.post('/userdata',params, function() {});
	return true;
}

function doc_related_view(uid,title,sc_a,sc_b,sc_c){
	$.post('/doc_related_view?q=cview:'+uid, function(data) {
		var ret = parseJSON(data);
		if (ret!=undefined){
			get_doc_related_data(ret,uid,title,0,sc_a,sc_b,sc_c);
			get_doc_related_data(ret,uid,title,1,sc_a,sc_b,sc_c);	
		}
	});
}
function get_doc_related_data(ret,uid,title,n,sc_a,sc_b,sc_c){
		var html="";
		if (n==0){
			var ctype="webpage";
			var str=sc_b;
			var url="";
		}else{
			var ctype="patent";
			var str=sc_c;
			var url="&tab=1";
		}
		if (ret[ctype].numReturned>0){
			html="<div class='sb related'><b>"+str+" </b><a href='/search?q=related:"+encodeURI(uid)+url+"&l=all'>"+sc_a+""+str+"</a>"
			+"<br style='clear: both;'><br style='clear: both;'></div><div class='related'>";
			for (i in ret[ctype].docList){
					html+="<div class='related-title'>"
					+"<a href='/search?q=cview:"+encodeURI(ret[ctype].docList[i].uid) +url+"&l=all' onclick='openc(this,1)'>"
         			+ret[ctype].docList[i].m2d.title+"</a></div><div class='related-meta'>";
					if (ret[ctype].docList[i].m2d.class_name)html+="[ "+ret[ctype].docList[i].m2d.class_name+" ]"
					if (ret[ctype].docList[i].m2d.author)html+=" -- "+ret[ctype].docList[i].m2d.author
					if (ret[ctype].docList[i].m2d.content_time>"19700000000000" ) html+=" -- "+strToDate(ret[ctype].docList[i].m2d.content_time);
					html+="</div><br style='clear: both;'>";
			}
			html+="</div>";
			$("#related td:eq("+n+")").html(html);
			$('a').mousedown(function(){return getUserData(this);});
		}
return;
}

function get_ads_baidu(query,num){
	var html="";
	$.post('/ads_baidu?q='+query, function(data) {
		var ret = parseJSON(data);
		if (ret!=undefined){	
			for (i in ret.docList){
				if (i>num-1 || ret.docList[i].m2d.rank==undefined) break;
				html+="<div class='result-title' ><a href='"+ret.docList[i].m2d.url+"'>"+ret.docList[i].m2d.title+"<a/></div>"
				+"<div class='result-snippet'>"+ret.docList[i].m2d.abstract+"</div>"
				+"<div class='result-meta' >"+ret.docList[i].m2d.site+"</div><br/>"
			}
		}
			if (html!="")html="百度推广<p>"+html;
			$("#baidu_ads").html(html);
	});
}

function reloadImg(str){
	var o=document.getElementById("checkcode_"+str);
	if(o.createTextRange){//IE
		var u=o.createTextRange();
		u.collapse(true);
		u.moveStart("character",0);
		u.moveEnd('character', 4);
		u.select();
	}else if(o.setSelectionRange){//FF
		o.setSelectionRange(0,4)
		o.focus();
	}
	var plink = document.getElementById("ccImg_"+str);	
	plink.src = "/include/lib/checkcode.php?img="+str+"&temp=" + Math.random();
}
