﻿$(document).ready(function(){

    _initselectsearch();
	
	_initstartsearch();

	_initLogin();
	
	_initKeyup();
	
})



function _initselectsearch(){
	
     $(".selectitem li").click(function(){
				
					$(".selectitem li[@class='selected']").removeClass("selected");
					
					$(this).addClass("selected");
					
					var id   =  $(".selectitem li").index($(".selectitem li[@class='selected']"));
		
						id++;
						
					if(id==1)
					{
						$(".sear-r img:last").show();
					}
					else
					{
						$(".sear-r img:last").hide();
					}
					
					if($("#keyword").val()!="")
					{
					  $(".sear-r img:first").click();
					}
	 })
}

function _initstartsearch(){
	
	  $(".sear-r img:first").click(function(){
									 
	        var keyword = encodeURI($("#keyword").val());
		     
			if(keyword=="")
			{
			    alert("请输入需要搜索的内容");	
		    }
			else
			{
				var id        =  $(".selectitem li").index($(".selectitem li[@class='selected']"));

				id++;
				
				window.location.href = "/search/index?id="+id+"&keyword="+keyword;
		    }
	   
	   })
}





function _initLogin(){
	
	   $(".login img").click(function(){
												 
		 var username = $(".login input:first").val();
		 
		 var passwd   = $(".login input:last").val();

		 if(username=="")
		 {
		    alert("请输入用户名");	 
			
			$(".login input:first").focus();
		 }
		 else if(passwd=="")
		 {
		    alert("请输入密码");	 
			
			$(".login input:last").focus();
		 }
		 else
		 {
		     <!-- start -->
				$.ajax({
					  url: "/login/Loginindex",
					  type: 'post',
					  dataType: 'json',
					  data: 'name='+username+'&pass='+passwd,
					  success: function(data){
						  
						    if(data.username=="nouser")
							 {
								    $(".login input:first").val("");
								 
								    $(".login input:last").val("");
									
								    alert("此用户不存在");
							 }
							 else if(data.username=="nopasswd")
					         {
								  $(".login input:last").val("");
								  
								  alert("密码错误,请重新输入密码");  
					         }
							 else
							 {
								 var html;
								 
								 html ='欢迎回来，<b><a href="/'+data.domain+'">'+data.username+'</a></b> ：<a href="/'+data.domain+'/manage/dreamadd">添加梦想</a>  | ';
								 
							     html+='<a href="/'+data.domain+'/manage/blogadd">发表日志</a> | <a href="/'+data.domain+'/manage/photoupload">上传图片</a> | ';
								 
							     html+='<a href="/'+data.domain+'/inspiration">添加灵感</a> |<a href="/login/logout">退出</a>  ';
								 
								$(".login").html(html);
							}
					  }
				});
	         <!-- end -->
	     }
    })	

}


function AddFavorite(sURL,sTitle) {

       try{
            window.external.addFavorite(sURL, sTitle);
       }
       catch (e)       
	   {
         try         
		   {
                window.sidebar.addPanel(sTitle, sURL, "");
           }

           catch (e){

               alert("加入收藏失败，请使用Ctrl+D进行添加");
           }

 } } 
 
 
 function _initKeyup(){
	
	if($.browser.msie)
	{
	    $(document).keyup(function(){
			if(window.event.keyCode==13)
			{
			         $(".sear-r img:first").click();
			}	
		})
	}
	else
	{
         $(document).keydown(function(event){
				  
				if(event.keyCode==13){ 
				
			         $(".sear-r img:first").click();
				  
			    }
	      })
	}
}

