
//jquery 插件
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(function(){
	//初始化
	//$('.search_one').html($.cookie('type'));

	var search_what = $(".search .search_title ul.any li a");
	var search_list= $(".search .search_title ul.any");
	//鼠标移动到搜索之后显示列表
	$(".search_title").mouseover(function(){
			search_list.css({
				"display":"block",
				"zIndex":"1000"
			});	
		});
	$(".search_title").mouseout(function(){
			search_list.css({
				"display":"none",
				"zIndex":"1000"
			});
		});

	search_list.mouseover(function(){
			search_list.css({
				"display":"block",
				"zIndex":"1000"
			});
		});

	search_list.mouseout(function(){
			search_list.css({
				"display":"none",
				"zIndex":"1000"
			});
		});

	//点击列表项之后改变搜索内容
	search_what.click(function(){
			//隐藏
			search_list.css({
				"display":"none",
				"zIndex":"1000"
			});
			var tmp=$(this).html();
			$(".search_one").html(tmp);
	});

	/**///搜索框焦点
	$('.keyword').blur(function(){
		var f= function(){
			$('.searh_fuzzy').css({
				"display":"none"
			});
		}
		setTimeout(f,300);
	});
	$('.keyword').focus(function(){
			$(this).val('');
	});
	//加朋友s




	//搜索
	dadi_new_search_box('.keyword','.searh_fuzzy','.search_submit','.search_one',1300,true,'true');
});

function dadi_new_search_box(keyword,down_list,submit,search_obj,time,all,link){

	set_up_down(keyword,down_list);
	$(keyword).keyup(function(event){
		if(all){
			var tmp=$(search_obj).html();
			var isAll = false; 

			switch(tmp){
				case '全部':
					isAll = true;
					style='all';
					break;
				case '地点':
					style='point';

					break;

				case '故事':
					style='story';

					break;

				case '群组':
					style='square';

					break;
				case '收件人':
				case '用户': 
					style='user';
					break;

				case '相册':
					style='album';

					break;

				case '图片':
					style='pic';


				break;

			}
			//$.cookie('type', $('.search_one').html())
			if(!isAll){
				var method="find_page_possible(\'" + keyword +'\''+ "," +'\''+ style +'\''+ "," + '\'' + down_list  + '\'' + "," + '\'' +  link  + "\')";
			}
			
			$(submit).click(function(){
				window.location.href="/search/result/type/"+style+"/keyword/"+$(keyword).val();
			});

		}else{
			$.cookie('type', $('.search_one').html())

			var method="find_page_possible(\'" + keyword +'\''+ "," +'\''+ type +'\''+ "," + '\'' + down_list + '\'' + ","  + '\''+   link + "\')";

			$(submit).click(function(){
				window.location.href="/search/result/type/"+type+"/keyword/"+$(keyword).val();

			});

		}
		if(!isAll){
			if((event.keyCode != 13) && (event.keyCode != 40) && (event.keyCode != 38) ){
				//延时发送
				setTimeout(method,time);
			}
		}
		if(event.keyCode == 13){
			if($(this).val()){
				$(submit).click();
			}
			else
				info("搜索框不能为空");
		}


	});

}

//up down
function set_up_down(input , list){
	//上下键响应
	var select=0;//用来控制上下
	$(input).keydown(function(event){
		var obj=$(list).children();
		if((event.keyCode == 40)){	

			if(select >= 1){
				obj[select-1].style.background="#fff";
				if(  select <=(obj.length-2))
					obj[select+1].style.background="#fff";
			}
			if(select==0)
				obj[obj.length-1].style.background="#fff";

			obj[select].style.background="#007AB9";
			$(input).val(text(obj[select]));

			select++;
			if( select> (obj.length-1)){
				select=0;
			}
		}

		if( event.keyCode == 38){

			if(select >0 ){
				obj[select-1].style.background="#fff";
				if(  select <=(obj.length-2))
					obj[select+1].style.background="#fff";
			}
			if(select==0)
				obj[1].style.background="#fff";

			if(select==(obj.length-1)){
				obj[0].style.background="#fff";
			}

			obj[select].style.background="#007AB9";
			$(input).val(text(obj[select]));

			select--;
			if(select<0){
				select=obj.length-1;
			}
		}

	});
}
//获取自对象的文本
function text(e){
	var t="";
	e=e.childNodes || e;
	for(var i=0;i<e.length;i++){
		t += e[i].nodeType != 1 ? e[i].nodeValue : text(e[i].childNodes);
	}
	return t;
}
function find_page_possible(kw,type,show_list,link){
	var keyword=$(kw).val();

	if(keyword==""){

		$(show_list).css({"display":"none"});
		$(show_list).empty();

		return true;
	}
	//会调函数
	var data={"type":type,"keyword":keyword };
	var fun=function(json){
			if(json.flag){
				$(show_list).empty();
				data=json.data;
				for(i=0;i<json.data.length;i++){
					//添加元素
					if(link=='true'){
						if(type=='album'){
							var str=$(show_list).html() + "<li><a class='pname' href=/point/"+type+"/id/" + json.data[i].id +  ">" + json.data[i].name+"</a></li>";
						}else if(type=="pic"){
							var str=$(show_list).html() + "<li><a class='pname' href=/point/albumlist/id/" + json.data[i].id +  ">" + json.data[i].name+"</a></li>";

						}else if(type=='user'){
							var str=$(show_list).html() + "<li><a class='pname' href=/"+type+"/index/id/"+ json.data[i].id +  ">" + json.data[i].name+"</a></li>";

						}else{
							var str=$(show_list).html() + "<li><a class='pname' href=/"+type+"/display/id/"+ json.data[i].id +  ">" + json.data[i].name+"</a></li>";
						
						}
					}else{
						str=$(show_list).html() + "<li><a class='pname'  href='javascript:;'>" + json.data[i].name+"</a></li>"
					}
					$(show_list).html(str);
				}
				if(json.data.length>0){
					$(show_list).css({"display":"block"});
				}

				//截获消息
				$(show_list).click(function(){
					$(keyword).val($(this).text());
					//$(show_list).css({"display":"none"});
					if(link=='true')
						window.location.href="/search/result/type/"+type+"/keyword/"+$(this).text();

					$(keyword).focus();

				});

			}
		}

	$ajax("/search/near",data,fun);
}

//story

//模糊搜索提示索引
function select_find(){

	switch($(".search").text()){
		case '地点':
			find_page_possible('.hkeyword','point','.search_near');
			break;

		case '故事':
			find_page_possible('.hkeyword','story','.search_near');

			break;

		case '广场':
			find_page_possible('.hkeyword','square','.search_near');

			break;

		case '用户':
			find_page_possible('.hkeyword','user','.search_near');

			break;

		case '相册':
			find_page_possible('.hkeyword','ablum','.search_near');
			break;

		case '图片':
			find_page_possible('.hkeyword','pic','.search_near');
		break;
	}
}
//加好友
function s_friend(uid,uname){
	var content = $("#note").val();
	$ajax('/public/friend',{'uid':uid,"content":encodeURI(content),"uname":encodeURI(uname)},function(json){
		if(json.flag){
			$("#u"+uid).val("等待验证").attr("disabled","disabled");
			info(json.info);
		}else{info(json.info);}
	});
}
