﻿$(document).ready(function () {
    var STEP = 0;
    function getCurrentObj(vl) {
        var cur;
        $.each(games, function (i, obj) {
            if (obj && obj !== "undefined" && obj.id == vl) {
                cur = obj;
            }
        });
        return cur;
    }
    function setupKindSelect() {
        STEP = 1;
        $('#mask-one').hide();
        var items = $('div.one a:not(.dead)');

        items.bind('click', function (evt) {
            var gaid = $(evt.target).attr("rel");
            $("#jumpFormId input[name=gameOrgId]").val(gaid);

            items.removeClass("on");
            $(this).addClass("on");
            $('#mask-two').hide();
            $('#mask-three').show();
            //$('#mask-one').show();
            $('#mid_ct').attr("class", "mid_ctbg_02");
            //$('.equip a').removeClass("selected");
            //$('.gold a').removeClass("selected");

            $("#bizTypeId a").each(function () {
                $(".gold a").css({ 'background-image': '' });
                $(".equip a").css({ 'background-image': '' });
            });

            var obj = getCurrentObj(gaid);
            var gGold = false;
            var gItem = false;
            for (var i = 0; i < obj.bt.length; i++) {
                var strBT = obj.bt[i];
                if (strBT && strBT == "GameGold") { gGold = true; }
                if (strBT && strBT == "GameEquip") { gItem = true; }

            }
            if (gGold) {
                $("#bizTypeId .goldNo").removeClass("goldNo").addClass("gold");
                $("#bizTypeId .gold a").attr("title", "");
            } else {
                $('.gold a').unbind('click');
                $("#bizTypeId .gold").removeClass("gold").addClass("goldNo");
                $("#bizTypeId .goldNo a").attr("title", "该游戏币交易尚未开放");
            }

            if (gItem) {
                $("#bizTypeId .equipNo").removeClass("equipNo").addClass("equip");
                $("#bizTypeId .equip a").attr("title", "");
            } else {
                $('.equip a').unbind('click');
                $("#bizTypeId .equip").removeClass("equip").addClass("equipNo");
                $("#bizTypeId .equipNo a").attr("title", "该游戏装备交易尚未开放");
            }
            /*
            if (gaid == "004") {
            $('.equip a').unbind('click');
            $("#bizTypeId .equip").removeClass("equip").addClass("eqdiable");
            $("#bizTypeId .eqdiable a").attr("title", "该游戏装备交易尚未开放");
            } else {
            
            }
            */
            setupGoodsSelect();
        });
    }

    function setupGoodsSelect() {
        STEP = 2;
        $('.gold a').bind('click', function (evt) {
            $(this).css({ 'background-image': 'url(images/gold-trans_on02.png)' })
            $(".equip a").css({ 'background-image': '' });

            $("#jumpFormId input[name=bizType]").val($(this).attr("rel"));
            //alert($("#jumpFormId input[name=bizType]").val());
            $('#mid_ct').attr("class", "mid_ctbg_03");
            $('#mask-three').hide();
        });
        $('.equip a').bind('click', function (evt) {
            $(".gold a").css({ 'background-image': '' });
            $(this).css({ 'background-image': 'url(images/equip-trans_on02.png)' })

            $("#jumpFormId input[name=bizType]").val($(this).attr("rel"));
            //alert($("#jumpFormId input[name=bizType]").val());

            $('#mid_ct').attr("class", "mid_ctbg_03");
            $('#mask-three').hide();
        });

        var items = $('div.three a');
        $(items[0].parentNode).css({
            position: 'relative'
        });
        var nonius = $('div.three .scroll_bg>div').css({
            position: 'relative',
            backgroundColor: 'transparent'
        });
        var current = $(items[0]);

        function setCurrent(item) {
            current = $(item);
        }

        function noniusFollow(elm) {
            nonius.stop().animate({
                left: elm.offsetLeft - 2
            }, 1000, 'backout');
        }
        items.bind('click', function (evt) {
            var p = $(evt.target).parent();
            $("a", p).removeClass("selected");
            $(evt.target).addClass("selected");

            noniusFollow(evt.target);
            if (evt.target === items[0]) {
                $(items[0]).addClass('text-highlight');
                $(items[1]).removeClass('text-highlight');
            } else {
                $(items[1]).addClass('text-highlight');
                $(items[0]).removeClass('text-highlight');
            }

            var relId = $(evt.target).attr("rel");
            if (relId && relId.length > 0) {
                $("#jumpFormId input[name=tradeType]").val(relId);
                $("#jumpFormId").submit();
            }
            return false;
        });
    }

    function setupGameSelect() {
        STEP = 3;
        $('#mask-three').hide();
    }
    function initGame() {
        $("#gameId").empty();
        var temStr = "";
        var maxLen = 5;
        if (games && games.length < maxLen) {
            for (var i = (games.length + 1); i <= maxLen; i++) {
                games.push("undefined");
            }
        }
        $.each(games, function (i, obj) {
            if (obj && obj !== "undefined") {
                temStr += '<a href="javascript:void(0)" rel="' + obj.id + '">' + obj.name + '</a>';
            } else {
                temStr += '<a href="javascript:void(0)" class="dead">暂未开通</a>';
            }
        });
        $("#gameId").html(temStr);
        setupKindSelect();
    }

    initGame();

});
