
//弹出层
function openLayer(objId, conId) {
    var arrayPageSize = getPageSize(); //获取页面实际大小
    var arrayPageScroll = getPageScroll(); //获取滚动条的高度
    if (!document.getElementById("popupAddr")) {
        //创建弹出内容层
        var popupDiv = document.createElement("div");
        //给这个元素设置属性与样式
        popupDiv.setAttribute("id", "popupAddr")
        popupDiv.style.position = "absolute";
        popupDiv.style.border = "1px solid #ccc";
        popupDiv.style.background = "#fff";
        popupDiv.style.zIndex = 999;
        //创建弹出背景层
        var bodyBack = document.createElement("div");
        bodyBack.setAttribute("id", "bodybg")
        bodyBack.style.position = "absolute";
        bodyBack.style.width = "100%";
        bodyBack.style.height = (arrayPageSize[1] + 'px');
        bodyBack.style.zIndex = 998;
        bodyBack.style.top = 0;
        bodyBack.style.left = 0;
        bodyBack.style.filter = "alpha(opacity=30)";
        bodyBack.style.opacity = 0.3;
        bodyBack.style.background = "#000";
        //创建iframe
        var iframe = document.createElement("IFRAME");
        iframe.setAttribute("id", "bodyIframe");
        iframe.style.position = "absolute";
        iframe.style.width = "100%";
        iframe.style.height = (arrayPageSize[1] + 'px');
        iframe.style.zIndex = 997;
        iframe.style.top = 0;
        iframe.style.left = 0;
        iframe.style.filter = "alpha(opacity=0)";
        iframe.style.opacity = 0;
        iframe.style.background = "#000";
        //实现弹出(插入到目标元素之后)
        var mybody = document.getElementById(objId);
        insertAfter(popupDiv, mybody); //执行函数insertAfter()
        insertAfter(bodyBack, mybody); //执行函数insertAfter()
        insertAfter(iframe, mybody); //执行函数insertAfter()
    }

    //显示背景层
    document.getElementById("bodybg").style.display = "";
    document.getElementById("bodyIframe").style.display = "";
    //显示内容层
    var popObj = document.getElementById("popupAddr")
    popObj.innerHTML = document.getElementById(conId).innerHTML;
    popObj.style.display = "";
    //让弹出层在页面中垂直左右居中(统一)
    //	popObj.style.width  = "600px";
    //	popObj.style.height = "400px";
    //	popObj.style.top  = arrayPageScroll[1] + (arrayPageSize[3] - 35 - 400) / 2 + 'px';
    //	popObj.style.left = (arrayPageSize[0] - 20 - 600) / 2 + 'px';
    //让弹出层在页面中垂直左右居中(个性)
    var arrayConSize = getConSize(conId)
    popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2 - 5 + 'px'; //滚动条高度+(窗口高度－内容高度)／２－５０
    popObj.style.left = (arrayPageSize[0] - arrayConSize[0]) / 2 - 5 + 'px';
}
//获取内容层内容原始尺寸
function getConSize(conId) {
    var conObj = document.getElementById(conId)
    conObj.style.position = "absolute";
    conObj.style.left = -1000 + "px";
    conObj.style.display = "";
    var arrayConSize = [conObj.offsetWidth, conObj.offsetHeight]
    conObj.style.display = "none";
    return arrayConSize;
}
function insertAfter(newElement, targetElement)//插入
{
    var parent = targetElement.parentNode;
    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    }
    else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

//获取滚动条的高度
function getPageScroll() {
    var yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        yScroll = document.documentElement.scrollTop;
    }
    else if (document.body) {
        yScroll = document.body.scrollTop;
    }
    arrayPageScroll = new Array('', yScroll)
    return arrayPageScroll;
}

//获取页面实际大小
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) {
        sScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    //var pageHeight,pageWidth;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }


    var pageWidth, pageHeight
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

//关闭弹出层
function closeLayer() {
    document.getElementById("popupAddr").style.display = "none";
    document.getElementById("bodybg").style.display = "none";
    document.getElementById("bodyIframe").style.display = "none";
    return false;
}

//拖拽
//对“拖动点”定义：onMousedown="StartDrag(this)" onMouseup="StopDrag(this)" onMousemove="Drag(this)"即可
var move = false, oldcolor, _X, _Y;
function StartDrag(obj)//定义准备拖拽的函数
{
    obj.setCapture(); //对当前对象的鼠标动作进行跟踪
    oldcolor = obj.style.backgroundColor;
    obj.style.background = "#FBFBFB";
    move = true;
    //获取鼠标相对内容层坐标
    var parentwin = document.getElementById("popupAddr");
    _X = parentwin.offsetLeft - event.clientX
    _Y = parentwin.offsetTop - event.clientY
}

function Drag(obj) {        //定义拖拽函数
    if (move) {
        var parentwin = document.getElementById("popupAddr");
        parentwin.style.left = event.clientX + _X;
        parentwin.style.top = event.clientY + _Y;
    }
}
function StopDrag(obj)//定义停止拖拽函数
{
    obj.style.background = oldcolor;
    obj.releaseCapture(); //停止对当前对象的鼠标跟踪
    move = false;
}



function $(id) { return document.getElementById(id); }

//face
function face(obj, strface) {
    obj.value += strface;
    obj.focus();
}
function insertface(fid, tid) {
    var strface = "";
    strface += "<img src=../../UserFiles/face/01.gif width=20 height=20 onClick=face($('" + tid + "'),'[:Q]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/02.gif width=20 height=20 onClick=face($('" + tid + "'),'[:A]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/03.gif width=20 height=20 onClick=face($('" + tid + "'),'[:Z]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/04.gif width=20 height=20 onClick=face($('" + tid + "'),'[:W]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/05.gif width=20 height=20 onClick=face($('" + tid + "'),'[:S]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/06.gif width=20 height=20 onClick=face($('" + tid + "'),'[:X]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/07.gif width=20 height=20 onClick=face($('" + tid + "'),'[:E]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/08.gif width=20 height=20 onClick=face($('" + tid + "'),'[:D]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/09.gif width=20 height=20 onClick=face($('" + tid + "'),'[:C]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/10.gif width=20 height=20 onClick=face($('" + tid + "'),'[:R]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/11.gif width=20 height=20 onClick=face($('" + tid + "'),'[:F]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/12.gif width=20 height=20 onClick=face($('" + tid + "'),'[:V]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/13.gif width=20 height=20 onClick=face($('" + tid + "'),'[:T]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/14.gif width=20 height=20 onClick=face($('" + tid + "'),'[:G]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/15.gif width=20 height=20 onClick=face($('" + tid + "'),'[:B]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/16.gif width=20 height=20 onClick=face($('" + tid + "'),'[:Y]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/17.gif width=20 height=20 onClick=face($('" + tid + "'),'[:H]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/18.gif width=20 height=20 onClick=face($('" + tid + "'),'[:N]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/19.gif width=20 height=20 onClick=face($('" + tid + "'),'[:U]') style=cursor:pointer>&nbsp;&nbsp;";
    strface += "<img src=../../UserFiles/face/20.gif width=20 height=20 onClick=face($('" + tid + "'),'[:J]') style=cursor:pointer>&nbsp;&nbsp;";
    $(fid).innerHTML = strface;
}
//insert flash
function insertflash(flashurl, width, height, linkurl, target) {
    document.write('<div style="position:relative; height:' + height + 'px; width:' + width + 'px; margin-bottom:10px; overflow:hidden; float:left">');
    document.write('<EMBED style="position:absolute;z-index:0; left:0px; top:0px;" src="' + flashurl + '" quality="high" width="' + width + '" height="' + height + '" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" wmode="opaque">');
    document.write('<a href="' + linkurl + '" target="' + target + '" style="cursor:pointer" style="position:absolute;filter:alpha(opacity=0);-moz-opacity:0;left:0;top:0; background:#CDEAF6; width:' + width + 'px; height:' + height + 'px; z-index:10; display:block">');
    document.write('</a></div>');
}

//ajax object
function TryThese() {
    for (i = 0; i < arguments.length; i++) {
        try {
            return arguments[i]();
        } catch (e) { }
    }
    return false;
}
function CreateXMLHTTP() {
    return TryThese(
                function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
                function() { return new ActiveXObject("Microsoft.XMLHTTP"); },
                function() { return new XMLHttpRequest(); }
            ) || false;
}

function SendRequest(url, param, method, echofun) {
    var xmlHTTP = CreateXMLHTTP();
    if (xmlHTTP) {
        xmlHTTP.onreadystatechange = function() {
            if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
                if (echofun != null) {
                    echofun(xmlHTTP.responseText);
                }
            }
        }
        xmlHTTP.open(method, url, true);
        xmlHTTP.setRequestHeader("Content-Length", param.length);
        xmlHTTP.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded"); //post方法必需设置此项
        xmlHTTP.send(param);
    }
    else {
        NoXMLHTTP();
    }
}

function NoXMLHTTP() {
    alert("Sorry, your browser doesn't support XMLHTTP");
}

//message
function Showad(nid) {
    param = "type=Showad&id=" + nid + "&r=" + Math.random();
    SendRequest("../../sn.ashx", param, "POST",
                        function(responseText) {
                            var array = responseText.split("|");
                            $("newtopad").innerHTML = array[0];
                            $("newleftad").innerHTML = array[1];
                            $("newrightad").innerHTML = array[2];
                        });
}
function Showpl(nid) {
    param = "type=Showpl&id=" + nid + "&r=" + Math.random();
    SendRequest("../../sn.ashx", param, "POST",
                        function(responseText) {
                            var array = responseText.split("|");
                            $("newspldl").innerHTML = array[0];
                            $("plPersonal").innerHTML = array[1];
                            $("plcount1").innerHTML = $("plcount2").innerHTML = array[2];
                            $("newhit").innerHTML = array[3];
                            $("newshot").innerHTML = array[4];
                            $("newsimglis").innerHTML = array[5];
                        });
}

function Shownewslist(nid) {
    param = "type=newslist&id=" + nid + "&r=" + Math.random();
    SendRequest("../../sn.ashx", param, "POST",
                        function(responseText) {
                            var array = responseText.split("|");
                            $("newsnav").innerHTML = array[0];
                            $("newright").innerHTML = array[1];
                        });
}


function SaveMSN(uname, nid) {
    if ($("textword").value.replace(/^\s+|\s+$/g, "") == "" || $("textword").value.length > 200) {
        alert('请输入200字符以内的评论内容!');
        $("textword").value = "";
        $("textword").focus();
        return false;
    }
    else {
        param = "type=savemsn&id=" + nid + "&uid=" + uname + "&utxt=" + $("textword").value + "&r=" + Math.random();
        SendRequest("../../sn.ashx", param, "POST",
                        function(responseText) {
                            if (responseText == "1") {
                                alert('评论成功！');
                                Showpl(nid);
                                window.location.hash = "ST";
                                $("textword").value = "";
                            }
                            else {
                                alert(responseText);
                                return;
                            }
                        });
    }
}
//login
function UserLogin(uid, pwd, che) {
    var temp = true;
    if (uid.length < 2) {
        $("loginmessage").innerHTML = '登陆名长度太短,不能小于2位!';
        $("txtuid").focus();
        return false;
    }
    if (pwd.length < 6) {
        $("loginmessage").innerHTML = '登陆密码长度太短,不能小于6位!';
        $("txtpwd").focus();
        return false;
    }
    if (temp == true) {
        $("loginmessage").innerHTML = '正在验证．．．';
        param = "action=login&uid=" + escape(uid) + "&pwd=" + escape(pwd) + "&che=" + escape(che) + "&r=" + Math.random();
        SendRequest("../../login.ashx", param, "POST",
                        function(responseText) {
                            if (responseText == "1") {
                                closeLayer();
                                window.location.href = window.location.href;
                            }
                            else {
                                $("loginmessage").innerHTML = responseText;
                            }
                        });
    }
}

function LoginOut() {
    param = "action=quit&r=" + Math.random();
    SendRequest("../../login.ashx", param, "POST",
                        function(responseText) {
                            if (responseText == "1") {
                                window.location.href = location.reload();
                            }
                        });
}

function ShowLoginbox() {
    param = "action=enter&r=" + Math.random();
    SendRequest("../../login.ashx", param, "POST",
                        function(responseText) {
                            $("toploginspan").innerHTML = responseText;
                            ShowCookie();
                        });
}

function ShowCookie() {
    param = "action=cookie&r=" + Math.random();
    SendRequest("../../login.ashx", param, "POST",
                        function(responseText) {
                            if (responseText != "1") {
                                $("txtuid").value = responseText;
                                $("CheckUid").checked = true;
                            }
                        });
}
function ResizeImages(id, maxwidth, maxheight) {
    var myimg, oldwidth, oldheight;
    var imgs = $(id).getElementsByTagName('img');
    for (i = 0; i < imgs.length; i++) {
        myimg = imgs[i];

        if (myimg.width > myimg.height) {
            if (myimg.width > maxwidth) {
                oldwidth = myimg.width;
                myimg.height = myimg.height * (maxwidth / oldwidth);
                myimg.width = maxwidth;
            }
        } else {
            if (myimg.height > maxheight) {
                oldheight = myimg.height;
                myimg.width = myimg.width * (maxheight / oldheight);
                myimg.height = maxheight;
            }
        }
    }
}

//加载页面
window.onload = function() {
    ResizeImages("newstxt", "600", "1000");
    Showad($("newid").innerHTML);
    Shownewslist($("newid").innerHTML);
    Showpl($("newid").innerHTML);
    insertface("faceimg", "textword");
    ShowLoginbox();
}