//document.domain = "hunantv.com";

var hunantv = {
	Browser : {  																		
	    IE:     !!(window.attachEvent && !window.opera),
	    Firefox : navigator.userAgent.indexOf('Firefox') > -1,
	    Opera:  !!window.opera,
	    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
	    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
  	},
  	main_web : window.location.hostname
  
};

function $Z(element) {
    return document.getElementById(element);
}

var Cookie = {
	addCookie : function (objName,objValue,objHours,objdomain)
		{//添加cookie
			var str = objName + "=" + escape(objValue);
			if(objHours > 0){//为0时不设定过期时间，浏览器关闭时cookie自动消失
				var date = new Date();
				var ms = objHours*3600*1000;
				date.setTime(date.getTime() + ms);
				str += "; domain="+(objdomain?"hunantv.com":document.domain)+"; expires=" + date.toGMTString();
			}
			document.cookie = str;
		},

	getCookie : function (objName)
		{//获取指定名称的cookie的值
			var arrStr = document.cookie.split("; ");
			for(var i = 0;i < arrStr.length;i ++){
				var temp = arrStr[i].split("=");
				if(temp[0] == objName) return decodeURIComponent(temp[1]);
			} 
		},

	delCookie : function (name)
		{//为了删除指定名称的cookie，可以将其过期时间设定为一个过去的时间
			var exp = new Date();    
		    exp.setTime(exp.getTime()-1000);    
		    var cval = Cookie.getCookie(name);
		    document.cookie = name + "=; domain="+ document.domain +"; expires=" + exp.toGMTString();
		},

	allCookie : function ()
		{//读取所有保存的cookie字符串
			var str = document.cookie;
			if(str == ""){
				str = "没有保存任何cookie";
			}
			alert(str);
		}
};
//Cookie.addCookie("co-cookie","co-enable",1,1);
var user = {
	dismesg : "<div class=\"inner\"><ul class=\"list\"><li class=\"home\"><a href=\"http://login.hunantv.com/login.php?ref="+encodeURIComponent(top.window.location.href)+"\" target=\"_blank\">登录</a></li><li><a href=\"http://login.hunantv.com\" target=\"_blank\">注册</a></li><li><a href=\"http://www.hunantv.com/pub/help.html\" target=\"_blank\">帮助</a></li></ul><span class=\"time\"></span><span class=\"weather\" style=\"display:none;\"></span></div>",
	disLoginmesg: '<div class="no"><div class="welcome">欢迎您，游客</div><div class="form"><form action="http://login.hunantv.com/login.php?ref=http://vblog.hunantv.com" method="post" ><p><strong>E-Mail</strong><input type="text" class="text" name="user" value="" /><input name="rem" value="1" type="checkbox" />记住我</p><p><strong>密　码</strong><input type="password" class="text" value="" name="pwd" /><input type="hidden" name="su" value=1 /><input class="btn" type="submit" value="登录" /></p><p class="op"><a href="http://login.hunantv.com" target="_parent">立即注册</a> <!--a href="#">忘记密码</a--></p></form></div></div>',
	disDivName : "topbar",
	disDivLoginName : "indexlogin",
	headerName : "toplogin",
	divLoginCon : '<iframe src="" id="header_iframe" name="header_iframe" width=0 height=0 frameborder="0"></iframe>',
	co : Cookie.getCookie("TICKET"),
	tempdata:'',
	userdata : Cookie.getCookie("userdata"),
	addURL:document.URL,
	userLogin:function ()
		{
			var obj= $Z("uname").value;
			var url=hunantv.main_web+"/source/php/userlogin.php";	
			callback=this.userLogin_back;
			data="username="+obj+"&password="+$Z("pwd").value;
			Request.reSend(url,data,callback);	
		},
	userLogin_back:function (httpObj)
		{
			var obj = httpObj.responseText ;
			if(obj==1)
			user.userMesg();
				
		},
	userMesg:function ()
		{
			var obj= Cookie.getCookie("TICKET");			
			if (typeof(obj)=="undefined")
			{
				this.userLoginF();
				return false;
			}
			var v=arguments[0]?arguments[0]:1;
			//var url = hunantv.Browser.Firefox?hunantv.main_web:"";
			var url = hunantv.main_web;
			url = "http://"+url + "/source/php/usermesg.php?obj="+obj;
			callback=(v==2?this.action_login_back:this.userMesg_Login_back);
			data="";
			Request.reSend(url,data,callback);
		},
	
	userMesg_Cookie:function (data,v)
		{
			eval("var obj=("+data+")");
			if (obj["co-vblog"]!=this.co){ this.userMesg();return false;};
			
			if (v=="1")
			{
				user.userDownLogin(obj);
			}else
			{
				user.userLoginS(obj);
			}
				
		},
	action_login_back:function (httpObj)
		{
			//Cookie.addCookie("userdata",httpObj.responseText,2);
			eval("var obj=("+httpObj.responseText+")");
			alert(constructor(obj));
			if (obj=="-1")
			{
				user.userLoginF();	
			}else
			{
				if ($Z("login")){$Z("login").style.display="none";};
				window.location.replace("http://vblog.hunantv.com/viviup/upload_msdz.php?tagstype=0&header=0&tags=12&label=1&company=&aimurl=L215c3BhY2UvdmxvZ192bS5waHA=");
			}
				
		},
	userMesg_Login_back:function (httpObj)
		{
			eval("var obj=("+httpObj.responseText+")");
			if (obj=="-1")
			{
				user.userLoginF();	
			}else
			{
				user.userdata=true;
				user.userDownLogin(obj);
				//Cookie.addCookie("userdata",httpObj.responseText,2,0);
				if ($Z(user.disDivLoginName)){user.userLoginS(obj);};
			}
				
		},
		
	userUnLogin:function ()
		{
			alert("还未登陆");
		},
	userLoginF:function ()
		{
			alert("登陆失败！用户名密码错误或网络连接失败请重试！");
			if ($Z(this.disDivName))
			$Z(this.disDivName).innerHTML = this.dismesg;
			if ($Z(this.disDivLoginName))
			$Z(this.disDivLoginName).innerHTML = this.disLoginmesg;
			return false;
		},
	userLoginS:function (obj)
		{
			var logindata='<div class="yes">';
            logindata += '<div class="welcome"><span class="r"><a href="#" onclick=\"user.logout();\" style="padding-right:15px;">[退出]</a></span>欢迎您，<a class="name" href="/u/u-'+obj["uid"]+'/" target="_parent">'+obj["nickname"]+'</a></div>';
            logindata += '	<div class="userinfo">';
    		logindata += '<dl>';
        	logindata += '<dt><a href="http://space.hunantv.com/?'+obj["comsenz_uid"]+'" target="_blank"><img src="http://space.hunantv.com/avatar.php?uid='+obj["comsenz_uid"]+'" /></a><span><a href="http://space.hunantv.com/spacecp.php?action=userspaces" target="_blank">更改头像</a></span></dt>';
            logindata += '<dd class="op"><a href="http://space.hunantv.com/spacecp.php?action=spaceblogs&op=add" class="blog" target="_blank">发布日志</a></dd>';
            logindata += '<dd class="op"><a href="http://vblog.hunantv.com/upload/upload.php" class="video" target="_blank">上传视频</a></dd>';
            logindata += '<dd class="op"><a href="http://space.hunantv.com/spacecp.php" class="space" target="_blank">空间管理</a></dd>';
            logindata += '<dd class="op"><a href="http://space.hunantv.com/spacecp.php?action=userspaces" class="setting" target="_blank">个人设置</a></dd>';
            logindata += '<dd class="num"><a href="http://space.hunantv.com/spacecp.php?action=spaceblogs" target="_blank">日志</a><a href="http://vblog.hunantv.com/user/myvideo.php" target="_blank">视频</a></dd>';
		    logindata += '</dl>';
		    logindata += '</div>';
			logindata += '</div>';
            
            $Z(this.disDivLoginName).innerHTML=logindata;
			
		},
	userDownLogin:function (obj)
		{
			if (this.addURL.match(RegExps.isUser))
			{
			this.dismesg='<div class=\"inner\"><strong>欢迎</strong>,<a href="http://space.hunantv.com/?'+obj["comsenz_uid"]+'/" target="_blank">'+decodeURIComponent(obj["nickname"])+'</a> <a href="http://space.hunantv.com/spacecp.php" target="_parent">空间管理</a> <a href="http://vblog.hunantv.com/user/myvideo.php" target="_parent">播客管理</a> <a onclick=\"user.logout();\" href="#" >登出</a> <a href=\"http://www.hunantv.com/pub/help.html\" target=\"_blank\">帮助</a>'+this.divLoginCon+'</div>';
		  }else{
		  this.dismesg='<div class=\"inner\"><strong>欢迎</strong>,<a href="http://space.hunantv.com/?'+obj["comsenz_uid"]+'/" target="_blank">'+decodeURIComponent(obj["nickname"])+'</a> <a href="#" onclick=\"user.logout();\">登出</a> <a href=\"http://www.hunantv.com/pub/help.html\" target=\"_blank\">帮助</a>'+this.divLoginCon+'</div>';
		  }
			if ($Z(this.disDivName)) $Z(this.disDivName).innerHTML=this.dismesg;			
		},
	setTimeFun:function (fun,time_d)
		{
		
			window.setTimeout(fun, time_d);
		
		},
	uwrite:function(v)
		{
		 	//Cookie.addCookie("co-cookie","co-enable",1,1);
		 	if(typeof(Cookie.getCookie("TICKET"))!="undefined" && typeof(Cookie.getCookie("userdata"))=="undefined") {  this.userMesg();}
		 	else if(typeof(Cookie.getCookie("TICKET"))!="undefined" && typeof(Cookie.getCookie("userdata"))!="undefined") {  this.userMesg_Cookie(this.userdata,1); return false;}
		 	else { $Z(this.disDivName).innerHTML = this.dismesg;}
		 	
		},
	indexloginwrite:function()
		{
		 	
		 	if(typeof(Cookie.getCookie("TICKET"))!="undefined" && typeof(Cookie.getCookie("userdata"))=="undefined") {setTimeout('',2000); this.userMesg();}
		 	else if(typeof(Cookie.getCookie("TICKET"))!="undefined" && typeof(Cookie.getCookie("userdata"))!="undefined") {  this.userMesg_Cookie(this.userdata,2); return false;}
		 	else {	$Z(this.disDivLoginName).innerHTML = this.disLoginmesg;}
		},
	actionlogin:function()
		{
		 	if(typeof(Cookie.getCookie("userdata"))!="undefined" && typeof(Cookie.getCookie("uname"))!="undefined") { window.location.href="http://show.hunantv.com/viviup/upload_msdz.php?tagstype=0&header=0&tags=12&label=1&company=&aimurl=L215c3BhY2UvdmxvZ192bS5waHA=";}
		 	else {	$Z("login").style.display = "block";}
		},
	logout:function(v)
		{
			Cookie.delCookie("userdata");
			var url = "http://login.hunantv.com/logout.php?ref="+encodeURIComponent(window.top.location);
			window.top.location.replace(url);
		},
	logout_reload:function(v)
		{
			$Z("userloin_header").action="http://bbs.hunantv.com/showlogin.php?action=login";
			this.dismesg = this.ldismesg;
			user.uwrite();
			this.disLoginmesg = this.ldisLoginmesg;
			if ($Z("block_login_side_ok")){ user.indexloginwrite();}
			
		}

};


function trim(_s){
 return _s.replace(/(^\s*)|(\s*$)/g,"");
}

var RegExps = function(){};
RegExps.isUser = /uc|user/;

//下拉菜单
function showmenu(menu,status){
		objmenu = document.getElementById(menu);
		if(status){
			objmenu.style.display = "block";
		}else{
			objmenu.style.display = "none";
		}
	}