$(document).ready(function(){
	radioEvent();
});

$(document).ready(function(){
	if(window.location!='http://time.hudong.com/100000000'){
		var url = window.location.href;
		if(-1==url.indexOf("http://so.hudong.com/s/")){	
			jQuery.ajax({
				url: '/calendarInterrelatedInfo.wiki',
				cache: false,
				dataType: 'xml',
				type:'get',
				async:false, 
				data: {docTitle:document.getElementById('doctitle')==null?"":document.getElementById('doctitle').value},
				timeout:1000,
				error: function(){
					if(document.getElementById('adv')!=null){
						document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
					}
					if(document.getElementById('wikibarList')!=null){
						document.getElementById('wikibarList').parentNode.removeChild(document.getElementById('wikibarList'));
						$('#wikibartitle').attr('href','http://wikibar.hudong.com/');
					}
					if($('#doctitle').val()=='' || document.getElementById('doctitle')==null){
						$('#publicspeechul').hide();
					}else{
						if(getHdCookie(hdCookieName)==null){//如果没有登陆
							$('#emailli').show();
							$('#pwdli').show();
							$('#registeredli').show();
						}
					}
					$('#topic_title').val('');
					$('#topic_content').val('');
					$('#right').show();
				},
				success: function(xml){
					var relateinfo=xml.getElementsByTagName('CALENDARRELATEDINFO')[0];
					if(relateinfo!=null){
						//广告
						var advobj=relateinfo.getElementsByTagName('ADVTISEMENT')[0];
						if(advobj!=null){
							var advinfo=advobj.getElementsByTagName('ADVTISEMENTINFO')[0];
							if(advinfo!=null){
								var advpicurl=advinfo.getElementsByTagName("ADVTISEMENTPIC")[0].firstChild.data;
								var advurl=advinfo.getElementsByTagName("ADVTISEMENTURL")[0].firstChild.data;
								$('#advimg').attr('src',advpicurl);
								$('#adv').attr('href',advurl);
							}else{
								if(document.getElementById('adv')!=null){
									document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
								}
							}
						}else{
							if(document.getElementById('adv')!=null){
								document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
							}
						}
						//维吧部分
						var wikibarinfo=relateinfo.getElementsByTagName('WIKIBAR')[0];
						if(wikibarinfo!=null){
							var wikisrc=wikibarinfo.getElementsByTagName('WIKIBARSRC')[0].firstChild;
							$('#wikibartitle').attr('href','http://wikibar.hudong.com/'+encodeURI(wikisrc.data));
							$(wikibarinfo).find('WIKIBARINFO').each(function(){
								var wikibartitle=$(this).find('WIKIBARTITLE').text();
								var wikibariden=$(this).find('WIKIBARIDEN').text();
								$('#wikibarList').html($('#wikibarList').html()+'<li><a target="_blank" href="http://wikibar.hudong.com/posts/'+wikibariden+'" >'+wikibartitle+'</a></li>');
							})
						}else{
							if(document.getElementById('wikibarList')!=null){
								document.getElementById('wikibarList').parentNode.removeChild(document.getElementById('wikibarList'));
								$('#wikibartitle').attr('href','http://wikibar.hudong.com/');
							}
						}
					}
					//维吧发言
					if($('#doctitle').val()=='' || document.getElementById('doctitle')==null){
						$('#publicspeechul').hide();
					}else{
						if(getHdCookie(hdCookieName)==null){//如果没有登陆
							$('#emailli').show();
							$('#pwdli').show();
							$('#registeredli').show();
						}
					}
					$('#topic_title').val('');
					$('#topic_content').val('');
					$('#right').show();
				}
			});
		}else{
			$.getJSON("http://www.hudong.com/calendarInterrelatedInfo.do?callback=?",
			function(data) {
			    if(null!=data.xml && undefined!=data.xml)
			    {
			    	// 加载xml文档
					var xmlDom;	
					var xmlstr = data.xml;
				　　 if(document.all){ 
					　　 var xmlDom=new ActiveXObject("Microsoft.XMLDOM"); 
					　　 xmlDom.loadXML(xmlstr);  
				　　 }else{ 
				　　 	xmlDom = new DOMParser().parseFromString(xmlstr, "text/xml") 
					}
			    	
					var relateinfo= xmlDom.getElementsByTagName('CALENDARRELATEDINFO')[0];
					if(relateinfo!=null){
						//广告
						var advobj=relateinfo.getElementsByTagName('ADVTISEMENT')[0];
						if(advobj!=null){
							var advinfo=advobj.getElementsByTagName('ADVTISEMENTINFO')[0];
							if(advinfo!=null){
								var advpicurl=advinfo.getElementsByTagName("ADVTISEMENTPIC")[0].firstChild.data;
								var advurl=advinfo.getElementsByTagName("ADVTISEMENTURL")[0].firstChild.data;
								$('#advimg').attr('src',advpicurl);
								$('#adv').attr('href',advurl);
							}else{
								if(document.getElementById('adv')!=null){
									document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
								}
							}
						}else{
							if(document.getElementById('adv')!=null){
								document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
							}
						}
						//维吧部分
						var wikibarinfo=relateinfo.getElementsByTagName('WIKIBAR')[0];
						if(wikibarinfo!=null){
							var wikisrc=wikibarinfo.getElementsByTagName('WIKIBARSRC')[0].firstChild;
							$('#wikibartitle').attr('href','http://wikibar.hudong.com/'+encodeURI(wikisrc.data));
							$(wikibarinfo).find('WIKIBARINFO').each(function(){
								var wikibartitle=$(this).find('WIKIBARTITLE').text();
								var wikibariden=$(this).find('WIKIBARIDEN').text();
								$('#wikibarList').html($('#wikibarList').html()+'<li><a target="_blank" href="http://wikibar.hudong.com/posts/'+wikibariden+'" >'+wikibartitle+'</a></li>');
							})
						}else{
							if(document.getElementById('wikibarList')!=null){
								document.getElementById('wikibarList').parentNode.removeChild(document.getElementById('wikibarList'));
								$('#wikibartitle').attr('href','http://wikibar.hudong.com/');
							}
						}
					}
					//维吧发言
					if($('#doctitle').val()=='' || document.getElementById('doctitle')==null){
						$('#publicspeechul').hide();
					}else{
						if(getHdCookie(hdCookieName)==null){//如果没有登陆
							$('#emailli').show();
							$('#pwdli').show();
							$('#registeredli').show();
						}
					}
					$('#topic_title').val('');
					$('#topic_content').val('');
					$('#right').show();
			    }else{
			  		if(document.getElementById('adv')!=null){
						document.getElementById('adv').parentNode.removeChild(document.getElementById('adv'));
					}
					if(document.getElementById('wikibarList')!=null){
						document.getElementById('wikibarList').parentNode.removeChild(document.getElementById('wikibarList'));
						$('#wikibartitle').attr('href','http://wikibar.hudong.com/');
					}
					if($('#doctitle').val()=='' || document.getElementById('doctitle')==null){
						$('#publicspeechul').hide();
					}else{
						if(getHdCookie(hdCookieName)==null){//如果没有登陆
							$('#emailli').show();
							$('#pwdli').show();
							$('#registeredli').show();
						}
					}
					$('#topic_title').val('');
					$('#topic_content').val('');
					$('#right').show();
			    }
			});
		}
	}
});

$(document).ready(function(){
	$('#topic_title').focus(function(){
		showsec('imgsec','comments');
		$('#seccodeli').show();
		$('#refreshsecode').show();
	});
});

$(document).ready(function(){
	$('input[name="functionType"]').click(function(){
		radioEvent();
	}); 
});
/**
 * radio事件操作
 */
function radioEvent(){
	var functionType=document.getElementsByName('functionType');
	if(functionType[0].checked){
		$('#selectevent').attr('style','display:none');
		$('#selectyear').attr('style','');
		document.getElementById("selectTime").style.display = "";
		
	}
	if(functionType[1].checked){
		$('#selectyear').attr('style','display:none');
		document.getElementById('selectTime').style.display='none';
		$('#selectevent').attr('style','');
	}
}

//按年代
$(document).ready(function(){
	$('#gotime').click(function(){
		gotimeevent();
	}); 
});
$(document).ready(function(){
	$('#yearstr').keydown(function(event){
		if(event.keyCode==13){
			gotimeevent();
		}
	}); 
});
//前往事件
function gotimeevent(){
	//判断输入字符是否是全角数字且为正数
	var year=$('#yearstr').val();
	var url='http://time.hudong.com/';
	var para=''
	if(year==''){
		alert('请输入年份，例如：1949');
		return;
	}
	var selectObj=document.getElementById('selectTime');
	if(selectObj!=null){
		var timeflag=selectObj.options[selectObj.selectedIndex].value;
		if(timeflag==3 || timeflag==2){
			if(!isEntiretyNum(year)){
				alert("输入不合法，请输入正整数，例如：1949");
				return;
			}else{
				year=year.replace(/\b(0+)/gi,"");
				var yearNum=Number(year);
				if(yearNum<=0 || yearNum>9999){
					alert('请输入1-9999的正整数，例如：1949');
					return;
				}else {
					para=timeflag;
					for(var i=0;i<4-(''+yearNum).length;i++){
						para=para+'0';
					}
					para=para+yearNum+'0000';
					window.location.href=url+para;
				}
			}
		}else{
			alert('请选择年代标识，例如：1949');
			return;
		}
	}else{
		return;
	}
}

//按事件
$(document).ready(function(){
	$('#searchTime').click(function(){
		SearchEvent();
	}); 
});
$(document).ready(function(){
	$('#eventstr').keydown(function(event){
		if(event.keyCode==13){
			SearchEvent();
		}
	}); 
});

//搜索事件
function SearchEvent(){
	var eventstr=$('#eventstr').val();
	if(eventstr==''){
		alert('请输入搜索内容！');
		return;
	}
	window.location.href='http://so.hudong.com/s/history/'+encodeURI(eventstr);
}

/**
 * 点击发表留言
 */
var gSubmitFlag = true; //防止重复提交维吧话题
$(document).ready(function(){
	$('#publishbtn').click(function(){
		if (!gSubmitFlag){//防止重复提交
			return false;
		}
		gSubmitFlag = false;
		var topic_title=KillSpace($('#topic_title').val().Trim());
		var topic_content=KillSpace($('#topic_content').val().Trim());
		var error_str='';
	    if(topic_title.length==0){
	    	$('#errorblock').html('<span class="img_yzm red">标题不能为空！</span>');
	    	$('#errorblock').show();
	    	setTimeout("$('#errorblock').hide();",2500);
			gSubmitFlag = true;	
			return false;
		}
		if(topic_title.length>30){
			$('#errorblock').html('<span class="img_yzm red">标题不能过长！</span>');
	    	$('#errorblock').show();
	    	setTimeout("$('#errorblock').hide();",2500);
			gSubmitFlag = true;	
			return false;
		}
		if(topic_content.length==0){
	    	$('#errorblock').html('<span class="img_yzm red">内容不能为空！</span>');
	    	$('#errorblock').show();
	    	setTimeout("$('#errorblock').hide();",2500);
			gSubmitFlag = true;	
			return false;
	 	}
		if(topic_content.length>10000){
	    	$('#errorblock').html('<span class="img_yzm red">内容不能超过1万字！</span>');
	    	$('#errorblock').show();
	    	setTimeout("$('#errorblock').hide();",2500);
			gSubmitFlag = true;	
			return false;
		}
		if(getHdCookie(hdCookieName)==null){//未登陆
			var email=$('#email').val();
			if(email.length==0) {
				$('#errorblock').html('<span class="img_yzm red">邮件地址不能为空！</span>');
		    	$('#errorblock').show();
				setTimeout("$('#errorblock').hide();",2500);
				gSubmitFlag = true;	
				return false;
		 	}
		 	var pwd=$('#pwd').val();
			if(pwd.length==0) {
				$('#errorblock').html('<span class="img_yzm red">密码不能为空！</span>');
		    	$('#errorblock').show();
				setTimeout("$('#errorblock').hide();",2500);
				gSubmitFlag = true;	
				return false;
		 	}
		}
		var seccode=$('#seccode').val();
		if(seccode.length==0) {
			$('#errorblock').html('<span class="img_yzm red">验证码不能为空！</span>');
		    $('#errorblock').show();
			setTimeout("$('#errorblock').hide();",2500);
			gSubmitFlag = true;	
			return false;
		}
		checkRand();
	});
});

function checkRand(){
	jQuery.ajax({
		url: '/ajaxCheckRand.do',
		cache: false,
		dataType: 'xml',
		type:'get',
		async:false,
		data: {randType:'comments',rand:$('#seccode').val().Trim()},
		timeout:1000,
		error: function(){
			$('#errorblock').html('<span class="img_yzm red">验证码不准确，请重新输入！</span>');
		    $('#errorblock').show();
			setTimeout("$('#errorblock').hide();",2500);
			$('#seccode').focus();
			$('#seccode').val('');
			showsec('imgsec','comments');
			gSubmitFlag = true;	
			return false;
		},
		success: function(xml){
			var flag = xml.getElementsByTagName("flag")[0].firstChild.data;
			if(flag=="true"){
				doctopicsubmit();
         		return ;
			}else {
				$('#errorblock').html('<span class="img_yzm red">验证码不准确，请重新输入！</span>');
		    	$('#errorblock').show();
				setTimeout("$('#errorblock').hide();",2500);
				$('#seccode').focus();
				$('#seccode').val('');
				showsec('imgsec','comments');
				gSubmitFlag = true;	
				return false;
			}
		}
	});
}

function doctopicsubmit(){
	jQuery.ajax({
		url: '/createDocTopic.do',
		cache: false,
		dataType: 'xml',
		type:'POST',
		async:false,
		data: {doc_title:$('#doctitle').val(),topic_title:$('#topic_title').val(),content:$('#topic_content').val(),type:'doc',email:$('#email').val(),pwd:$('#pwd').val(),seccode:$('#seccode').val()},
		timeout:1000,
		error: function(){
			$('#errorblock').html('<span class="img_yzm red">发布失败！</span>');
		    $('#errorblock').show();
			setTimeout("$('#errorblock').hide();",2500);
			$('#seccode').focus();
			$('#seccode').val('');
			showsec('imgsec','comments');
			gSubmitFlag = true;	
			return false;
		},
		success: function(xml){
			var flag=xml.getElementsByTagName("FLAG")[0].firstChild.data;
			if(flag=="true"){
				location.reload();
			}
			else{
				var errorinfo=xml.getElementsByTagName("FAILUREREASON")[0].firstChild.data;
				$('#errorblock').html('<span class="img_yzm red">'+errorinfo+'</span>');
			    $('#errorblock').show();
				setTimeout("$('#errorblock').hide();",2500);
//				$('#seccode').focus();
//				$('#seccode').val('');
//				showsec('imgsec','comments');
				gSubmitFlag = true;	
				return false;
			}
		}
	});
}

/**
 * 是否是非负整数
 */
function isEntiretyNum(s) {
	var regu = "^([0-9]*[.0-9])$"; // 小数测试 
	var re = new RegExp(regu); 
	if (s.search(re) != -1) 
		return true;
	else 
		return false; 
}

/**
 * 去多余的空格
 */
function KillSpace(text){
	var i=0;
	var len=0;
	var lentmp=0;
	var a=0;
	var b=0;
	while (i==0){
		lentmp = text.length;
		text = text.replace(/(^\s*)|(\s*$)/g,"");
		len = text.length;
		if(lentmp==len){
			a=0;
		}else{
			a=1;
		}
	
		lentmp = text.length;
		text = text.replace(/(^\u3000*)|(\u3000*$)/g,"");
		text = text.replace(/(^\ue4c6*)|(\ue4c6*$)/g,"");
		len = text.length;
		if(lentmp==len){
			b=0;
		}else{
			b=1;
		}
		if (a==0 && b==0){
			i = 1;
		}else{
			i = 0;
		}
	}
	return text;
}