
//===========================Ajax标签结束开始===============================================

$("span[type=ajax]").each(function() {
    loadAjaxList($(this));
});

$("*[linkref]").click(function() {
    var linkid = $(this).attr("linkref");
    var url = appurl + "plus/Ajaxs.ashx?a=linkhits&linkid=" + linkid + "&anticache=" + Math.floor(Math.random() * 1000);
    $.ajax({
        type: "get",
        async: false,
        url: url,
        cache: false,
        success: function(d) {}
    });
});
$("font[type=showhits]").each(function() {
    var url = appurl + "index.aspx?c=hits&action=" + $(this).attr("action") + "&f=" + $(this).attr("f") + "&t=" + $(this).attr("t") + "&modelid=" + $(this).attr("modelid") + "&id=" + $(this).attr("id") + "&anticache=" + Math.floor(Math.random() * 1000);
    var o = $(this);
    $.ajax({
        type: "get",
        async: false,
        url: url,
        cache: false,
        success: function(d) {
            o.html(d);
        }
    });

});


//========================ajax方式加载标签 参数：obj 标签ID ,labelParam 标签参数========================
function loadAjaxList(o) {
    var id = o.attr("id");
    var idarr = id.split('_');
    var labelid = idarr[0].replace(/ks/g, "").replace(/SQL/g, "").replace(/Special/g, "");
    var typeid = idarr[1]; //是否分页
    var channelid = idarr[2];
    var classid = idarr[3];
    var infoid = idarr[4];
    var specialid = idarr[5];
    if (id.substring(0, 7) == "Special") {
        loadPage('Special', id, labelid, 1, channelid, classid, infoid, refreshtype, specialid, userparam);
    } else if (id.substring(0, 2) == "ks") {
        if (typeid == 1) { //分页
            loadPage('Label', id, labelid, 1, channelid, classid, infoid, refreshtype, specialid, userparam);
        } else {
            getlabelcontent("plus/ajaxLabel.aspx", id, labelid, "Label", typeid, channelid, classid, infoid);
        }
    } else if (id.substring(0, 3) == "SQL") {
        var refreshtype = o.attr("refreshtype");
        var userparam = o.attr("userparam");
        if (idarr[1] == 1) {
            loadPage('SQL', id, labelid, 1, channelid, classid, infoid, refreshtype, specialid, userparam);
        } else {
            getlabelcontent("plus/ajaxLabel.aspx?1=1" + getUrlParam(), id, labelid, "SQL", userparam, channelid, classid, infoid);
        }
    }
};

function getlabelcontent(posturl, id, labelid, action, userparam, channelid, classid, infoid) {
    $.get(appurl + posturl, {
        action: action,
        labelid: labelid,
        userparam: userparam,
        channelid: channelid,
        m_classid: classid,
        m_infoid: infoid
    }, function(d) {
        $("#" + id).html(d);
    });
};

function loadPage(action, id, labelid, page, modelid, classid, infoid, refreshtype, specialid, userparam) {
        if (userparam == '') {
            $("#" + id).html("<div align='center'>正在读取数据...</div>");
        } else {
            $("span[userparam^='" + userparam + "']").html("<div align='center'>正在读取数据...</div>");
        }
        var s = appurl + "plus/ajaxLabel.aspx?action=" + action + "&ispage=1&objid=" + escape(id) + "&m_infoid=" + infoid + "&m_id=" + modelid + "&labelid=" + escape(labelid) + "&m_classid=" + classid + "&refreshtype=" + refreshtype + "&specialid=" + specialid + "&page=" + page + "&userparam=" + escape(userparam) + getUrlParam();
        $.ajax({
            type: "post",
            url: s,
            cache: false,
            success: function(d) {
                if (userparam == '') {
                    $("#" + id).html(d);
                } else {

                    $("span[userparam='" + userparam + "']").html(d);
                }

            }
        });
    };
    //取url传的参数

function getUrlParam() {
    var URLParams = new Object();
    var aParams = document.location.search.substr(1).split('&'); //substr(n,m)截取字符从n到m,split('o')以o为标记,分割字符串为数组
    if (aParams != '' && aParams != null) {
        var sum = new Array(aParams.length); //定义数组
        for (i = 0; i < aParams.length; i++) {
            sum[i] = new Array();
            var aParam = aParams[i].split('='); //以等号分割
            URLParams[aParam[0]] = aParam[1];
            sum[i][0] = aParam[0];
            sum[i][1] = aParam[1];
        }
        var p = '';
        for (i = 0; i < sum.length; i++) {
            p = p + '&' + sum[i][0] + "=" + sum[i][1];
        }
        return p;
    } else {
        return "";
    }
};

function turn(page, objId, userparam) {
        if (page == -1) {
            alert("已经是首页了！");
        } else if (page == -2) {
            alert("已经是最后一页了！");
        } else {
            var idarr = objId.split('_');
            var labelid = idarr[0].replace("ks", "").replace("SQL", "");
            var channelid = idarr[2];
            var classid = idarr[3];
            var infoid = idarr[4];
            var specialid = idarr[5];
            var refreshtype = $("#" + objId).attr("refreshtype");
            var action = 'Label';
            if (objId.substring(0, 3) == "SQL") action = 'SQL';
            loadPage(action, objId, labelid, page, channelid, classid, infoid, refreshtype, specialid, userparam);
        }
 };
//============================Ajax标签结束==================================================

function showuserlogin() {
    $.dialog.open(appurl + "plus/userlogin.aspx", {
        id: "loginbox",
        lock: true,
        fixed: true,
        title: "<div style='text-align:left'><img src='" + appurl + "sysimg/default/user.png' align='absmiddle'>用户登录</div>",
        width: 380,
        height: 500,
        max: false,
        min: false
    });
};

function StringToDate(DateStr) {
    var converted = Date.parse(DateStr);
    var myDate = new Date(converted);

    if (isNaN(myDate)) {
        var arys = DateStr.split(' ');
        var arys1 = arys[0].split('-');
        var arys2 = arys[1].split(':');
        myDate = new Date(arys1[0], --arys1[1], arys1[2], arys2[0], -arys2[1], arys2[2]);
    }

    return myDate;
};

function show_date_time(begin, end, bjq, f) {
    if (begin == '' || end == '') return;
    var beginTime = StringToDate(begin);
    var endTime = StringToDate(end);
    var nowTime = new Date();
    var nMS = endTime.getTime() - nowTime.getTime();
    var myD = Math.floor(nMS / (1000 * 60 * 60 * 24));
    var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
    var myM = Math.floor(nMS / (1000 * 60)) % 60;
    var myS = Math.floor(nMS / 1000) % 60;
    var myMS = Math.floor(nMS / 100) % 10;
    if (myD >= 0) {
        var str = '';
        var left = '';
        if (myD > 0) str = "<span><font class='d'>" + myD + "</font>天</span>";
        str += "<span><font class='h'>" + myH + "</font>小时</span><span><font class='m'>" + myM + "</font>分</span><span><font class='s'>" + myS + "." + myMS + "</font>秒</span>";
        str = left + str;
    } else {
        var str = "";
        if (f == 0) {
            var tt = beginTime.getTime() - nowTime.getTime();
            if (tt > 0) {
                str = "火爆进行中...";
            } else {
                str = "已结束.";
            }
        } else if (f == 2) { //一元购开奖
            str = "开奖中...";
        } else {
            str = "已结束.";
        }
    }
    $("#" + bjq).html(str);
    var tfun = "show_date_time('" + begin + "','" + end + "','" + bjq + "','" + f + "')";
    eval("t" + bjq + "=window.setTimeout(tfun, 100);");

};

function show_date_time2(begin, end, bjq, f) {
    if (begin == '' || end == '') return;
    var beginTime = StringToDate(begin);
    var endTime = StringToDate(end);
    var nowTime = new Date();
    var nMS = endTime.getTime() - nowTime.getTime();
    var myD = Math.floor(nMS / (1000 * 60 * 60 * 24));
    var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
    var myM = Math.floor(nMS / (1000 * 60)) % 60;
    var myS = Math.floor(nMS / 1000) % 60;
    var myMS = Math.floor(nMS / 100) % 10;
    if (myD >= 0) {
        var str = '';
        if (myD > 0) str = "" + myD + ":";
        str += "" + myH + ":" + myM + ":" + myS + "." + myMS + "";
    } else {
        var str = "";
        if (f == 0) {
            var tt = beginTime.getTime() - nowTime.getTime();
            if (tt > 0) {
                str = "火爆进行中...";
            } else {
                str = "已结束.";
            }
        } else if (f == 2) { //一元购开奖
            str = "开奖中...";
        } else {
            str = "已结束.";
        }
    }
    $("#" + bjq).html(str);
    var tfun = "show_date_time2('" + begin + "','" + end + "','" + bjq + "','" + f + "')";
    eval("t" + bjq + "=window.setTimeout(tfun, 100);");
};


// utility function called by getCookie( )
function getCookieVal(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1) {
            endstr = document.cookie.length;
        }
        return unescape(document.cookie.substring(offset, endstr));
    };
    // primary function to retrieve cookie by name

function getCookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg) {
                return getCookieVal(j);
            }
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
        return "";
    };
    // store cookie value with optional details as needed

function setCookie(name, value) {
        document.cookie = name + "=" + escape(value);
 };
    // remove the cookie by setting ancient expiration date

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=";
    }
};
