﻿
// Project: Dynamic Date Selector (DtTvB) - 2006-03-16
// Script featured on JavaScript Kit- http://www.javascriptkit.com
// Code begin...
// Set the initial date.

//var open_d = new Array('2008-5-19','2008-5-14');
//alert(open_d[0]);
//var open_d = new Array();

var open_d;

function GetAllOpenServerDay() {
    var Action = 'action=OpenDay';
    var options = { method: 'get', parameters: Action, asynchronous: false,
        onComplete: function(transport) {
            var returnvalue = transport.responseText;
            var str = returnvalue.substr(0, returnvalue.length - 1);
            open_d = str.split(',');
        }
    }
    new Ajax.Request('/Ajax.aspx?no-cache=' + Math.random(), options);
}

GetAllOpenServerDay();
function strToDay1(str) {
    try {
        if (str != "") {
            return parseInt(str);
        } else return "";
    }
    catch (e) {
        return null;
    }

}
function strToDay(str) {
    try {
        if (str.split("-")[2].length > 1) {
            if ((str.split("-")[2]).indexOf("0", 0) == -1) { return parseInt(str.split("-")[2]); }
            else {
                if ((str.split("-")[2]).indexOf("0", 0) == 0) {
                    return parseInt((str.split("-")[2]).replace("0", ""));
                } else {
                    return parseInt(str.split("-")[2]);
                }
            }
        } else {
            return parseInt(str.split("-")[2]);
        }
    }
    catch (e) {
        return null;
    }
}
function strToMonth(str) {
    try {
        if (str.split("-")[1].length > 1) {
            if ((str.split("-")[1]).indexOf("0", 0) == -1) { return parseInt(str.split("-")[1]); }
            else {
                if ((str.split("-")[1]).indexOf("0", 0) == 0) {
                    return parseInt((str.split("-")[1]).replace("0", ""));
                } else {
                    return parseInt(str.split("-")[1]);
                }
            }
        } else {
            return parseInt(str.split("-")[1]);
        }
    }
    catch (e) {
        return null;
    }
}
function strToYear(str) {
    try {
        return parseInt(str.split("-")[0]);
    }
    catch (e) {
        return null;
    }
}

function getDays(m, y) {
    str = "";
    for (var i = 0; i < open_d.length; i++) {
        if (open_d != "" && strToMonth(open_d[i]) == m && strToYear(open_d[i]) == y) {
            str += strToDay(open_d[i]) + "|";
        }
    }
    return str;
}



var ds_i_date = new Date();
ds_c_month = ds_i_date.getMonth() + 1;
ds_c_year = ds_i_date.getFullYear();

// Get Element By Id
function ds_getel(id) {
    return document.getElementById(id);
}

// Get the left and the top of the element.
function ds_getleft(el) {
    var tmp = el.offsetLeft;
    el = el.offsetParent
    while (el) {
        tmp += el.offsetLeft;
        el = el.offsetParent;
    }
    return tmp;
}
function ds_gettop(el) {
    var tmp = el.offsetTop;
    el = el.offsetParent
    while (el) {
        tmp += el.offsetTop;
        el = el.offsetParent;
    }
    return tmp;
}

// Output Element
var ds_oe = ds_getel('ds_calclass');
// Container
var ds_ce = ds_getel('ds_conclass');

// Output Buffering
var ds_ob = '';
function ds_ob_clean() {
    ds_ob = '';
}
function ds_ob_flush() {
    ds_oe.innerHTML = ds_ob;
    ds_ob_clean();
}
function ds_echo(t) {
    ds_ob += t;
}

var ds_element; // Text Element...

var ds_monthnames = [
'一月', '二月', '三月', '四月', '五月', '六月',
'七月', '八月', '九月', '十月', '十一月', '十二月'
]; // You can translate it for your language.

var ds_daynames = [
'日', '一', '二', '三', '四', '五', '六'
]; // You can translate it for your language.

// Calendar template
function ds_template_main_above(t) {
    return '<table cellpadding="3" cellspacing="1" class="ds_tbl">'
	     + '<tr>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_py();">&lt;&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_pm();">&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" colspan="3">&nbsp;</td>'  //onclick="ds_hi();"
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_nm();">&gt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_ny();">&gt;&gt;</td>'
		 + '</tr>'
	     + '<tr>'
		 + '<td colspan="7" class="ds_head">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';
}

function ds_template_day_row(t) {
    return '<td class="ds_subhead">' + t + '</td>';
    // Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
}

function ds_template_new_week() {
    return '</tr><tr>';
}

function ds_template_blank_cell(colspan) {
    return '<td colspan="' + colspan + '"></td>'
}

function ds_template_day(d, m, y) {
    return '<td class="ds_cell" >' + d + '</td>';
    // Define width the day row.
}

function ds_template_day_o(d, m, y) {

    var strdate = y + "-" + m + "-" + d;
    var today = new Date().getDate();
    var ty = new Date().getFullYear();
    var tm = new Date().getMonth() + 1;
    var toOpen = '<td class="ds_cell" style="color:#fff;font-weight:700;background-color:#0100FE;" onclick="javascript:ShowDetail(this,0,\'' + strdate + '\')" onmouseover="javascript:ShowDetail(this,1,\'' + strdate + '\')";>' + d + '</td>';
    var CurOpen = '<td class="ds_cell" style="color:#fff;font-weight:700;background-color:#009f12;" onclick="javascript:ShowDetail(this,0,\'' + strdate + '\')" onmouseover="javascript:ShowDetail(this,1,\'' + strdate + '\')";>' + d + '</td>';
    var yetOpen = '<td class="ds_cell" style="color:#fff;font-weight:700;background-color:#ed6400;" onclick="javascript:ShowDetail(this,0,\'' + strdate + '\')" onmouseover="javascript:ShowDetail(this,1,\'' + strdate + '\')";>' + d + '</td>';
    if (ty == y) {
        if (tm == m) {
            if (today < d) {
                return toOpen;
            } else if (today> (d+5)) {
                return yetOpen ;
            } else { return CurOpen; }
        } else if (tm > m) { return yetOpen; }
        else { return toOpen; }
    } else if (ty > y) {
        return yetOpen;
    } else { return toOpen }

    // Define width the day row.
}


function ds_template_main_below() {
    return '</tr>'+ '<tr><td colspan=\'7\'><img src=\'Images/to.jpg\' style=\'vertical-align:middle\'  /> 新开服 <img src=\'Images/cur.jpg\' style=\'vertical-align:middle\'  /> 即将开服 <img src=\'Images/yet.jpg\' style=\'vertical-align:middle\'  /> 已开服</td><tr>'
	     + '</table>';
}

// This one draws calendar...
function ds_draw_calendar(m, y) {
    // First clean the output buffer.
    ds_ob_clean();
    // Here we go, do the header
    ds_echo(ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
    for (i = 0; i < 7; i++) {
        ds_echo(ds_template_day_row(ds_daynames[i]));
    }
    // Make a date object.
    var ds_dc_date = new Date();
    ds_dc_date.setMonth(m - 1);
    ds_dc_date.setFullYear(y);
    ds_dc_date.setDate(1);
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        days = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
        days = 30;
    } else {
        days = (y % 4 == 0) ? 29 : 28;
    }
    var first_day = ds_dc_date.getDay();
    var first_loop = 1;
    // Start the first week
    ds_echo(ds_template_new_week());
    // If sunday is not the first day of the month, make a blank cell...
    if (first_day != 0) {
        ds_echo(ds_template_blank_cell(first_day));
    }
    var j = first_day;

    var o_days = getDays(m, y);

    var o_days_a = o_days.split("|");

    for (i = 0; i < days; i++) {
        // Today is sunday, make a new week.
        // If this sunday is the first day of the month,
        // we've made a new row for you already.
        if (j == 0 && !first_loop) {
            // New week!!
            ds_echo(ds_template_new_week());
        }
        // Make a row of that day!
        var temp = false;

        for (var k = 0; k < o_days_a.length; k++) {

            if (strToDay1(o_days_a[k]) != "" && parseInt(o_days_a[k]) == (i + 1)) {
                temp = true;
                break;
            }
        }

        if (temp) {
            ds_echo(ds_template_day_o(i + 1, m, y));
        }
        else {
            ds_echo(ds_template_day(i + 1, m, y));
        }



        // This is not first loop anymore...
        first_loop = 0;
        // What is the next day?
        j++;
        j %= 7;
    }
    // Do the footer
    ds_echo(ds_template_main_below());
    // And let's display..
    ds_ob_flush();
    // Scroll it into view.
    //ds_ce.scrollIntoView();
}

// A function to show the calendar.
// When user click on the date, it will set the content of t.
function ds_sh(t) {
    // Set the element to set...
    ds_element = t;
    // Make a new date, and set the current month and year.
    var ds_sh_date = new Date();
    ds_c_month = ds_sh_date.getMonth() + 1;
    ds_c_year = ds_sh_date.getFullYear();
    // Draw the calendar
    ds_draw_calendar(ds_c_month, ds_c_year);
    // To change the position properly, we must show it first.
    ds_ce.style.display = '';
    // Move the calendar container!
    the_left = ds_getleft(t);
    the_top = ds_gettop(t) + t.offsetHeight;
    ds_ce.style.left = the_left  + 'px';
    ds_ce.style.top = the_top  + 'px';
    // Scroll it into view.
    //ds_ce.scrollIntoView();
}

// Hide the calendar.
function ds_hi() {
    //ds_ce.style.display = 'none';
    var src = window.location.href;
    var obj = src.substring(src.indexOf("?") + 1);
    try {
        parent.document.getElementById(obj + "_Calendar").style.display = "none";
    } catch (e) { }
}

// Moves to the next month...
function ds_nm() {
    // Increase the current month.
    ds_c_month++;
    // We have passed December, let's go to the next year.
    // Increase the current year, and set the current month to January.
    if (ds_c_month > 12) {
        ds_c_month = 1;
        ds_c_year++;
    }
    // Redraw the calendar.
    ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous month...
function ds_pm() {
    ds_c_month = ds_c_month - 1; // Can't use dash-dash here, it will make the page invalid.
    // We have passed January, let's go back to the previous year.
    // Decrease the current year, and set the current month to December.
    if (ds_c_month < 1) {
        ds_c_month = 12;
        ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
    }
    // Redraw the calendar.
    ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the next year...
function ds_ny() {
    // Increase the current year.
    ds_c_year++;
    // Redraw the calendar.
    ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous year...
function ds_py() {
    // Decrease the current year.
    ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
    // Redraw the calendar.
    ds_draw_calendar(ds_c_month, ds_c_year);
}

// Format the date to output.
function ds_format_date(d, m, y) {
    // 2 digits month.
    m2 = '00' + m;
    m2 = m2.substr(m2.length - 2);
    // 2 digits day.
    d2 = '00' + d;
    d2 = d2.substr(d2.length - 2);
    // YYYY-MM-DD
    return y + '-' + m2 + '-' + d2;
}

// When the user clicks the day.
function ds_onclick(d, m, y) {
    var src = window.location.href;
    var obj = src.substring(src.indexOf("?") + 1);
    // Hide the calendar.
    ds_hi();
    // Set the value of it, if we can.
    try {
        if (typeof (parent.document.getElementById(obj).value) != 'undefined') {
            parent.document.getElementById(obj).value = ds_format_date(d, m, y);
            // Maybe we want to set the HTML in it.
        } else if (typeof (parent.document.getElementById(obj).innerHTML) != 'undefined') {
            parent.document.getElementById(obj).innerHTML = ds_format_date(d, m, y);
            // I don't know how should we display it, just alert it to user.
        } else {
            alert(ds_format_date(d, m, y));
        }
    } catch (e) {
        alert(ds_format_date(d, m, y));
    }
    parent.document.getElementById(obj + "_Calendar").style.display = "none";
}

// And here is the end.

ds_sh(document.getElementById('s'));

function ge(z) { return typeof (z) == "object" ? z : document.getElementById(z); }
function gE(o, z) { return typeof (o) == "string" ? $e(o).getElementsByTagName(z) : o.getElementsByTagName(z); }
function ac(z) { var a = 0, b = 0; while (z) { a += z.offsetLeft; b += z.offsetTop; z = z.offsetParent } return [a, b] }

//加载相关新闻活动数据
var ListDataNews;
function LoadDataNews(strdate) {
    ListDataNews = COH.index.GetNewList(strdate).value;
    FormatDataNews();
}
function FormatDataNews() {
    if (ListDataNews == null || ListDataNews.IsSuccess == false) {
        alert('系统忙,请重试！');
    }
    else {
        var DateGroup = ListDataNews.DateGroup;
        if (DateGroup == null || DateGroup.length < 1) {

        }
        else {
            var DataList = DateGroup[0];
            var i = 0;
            var row = DataList[i];
            var temp = 0;
            var temp1 = true;
            var temp2 = true;
            var temp3 = true;
            var html = "";
            while (row != null) {
                var temp1 = true;
                var temp2 = true;
                if (temp != row[0]) {
                    if (temp3) {
                        html += '<p class="pictext_1"><b class="SFD34905">服务器信息(点击服务器名称进入游戏)</b><br /><span id="OpenTime">' + row[3] + '</span></p><div class="dianb">';
                        temp3 = false;
                    }
                    html += '<p class="centext_1"><b class="SFCD0A0014"><span>' + row[4] + '：</span><span id="serverName"><a href=\'' + row[5] + '\'  target=\'_blank\'>' + row[1] + '</a></span></b></p>';
                    html += '<ul class="listProp_2 Pro" >';
                    html += '<li><b>备注：</b></li>';
                    html += '<li class="lefts SFD34905" id="serverRemark">' + row[2] + '</li>';
                    html += '</ul>';
                    temp = row[0];
                }
                var DataList1 = DateGroup[1];
                var j = 0;
                var row1 = DataList1[j];
                while (row1 != null) {
                    if (row1[5] == row[0]) {
                        if (row1[0] == 0) {
                            if (temp1) {
                                html += '<p class="listProp_2" ><b>【相关新闻】</b></p>';
                                temp1 = false;
                            }
                            html += '<p class="listProp_2"><span><a href=' + row1[2] + ' target=\'_blank\'>' + row1[1] + '</a></span></p>';
                        }
                        else if (row1[0] == 1) {
                            if (temp2) {
                                html += '<p class="listProp_2" ><b>【相关活动】</b></p>';
                                temp2 = false;
                            }
                            html += '<p class="listProp_2"><span ><a href=' + row1[2] + ' target=\'_blank\'>' + row1[1] + '</a></span></p>';
                        }
                    }
                    j++;
                    row1 = DataList1[j];
                }
                i++;
                row = DataList[i];
            }
            html += '</div>';
            html += '<p class="quit" id="buttonDiv"><a href="javascript:$hide(\'select\');"><img src="Images/quit1.jpg" border="0" /></a></p>';
            document.getElementById('select').innerHTML = html;
        }

    }
}

//显示开服信息层
var onc = 0;
function ShowDetail(obj, num, strdate) {

    if (num == 0) {
        document.getElementById("select").style.display = "";
        afreshPos(obj, 'select', 0);
        onc = 1;
        document.getElementById("buttonDiv").style.display = "";
        LoadDataNews(strdate);
    }
    if (num == 1) {
        if (onc != 1) {
            LoadDataNews(strdate);
            document.getElementById("select").style.display = "";
            document.getElementById("buttonDiv").style.display = "";
            afreshPos(obj, 'select', 0);
        }
    }
    else if (num == 2) {
        if (onc != 1) {
            document.getElementById("select").style.display = "none";
            document.getElementById("buttonDiv").style.display = "none";
        }
    }
}

//  获取文档信息
var Get = {
    winWidth: function() { return document.documentElement.clientWidth; },
    winHeight: function() { return document.documentElement.clientHeight; },
    docWidth: function() { return document.body.scrollWidth; },
    docHeight: function() { return document.body.scrollHeight; },
    scrollTop: function() { return document.documentElement.scrollTop },
    scrollLeft: function() { return document.documentElement.scrollLeft }
}
//定位显示层
function afreshPos(ts, tp, mode) {
    tp = ge(tp);
    var bcw = Get.winWidth(), bch = Get.winHeight(),
        tow = tp.offsetWidth, toh = tp.offsetHeight,
        bsl = Get.scrollLeft(), bst = Get.scrollTop();
    if (!mode) {
        var minx = 0, miny = 0,
	        left = ac(ts)[0], top = ac(ts)[1],
	        dw = ts.offsetWidth, dh = ts.offsetHeight;

        if (left + tow > bcw)
            left = Math.max(left - tow - 5, minx);

        if (left < minx)
            left = minx;
        else if (left + tow > bsl + bcw)
            left = bsl + bcw - tow;

        if (top + toh + dh > bst + bch)
            top -= toh + dh;
        else
            top += dh + 5;

        if (top < miny)
            top = miny;
        else if (top < bst)
            top = bst + miny;
        // 	
    } else {
        left = (bcw - tow) / 2;
        top = bst + (bch - toh) / 2;
    }

    tp.style.left = left + "px";
    tp.style.top = top < 0 ? 0 : top + "px";
    tp.style.visibility = "visible";
}
//隐藏层
function $hide() {
    for (var i = 0; i < arguments.length; i++) {
        var z = ge(arguments[i]);
        z.style.visibility = "hidden";
        z.style.left = "";
        z.style.top = "";
        onc = 0;
    }
}
function displayDiv() {
    document.getElementById("select").style.display = "none";
}
function showDiv() {
    document.getElementById("select").style.display = "";
 }
