﻿
function refreshPage()
{
    var c = $("category").getAttribute("val");
    var l = $("colorSel").getAttribute("val");
    var m = $("Ms_Sort").value;
    var b = $("Ms_Bos").value;
    var d = "0";
    if ($("depColor").getAttribute("va") == "1")
    {
        d = "1";
    }
    else if ($("lightColor").getAttribute("va") == "1")
    {
        d = 2;
    }
    var str = "Display.aspx";
    var next = false;
    if (c != "0")
    {
        str += "?C=" + c;
        next = true;
    }
    if (l != "0")
    {
        if (next == true)
        {
            str += "&";
        }
        else
        {
            str += "?";
            next = true;
        }
        str += "L=" + l;
    }
    if (d != "0")
    {
        if (next == true)
        {
            str += "&";
        }
        else
        {
            str += "?";
            next = true;
        }
        str += "D=" + d;
    }
    if (b != "2")
    {
        if (next == true)
        {
            str += "&";
        }
        else
        {
            str += "?";
            next = true;
        }
        str += "B=" + b;
    }
    if (m != "2")
    {
        if (next == true)
        {
            str += "&";
        }
        else
        {
            str += "?";
            next = true;
        }
        str += "M=" + m;
    }
    if (b == "1")
    {
        var u = $("AutoDis") != null && $("AutoDis").getAttribute("va") == "1" ? "1" : "0";
        if (u == "1")
        {
            if (next == true)
            {
                str += "&";
            }
            else
            {
                str += "?";
                next = true;
            }
            str += "U=" + u;
        }
        if (next == true)
        {
            str += "&";
        }
        else
        {
            str += "?";
            next = true;
        }
        str += "A=" + curID;
    }

    window.location = str;
}

function changeCate()
{
    if ($("Ms_Bos").value == "1")
    {
        clearTime();
        getFirstWorksID();
    }
    refreshPage();
}

function onClickType(id)
{
    if ($("Ms_Bos").value == "1")
    {
        clearTime();
    }
    if ($("Ms_Sort").value != id)
    {
        if (id == "1")
        {
            $("radomSort").className = "selAct";
            $("dateSort").className = "selNormal";
            $("Ms_Sort").value = "1";
        }
        else
        {
            $("radomSort").className = "selNormal";
            $("dateSort").className = "selAct";
            $("Ms_Sort").value = "2";
        }
        getFirstWorksID();
        refreshPage();
    }
}

function changeColor()
{
    if ($("Ms_Bos").value == "1")
    {
        clearTime();
        getFirstWorksID();
    }
    refreshPage();
}

function onClickColor(id)
{
    if (id == "1")
    {
        if ($("depColor").getAttribute("va") == "0")
        {
            $("depColor").className = "colNormalAct";
            $("depColor").setAttribute("va", "1");
        }
        else
        {
            $("depColor").className = "colNormal";
            $("depColor").setAttribute("va", "0");
        }
        $("lightColor").className = "colNormal";
        $("lightColor").setAttribute("va", "0");
    }
    else
    {
        if ($("lightColor").getAttribute("va") == "0")
        {
            $("lightColor").className = "colNormalAct";
            $("lightColor").setAttribute("va", "1");
        }
        else
        {
            $("lightColor").className = "colNormal";
            $("lightColor").setAttribute("va", "0");
        }
        $("depColor").className = "colNormal";
        $("depColor").setAttribute("va", "0");
    }
    if ($("Ms_Bos").value == "1")
    {
        clearTime();
        getFirstWorksID();
    }
    refreshPage();
}

function onClickBrw(id)
{
    if ($("Ms_Bos").value == "1")
    {
        clearTime();
    }
    if ($("Ms_Bos").value != id)
    {
        if (id == "1")
        {
            $("singleBrw").className = "brwAct";
            $("moreBrw").className = "brwNormal_s";
            $("Ms_Bos").value = "1";
            getFirstWorksID();
        }
        else
        {
            $("singleBrw").className = "brwNormal";
            $("moreBrw").className = "brwAct_s";
            $("Ms_Bos").value = "2";
        }
        refreshPage();
    }
}

function getFirstWorksID()
{
    var c = $("category").getAttribute("val");
    var l = $("colorSel").getAttribute("val");
    var m = $("Ms_Sort").value;
    var d = "0";
    if ($("depColor").getAttribute("va") == "1")
    {
        d = "1";
    }
    else if ($("lightColor").getAttribute("va") == "1")
    {
        d = 2;
    }
    var res = Vxun.BL.ITF.RecordsComment.GetFirstID(c, l, d, m);
    if (res.error != null)
    {
        curID = "999";
        return;
    }
    curID = res.value;
}

function setLocation()
{
    var expdate = new Date();
    expdate = expdate.addDays(7);
    Cookies.setValue("LastLocation", window.location.href, expdate);
}

function showBox(mes)
{
    $("MesInfo").innerHTML = mes;
    box.Show();
}

function closeBox()
{
    box.Close();
}

DataList.doAction = function(actionIndex, actionName, index, id)
{
    switch (actionIndex)
    {
        case 0:
            $("Ms_Bos").value = "1";
            curID = id;
            refreshPage();
            break;
        case 1:
            clearTime();
            var res = Vxun.BL.ITF.RecordsComment.AddToCollect(id.toString());
            if (res.error != null)
            {
                showBox(res.error.Message);
                return;
            }
            showBox(res.value);
            break;
        case 2:
            clearTime();
            window.clipboardData.setData("Text", window.location.href);
            showBox("作品地址已成功复制到剪切板。");
            break;
        case 3:
            clearTime();
            if (actionName != "0")
            {
                curID = actionName;
                refreshPage();
            }
            break;
        case 4:
            clearTime();
            if (actionName != "0")
            {
                curID = actionName;
                refreshPage();
            }
            break;
        case 5:
            if ($("AutoDis").getAttribute("va") == "0")
            {
                $("AutoDis").setAttribute("va", "1");
                $("AutoDis").innerHTML = "【停止播放】";
                timeID = window.setInterval(setTime, 10000);
            }
            else
            {
                clearTime();
            }
            break;
    }
};

function setPicMove(evt)
{
    var obj = $("DataList_Img");
    var img = $("ContImg");
    var event = window.event || evt;
    if (ppid == "0")
    {
        img.style.cursor = "url(../Images/Interface/arr_right.cur)";
        direc = "N";
    }
    else if (nnid.toString() == "0")
    {
        img.style.cursor = "url(../Images/Interface/arr_left.cur)";
        direc = "P";
    }
    else
    {
        var point = getPos(obj);
        var weight = obj.clientWidth;
        var left = point.x;
        var x = event.x ? event.x : event.pageX;

        if (x > weight / 2 + left)
        {
            img.style.cursor = "url(../Images/Interface/arr_right.cur)";
            direc = "N";
        }
        else
        {
            img.style.cursor = "url(../Images/Interface/arr_left.cur)";
            direc = "P";
        }
    }
}

function setPicClick()
{
    clearTime();
    if (direc == "N")
    {
        if (nnid != "" && nnid != "0")
        {
            DataList.doAction(4, nnid, 0, 0);
        }
    }
    else
    {
        if (ppid != "" && ppid != "0")
        {
            DataList.doAction(3, ppid, 0, 0);
        }
    }
}

function clearTime()
{
    $("AutoDis").setAttribute("va", "0");
    $("AutoDis").innerHTML = "【自动播放】";
    if (timeID != null)
    {
        window.clearInterval(timeID);
    }
}

function setTime()
{
    if ($("Ms_Bos").value == "1")
    {
        if (nnid == null || nnid == "" || nnid == "0")
        {
            clearTime();
        }
        else
        {
            curID = nnid;
            refreshPage();
        }
    }
    else
    {
        clearTime();
    }
}

function init()
{
    if ($("Ms_Bos").value == "1")
    {
        ppid = $("PPID").value;
        nnid = $("NNID").value;
        
        if ($("AutoDis").getAttribute("va") == "1")
        {
            timeID = window.setInterval(setTime, 8000);
        }
        
        picTimeID = window.setInterval(checkImgOk, 10);
    }
}

function checkImgOk()
{
    var img = $("DataList_Img");
    if (img != null)
    {
        if (appname.indexOf("netscape") == -1)
        {
            if (img.readyState == "complete")
            {
                window.clearInterval(picTimeID);
                openHidden();
            }
        }
        else
        {
            if (img.complete == true)
            {
                window.clearInterval(picTimeID);
                openHidden();
            }
        }
    }
}

function openHidden()
{
    if ($("Ms_Bos").value == "1")
    {
        var div = $("hidDiv");
        var obj = $(DataList.v.ID + "_Img");
        var img = $("ContImg");

        div.style.position = "absolute";
        div.style.width = obj.offsetWidth;
        div.style.height = obj.offsetHeight;
        var point = getPos(obj);
        div.style.left = point.x + "px";
        div.style.top = point.y + "px";
        div.style.zIndex = 888;

        img.style.height = obj.offsetHeight;
        img.style.width = obj.offsetWidth;

        div.style.display = "block";
    }
}

var curID = null;
var timeID = null;
var picTimeID = null;
var box = new LightBox("idBox");
var direc = "N";
var ppid = "0";
var nnid = "0";
var appname = navigator.appName.toLowerCase();
init();

document.onkeydown = function()
{
    if ($("Ms_Bos").value == "1")
    {
        if (event.keyCode == 37)
        {
            DataList.doAction(3, $("PPID").value, 0, 0);
        }
        else if (event.keyCode == 39)
        {
            DataList.doAction(4, $("NNID").value, 0, 0);
        }
    }
}
