//把$让给第一个实现它的库，用jq代替
if(typeof(jq)=="undefined")
{
    jq = jQuery.noConflict(); 
}

//function $(element)
//{
//  return document.getElementById(element);
//}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

String.prototype.replaceAll = function(s1,s2)
{
    return this.replace(new RegExp(s1,"gm"),s2);   
}

/*获取绝对位置*/
function calculateOffsetTop(field)
{
    return calculateOffset(field,"offsetTop");
}
function calculateOffsetLeft(field)
{
    return calculateOffset(field,"offsetLeft");
}
function calculateOffset(field, attr)
{
    var offset = 0;
    while(field)
    {
        offset += field[attr]; 
        field = field.offsetParent;
    }
    return offset;
}

/*获取参数*/
function getUrlParam()
{
	var Url = top.window.location.href;
	var u,g,StrBack='';
	if(arguments[arguments.length-1]=="#")
	    u=Url.split("#");
	else
	    u=Url.split("?");
	if (u.length==1) 
	    g='';
	else
	    g=u[1];
	if(g!='')
	{
		gg=g.split("&");
		var MaxI=gg.length;
		str = arguments[0]+"=";
		for(i=0;i<MaxI;i++)
		{
			if(gg[i].indexOf(str)==0) 
			{
			    StrBack=gg[i].replace(str,"");
			    break;
			}
		}
	}
	return StrBack;
}


//设为首页
function setHomepage()
{
     if (document.all)
     {
         document.body.style.behavior='url(#default#homepage)';
         document.body.setHomePage('http://' +document.location.host);
     }
     else if (window.sidebar)
     {
         if(window.netscape)
         {
             try
             {
                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
             }
             catch(e)
             {
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
             }
         }
         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
         prefs.setCharPref('browser.startup.homepage', 'http://' +document.location.host);
     }
}


// 添加到收藏夹
function addBookmark()
{
	var title=document.title;
	var url=document.location.href;
	if (window.sidebar)
	    window.sidebar.addPanel(title, url,"");
	else if( window.opera && window.print )
	{
	    var mbm = document.createElement('a');
	    mbm.setAttribute('rel','sidebar');
	    mbm.setAttribute('href',url);
	    mbm.setAttribute('title',title);
	    mbm.click();
	}
	else if( document.all )
	    window.external.AddFavorite(url, title);
}


//搜索，清空提示
function SearchNoTip(searchTextId)
{
	var obj = document.getElementById(searchTextId);
	if(obj.value == "请输入主播")
	{
		obj.value="";
	}
}

//搜索，加上提示
function SearchTip(searchTextId)
{
	var obj = document.getElementById(searchTextId);
	if(obj.value.trim() == "")
	{
		obj.value = "请输入主播";
		return;
	}
}
//回车,搜索
function huiche(searchTextId)
{
    if(document.addEventListener)
    {//如果是Firefox  
        document.addEventListener("keypress",_fireFoxSearchHandler(evt,searchTextId) , true);         
    }
    else
    {  
        document.attachEvent("onkeypress",ieSearchHandler);  
    }  
}

//ff回车搜索
function fireFoxSearchHandler(evt,searchTextId)
{ 
    if(evt.keyCode==13)
    {
        if(typeof(searchTextId) == "undefined")
            searchMember("txtSearch");
        else
            searchMember(searchTextId);  
    }  
    else
    {
        return;
    } 
}
function _fireFoxSearchHandler(evt,searchTextId)
{ 
    return  fireFoxSearchHandler(evt,searchTextId);
}
//IE回车搜索    
function ieSearchHandler(evt,searchTextId)
{      
    if(evt.keyCode==13)
    {   
        if(typeof(searchTextId) == "undefined")
            searchMember("txtSearch");
        else
            searchMember(searchTextId);    
    }  
    else 
    {
        return;
    } 
} 

//搜索主播
function searchMember(searchTextId)
{
	var obj=document.getElementById(searchTextId);

	if(obj.value.trim() == "请输入主播"||obj.value.trim() == "" )
	{
		alert('请输入主播!');
		return;
	}
	else
	{
        var url="/Default.aspx?type=0&group=0&keyword=" +escape(obj.value.trim());//要改成搜索结果页
	    window.open(url, "search");
	}
}

//传进cookie的key 得到value
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        if(document.cookie.indexOf("U_info=")==-1)
        {
            return "";
        }
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        { 
            c_start = c_start + c_name.length+1 
            c_end = document.cookie.indexOf("&", c_start)
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end))
        } 
    }
    return "";
}

//传进cookie的key 得到value
function getCookie_(c_name)
{
    if (document.cookie.length>0)
    {
        if(document.cookie.indexOf("U_info=")==-1)
        {
            return "";
        }
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        { 
            c_start = c_start + c_name.length+1 
            c_end = document.cookie.indexOf("&", c_start)
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end))
        } 
    }
    return "";
}

//顶部登录状态
function TopState()
{
    var useridx=getCookie('useridx');
    var Thtmlcode = "";
    
    if(useridx!="0"&&useridx!="-1"&&useridx!="-2"&&useridx!="-3" &&(useridx.length)>0)//成功登录的
    {   
        jq.ajax
        ({
            url:"/ajax/memberinfo.aspx?"+(new Date()),
            type:"GET",
            success:function(data)
            {        
                var edata=eval('(' +data+ ')');

                //顶部登录状态
                var pic = "";
                var isvip = parseInt(edata["isvip"]);//是否Vip 
                var level = parseInt(edata["level"]);//等级
                if(isvip == 0)
                {
                    pic = "normal.gif";
                }
                else
                {
                                        switch(level)
                    {
                       case 100://巡管
                            pic = "icon_xunguan.gif"; break;
                        case 80://代理
                            pic = "icon_daili.gif"; break;
                        case 0://0-8都为VIP                
                            pic = "icon_vip.gif"; break;    
                        case 1:               
                            pic = "icon_vip.gif"; break;
                        case 2:
                            pic = "icon_vip1.gif"; break;
                        case 3:
                            pic = "icon_vip2.gif"; break;
                        case 4:
                            pic = "icon_vip3.gif"; break;   
                        case 5:
                            pic = "icon_vip4.gif"; break;
                        case 6:
                            pic = "icon_vip5.gif"; break;
                        case 7:
                            pic = "icon_vip6.gif"; break;
                        case 8:
                            pic = "icon_vip7.gif"; break;                     
                        default:                
                            pic = "normal.gif"; break;
                    }
                }
                var Thtmlcode = '';                    
            	Thtmlcode+='<ul class="clearfix"><li><img src="../images/'+pic+'" align="absmiddle" />&nbsp; <span class="c_light">';
            	Thtmlcode+='<strong>'+edata["nickname"]+'</strong>&nbsp;('+edata["idx"]+')</span>&nbsp;<a href="#1" onclick="logoutTop();">[退出]</a></li>';
            	Thtmlcode+='<li><span class="cash">'+edata["ownerbill"]+'</span></li>';
            	Thtmlcode+='<li><a href="/">返回首页</a></li>';
            	Thtmlcode+='<li><a href="javascript:setHomepage();">设为首页</a></li>';
            	Thtmlcode+='<li><a href="javascript:addBookmark();">收藏本页</a></li>';
            	Thtmlcode+='<li><a href="./web" target="_blank">帮助</a></li></ul>';                
                jq("#loginin").html(Thtmlcode);      
            }
        });//end ajax 
    }
    else
    {
        Thtmlcode += '<p class="fleft"><label>用户名:</label><input id="userid" name="userid" type="text" class="input_text" /></p>';
        Thtmlcode += '<p class="fleft"><label>密码:</label><input id="psw" name="psw" type="password" onkeypress="keyPressLogin();" value="" class="input_text" /></p>';
        Thtmlcode += '<p class="fleft"><label>验证码:</label><input id="Validate" name="Validate" type="text" class="input_text" style="float:left;width:30px;" maxLength="4" size="4" /><img id="Image1" width="60px" height="24px" alt="点击换一个" style="float:left;margin:5px 0px 0 5px;" src="ValidateNum.aspx?t=Math.random()" onclick="changeValidate();" /></p>';
        Thtmlcode += '<p class="fleft"><button type="button" class="sub" onclick="Login()"><span>登录</span></button><button type="button" class="reg" onclick="location.href=\'register.aspx\';"><span>注册</span></button></p>';
        jq("#loginin").html(Thtmlcode);
    }        
}

//写登录成功后的html
function memberInfo()
{
    jq.ajax
    ({
        url:"/ajax/memberinfo.aspx?" +(new Date()),
        type:"GET",
        success:function(data)
        {
            var edata=eval('('+data+')');
            //左则菜单
            var htmlcode='';
            htmlcode+='<h4>欢迎您：<span class="c_light"><strong>'+edata["nickname"]+'</strong>('+edata["idx"]+')</span></h4>';
            htmlcode+='<ul class="img"><li>';
            htmlcode+='<img src="'+edata["picurl"]+'" onerror="this.src=\'../images/tmp01.gif\';" /></li>';
            htmlcode+='</ul><ul class="active"><li><span class="cash">'+edata["ownerbill"]+'</span></li>';
            htmlcode+='<li><a href="mysend.aspx">管理中心</a></li>';
            htmlcode+='<li><a href="room_apply.aspx">主播申请</a></li>';
            htmlcode+='<li><a href="#1" onclick="logout();">[退出]</a></li>';
            htmlcode+='</ul>';
            htmlcode+='<ul class="btn">';
            htmlcode+='<li><a href="live.aspx?roomid='+edata["commendidx"]+'" title="进入房间">进入房间</a></li>';
            htmlcode+='</ul>';
            jq("#logined").html(htmlcode);
            
            TopState();//顶部登录状态
        }//end function
    });//end ajax
}  
    
//初始化窗口时加载,为了登录窗口
function initi()
{
    var useridx=getCookie('useridx');
    
    if(useridx!="0"&&useridx!="-1"&&useridx!="-2"&&useridx!="-3" &&(useridx.length)>0)//成功登录的
    {   
        memberInfo();
        document.getElementById("nologin").style.display="none";
        document.getElementById("logined").style.display="block";       
    }
    else
    {
        return;
    }
}
    
//退出登录,适用于左侧登录框
function logout()
{
    jq.ajax
    ({
        url:"/ajax/logout.aspx?" +(new Date()),
        type:"GET",
        success:function()
        {
            var logincookie=document.cookie;
            if(logincookie.length<1)//成功清除cookie
            {
                document.getElementById("login").style.display="block";
                document.getElementById("login_in").style.display="none";  
                
                document.getElementById("nologin").style.display="block";
                document.getElementById("logined").style.display="none"; 
                
                document.getElementById("userid").value="";
                document.getElementById("psw").value="";                         
            }
            else//传输较慢时
            {
                location.reload();                    
            }//end if                
        }//end function
    });
}
//退出登录,适用于顶部工具条登录框    
function logoutTop()
{
    jq.ajax
    ({
        url:"/ajax/logout.aspx?"+(new Date()),
        type:"GET",
        success:function()
	{
            var logincookie=document.cookie;
            if(logincookie.length<1)//成功清除cookie
            {                    
                document.getElementById("nologin").style.display="none";
                document.getElementById("logined").style.display="block";                          
            }//end if
             document.location.reload();
            //window.location.href=window.location.href;
        }//end function
    });//end ajax    
}
//退出登录,适用于个人管理中心，头部
function logoutForMyCenter()
{
    jq.ajax
    ({
        url:"/ajax/logout.aspx?"+(new Date()),
        type:"GET",
        success:function()
	{
            document.location.reload();//重载页面，如果没有cookie则跳转首页
        }//end function
    });//end ajax
}
//去过的房间
function goTrack()
{
    if(document.location.host=='localhost:4304')//调试用的本机
        document.location.href= 'http://'+document.location.host+"/web/track.aspx";        
    else//上线的服务器用
        document.location.href= 'http://'+document.location.host+"/track.aspx";
}

//收藏的房间
function goFavorite()
{
    if(document.location.host=='localhost:4304')//调试用的本机
        document.location.href= 'http://'+document.location.host+"/web/myfavorite.aspx";        
    else//上线的服务器用
        document.location.href= 'http://'+document.location.host+"/myfavorite.aspx";
}


//登录回车
function keyPressLogin()
{  
     if(document.addEventListener)
     {//如果是Firefox  
        document.addEventListener("keypress",fireFoxLoginHandler , true);         
     }
     else
     { 
        //document.attachEvent("onkeypress",ieLoginHandler); 
        var e = e || window.event;
        var currKey = e.keyCode || e.which || e.charCode;
        if (currKey==13) 
        {
            Login();
            return;
        } 
     }       
}

//ff回车登陆
function fireFoxLoginHandler(evt)
{ 
    if(evt.keyCode==13)
    {   
        Login();    
    }  
    else 
    {
        return;
    } 
}
//IE回车登录,由于然次执行，暂时不用  
function ieLoginHandler(evt)
{      
    if(evt.keyCode==13)
    {   
        Login();   
    }  
    else 
    {
        return;
    } 
} 
