﻿function H_GetRootPath()
{
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;
    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
}
var m_WebPath = H_GetRootPath();

//------------设置Cookie----------
function H_SetCookie(name, value, minPra, path, domain, secure) {
    var nameString = name + "=" + escape(value);
    var expires = new Date();
    var expiryString = ((minPra) ? ";expires=" + expires.toGMTString(expires.setTime(expires.getTime() + minPra * 1000 * 60)) : '');
    var pathString = ((path) ? ";path=" + path : "");
    var domainString = ((domain) ? ";domain=" + domain : "");
    var secureString = ((secure) ? ";secure" : "");
    document.cookie = nameString + expiryString + pathString + domainString + secureString;
}

//----------获取Cookie--------
function H_GetCookie(name)
{
    if (document.cookie.length > 0)
    {
        begin = document.cookie.indexOf(name + "=");
        if (begin != -1)
        {
            begin += name.length + 1;
            if (document.cookie.indexOf("&") > 0) {
                end = document.cookie.indexOf("&", begin);
            }
            else {
                end = document.cookie.indexOf(";", begin);
            }       
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}


//------删除Cookie-----------
function H_DeleteCookie(name, path, domain) {
    if (getCookie(name)) {
        setCookie(name, "", -1, path, domain);
    }
}

//------------Digg操作(Begin)----------------
function H_Digg(b_ContentType, b_DoType, b_ID)
{
//    var b_CookieName = 'DiggIDStr_' + b_ContentType;
//    var b_DiggCookie = H_GetCookie(b_CookieName);
//    if (b_DiggCookie != null) {
//        var b_DiggArray = b_DiggCookie.split(',');
//        var b_IsHave = false;

//        for (i = 0; i < b_DiggArray.length; i++) {
//            if (b_DiggArray[i] == b_ID) {
//                b_IsHave = true;
//                break;
//            }
//            continue;
//        }

//        if (b_IsHave) {
//            alert("对不起，您已经顶过了，不能重复顶哦！");
//            return;
//        }
//        else {
//            if (b_DiggCookie != "") {
//                b_DiggCookie += ",";
//            }
//            b_DiggCookie += b_ID;

//            H_SetCookie(b_CookieName, b_DiggCookie);
//        }
//    }
//    else {
//        H_SetCookie(b_CookieName, b_ID);
//    }
    
    $.ajax(
    {
        type: "post",
        url: m_WebPath + "/Ajax.aspx",
        data: "t=digg&ct=" + b_ContentType + "&do=" + b_DoType + "&id=" + b_ID,
        success: function (data, textStatus)
        {
            var b_Array = data.split('|');
            if (b_Array[0] == "true")
            {
                var b_ReturnDiggArray = b_Array[1].split(',');
                $("#Digg_GoodCount").html(b_ReturnDiggArray[0]);
                $("#Digg_MiddleCount").html(b_ReturnDiggArray[1]);
                $("#Digg_BadCount").html(b_ReturnDiggArray[2]);
            }
            else
            {
                alert(b_Array[1]);
            }
        },
        error: function ()
        {
            alert('提交错误！');
        }
    });
}

//-------------Digg操作(End)----------------

function H_ContentVote(b_ContentType, b_ID, b_Num) {

    if ($("#Body_Vote").length > 0) {
//        if (b_Num >= 0) {
//            var b_CookieName = 'VoteIDStr_' + b_ContentType;
//            var b_VoteCookie = H_GetCookie(b_CookieName);            
//            if (b_VoteCookie != null) {
//                var b_DiggArray = b_VoteCookie.split(',');
//                var b_IsHave = false;

//                for (i = 0; i < b_DiggArray.length; i++) {
//                    if (b_DiggArray[i] == b_ID) {
//                        b_IsHave = true;
//                        break;
//                    }
//                    continue;
//                }

//                if (b_IsHave) {
//                    alert("对不起，您已经投过票了，不能重复投票哦！");
//                    return;
//                }
//                else {
//                    if (b_VoteCookie != "") {
//                        b_VoteCookie += ",";
//                    }
//                    b_VoteCookie += b_ID;

//                    H_SetCookie(b_CookieName, b_VoteCookie);
//                }
//            }
//            else {
//                H_SetCookie(b_CookieName, b_ID);
//            }
//        }
        $.ajax(
        {
            type: "post",
            url: m_WebPath + "/Ajax.aspx",
            data: "t=contentvote&ct=" + b_ContentType + "&id=" + b_ID + "&n=" + b_Num,
            success: function (data, textStatus) {
                var b_Array = data.split('@@!^#$%^&*()');
                if (b_Array[0] == "true") {
                    $("#Body_Vote").html(b_Array[1]);
                }
            },
            error: function () {
                alert('提交错误！');
            }
        });
    }
}

//----------------------------选中本页所有复选框按键------------------------------------
function H_CheckAll(form)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if (e.name != 'k_AllCheckBox' && e.disabled == false && e.type == 'checkbox')
        {
            e.checked = form.k_AllCheckBox.checked;
        }
    }
}
function H_CheckAll_Other(form, b_Str)
{
    var b_CheckControlArray = b_Str.split("|");
    for (var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        var b_IsCheck = true;
        for (var j = 0; j < b_CheckControlArray.length; j++)
        {
            if (e.name == b_CheckControlArray[j])
            {
                b_IsCheck = false;
                break;
            }
        }
        if (b_IsCheck == true && e.disabled == false && e.type == 'checkbox')
        {
            e.checked = form.k_AllCheckBox.checked;
        }
    }
}

function H_UserLoginShow() {
    var b_NickName = H_GetCookie('user_name');
    if (b_NickName != null) {
        $('#Div_UserNickName').html(b_NickName);
        $('#Div_UserLoginOk').show();
        $('#Div_UserNotLogin').hide();
    }
    else {
        $('#Div_UserLoginOk').hide();
        $('#Div_UserNotLogin').show();
    }
}

//判断是否是数字
function H_IsNumber(b_Str)
{
    var b_Rege = /^\d+$/;   //判断是否是整数
    if (b_Rege.test(b_Str))
    {
        return true;
    }
    else
    {
        return false;
    }
}

function H_Stat_Js(b_Type) {
    $.ajax({
        type: "post",
        url: m_WebPath + "/Ajax.aspx",
        data: 't=stat&st=' + b_Type,
        success: function (data, textStatus) {
            switch (b_Type) {
                case 'a':
                    $("#Stat_Article").html(data);
                    break;
                case 'p':
                    $("#Stat_Picture").html(data);
                    break;
                case 's':
                    $("#Stat_Soft").html(data);
                    break;
                case 'i':
                    $("#Stat_Sorninfo").html(data);
                    break;
                case 'm':
                    $("#Stat_MessageBoard").html(data);
                    break;
                case 'ol':
                    $("#Stat_Online").html(data);
                    break;
                case 'u':
                    $("#Stat_User").html(data);
                    break;
                case 'pn':
                    $("#Stat_PvNow").html(data);
                    break;
                case 'pa':
                    $("#Stat_PvAll").html(data);
                    break;
            }
        }
    });
}

var isIE = (document.all) ? true : false;

if (!isIE) {
    HTMLElement.prototype.__defineGetter__("currentStyle", function () {
        return this.ownerDocument.defaultView.getComputedStyle(this, null);
    });
}

function getStyleWidth(obj) {
    if (!obj) return;
    var w = obj.currentStyle.width ? obj.currentStyle.width : null;
    w = obj.style.width ? obj.style.width : w;
    if (!w || w == 'auto') {
        w = obj.offsetWidth ? obj.offsetWidth : null;
    }
    w = parseInt(w);
    return w;
}
function getStyleHeight(obj) {
    if (!obj) return;
    var h = obj.currentStyle.height ? obj.currentStyle.height : null;
    h = obj.style.height ? obj.style.height : h;
    if (!h || h == 'auto') {
        h = obj.offsetHeight ? obj.offsetHeight : null;
    }
    w = parseInt(h);
    return h;
}

function trace(str) {
    if ($('#DebugBox').length <= 0) return;
    $('#DebugBox')[0].value += str + '\n';
}

function H_Tab(btn, b_Count) {
    var idname = new String(btn.id);
    var s = idname.indexOf("_");
    var e = idname.lastIndexOf("_") + 1;
    var tabName = idname.substr(0, s);
    var id = parseInt(idname.substr(e, 1));

    for (i = 0; i < b_Count; i++) {
        document.getElementById(tabName + "_div_" + i).style.display = "none";
        document.getElementById(tabName + "_btn_" + i).className = "";
    }
    document.getElementById(tabName + "_div_" + id).style.display = "";
    btn.className = "Tabli";
}

/*----------------- 使用方法>>>>： --------------------------------


如果有需要的话指定的相关的属性：
m1.step = 2; //滚动的距离，单位像素。
m1.Speed = 25; //滚动的频度，单位毫秒。


------------------ <<< 使用方法 ---------------------------------*/
function Xpnew_Marqueen() {

    var _this = this;   //防止类成员的this和类本身的this混淆，所以在类成员的代码段里有_this来引用。
    this.step = 2;
    this.Speed = 25;
    this.Direction = 'left';
    //下以获取ID的方式来自于崔永祥
    this.ID = document.getElementById(arguments[0]);
    if (!this.ID) {
        alert("您要设置的\"" + arguments[0] + "\"初始化错误\r\n请检查标签ID设置是否正确!");
        this.ID = -1;
        return;
    }
    //解析div的结构，获取其中的ul和li
    if (!this.ID.hasChildNodes()) {
        alert("初始化错误：滚动字幕不包含任何内容。"); //暂时先这样，让我考虑一下是弹出对话框好，还是显示在状态栏好。
        return;
    }
    var nodeList01 = this.ID.childNodes; //nodeList01 是div下面的子节点列表
    var ULNode;    //ul标记
    //alert( this.ID.innerHTML);
    //alert(nodeList01);

    //特别声明，下面段代码写完了之后，曾经尝试通过xpath获取 .//li，结果没有成功。

    for (var i = 0; i < nodeList01.length; i++) {
        if ("UL" == nodeList01[i].nodeName) {
            ULNode = nodeList01[i];
        }
    }

    //获取li并组成新的ul
    //如果div下面不包含ul，那就尝试直接获取div的li
    var UL_new = document.createElement("UL");    //新的ul

    if (ULNode) {
        var nodeList02 = ULNode.childNodes;
        var i = 0;
        while (nodeList02.length > 0) {
            trace('测试循环的次数：' + i + ' name: ' + nodeList02[0].tagName);
            trace('\n******************************************\n');
            trace('ULNode的长度：' + nodeList02.length);
            if ("LI" == nodeList02[0].tagName) {
                UL_new.appendChild(nodeList02[0]);
            }
            else {
                ULNode.removeChild(nodeList02[0]);
            }

        }

    }
    else    //如果div下面不包含ul，那就尝试直接获取div的li
    {
        for (var i = 0; i < nodeList01.length; i++) {
            if ("LI" == nodeList01[i].tagName) {
                UL_new.appendChild(nodeList01[i]);
                this.ID.removeChild(nodeList01[i]); //顺便移除lis
            }
        }
    }

    if (!UL_new.hasChildNodes()) {
        alert("初始化错误：滚动字幕不包含任何重复内容。"); //暂时先这样，让我考虑一下是弹出对话框好，还是显示在状态栏好。
        return;
    }

    //重组div的内部结构

    this.Player = document.createElement("DIV");
    this.Box = document.createElement("DIV");
    this.Player.appendChild(this.Box);
    this.Box.appendChild(UL_new);
    if (ULNode) {
        ULNode.replaceNode(this.Player);
    }
    else {
        this.ID.appendChild(this.Player);
    }


    //获取div宽度、高度和li的宽度
    var w1, h1, w2;
    w1 = getStyleWidth(this.ID);
    h1 = getStyleHeight(this.ID);
    w2 = getStyleWidth(UL_new.childNodes[0]);
    h2 = getStyleHeight(UL_new.childNodes[0]);

    var w3 = w2 * UL_new.childNodes.length; //当前所有的li宽度。


    /******************************************************************************
    * 务必让滚动的内容的宽度超过外面div的宽度。
    * 这是为了内容比较少的情况准备的。比如说li宽为160，并且只有3个,而外部div预留了750。这样所有的li加在一起只有480
    * 如果li的数量太少，就要复制若干份。这个复制，是要填满一屏。（后面还要另外把所有的内容另外复制）
    * 先计算，需要复制多少份，然后，修改w3(所有的li宽度)
    * 根据份数，能过UL_new.innerHTML复制内容。
    * 
    * 
    *********************************************************************************/


    //需要复制的份数
    var CopyTotal = Math.floor(parseInt(w1) / parseInt(w3));

    var CopyHTML = UL_new.innerHTML;
    w3 = w3 * (CopyTotal + 1);
    for (var i = 0; i < CopyTotal; i++) {
        UL_new.innerHTML += CopyHTML;
    }


    trace('w1: ' + w1 + ' h1: ' + h1 + ' w2: ' + w2 + ' h2: ' + h2 + ' w3: ' + w3);
    h1 = h1 > h2 ? h2 : h1;


    //测试滚动字幕的内部大小
    this.Player.style.width = w1;
    this.Player.style.height = h1;
    this.Player.style.overflow = "hidden";
    UL_new.innerHTML += UL_new.innerHTML; //把内容复制一份。
    w3 = w3 * 2;       //因为内容增加了，所以宽度也要增加 （* 2）
    this.Box.style.width = w3;
    UL_new.style.padding = "0px";
    UL_new.style.margin = "0px";
    //this.Box.style.border = "1px solid #F00";

    _this.Player.onmouseover = function () { clearInterval(_this.MyMar); }
    _this.Player.onmouseout = function () { _this.Refresh(); }


    trace('实际内部大小：' + this.Box.offsetWidth);
    this.MyMar;
}
Xpnew_Marqueen.prototype.Refresh = function () {
    var _this = this;
    //this.MyMar=setInterval(function(){this.play},this.Speed);
    this.MyMar = setInterval(function () { _this.play(); }, this.Speed);
    //this.MyMar=setTimeout(function(){_this.play();},this.Speed);
}

Xpnew_Marqueen.prototype.play = function () {
    // trace('方向：' + this.Direction);
    switch (this.Direction) {

        case 'left':
            //this.Player.scrollLeft = 155;

            //trace(" Box.offsetWidth:" + _this.Box.offsetWidth + " _this.Player.scrollLeft:" + _this.Player.scrollLeft );

            if (this.Box.offsetWidth / 2 - this.Player.scrollLeft <= 0) {
                this.Player.scrollLeft -= this.Box.offsetWidth / 2;
                if (this.Player.scrollLeft < this.step) {
                    this.Player.scrollLeft = this.step;
                }
            }
            else {
                this.Player.scrollLeft += this.step;
            }
            break;
        case 'right':
            if (this.Player.scrollLeft <= 0) {
                this.Player.scrollLeft += this.Box.offsetWidth / 2;
            }
            else {
                this.Player.scrollLeft -= this.step;
            }
            break;
        default:
            break;
    }
};
