/**
 * @author Suker
 * 系统入口
 */
$(function() {
    //页面初始化
    var Page_Load = function() {
        var offsetObj = $('#indexAdvMenuBox').offset();
        var indexAdvMenuMarkObj = $('#indexAdvMenuMark')
			.css({ left: offsetObj.left, top: (offsetObj.top - 10) })
			.show();

        var advCount = $('#indexAdvMenus').children().length; //广告栏目数
        var headAdvIndex = 0; //开头索引号
        var currentAdvIndex = 0; //当前选中的栏目索引号
        var advShowCount = 5; //可见广告栏目数
        var advMenusArray = $('div.indexAdvMenu'); //广告栏目按钮集合
        advMenusArray.each(function(n) {
            $(this).click(function() {
                var _n = n;
                currentAdvIndex = _n;
                $('div.indexAdvMenuTitle').css({ color: '#15488C' });
                var _this = $(this);
                var _thisOffsetObj = _this.offset();
                indexAdvMenuMarkObj.animate({
                    left: _thisOffsetObj.left
                }, {
                    complete: function() {
                        $('.indexAdvMenuTitle', _this).css({ color: '#FFF' });
                        $('#indexAdvScreen').animate({ backgroundPosition: -(n * 500) })
							.parent().attr('href', $('img', _this).attr('href'));
                    }
                });
            });
        }).eq(currentAdvIndex).click();

        //订时器
        var timeout = 6000;
        var timer = $.timer(timeout, function() {
            advMenusArray.eq((currentAdvIndex + 1) % advShowCount).click();
        });

        var indexAdvPageNosDivArray = $('#indexAdvPageNosDiv > font'); //页码容器集合
        indexAdvPageNosDivArray.eq(2).html(advCount);
        var indexAdvMenusObj = $('#indexAdvMenus'); //广告按钮集合的可滚动容器
        $('#indexAdvTurnPagePreviousButton').click(function() {
            if (headAdvIndex > 0) {
                indexAdvMenusObj.animate({ left: '+=100' }, { complete: function() {
                    advMenusArray.eq(currentAdvIndex).click();
                }
                });
                headAdvIndex--;
                currentAdvIndex--;
                indexAdvPageNosDivArray.eq(0).html(parseInt(indexAdvPageNosDivArray.eq(0).html()) - 1);
                indexAdvPageNosDivArray.eq(1).html(parseInt(indexAdvPageNosDivArray.eq(1).html()) - 1);
                if (headAdvIndex == 0)
                    timer.reset(timeout);
            }
        });

        $('#indexAdvTurnPageNextButton').click(function() {
            if (headAdvIndex < advCount - advShowCount) {
                indexAdvMenusObj.animate({ left: '-=100' }, { complete: function() {
                    advMenusArray.eq(currentAdvIndex).click();
                }
                });
                headAdvIndex++;
                currentAdvIndex++;
                indexAdvPageNosDivArray.eq(0).html(parseInt(indexAdvPageNosDivArray.eq(0).html()) + 1);
                indexAdvPageNosDivArray.eq(1).html(parseInt(indexAdvPageNosDivArray.eq(1).html()) + 1);
                timer.stop();
            }
        });

        $('#viewCartButton').click(function() {
            $.MenuNotify.Send($.MenuNotify.Keys.LoadShoppingCart);
        });

        $('#checkOutButton').click(function() {
            $.MenuNotify.Send($.MenuNotify.Keys.LoadCustomerOrderForm);
            if ($.MenuNotify.Items.ShoppingCartWindow) {
                $.MenuNotify.Items.ShoppingCartWindow.hide();
                $('#' + $.MenuNotify.Ids.ShoppingCartWindow).attr('state', 'hide');
            }
        });
    };

    $.Path = 'Project/';
    $.LoginPagePath = '';
    $.ImagePath = '';
    $.ShoppingCartUrlPath = 'p/';
    jQuery.Main = {
        //系统初始化
        Init: function(fn) {
            $.getScript($.Path + 'lib/jquery/jquery.courage.global.js', function() {
                $.getScript($.Path + 'lib/jquery/jquery.courage.object.couragequeue.js', function() {
                    $.CourageQueue.QueueName = 'LoadScript';
                    $.Global.CodesFun.push(fn);
                    $.CourageQueue.EnQueue($.Global.CodesFun);
                    $.CourageQueue.DeQueue();
                });
            });
        }
    };
    $.Main.Init(Page_Load);
});

