/*唉 发现长时间不看 自己写的代码都不认识了 这次不偷懒了老老实实把注释写上*/

	$(function(){	

		
		$('.entry a:has(img)').lightBox(); //LightBox效果
		$('.commentlist a[href^="http://"]').attr({target:"_blank"});
		
		function stc(){
			var tar = $(this).attr('href');
		 	$.scrollTo(tar,1000);
			return false;
		}
		 
		function reply(){
			var tar = $(this).attr('href');
		 	$.scrollTo(tar,1000);
			setTimeout(function(){$('textarea').focus();},1000);
			return false;
		}
		 
		$('.commentlist a[href^="#comment-"]').click(stc); //@someone平滑移动
		$('.commentlist a[href="#commentform"]').click(reply); //回复添加留言平滑移动
		 
		 
		$('#back').click(function(){
							$.scrollTo('#topbar',1000);
							return false;
						}); //返回顶部
	
/*这里开始是隐藏老用户输入框 增加了一些功能 希望可以改善一点用户体验 没有使用淡入淡出是因为IE下会有轻微的对齐Bug 如果对此功能有任何改的地方 还请与我分享*/


		if($('#author').length>0 && $('#author').val().length>0){ //判断ID="author"是否存在 值不为空执行以下代码
	
			$('<p id="welcome">欢迎回来<span id="username"></span></p>').insertBefore('#author_info');
			$('#author_info').slideUp();
		
		
		var ss = $('input[id="author"]').attr("value");
		$('#username').html(" "+ss+"：");
		
		$('#welcome').append("<span id=\"change\">修改资料<\/span><span id=\"update\">点击这里更新资料<\/span><span id=\"hint\">资料修改成功<\/span")
		
		$('#change').click(function(){
							$('#author_info').slideDown(); //显示资料框
							$(this).hide(); //隐藏 修改资料
							$('#update').show(); //显示 更新资料
						});
		
		$('#update').click(function(){
							$('#author_info').slideUp(); //隐藏资料框
							$(this).hide(); //隐藏 更新资料
							$('#hint').show(); //显示 资料修改成功
							setTimeout(function(){$('#hint').hide();},2000); //隐藏 资料修改成功
							setTimeout(function(){$('#change').show();},2000); //还原 修改资料
							var s = $('input[id="author"]').attr("value");
							$('#username').html(" "+s+"：");
						});
		}
		

		/*回复按钮 鼠标悬停显示*/
		
		$('.commentlist li').hover(function(){$(this).find('.reply').fadeIn();},function(){$(this).find('.reply').fadeOut();});
		
		
		/*这里没什么可说的 为input增加focus效果 */
		
		$('input[type="text"][class!="input"],textarea').focus(function(){
			$(this).addClass('typing');
		})


		$('input[type="text"][class!="input"],textarea').blur(function(){
			$(this).removeClass('typing');
		})
		
		/*Ctrl+Enter提交评论*/
		
		$('#comment').focus(function(){
								$(document).keydown(function(e){
														if( e.ctrlKey && e.keyCode == 13 ){
															$('#submit').click();
														}
													});
							});

		
		
		//增加cookie记录广告功能
	


		
		if( $.cookie('popup_ad') != 'close' ){
			//cookie不存在 显示广告
			$('#popup').show();
		}
		else if( $.cookie('popup_ad') == 'close' ){
			//cookie存在 所以不显示广告
			$('#popup').hide();
		}

		
		$('#close_popup').click(function(){
				$('#popup').fadeOut(500);
				$.cookie('popup_ad', 'close', { expires: 48 * 60 * 60, path: '/' });
		});
		
		//评论提示
		
		$('#author').after('<span class="hint">必填，留下个姓名中英文都可以！</span><span class="hint_e">请填写您的姓名！</span><span class="exact">正确</span>');
		
		$('#email').after('<span class="hint">必填，有回复时方便邮件通知！</span><span class="hint_e">邮箱不能为空！</span><span class="exact">正确</span>');
		
		$('#url').after('<span class="hint">这个可以不填的，填了方便回访！</span><span class="hint_e">为什么不留下个网址呢？当然也可以不填。</span><span class="exact">正确</span>');
		
		
		//一开始全部不显示 focus时 显示hint 隐藏hint_e 和 exact
		//blur时 如果为空的话 显示hint_e 隐藏hint
		$('#author').focus(function(){
			if( $(this).val().length >= 0){
				$(this).parents('p').find('.hint').show();
				$(this).parents('p').find('.hint_e').hide();
				$(this).parents('p').find('.exact').hide();
			}
		})
		
		$('#author').blur(function(){
			if( $(this).val().length == 0){
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.hint_e').show();
			}
			else{
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.exact').show();
			}
		})
		
		
		$('#email').focus(function(){
			if( $(this).val().length >= 0){
				$(this).parents('p').find('.hint').show();
				$(this).parents('p').find('.hint_e').hide();
				$(this).parents('p').find('.exact').hide();
			}
		})
		
		$('#email').blur(function(){
			var email_format = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
			if( email_format.test($('#email').val()) ){
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.exact').show();
			}
			else if( $('#email').val() == 0){
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.hint_e').html('邮箱可不能为空！').show();
			}
			else{
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.hint_e').html('邮箱是不是写错了？').show();
			}
		})
		
		$('#url').focus(function(){
			if( $('#url').val().length >= 0){
				$(this).parents('p').find('.hint').show();
				$(this).parents('p').find('.hint_e').hide();
				$(this).parents('p').find('.exact').hide();
			}
		})
		
		$('#url').blur(function(){
			var url_format = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;
			if( url_format.test($('#url').val()) ){
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.exact').show();
			}
			else if( $('#url').val() == 0){
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.hint_e').hide();
			}
			else{
				$(this).parents('p').find('.hint').hide();
				$(this).parents('p').find('.hint_e').html('网址是不是写错了？').show();
			}
		})
		

		function validator(){
			
			for(i=0;i<3;i++){
	
				if($('#author').val()=="" && $('#email').val()=="" ){			
					$('#author_info input').parents('p').find('.hint').hide();
					$('#author_info input').parents('p').find('.exact').hide();
					$('#author_info input').parents('p').find('.hint_e').show();
					$('#url').parents('p').find('.hint_e').hide();
					
					return false;
				}

			}
			
			if($('#author').val()=="" || $('#author').length<0){
					$('#author').parents('p').find('.hint').hide();
					$('#author').parents('p').find('.exact').hide();
					$('#author').parents('p').find('.hint_e').show();
					return false;
			}
			
			else if($('#email').val()=="" || $('#email').length<0){
					$('#email').parents('p').find('.hint').hide();
					$('#email').parents('p').find('.exact').hide();
					$('#email').parents('p').find('.hint_e').show();
					return false;
			}
			
			else if( !/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test($('#email').val()) && $('#email').length>0){
				$('#email').parents('p').find('.hint').hide();
				$('#email').parents('p').find('.exact').hide();
				$('#email').parents('p').find('.hint_e').html('邮箱是不是写错了？').show();
				return false;
			}
			
			else if( $('#comment').val() == "" || $('#comment').val() == "请输入评论内容！" ){
					$('#comment').css({'color':'#ffcd00'}).html('请输入评论内容！');
					return false;
			}

			

		}
		
		$('#submit').click(validator);
		
		$('#reset').click(function(){		   
			$('#author_info span').hide();
			$('#comment').val('');
		});
		
		$('#comment').focus(function(){
			$('#comment').css({'color':'#000'});
			if($(this).html() == '请输入评论内容！'){
				$(this).html('');
			}
		})
		
		
		//搜索框提示
				
		if($('#search-form .input').val()==""){
			$('#search-form .input').val('请输入内容搜索！');
		}
		
		$('#search-form .input').focus(function(){
			if($(this).val()=='请输入内容搜索！'){
				$('#search-form .input').val('');
			}
		});
		
		$('#search-form .input').blur(function(){
			if($(this).val()==''){
				$('#search-form .input').val('请输入内容搜索！');
			}
		});
		
		//LOGO渐变效果
		
		$('.logo').css({'opacity':0});
		
		$('h1').hover(function(){
					$('.logo').stop().animate({'opacity':1},{queue:false,duration:1000});
				},function(){
					$('.logo').stop().animate({'opacity':0},{queue:false,duration:1000});
				});



		/*Konami Code 彩蛋*/
		
		

		
		
		
		var k = [];
		$(document).keydown(function(e){
									k.push(e.keyCode);
									if(k.toString().indexOf('87,65,78,71,80,73,78')>=0){

										$('body').append('<div id="wangpin_bg"></div><div id="wangpin"><p>为了宝宝，我要赶紧好起来！</p></div>');
										var pageHeight = document.body.offsetHeight; //获取整个网页的高度
		
		
										var winWidth = $(window).width();
										
										$('#wangpin_bg').css({'opacity':0.5,'height':pageHeight}); /*Lightbox效果*/
										var wangpinWidth = $('#wangpin').width();
										var left = winWidth/2 - wangpinWidth/2;
										$('#wangpin').css({'left':left,'top':200}); /*提示居中,高度*/
										
									$('#wangpin_bg').show();
									$('#wangpin').fadeIn(500);
									k = [];
									}
									
		function scrollWangpin(){
			var winScroll = $(document).scrollTop();
			var top = 200 + winScroll;
			$('#wangpin').animate({'top':top,'left':left},{duration:800,queue:false});
		}
		
		
		$(window).scroll(scrollWangpin); /*调用滚动函数*/
		
		
		function closeWangpin(){
			$('#wangpin,#wangpin_bg').fadeOut();
		}
		
		$('#wangpin_bg').click(closeWangpin);
		
		$(document).keypress(function(e){
								if( e.keyCode == 27 || e.keyCode == 120 || e.keyCode == 88 || e.which == 120 || e.which == 88 ){
									closeWangpin();
								}
							});
		});
		
		
		
		
		

	
	})
