var ShowHideTag = $("ShowHideTag");
var RightTdObj = $("right2");
var LeftDivObj = $("left");
var RightDivObj = $("right");
var MidBar = $("bar");

var CurrentSlelect = $("curr_select");
var isSlected = false;
var isBBSSlected = false;
var CookieName = "CurrentSelect";
var ChannelName = "";

function $(id){return document.getElementById(id);}

function InitSet()
{
	//alert( DataMap.get("forum669"));
	$("searchtxt").focus();//输入框获得焦点
	RightDivObj.style.height = document.documentElement.clientHeight+"px";
	MidBar.style.height = document.documentElement.clientHeight+"px";
	
	
	var prv_select = dealCookie(CookieName);//dealCookie(CookieName);
	if(prv_select == "allsite"){
		OpenAllSite();
	}
	else if(prv_select == "specialarea"){
		OpenSpecialArea("hide");
		var prv_channel = dealCookie("channel");//dealCookie("channel");
		
		if(prv_channel)
		{
			isSlected = true;
		
			var temp_channel = DataMap.get(prv_channel);
			CurrentSlelect.innerHTML = "您选择的是：<b>"+temp_channel+"</b>";	
			$("channel").value = prv_channel;	
			ChannelName = temp_channel;
		}
	}
	else if(prv_select == "forum")
	{
		OpenForum("hide");
		var prv_channel = dealCookie("channel");//dealCookie("channel");
		if(prv_channel)
		{
			isBBSSlected = true;
		
			var temp_channel = DataMap.get(prv_channel);
			CurrentSlelect.innerHTML = "您选择的是：<b>"+temp_channel+"</b>";	
			$("channel").value = prv_channel;	
			ChannelName = temp_channel;
		}
	}
	else{
		OpenAllSite();
	}

	
}

ShowHideTag.onclick = function()
{
	if(RightTdObj.style.display == "none")
	{
		RightTdObj.style.display = "";
		LeftDivObj.style.width =document.documentElement.clientWidth - parseInt(RightTdObj.style.width) -14  + "px";
		this.src = "i/open.gif";
	}
	else
	{
		RightTdObj.style.display = "none";
		LeftDivObj.style.width = document.documentElement.clientWidth - 14 + "px";
		this.src = "i/close.gif";
	}
}

$("allsite").onclick = OpenAllSite;

$("specialarea").onclick = OpenSpecialArea;

$("forum").onclick = OpenForum;

function OpenAllSite()
{
	AddSelToCookie("allsite");
	$("allsite").checked = true;
	$("specialarea").checked = false;
	LeftDivObj.style.width = "100%";
	MidBar.style.display = "none";
	RightTdObj.style.display = "none";
	CurrentSlelect.innerHTML = "您选择的是：<b>全站</b>";	
	$("channel").value = "";
}

function OpenSpecialArea(par)
{
	AddSelToCookie("specialarea");	
		
	$("allsite").checked = false;
	$("specialarea").checked = true;
	LeftDivObj.style.width =document.documentElement.clientWidth - parseInt(RightTdObj.style.width) -14  + "px";
	
	MidBar.style.display = "";
	if(par)
	{
		LeftDivObj.style.width = document.documentElement.clientWidth - 14 + "px";
		RightTdObj.style.display = "none";	
	}
	else
	{
		RightTdObj.style.display = "";
	}
	$("result").style.display = "";
	$("bbs_result").style.display = "none";
	this.src = "i/open.gif";
	if(ChannelName!="")
	{
		CurrentSlelect.innerHTML = "您选择的是：<b>"+ChannelName+"</b>";	
	}
	else
	{
		CurrentSlelect.innerHTML = "您选择的是：<b>专区文章</b>";	
	}
	$("channel").value = "";
	
	
}

function OpenForum(par)
{
	AddSelToCookie("forum");	
		
	$("allsite").checked = false;
	$("specialarea").checked = false;
	$("forum").checked = true;
	LeftDivObj.style.width =document.documentElement.clientWidth - parseInt(RightTdObj.style.width) -14  + "px";
	
	MidBar.style.display = "";
	if(par)
	{
		LeftDivObj.style.width = document.documentElement.clientWidth - 14 + "px";
		RightTdObj.style.display = "none";	
	}
	else
	{
		RightTdObj.style.display = "";
		
	}
	
	$("result").style.display = "none";
	$("bbs_result").style.display = "";
	this.src = "i/open.gif";
	if(ChannelName!="")
	{
		CurrentSlelect.innerHTML = "您选择的是：<b>"+ChannelName+"</b>";	
	}
	else
	{
		CurrentSlelect.innerHTML = "您选择的是：<b>专区文章</b>";	
	}
	$("channel").value = "";	
}


$("searchbtn").onclick = function()
{
	if($("specialarea").checked)
	{
		if(!isSlected)
		{
			alert("请从右边选择专区！");
			return false;
		}
	}
	if($("forum").checked)
	{
		if(!isBBSSlected)
		{
			alert("请从右边选择论坛！");
			return false;
		}	
	}
	

    if($("searchtxt").value==null || $("searchtxt").value==" " || $("searchtxt").value==""){
		 alert("请输入关键字内容");
		 return false; 
	}

	return true;
}

function AddSelToCookie(CookieCon)//将选择的结果放入cookie
{
	dealCookie(CookieName,CookieCon,{expires:30});
	//var date = new Date();
	//date.setTime(date.getTime() + (30  * 24 * 60 * 60 * 1000));
	//setCookie(CookieName,CookieCon,date,"","duowan.com","");
}

function setCookie(name,value,expires,path,domain,secure) 
{
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

function dealCookie(name, value, options) 
{
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			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
		}
		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 = trimStr(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 trimStr(text){
   return (text || "").replace( /^\s+|\s+$/g, "" );
}

InitSet();