/**
 *      
 *    forum.js 论坛、贴吧频道页
 *    dwk 2010年6月23日
 *      
 */
 
if(document.all){
	try{
		EvPNG.fix('a,span');  //EvPNG.fix('包含透明PNG图片的标签'); 多个标签之间用英文逗号隔开。
	}catch(e){}
}
function setTab(m,n){
	var menu = document.getElementById("tab"+m).getElementsByTagName("li");  
	var div = document.getElementById("tablist"+m).getElementsByTagName("div");
	var showdiv = [];

	for (var i=0; i<div.length; i++){
		var classN = div[i].className;
		if (classN.indexOf("tablist"+m) != -1){
			showdiv.push(div[i]);
		}
	} 
	for(var i=0; i<menu.length; i++){
		menu[i].className = i==n?"now":"";
		showdiv[i].style.display = i==n?"block":"none";
	}
}
/*
	@textname 用户名
	@textpass 密码
	@form 提交表单
*/

function login(textname,textpass,form){
	if(textname.val()==''){
		alert('请输入您的用户名');
		return false;
	}
	if(textpass.val()==''){
		alert('密码不能为空');
		return false;
	}
	form.submit();
}
//var srchtxt_threads = Array('请输入关键字...','post','search.php?searchsubmit=yes','srchtxt','fulltext');
var srchtxt_threads = Array('请输入关键字...','post','/searchlist.php','keyword');
var srchtxt_kuaiba = Array('请输入楼盘名...','get','../kuaiba/search.php','wd');
//var srchtxt_author = Array('请输入用户名...','post','search.php?searchsubmit=yes','srchuname','title');
var srchtxt = '请输入关键字...';
var resultSize = 0;
var nowlx = -1;

var XMLHttp = null;
(function($){
$(function(){
	/*//头部搜索选项 点击版
	$('.search_nav').bind('click',function(e){
		var slt = $(this).children('ul');
		slt.toggle();
		e.stopPropagation();
		$(document).one('click',function(){
			slt.hide();
		});
	}).children('ul').children('li').children('a').bind('click',function(e){
		$('#searchtypespan').text($(this).text());
		//$('#searchtype').val($(this).attr('id'));
		eval('tmp = srchtxt_' + $(this).attr('id') + ';');
		$('#searchform').attr('method',tmp[1]);
		$('#searchform').attr('action',tmp[2]);
		srchtxt = tmp[0];
		$('#wd').val(srchtxt).attr('name',tmp[3]);
		e.preventDefault();
	});*/
	//头部搜索选项 鼠标版
	$('.search_nav').bind('mouseover',function(){
		$(this).children('ul').show();
	}).bind('mouseout',function(){
		$(this).children('ul').hide();
	}).children('ul').children('li').children('a').bind('click',function(e){
		$('.search_nav').children('ul').hide();
		$('#searchtypespan').text($(this).text());
		//$('#searchtype').val($(this).attr('id'));
		eval('tmp = srchtxt_' + $(this).attr('id') + ';');
		$('#searchform').attr('method',tmp[1]);
		$('#searchform').attr('action',tmp[2]);
		srchtxt = tmp[0];
		$('#wd').val(srchtxt).attr('name',tmp[3]);
		//$('#srchtype').val(tmp[4]);
		e.preventDefault();
	});
	
	//点击搜索输入框执行的动作
	$('#wd').bind('focusin',function(){
		var obj = $(this);
		if(obj.val() == srchtxt){
			obj.val('');
		}
	}).bind('focusout',function(){
		var obj = $(this);
		if(obj.val() == ''){
			obj.val(srchtxt);
		}
	});
	//提交搜索时执行的动作
	$('#searchform').bind('submit',function(e){
		var val = $('#wd').val();
		if(val == srchtxt || val == ''){
			alert('请输入内容！');
			e.preventDefault();
		}
	});
	
	//点击登录提交表单
	$('#loginbutton').bind('click',function(){
		login($('#userid'),$('#pwd'),$('#loginform'));
	});
	//Ctrl+Enter登录
	$('#loginform').bind('keydown',function(event){
		if(event.keyCode==13){
			this.submit();
		}
	});

	//根据关键字获取相关楼盘
	function getlikekeword(k){
		if(XMLHttp){
			XMLHttp.abort();
		}
		XMLHttp = $.ajax({
			type:'POST',
			url:'/searchpost.php',
			data:'act=searchkey&keyword='+encodeURIComponent(k),
			success:function(result){
				$('#likekearchlist').empty();
				if(result){
					$('#likekearchlist').append(result);
					resultSize = $('.lx').size();
					nowlx = -1;
				}else{
					$(".association").hide();
				}
			}
		});
	}
	//设置搜索框的值
	function setkword(kw){
		$("#wd").val(kw).focus();
		$(".association").hide();
	}
	//方向键
	function selectUD(e){
		if(resultSize>0){
			if(e.keyCode==40){
				$('.lx').eq(nowlx).blur();
				nowlx += 1;
				if(nowlx>(resultSize-1)){
					nowlx = 0;
				}
				$('.lx').eq(nowlx).focus();
				//$('#wd').val($('.lx').eq(nowlx).text());
				e.preventDefault();
			}else if(e.keyCode==38){
				$('.lx').eq(nowlx).blur();
				if(nowlx<=0){
					nowlx = resultSize;
				}
				nowlx -= 1;
				$('.lx').eq(nowlx).focus();
				//$('#wd').val($('.lx').eq(nowlx).text());
				e.preventDefault();
			}
		}
	}
	//点击列表设置搜索框的值
	$('.lx').live('click',function(e){
		 setkword($(this).text());
		 e.preventDefault();
	}).live('focus',function(){
		$(this).css({'backgroundColor':'#3444A2','color':'#ea5504'});
		$(this).bind('keydown',function(e){
			if(e.keyCode==13){
				setkword($('.lx').eq(nowlx).text());
				nowlx = -1;
				e.preventDefault();
			}
		});
	}).live('blur',function(){
		$(this).css({'backgroundColor':'#FFFFFF','color':'#333333'});
		$(this).unbind('keydown');
	});
	//点击隐藏联想列表
	$(".close").live('click', function(e){
		$(".association").hide();
		e.preventDefault();
	})
	//点击搜索框联想功能
	$('#wd').bind('click',function(){
		if($.trim(this.value) == srchtxt){
			this.value='';
		}
	}).bind('blur',function(){
		if($.trim(this.value)==''){
			this.value = srchtxt;
		}
	}).bind('input',function(){
		if($.trim(this.value)=='' || $.trim(this.value)==srchtxt){
			$(".association").hide();
			return;
		}else{
			getlikekeword($.trim(this.value));
			$(".association").show();
		}
	}).bind('propertychange',function(){
		if($.trim(this.value)=='' || $.trim(this.value)==srchtxt){
			$(".association").hide();
			return;
		}else{
			getlikekeword($.trim(this.value));
			$(".association").show();
		}
	})
	$(document).bind('keydown',function(e){
		selectUD(e);
	});
	
	
	//在本版内搜索
	$('#search_thread').bind('click',function(){
		checksearchtext();
	});
	
	$('#search_thread_form').bind('keydown',function(event){
		if(event.keyCode==13){
			checksearchtext();
		}
	});
	
	$('#forumsrchtxt').bind('focusin',function(){
		var v = $(this).val();
		if(v=='在本版内搜索...'){
			$(this).val('');
		}
	}).bind('focusout',function(){
		var v = $(this).val();
		if(v==''){
			$(this).val('在本版内搜索...');
		}
	});

	function checksearchtext(){
		var search_text = $('#forumsrchtxt').val();
		if(search_text=='在本版内搜索...'){
			$('#forumsrchtxt').val('');
		}
		if($('#forumsrchtxt').val()==''){
			alert('请输入关键字进行搜索');
			//return false;
		}else{
			$('#search_thread_form').submit();
		}
	}
	
	
	$('#checksubmit').bind('click',function(){
		var prizetype = $('#vote_prizetype').val();
		alert(prizetype);
		var prizenum = $('#prizenum').val();
		var polloptionid = parseInt($('#polloptionid').val());
		alert(polloptionid);
		if(polloptionid){
			if(!prizenum || !(/^[\d]+$/.test(prizenum))){
				alert('请输入奖励数量');
				return false;
			}else{
				var data = 'opid='+polloptionid+'&prizetype='+prizetype+'&prizenum='+prizenum;
				alert(data);
				$.getJSON('getjson.php?action=award&'+data,'',function(e){
					alert(e.msg);
					floatwin('close_viewvote');
				});
			}	
		}
	});
	
	$("#searchall").click(function(){
		$("#searchnew").removeClass("now");
		$(this).removeClass("now").addClass("now");
		$("#div_searchnew").hide();
		$("#div_searchall").show();
	});
	
	$("#searchnew").click(function(){
		$("#searchall").removeClass("now");
		$(this).removeClass("now").addClass("now");
		$("#div_searchall").hide();
		$("#div_searchnew").show();
	});
	
	$('#keyword')
	.val(function(){
		if($(this).val() == ""){
			return '请输入关键字...';
		}else{
			return $(this).val();
		}
	})
	.autocomplete('http://www.kfw001.com/searchpost.php?act=searchkey')
	.bind('focusin',function(){
		var v = $.trim($(this).val());
		if(v == '请输入关键字...'){
			$(this).val('');
		}
	}).bind('focusout',function(){
		var v = $.trim($(this).val());
		if(v == ''){
			$(this).val('请输入关键字...');
		}
	});
	
	$('#keywordall')
	.val(function(){
		if($(this).val() == ""){
			return '请输入关键字...';
		}else{
			return $(this).val();
		}
	})
	.autocomplete('http://www.kfw001.com/searchpost.php?act=searchkey')
	.bind('focusin',function(){
		var v = $.trim($(this).val());
		if(v == '请输入关键字...'){
			$(this).val('');
		}
	}).bind('focusout',function(){
		var v = $.trim($(this).val());
		if(v == ''){
			$(this).val('请输入关键字...');
		}
	});
	
})
})(jQuery);
