/*********************************
*	作用：群中一些场用到的js方法的包含
*	日期：2009-3-2
*	作者：耿鸿飞
*
*	主要方法： GHF.Utils
*			 copy
*			 setHomepage
*			 AddFavorite
*			 getMsgCenter
*			 shrinkage
*
*
*
*****************************************/
if(typeof GHF == "undefined")
{
	GHF ={};
}
GHF.Utils = {
	/**
	*	 拷贝文字
	**/
	copy : function(txt)
	{
		if(/\n/.test(txt)) {
			txt =  txt;
		} else {
			txt = "本文转载自有伴网，原文地址:" + txt;
		}
		if(Prototype.Browser.IE)
		{
			 window.clipboardData.clearData();   
			 window.clipboardData.setData("Text", txt); 
			 alert("复制成功");
			 return true;
		}else if(Prototype.Browser.Opera)
		{
		   window.location = txt;  
		   return true;
		}else if(Prototype.Browser.Gecko)
		{
			try
			{
				 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}
			catch (e)
			{
				alert("浏览器不支持该复制方式，请使用CTRL+C或者鼠标右键复制。")
				
			}{}
			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
			  if (!clip)   
			  {
				  alert("复制失败");
				   return false;   
			  }
			  var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
			  if (!trans)   
			  {
				  alert("复制失败");
				   return false;   
			  }
			  trans.addDataFlavor('text/unicode');   
			  var str = new Object();   
			  var len = new Object();   
			  var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
			  var copytext = txt;   
			  str.data = copytext;   
			  trans.setTransferData("text/unicode",str,copytext.length*2);   
			  var clipid = Components.interfaces.nsIClipboard;   
			  if (!clip)
			  {
				  alert("复制失败");
				   return false;   
			  }
			  clip.setData(trans,null,clipid.kGlobalClipboard);
			  alert("复制成功");
			 return true;
		}	
	} ,
	/**
	*  设为主页
	**/
	setHomepage : function(obj,url)
	{
		if(Prototype.Browser.IE)
		{
			obj.style.behavior='url(#default#homepage)';
			obj.setHomePage(url);
		}else
		{
			  try 
			   {
                   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
                } catch (e) { 
                   alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'"); 
                }
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',url);	
		}
	} ,
	/**
	*  添加到收藏夹
	**/
	AddFavorite : function(title,url)
	{
		if(Prototype.Browser.IE)
		{
			window.external.AddFavorite(url,title)
		} else
		{
			window.sidebar.addPanel(title, url, "");
		}
	},
	/**
	*	获取消息中心中的消息数据
	**/
	getMsgCenter : function()
	{
		var msgCenter = $("__ghf_msgcenter");
		if(msgCenter)
		{
			new Ajax.Request("/group_utils.php?cmd=msgcenter&time=" + new Date(),{
				onSuccess : function(rs)
				{
					var str = rs.responseText;
					if(str == "0" || str == 0)
					{
						msgCenter.update("0") ;
					}else
					{						
						eval("var list = " + str);
						if(list)
						{
							var total = 0 ;
							for(var i = 0 ; i < list.list.length ; i++)
							{								
								total = total + list.list[i].unread;
							}
							var old = parseInt(msgCenter.innerHTML);
							/*if(total > 0 && (!isNaN(lod) && total != old))
							{
								Sound.enable();
								Sound.play("/js/libs/pm_3.wav");
							}*/
							$("__ghf_msgcenter").update("" + total) ;
						}else
						{
							$("__ghf_msgcenter").update("==") ;
						}
					}
				}
			});
		}
	},
	/**
	*  关闭收缩菜单
	**/
	shrinkage : function(obj)
	{
		obj = Element.extend(obj);
		var ul = obj.parentNode.parentNode.getElementsByTagName("ul")[0];
		if(ul)
		{
			ul = Element.extend(ul);
			if(ul.getStyle("display") == "none")
			{
				//obj.removeClassName("sign1");
				//obj.addClassName("sign2");
				obj.update('<img src="images/group/g-icon23.gif" />');
				ul.show();
			}else
			{
				//obj.removeClassName("sign2");
				//obj.addClassName("sign1");
				obj.update('<img src="images/group/g-icon29.gif" />');
				ul.hide();
			}
		}
	},
	/**
	*	加入群
	**/
	collectGroup : function(gid)
	{
		new Ajax.Request("/group_utils.php?cmd=collectgroup&gid="+ gid +"&time=" + new Date(),{
			onSuccess : function(r)
			{
				alert('提示：收藏成功，在您的个人中心里可见并可随时取消收藏。');
				window.location.reload();

			}
		});
	},
	/**
	*	加入群
	**/
	appendGroup : function(gid)
	{
		new Ajax.Request("/group_utils.php?cmd=joingroup&gid="+ gid +"&time=" + new Date(),{
			onSuccess : function(r)
			{
				$("__ghf_join_group_pop").hide();
				if(r.responseText == "-1")
				{
					alert("加入失败");
				}else if(r.responseText == "0")
				{					
					if($("__ghf_join_wait_pop"))
					{
						GHF.Utils.showDiv("__ghf_join_wait_pop");
					}					
					window.setTimeout("document.location.reload();",5000);
				}else if(r.responseText == "622")
				{
					alert("该群没有通过审核，你还不能加入。");
				}
				else
				{					
					if($("__ghf_join_ok_pop"))
					{
						GHF.Utils.showDiv("__ghf_join_ok_pop");
					}					
					window.setTimeout("document.location.reload();",5000);
				}
			}
		});
	},
	/**
	*  退出群
	**/
	leavegroup : function (gid)
	{
		if($("__ghf_out_group_pop"))
		{
			if($("__ghf_out_group_pop").visible())
			{
				new Ajax.Request("/group_utils.php?cmd=leavegroup&gid="+ gid +"&time=" + new Date(),{
					onSuccess : function(r)
					{
						if(r.responseText == "1")
						{
							$("__ghf_out_group_pop").hide();
							GHF.Utils.showDiv("__ghf_leave_ok_pop");
							window.setTimeout("document.location.reload();",5000);
						}else
						{
							$("__ghf_out_group_pop").hide();							
							window.setTimeout("document.location.reload();",5000);
						}
					}
				});
			}else
			{
				this.showDiv("__ghf_out_group_pop");
				if($("__ghf_out_group_pop_exit"))
				{
					$("__ghf_out_group_pop_exit").observe("click",function(e)
						{
							GHF.Utils.leavegroup(gid);
						});
				}
			}
		}
	},
	/**
	*	显示弹出
	**/
	showDiv : function(div)
	{
		if($(div))
		{		
			$(div).show();
			var top = parseInt(document.scrollTop ? document.scrollTop : document.documentElement.scrollTop);
			var left = parseInt(document.scrollLeft ? document.scrollLeft : document.documentElement.scrollLeft);
			var bh = parseInt(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
			var bw = parseInt( document.documentElement.clientWidth ?  document.documentElement.clientWidth : document.body.clientWidth ); 		
			top += (bh - $(div).getHeight()) / 2;			
			left += (bw - $(div).getWidth()) / 2;	   		
			$(div).setStyle({position:"absolute",
					top : top + "px",
					left : left + "px",
					backgroundColor : "#FFFFFF"
			});
		}
	},
	urlids : ["__ghf_url_home","__ghf_url_album","__ghf_url_member"],
	urlSelected : function()
	{
		var url = document.location.pathname;
		if(/^\/[a-zA-Z0-9_-]+[\/]?$/i.test(url))
		{
			GHF.Utils.addB("__ghf_url_home");
		}else if(/^\/[a-zA-Z0-9_-]+\/member[0-9_P]?\.htm[l]?$/i.test(url))
		{
			GHF.Utils.addB("__ghf_url_member");
		}else if(/^\/[a-zA-Z0-9_-]+\/album\/[\d]+\.htm[l]?$/i.test(url))
		{
			var id = url.replace(/^\/[a-zA-Z0-9_-]+\/album\/([\d]+)\.htm[l]?$/i,"$1");			
			GHF.Utils.addB("__ghf_url_album_" + id);
		}
	},
	addB : function(id)
	{
		if($(id))
		{
			$(id).update("<b>" + $(id).innerHTML + "</b>");
		}
	},
	/**
	*	申请加入该社区
	**/
	jion_group : function()
	{
		if($("__ghf_join_group_pop"))
		{
			this.showDiv("__ghf_join_group_pop");
			if($("__ghf_join_group_pop_text"))
			{
				$("__ghf_join_group_pop_text").update("你确定要加入该社区吗？");
			}
		}
	},
	/**
	*	隐藏所有的sleect
	**/
	allSelect : [],
	hide_select_all : function()
	{
		this.allSelect = [];
		var list = document.getElementsByTagName("select");
		for(var i = 0; i < list.length ; i++ )
		{
			if($(list[i]))
			{
				if($(list[i]).visible())
				{
					this.allSelect.push($(list[i]));
					$(list[i]).hide();
				}
			}
		}
	},
	show_select_all : function()
	{
		for(var i = 0 ; i < this.allSelect.size(); i++ )
		{
			this.allSelect[i].show();
		}
	}
}
/**
*  定时去取消息中心的消息数据
**/
Event.observe(window,"load",function(e){
		GHF.Utils.urlSelected();
		GHF.Utils.getMsgCenter();
		window.setInterval(GHF.Utils.getMsgCenter,30000);
	});

function debug(o)
{
	var d = $("__ghf_debug");
	if(d == null)
	{
		d = document.createElement("div");
		d.style.border = "2px solid #FF0000";
		d.style.width = "400px";
		d.style.height = "400px";
		d.style.overflow = "auto";
		d.style.backgroundColor = "#FFF";
		d.id = "__ghf_debug" ;
		d.style.position = "absolute";
		d.style.top = "100px";
		d.style.left = "0px";
		document.body.appendChild(d);
		d = Element.extend(d);
	}
	d.observe("dbclick",function(ev){
		var dd = Event.element(ev)
		{
			if(dd.getHeight() > 300)
			{
				dd.setStyle({height:"20px",width:"20px"});
			} else
			{
				dd.setStyle({height:"400px",width:"400px"});
			}
		}
	});
	for(var i in o)
	{
		d.innerHTML += i + " == " + o[i] + "<br />";
	}
}
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  {
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    {
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + '; path=/ ; domain=.youban.com';
}
function to_focus(id ,obj) {
		var ele = obj.parentNode.getElementsByTagName('li');
		ele[1].className = 'b';
		ele[2].className = 'b';
		if(id == 0) { $('au_week').show() ; $('au_history').hide() ;  obj.className = 'a';}
		if(id == 1) { $('au_week').hide() ; $('au_history').show() ;  obj.className = 'a';}
}
function to_show(obj , k) {
                var ele = obj.parentNode.childNodes;
                for(var i = 0 ; i < ele.length ; i++) {
                        try {
                                ele[i].childNodes[1].style.display = 'none';
                                ele[i].childNodes[5].style.display = 'none';
                                ele[i].childNodes[0].className ='';
                                } catch(e) {}
                        }
                obj.childNodes[0].className='a';
		obj.childNodes[0].className='a';
		$("f1_"+k).show();
		$("u1_"+k).show();
}
function to_show1(obj , k) {
		var ele = obj.parentNode.childNodes;
		for(var i = 0 ; i < ele.length ; i++) {
			try {
				ele[i].childNodes[1].style.display = 'none';
				ele[i].childNodes[5].style.display = 'none';
				ele[i].childNodes[0].className ='';
				} catch(e) {}
			}
		obj.childNodes[0].className='a';
		$("f2_"+k).show();
		$("u2_"+k).show();
}
function check_form() {
	if($('title').value.replace(/\s*/g,'').length  == 0 || $('title').value == "标题长度40字内") { alert('请输入帖子标题'); return false; } 
	if($('content').value.replace(/\s*/g,'') == 0) { alert('内容不能为空'); return false; }
}
function get_subkind(sid ,gid) {
                                new Ajax.Request("/group_utils.php?cmd=subkind&gid="+ gid +"&subid=" + sid +"&"+Math.random() ,{
                                        onSuccess : function(r)
                                        {
						try {
						var obj = eval('('+r.responseText+')');
						if(obj.count == 0) { $('subkind').hide(); $('s_desc').hide() } else { $('subkind').show() ; $('s_desc').show();}
						$('subkind').options.length = 0;
						for(var i = 0 ; i < obj.list[0].list.length; i++) {
								var v = obj.list[0].list[i].subkindid ;
								var t = obj.list[0].list[i].subkindname ;
								$('subkind').options[i] = new Option(t,v);
							}
						}
						catch(e){}
                                        }
                                });
}
