//document.domain = "hwebook.cn";

/*废弃
var asynReqExt = ".do";
var asynReqLoginExt = ".action";
*/
var asynReqURL = "";
//var jsoncallback = "jsoncallback=?";
var jsoncallback = "";
//var domain = "friend-eye.net";
var domain = "hwebook.cn";


//url跳转
function toUrl(url){
	this.top.location =url;
}

//登录
function gotoLogin(){
	this.parent.location = "https://account.hw99.com?service="+getUrl();
}

//处理字符串前后空白字符
function trim(str){     //str是待处理的字符串
	return str.replace(/^\s*|\s*$/g,""); 
}

//得到浏览器url
function getUrl(){
	var url = this.window.location.toString();
	if(url.indexOf("searchType") > -1){		
		return escape(url);		
	}else{		
		return escape(encodeURI(url));		
	}
}

//找回密码			
function gotoFindPass(){
	this.top.location = "https://account.hw99.com/user/findPassword.do?service="+getUrl();
}
		
//注册
function gotoRegister(){
	this.top.location = "https://account.hw99.com/user/register.do?service="+getUrl();
}

//用户登录后，从内页调用该函数，修改登录状态			
function login(name){
	$("#welcome").html(name+"您好，欢迎来到汉王书城！&nbsp;<a href='https://account.hw99.com/logout?service="+getUrl()+"' target='_top' class='yellow12' style='cursor: pointer'>[退出]</a>");
}

//初始化搜索提示相关
$(function(){
	$('#hw_suggest').hide();
	
	$('body').click(function(){
		$('#hw_suggest').hide().empty();
	});
})

var selectedIndex=null;
//搜索框内键盘按下并弹起后执行的函数
var	searchSuggest=function(event, resType){
	event = event || window.event;
	if(event.keyCode==38){//up
		if(selectedIndex!==null){
			selectItem(selectedIndex-1);
			event.preventDefault;
		}else{
			selectItem($('#st tr').length-1);
			event.preventDefault;
		}
	}else if(event.keyCode==40){//down
		if(selectedIndex!=null){
			selectItem(selectedIndex+1);
			event.preventDefault;
		}else{
			selectItem(0);
			event.preventDefault;
		}
	}else if(event.keyCode==13){
		submitSearchForm(resType);
	}else{	
		selectedIndex=null;
		$('#hw_suggest').hide().empty();
		var inputStr=$.trim($("#searchName").val());
		if(null!=inputStr&&""!=inputStr){
			var url = '';
			if(resType == 1){
				url = 'http://book.'+domain+'/getTT.action?jsoncallback=?';
			}else if(resType == 64) {
				url = 'http://newspaper.'+domain+'/getTT.action';
			}else if(resType == 16){
				url = 'http://magazine.'+domain+'/getTT.action';
			}
		
		$.getJSON(url,
				{'key':inputStr,'keyType':getkeyType(resType)}, 
				function(data){
							var $hwsuggest=$('#hw_suggest');
							$hwsuggest.empty();
							if(data&&data.length>0){
								
							 	var $table=$("<table width='100%' style='table-layout:fixed' id='st' cellpadding='2' cellspacing='0'></table>").appendTo($hwsuggest);
							 	$.each(data,function(index,item){
							 		var trStr="<tr class='ml' trKeyType='"+item.keyType+"'><td width='0%' style='visibility:hidden;'>"
							 			trStr+="</td><td width='65%' align='left' style='cursor: default;overflow:hidden;white-space:nowrap;'>"
							 			trStr+=item.title;
								 		trStr+="</td><td width='35%' align='right' style='cursor: default;overflow:hidden;'>约";
								 		trStr+=item.resCount;
								 		trStr+="本</td></tr>";
							 		
							 		$(trStr).click(function(){
							 			$('#searchName').val(item.title);
							 			$('#hw_suggest').hide();
							 			submitSearchForm(resType);
							 		}).mouseover(function(){
							 			selectItemMouse(index);
							 		}).mouseout(function(){
							 			selectNoItem();
							 		}).appendTo($table);
							 	});
							 	$hwsuggest.show();
							 }
						}
				);
		}
	}
}

var getkeyType=function(resType){
	if(resType == 1){
		return 0;
	} else if (resType == 64){
		return 20;
	} else if (resType == 16){
		return 10;
	}
}


//鼠标在搜索提示上移动时改变被选中的提示条目
var selectItemMouse=function(index){
	selectedIndex=index;
	$('#st').find('tr').removeClass('selected').eq(selectedIndex).addClass('selected')
}

//选中某个提示条目
var selectItem=function(index){
	selectedIndex=index;

	if(selectedIndex<0){
		selectedIndex=$('#st tr').length-1;
	}
	if(selectedIndex>=$('#st tr').length){
		selectedIndex=0;
	}
	$('#st').find('tr').removeClass('selected').eq(selectedIndex).addClass('selected');
	$('#searchName').val($("#st").find('tr').eq(selectedIndex).find('td').eq(1).text());
}


//取笑提示条目选中
var selectNoItem=function(){
	selectedIndex=null;
	$('#st tr').removeClass("selected");
}

//提交搜索
function submitSearchForm(resType){
		var searchName =$.trim($('#searchName').val());

		var key=searchName;
		if(searchName.length==0){
			$("#searchName").css("color", "#999");
			$("#searchName").val($("#searchName")[0].defaultValue);
			return false;
		}else{
			if($("#searchName").val()==$("#searchName")[0].defaultValue){
				return false;
			}
			$.ajax({'url':asynReqURL+'addorinc.action',
			'data':{'key':key,'keyType':getkeyType(resType)},
			'dataType':'json',
			'type':'post'
			});
			
			var searchUrl = $("#searchUrl").val();
			searchUrl = searchUrl.replace(/key/,key);
			toUrl(searchUrl);
		}
}

//切换tab页
function switchTab(name,show,clazz){
	$("#" + name +" span").each(function(index){
		if(index == show){
			$(this).addClass(clazz);
			$("#"+name+"_"+index).show();
		} else {
			$(this).removeClass(clazz);
			$("#"+name+"_"+index).hide();
		}
		
	});
}

//取得cookie
function getCookie(name)	{
	var str = document.cookie;
	if(!str || str.indexOf(name+"=")<0){
		return;
	}
	var cookies = str.split("; ");
	for(var i=0;i<cookies.length;i++){
		var cookie = cookies[i];
		if(cookie.indexOf(name+"=")==0){
			var value = cookie.substring(name.length+1);
			return unescape(value);
			//return decodeURI(value);
		} 
	}
}

//设置cookie
function setCookie(name,value){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+30)
	document.cookie= name + "="+escape(value)+"; path=/; expires="+exdate.toGMTString();// encodeURI(value);
}

//隐藏收藏夹的东西
function hideShoucang(){
	$.unblockUI();
}

//跳转到收藏夹
function gotoCollect(){
	this.location="http://club."+domain+"/index.action?iframeInitAction=listCollectRecord.action?userType=1";
}

//隐藏购物车的东西
function hideGoumai(){
	$.unblockUI();
}

//跳转到购物车
function gotoGouwuche(){
	this.location="http://club."+domain+"/index.action?iframeInitAction=listShoppingCartItem.action";
}

//隐藏免费书直接下载
function hideDownload(){
	$.unblockUI();
}

//跳转到订阅列表
function gotoDingyue(){
	this.location="http://club."+domain+"/index.action?iframeInitAction=listUserSubscribeRecord.action";
}

//隐藏订阅提示信息
function hideDingyue(){
	$.unblockUI();
}

$(function(){
	/*图书子站.板块导航 START*/
	$("#book-category").mouseover(function(){
		$("#home-category").show();
	}).mouseout(function(){
		$("#home-category").hide();
	});
	/*图书子站.板块导航END*/
	
	//书城分类
	$("#home-category li").mouseover(function(){
		$(this).children("div").show();
		$(this).find("h1 a").eq(0).addClass("bg-white");
	}).mouseout(function(){
		$(this).children("div").hide();
		$(this).find("h1 a").eq(0).removeClass("bg-white");
	});
});

