﻿String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
        function (m, i) {
            return args[i];
        });
}

String.format = function () {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

$(function () {
    $('#ScriptMessage').dialog({
        autoOpen: false,
        width: 200
    });

    $("#head_pwd").keydown(function (event) {
        evt = (event) ? event : ((window.event) ? window.event : "") //兼容IE和Firefox获得keyBoardEvent对象
        var key = evt.keyCode ? evt.keyCode : evt.which; //兼容IE和Firefox获得keyBoardEvent对象的键值
        if (key == 13) { //判断是否是回车事件。       
            $("#head_login").click();
        }
    });
    $("#head_login").click(function () {
        var uname = $("#head_username").val();
        var pwd = $("#head_pwd").val();
        var check = $("#head_remember").attr("checked");
        if (uname == "用户名或邮箱") {
            alert("对不起,用户名不能为空");
            return;
        }
        if (pwd == "") {
            alert("对不起,密码不能为空");
            return;
        }
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/JSService.asmx/UserLogin",
            data: "{loginname:'" + uname + "',password:'" + pwd + "',Checked:" + check + "}",
            dataType: 'json',
            success: function (result) {
                switch (result.d) {
                    case 1:
                        pageload();
                        break;
                    case -1:
                        ScriptMessageShow("对不起，用户名密码错误");
                        break;
                }
            }
        });
    });
    $("#head_exit").click(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json",
            url: "/JSService.asmx/UserExit",
            dataType: 'json',
            success: function (result) {
                pageload();
            }
        });
    });
})

//ongsunTooltip 提示框开始
var ongsunTooltip = {};
ongsunTooltip.show = function (msg, time) {
    var _timer = null;
    var _openDiv = $("#ongsunTooltipDiv");
    if (_openDiv) {
        _openDiv.remove();
    }

    _openDiv = $("<div id='ongsunTooltipDiv' class='ongsun_messbox'></div>");
    _openDiv.width(200);
    var _titleDiv = $("<div></div>");
    var _titleSpan = $("<span class='title'></span>");
    _titleSpan.html("提示信息");
    _btnClose = $("<span class='close'><a href='javascript:void(0);'>关闭</a></span>");
    _btnClose.click(function () {
        clearTimeout(_timer);
        _openDiv.remove();
    });
    _titleDiv.append(_titleSpan);
    _titleDiv.append(_btnClose);
    _openDiv.append(_titleDiv);
    _contentDiv = $("<div class='content'></div>");
    _contentDiv.html(msg);
    _openDiv.append(_contentDiv);
    $("body").append(_openDiv);
    if ($.browser.msie && $.browser.version == "6.0") {
        _openDiv.css({
            position: "absolute",
            left: (document.documentElement.clientWidth / 2) - (_openDiv.width() / 2),
            top: (document.documentElement.clientHeight / 2) - (_openDiv.height() / 2)
        });
    } else {
        _openDiv.css({
            position: "fixed",
            left: "45%",
            top: "45%"
        });
    }

    if (time) {
        _timer = setTimeout(function () {
            _btnClose.click();
        }, time);
    }
}
//ongsunTooltip 提示框结束

function openWindow(url) {
    window.open(url);
//    var a = $("#ongsun_temp_a");
//    if (a.length > 0) {
//        a.attr("href", url);
//    } else {
//        a = $("<a></a>");
//        a.attr("href", url);
//        a.attr("target", "_blank");
//        a.hide();
//        $("body").append(a);
//    }
//    a[0].click();
}

// 单行滚动
function AutoScroll(obj) {
    $(obj).find("ul:first").animate({ marginTop: "-25px" }, 500, function () {
        $(this).css({ marginTop: "0px" }).find("li:first").appendTo(this);
    });
}
$(document).ready(function () {
    setInterval('AutoScroll("#scrollDiv")', 3000)
});
function ScriptMessageShow(mess) {
    //    $('#ScriptMessage').dialog('open');
    //    $("#ScriptMessageText").html(mess);
    //    setTimeout(function () { $('#ScriptMessage').dialog('close'); }, 3500);
    ongsunTooltip.show(mess, 3500);
}
function ScriptMessageClose() {
    //    $('#ScriptMessage').dialog('close');

}
function webedit(check) {
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/JSService.asmx/WebEdit",
        data: "{Checked:" + check + "}",
        dataType: 'json',
        success: function (result) {
            pageload();
        }
    });
}

function pageload() {
    window.location.reload();
    window.navigate(location.href);
    window.location.replace(location.href);
    window.location.href = "/location.href";
    window.location.assign(location.href);
    document.execCommand("Refresh");
    history.go(0);
}

function SetTabs_Mouseover(div) {
    $("#ul-" + div).find("a").mouseover(function () {
        $("#ul-" + div).find("li").attr("class", "");
        $(this).parent().attr("class", "current");
        var divid = $(this).attr("href");
        $("#" + div).find("div").each(function () {
            if ($(this).attr("id") != "")
                $(this).hide();
            $(divid).show();
        });
    });
    $("#ul-" + div).find("a").click(function () { return false; });
}

function SetTabs_Click(div) {
    $("#ul-" + div).find("a").click(function () {
        $("#ul-" + div).find("li").attr("class", "");
        $(this).parent().attr("class", "current");
        var divid = $(this).attr("href");
        $("#" + div).find("div").each(function () {
            if ($(this).attr("id") != "")
                $(this).hide();
            $(divid).show();
        });
    });
    $("#ul-" + div).find("a").click(function () { return false; });
}

function recommend(myid, num) {
    $("#" + myid + "_box").jCarouselLite({
        btnPrev: "#" + myid + "prev",
        btnNext: "#" + myid + "next",
        auto: 40000, //图片停留时间
        scroll: 1, //每次滚动覆盖的图片个数
        speed: 580, //设置速度，0是不动。其次就是数字越大 ，移动越慢。
        vertical: false, //横向（false），竖向（true ）
        visible: num, //显示的数量
        circular: true //是否循环
    });

}

function GetColumnList(pageIndex, pageSize, cid, IfShowPic, IfTitle, IfContents,Css) {
    var css = $("#" + cid).attr("class");
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/JSService.asmx/GetColumnList",
        data: "{pageIndex:" + pageIndex + ",pageSize:" + pageSize + "}",
        dataType: 'json',
        success: function (data) {
            $("#" + cid + " ul").empty();
            $(data.d).each(function () {
                var item = "<li>";
                if (IfShowPic == "True") {
                    item += "<div><a href=\"" + this['link'] + "\"><img Class=\""+Css+"_pic"+"\" src=\"" + this['Pic'] + "\"></a></div>";
                }
                item += "<h5><a href=\"" + this['link'] + "\">" + this['Name'] + "</a></h5>";
                if (IfTitle == "True") {

                    item += "<a href=\"" + this['replink'] + "\">" + this['Title'] + "</a>";
                }
                if (IfContents == "True") {
                    item += "<p>" + this['Synopsis'] + "</p>";
                }
                item += "</li>";
                $("#" + cid + " ul").append(item);
            });
        }
    });
}

//发布微博信息 加强版
function postMiniBlogEx(content, type, data, event) {
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/JSService.asmx/PostMiniBlog",
        data: "{content:'" + content + "',type:" + type + ",data:'" + data + "'}",
        dataType: 'json',
        success: function (result) {
            if (result.d.code == 1) {
                if (event && typeof (event) == "function") {
                    event(result.d.message);
                }
            } else {
                ScriptMessageShow(result.d.message);
            }
        }
    });
}


function search(t) {
    var q = $("#q").val();
    if (q == "输入搜索关键字") {
        ScriptMessageShow("请输入要搜索的关键字");
        return;       
    }
    q = encodeURIComponent(q);
    document.location.href = "/search-" + t + "-" + q+".html";
}

function addToShoppingCart(productType, productID, num,event) {
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/JSService.asmx/AddToShoppingCart",
        data: "{productType:" + productType + ",productID:" + productID + ",num:" + num + "}",
        dataType: 'json',
        success: function (result) {
            if (event != null && typeof (event) == "function") {
                event(result.d);
            }
        }
    });
}
