﻿var hsid = "0";
var ishs = "0";
var hsname = "海顺分析师";
var ymobile = "";
var keystring = "";
var xmlHttp;
var RequestArray = new Array();
window.onload = win_load;
function win_load()
{ 
    setInterval(function() { gethsmsg();hidkymsg(); },1500); 
    setInterval(function() { getonlines();},5000); 
    Init();
}

function Init()
{
    $("txt_hsmsg").onkeyup = function() {if(event.keyCode==13){Hs_sendmsg();}};
    checkykcookie();
    $("li_showhsbox").ondblclick = showcheckbox;
    //deleteCookie("ykcookie");
}

function hidkymsg()
{
    var url = "?fun=gethidkyid&" + Math.random();
    addarray(url, true, "GET", null, hidkyhtml); 
}

function hidkyhtml()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            try
            {
                var res = xmlHttp.responseText;
                if(res!="0")
                {
                    var ores = res.split(',');
                    for(var i = 0;i<ores.length;i++)
                    {
                        var tempid = ores[i];
                        if(tempid!="")
                        {
                            if($(tempid)!=null)
                            {
                                $(tempid).style.display = "none";
                            }
                        }
                    }
                }
            }
            catch(e)
            {}
        }
        RequestArray.shift();
        ExeRequestArray();
    }    
}

function showcheckbox()
{
    if($("div_checkbox")==null)
    {
        var test_div=document.createElement("div"); 
        test_div.setAttribute("id","div_checkbox"); 
        var strhtml = "<div id=\"div_cb\" class=\"show_win\" onmousedown=\"MDown(div_cb)\" style=\"left: 500px;padding:10px;top: 150px;\" ><span class=\"s_close\"  onclick=\"hidcheckdiv()\" ></span><div class=\"show_con\">";
        strhtml += "<input id=\"txt_hscode\" type=\"text\" style=\"solid 1px #ccc;width:100px;height:30px;line-height:30px;\" onkeyup=\"logkeyup()\" />&nbsp;<input type=\"button\" value=\"登陆\" onclick=\"sendcheckcode()\" style=\"font-size:14px;width:60px;height:34px;line-height:34px;\" /></div></div>";
        test_div.innerHTML = strhtml;
        document.body.appendChild(test_div); 
        $("txt_hscode").focus(); 
        $("txt_hscode").onclick = function() { $("txt_hscode").focus();  };
    }
}

function logkeyup()
{
    if(event.keyCode==13)
    {
        sendcheckcode();
    }
}

function sendcheckcode()
{
    keystring = $("txt_hscode").value;
    checkhs();
    
}

function hidcheckdiv()
{
    document.body.removeChild($("div_checkbox"));    
}

function checkhs()
{
    var url = "?fun=checkhs&hskey=" + escape(keystring) + "&" + Math.random();
    addarray(url, true, "GET", null, showhsbox);    
}

function showhsbox()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            var res = xmlHttp.responseText;
            if(res!="nodata")
            {
                ishs = "1";yid = "0";hsid = "0";
                $("ul_hsmsg").innerHTML = "";
                $("div_hsbox").style.display = "block";
                $("btn_hssend").onclick = Hs_sendmsg;
                hsname = res;
                addCookie("ykcookie",hsname,2);
                getfocus();    
                hidcheckdiv();            
            }
            else
            { alert("输入有误"); }
        }
        RequestArray.shift();
        ExeRequestArray();
    }
}

function createXMLHttpRequest() 
{
    if (window.ActiveXObject) 
    {        
        try 
        { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e1) 
        { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e2) { } }
    }
    else if (window.XMLHttpRequest) 
    { xmlHttp = new XMLHttpRequest(); }
}

function addarray(url, isAsy, method, parStr, callBackFun) {
    var ArgItem = new Array();
    ArgItem[0] = url; 
    ArgItem[1] = isAsy; 
    ArgItem[2] = method; 
    ArgItem[3] = parStr; 
    ArgItem[4] = callBackFun; 
    RequestArray.push(ArgItem); 
    if (RequestArray.length == 1)  
    { ExeRequestArray(); }
}

function ExeRequestArray() {
    if (RequestArray.length > 0) {
        var ArgItem = RequestArray[0];
        DoRequest(ArgItem[0], ArgItem[1], ArgItem[2], ArgItem[3], ArgItem[4]);
    }
}

function DoRequest(url, isAsy, method, parStr, callBackFun) {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = callBackFun;
    xmlHttp.open(method, url, isAsy);
    xmlHttp.send(parStr);
}

var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

//检测文字长度
function checktxtlen()
{
    var msglen = $("txt_ykmsg").value.length;
    var txtnlen = $("txtnlen");
    txtnlen.innerHTML = msglen;
    if(msglen>100)
    { txtnlen.style.color = "#f00"; }
    else
    { txtnlen.style.color = "#fff"; }
    if(event.keyCode==36)
    {
        var hskey = $("txt_ykmsg").value;        
    }
    else if(event.keyCode==13)
    {
        yk_sendmsg();
    }
}

function getfocus()
{
    $("txt_hsmsg").focus();
}

function getonlines()
{
     var url = "?fun=getonline&" + Math.random();
     addarray(url, true, "GET", null, showonlines);    
}

function showonlines()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            var res = xmlHttp.responseText;
            $("div_onlines").innerText = res;
        }
        RequestArray.shift();
        ExeRequestArray();
    }
}

/*显示分析师信息*/
function gethsmsg()
{
    var url = "?fun=gethsmsg&hsid=" + hsid + "&" + Math.random();
    addarray(url, true, "GET", null, showhsmsg); 
}

function showhsmsg()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            var oxml = xmlHttp.responseText;
            if(oxml!="nodata")
            {
                var objDom = new ActiveXObject('Microsoft.XMLDOM'); 
                objDom.loadXML(oxml);                
                var nodes = objDom.documentElement.childNodes;  
                var reslength = nodes.length;                   
                for (var i = 0; i < reslength; i++) 
                {
                    var autoid = nodes[i].childNodes[0].text;
                    var hs_name = nodes[i].childNodes[1].text;                    
                    var MsgText = nodes[i].childNodes[2].text;
                    if(MsgText.indexOf('img|')!=-1)
                    {                        
                        MsgText = "<img src=\"TalkImg/min"+MsgText.split('|')[1]+".jpeg\" style=\"cursor:pointer;\" onclick=\"showimg('"+autoid+"','"+MsgText.split('|')[1]+"')\" alt=\"点击查看大图\" />";
                    }
                    
                    var addtime = nodes[i].childNodes[3].text;
                    addtime = addtime.split(' ')[1];
                    var color = (Number(autoid)%2!=1)?"li_data_1":"li_data_2";        
                    var strdel = "";
                    if(ishs=="1") { strdel = "<span><img src=\"img/del.gif\" style=\"width:16px;height:16px;cursor:pointer\" onclick=\"delhsmsg('"+autoid+"')\"></span>"; }       
                    $("ul_hsmsg").innerHTML = "<li id=\"li_hs"+autoid+"\" class=\""+color+"\"><span class=\"sendName\">"+hs_name+"  "+addtime+"</span>"+strdel+"<br />"+MsgText+"</li>" + $("ul_hsmsg").innerHTML;                                      
                }
                $("div_hsmsg").scrollTop = 0;
                hsid = nodes[Number(reslength)-1].childNodes[0].text;                
            }
        }
        RequestArray.shift();
        ExeRequestArray();
    }
}

function delhsmsg(hsautoid)
{
     var url = "?fun=delhsmsg&hsautoid="+hsautoid+"&" + Math.random();
     addarray(url, true, "GET", null, delhsresult);   
}

function delhsresult()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            var res = xmlHttp.responseText;
            if(res=="0")
            { alert("删除数据时发生异常"); }
            else            
            { $("li_hs"+res).style.display = "none"; }
        }
        RequestArray.shift();
        ExeRequestArray();
    }    
}

function checkykcookie()
{
    var ykcookie = getCookie("ykcookie");
    if(ykcookie!=""&&ykcookie!="v")
    {
        ykcookie = unescape(ykcookie);
    }    
}

function Hs_sendmsg()
{
    var hsmsgtxt = $("txt_hsmsg").value;
    if(hsmsgtxt!="")
    {
        if(hsmsgtxt.length < 201)
        {             
            hsmsgtxt = hsmsgtxt.replace("<","&lt;").replace(">","&gt;");                   
            hsmsgtxt = escape(hsmsgtxt);
             var url = "?fun=sendhsmsg&hsname=" + escape(hsname) + "&hsmsgtxt="+hsmsgtxt+"&" + Math.random();
             addarray(url, true, "GET", null, res_hssdmsg);    
        }
        else
        { alert("发言内容超过最大长度！"); }
    }
    else
    { alert("发言内容不能为空哦！"); }
}

function res_hssdmsg()
{
    if (xmlHttp.readyState == 4) 
    {
        if (xmlHttp.status == 200) 
        {
            var res = xmlHttp.responseText;
            if(res!="1")
            {
               //$("ul_hsmsg").innerHTML += "<li class=\"li_onli_1\"><span style=\"color:green;\">【系统提示】提交失败，请刷新页面后重试!</span></li>";                
            }                        
            $("div_hsmsg").scrollTop = 0;             
            $('txt_hsmsg').value = "";                                           
        }
        RequestArray.shift();
        ExeRequestArray();
    }
}

/*添加Cookie*/
function addCookie(name, value, expireHours) {
    var cookieString = name + "=" + escape(value);
    /*判断是否设置过期时间*/
    if (expireHours > 0) {
        var date = new Date();
        if (expireHours == 2)
        { date.setTime(date.getTime() + 86400000); }
        else
        { date.setTime(date.getTime() + 31536000000); }
        cookieString = cookieString + "; expires=" + date.toGMTString();
    }
    document.cookie = cookieString;
}
/*获取指定name的Cookie值*/
function getCookie(name) {
    try {
        var strCookie = document.cookie;
        var arrCookie = strCookie.split("; ");
        for (var i = 0; i < arrCookie.length; i++)
        { var arr = arrCookie[i].split("="); if (arr[0] == name) return arr[1]; }
        return "";
    }
    catch (e) { return ""; }
}
/*删除指定名称的Cookie,cookie对象过期会自动删除*/
function deleteCookie(name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=v; expire=" + date.toGMTString();
}

var Obj=''
document.onmouseup=MUp
document.onmousemove=MMove

function MDown(Object){
Obj=Object.id
document.getElementById(Obj).setCapture()
pX=event.x-document.getElementById(Obj).style.pixelLeft;
pY=event.y-document.getElementById(Obj).style.pixelTop;
}

function MMove(){
if(Obj!=''){
	document.getElementById(Obj).style.left=event.x-pX;
	document.getElementById(Obj).style.top=event.y-pY;
	}
}

function MUp(){
if(Obj!=''){
	document.getElementById(Obj).releaseCapture();
	Obj='';
	}
}


function bookmark() 
{
    var title = "海顺股市直播室";
    var url = "http://zhibo.haishun.com/";
  if (document.all)
    window.external.AddFavorite(url, title);
  else if (window.sidebar)
    window.sidebar.addPanel(title, url, "")
}

function intval(v) { v = parseInt(v); return isNaN(v) ? 0 : v; }  /* 获取元素信息*/ 
function getPos(e) { var l = 0; var t = 0; var w = intval(e.style.width); var h = intval(e.style.height); var wb = e.offsetWidth; var hb = e.offsetHeight; while (e.offsetParent) { l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0); t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0); e = e.offsetParent; } l += e.offsetLeft + (e.currentStyle ? intval(e.currentStyle.borderLeftWidth) : 0); t += e.offsetTop + (e.currentStyle ? intval(e.currentStyle.borderTopWidth) : 0); return { x: l, y: t, w: w, h: h, wb: wb, hb: hb }; }  // 获取滚动条信息 
function getScroll() { var t, l, w, h; if (document.documentElement && document.documentElement.scrollTop) { t = document.documentElement.scrollTop; l = document.documentElement.scrollLeft; w = document.documentElement.scrollWidth; h = document.documentElement.scrollHeight; } else if (document.body) { t = document.body.scrollTop; l = document.body.scrollLeft; w = document.body.scrollWidth; h = document.body.scrollHeight; } return { t: t, l: l, w: w, h: h }; }  // 锚点(Anchor)间平滑跳转 
function scroller(el, duration) { if (typeof el != 'object') { el = document.getElementById(el); } if (!el) return; var z = this; z.el = el; z.p = getPos(el); z.s = getScroll(); z.clear = function () { window.clearInterval(z.timer); z.timer = null }; z.t = (new Date).getTime(); z.step = function () { var t = (new Date).getTime(); var p = (t - z.t) / duration; if (t >= duration + z.t) { z.clear(); window.setTimeout(function () { z.scroll(z.p.y, z.p.x) }, 13); } else { st = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.y - z.s.t) + z.s.t; sl = ((-Math.cos(p * Math.PI) / 2) + 0.5) * (z.p.x - z.s.l) + z.s.l; z.scroll(st, sl); } }; z.scroll = function (t, l) { window.scrollTo(l, t) }; z.timer = window.setInterval(function () { z.step(); }, 13); }


function insertimg()
{
    if($("div_test")==null)
    {
        var test_div=document.createElement("div"); 
        test_div.setAttribute("id","div_test"); 
        test_div.innerHTML = "<div id=\"div_msgbox\" class=\"show_win\"  style=\"left: 500px;padding:10px;top: 150px;  \" onmousedown=\"MDown(div_msgbox)\" ><span class=\"s_close\" onclick=\"hideinsertdiv()\" ></span><div class=\"show_con\"><iframe src=\"InsertImg.aspx\" frameborder=\"0\" width=\"400\" scrolling=\"no\" height=\"300\"></iframe></div></div>";
        document.body.appendChild(test_div);     
    }
}

function hideinsertdiv()
{
    document.body.removeChild($("div_test"));    
}

function showimg(autoid,imgname)
{
    if($("div_showimg_"+autoid)==null)
    {
        var test_div=document.createElement("div"); 
        test_div.setAttribute("id","div_showimg_"+autoid); 
        test_div.innerHTML = "<div id=\"div_"+autoid+"\" class=\"show_win\" onmousedown=\"MDown(div_"+autoid+")\" style=\"left: 500px;padding:10px;top: 150px;\" ><span class=\"s_close\"  onclick=\"hidimgdiv('"+autoid+"')\" ></span><div class=\"show_con\"><img src=\"TalkImg/max"+imgname+".jpeg\" /></div></div>";
        document.body.appendChild(test_div);    
    }
}

function hidimgdiv(autoid)
{
    document.body.removeChild($("div_showimg_"+autoid));    
}




function addEventSimple(obj,evt,fn)
{
    if(obj.addEventListener)
    {
        obj.addEventListener(evt,fn,false);
    }
    else if(obj.attachEvent)
    {
        obj.attachEvent('on'+evt,fn);
    }
}

addEventSimple(window,'load',initScrolling);
var scrollingBox;
var scrollingInterval;
var reachedBottom=false;
var bottom;
function initScrolling()
{
    scrollingBox = document.getElementById("div_ykmsg");
    scrollingBox.style.height = "470px";
    scrollingBox.style.overflow = "hidden";
    scrollingInterval = setInterval("scrolling()",50);
    scrollingBox.onmouseover = over;
    scrollingBox.onmouseout = out; 
}

function scrolling()
{
    var origin = scrollingBox.scrollTop++;
    if(origin == scrollingBox.scrollTop)
    {
        if(!reachedBottom)
        {
            scrollingBox.innerHTML+=scrollingBox.innerHTML;
            reachedBottom=true;
            bottom=origin;
        }
        else
        {
            scrollingBox.scrollTop=bottom;
        }
    }
}

function over()
{
    clearInterval(scrollingInterval);
}
function out()
{
    scrollingInterval = setInterval("scrolling()",50);
}
