﻿(function ($) {
    $.fn.jCarouselLite = function (o) {
        o = $.extend({
            btnPrev: null,
            btnNext: null,
            speed: 200,
            easing: null,
            vertical: false,
            circular: false,
            visible: 1,
            start: 0,
            scroll: 1
        }, o || {});

        return this.each(function () {
            var running = false, animCss = o.vertical ? "top" : "left", sizeCss = o.vertical ? "height" : "width";
            var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;
            var li = $("li", ul), itemLength = li.size(), curr = o.start;

            if (itemLength == 0) return;

            //初始化开始
            div.css("visibility", "visible");
            li.css({ overflow: "hidden", float: o.vertical ? "none" : "left" });
            ul.css({ margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1" });
            div.css({ overflow: "hidden", position: "relative", "z-index": "2", left: "0px" });

            var liSize = o.vertical ? height(li) : width(li);
            var ulSize = liSize * itemLength;
            var divSize = liSize * v;

            li.css({ width: li.width(), height: li.height() });
            ul.css(sizeCss, ulSize + "px").css(animCss, -(curr * liSize));
            div.css(sizeCss, divSize + "px");

            $(o.btnPrev).css("left", "-14px");
            $(o.btnNext).css("right", "-14px");
            //初始化结束
            //翻页事件开始
            if (o.btnPrev)
                $(o.btnPrev).click(function () {
                    return go(curr - o.scroll);
                });
            if (o.btnNext)
                $(o.btnNext).click(function () {
                    return go(curr + o.scroll);
                });
            //翻页事件结束
            var go = function (to) {
                if (!running) {

                    if (to < 0 || to > itemLength - v) return;else curr = to;

                    running = true;

                    ul.animate(
                        animCss == "left" ? { left: -(curr * liSize)} : { top: -(curr * liSize) }, o.speed, o.easing,
                        function () {
                            checkitem(tLi[curr], curr);
                            running = false;
                        }
                    );
                }
                return false;
            };
        });
    };

    var css = function (el, prop) {
        return parseInt($.css(el[0], prop)) || 0;
    };
    var width = function (el) {
        return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
    };
    var height = function (el) {
        return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
    };
    //数据加载
    var checkitem = function (_obj, page) {
        var strHtml = "";

        if ($(_obj).children(0).html() != "") return;

        $.getJSON("ashx/collect.ashx", { action: 'collect', type: 'discount', pages: page, cur: $("#hdCurrency").val(), game: $("#hdGameName").val() }, function (data) {
            if (data) {
                if (data.length) {
                    strHtml += "<div class=\"product_list\">";
                    for (var index in data) {
                        var obj = data[index];
                        strHtml += String.format("<div class='product'><img src='{0}' alt='items' />", obj.ImgUrl);
                        strHtml += "<div class='info'>";
                        strHtml += String.format("<a href='items-details.aspx?i={0}&n={2}' class='title'>{1}</a>", obj.ID, obj.ProductName + " × " + obj.Volume, obj.ProductName.replace(/ /g, "-"));
                        strHtml += String.format("<p class='price'>€ {0}</p>", getDecimal(obj.Price, 2));
                        strHtml += "</div></div>";
                    }
                    strHtml += "</div>";
                    $(_obj).html(strHtml)
                }
            }
        });
    };

})(jQuery);

//
$().ready(function () {
    $("#dv_discountitems").jCarouselLite({
        btnNext: ".special_box>.btn_right",
        btnPrev: ".special_box>.btn_left"
    });
    $('div.hot_sale div.top a').each(function (i) {
        $(this).click(function () {
            $('div.hot_sale div.top a').removeClass("on");
            $(this).addClass("on");
            // 
            var type = '';
            switch (i) {
                case 0: type = 'items';
                    break;
                case 1: type = 'tcgloot';
                    break;
                case 2: type = 'powerleveling';
                    break;
            }
            //
            var strHtml = "";
            $.getJSON("ashx/collect.ashx", { action: 'collect', type: 'hot', ptype: type, cur: $("#hdCurrency").val(), game: $("#hdGameName").val() }, function (data) {
                if (data) {
                    if (data.length) {
                        strHtml += "<div class=\"product_list\">";
                        for (var index in data) {
                            var obj = data[index];
                            strHtml += String.format("<div class='product'><img src='{0}' alt='items' />", obj.ImgUrl);
                            strHtml += "<div class='info'>";
                            strHtml += String.format("<a href='{1}' class='title'>{0}</a>", obj.ProductName, type == "powerleveling" ? "powerleveling-details.aspx?p=" + obj.ID + "&n=" + obj.ProductName.replace(/ /g, "-") : "items-details.aspx?i=" + obj.ID + "&n=" + obj.ProductName.replace(/ /g, "-"));
                            strHtml += String.format("<p class='price'>€ {0}</p>", getDecimal(obj.Price, 2));
                            strHtml += "</div></div>";
                        }
                        strHtml += "</div>";
                        strHtml += "<div class=\"clearer\"></div>";
                        //           
                        $("div.hot_sale div.mid").each(function (j) {
                            if (i != j) {
                                $(this).slideUp("slow");
                            }
                            else {
                                $(this).empty().html(strHtml).slideDown("slow");
                            }
                        });
                    }
                }
            });
        });
    });
});
