﻿$.imgPlayer = {
    curr: 0,
    next: 0,
    count: 0,
    ops: {
        container: 'imgs',
        speed: 'slow',
        activeClass: 'active',
        tick: 3000,
        width: 500
    },
    imgPlay: function() {
        $.imgPlayer.next = $.imgPlayer.curr + 1;
        if ($.imgPlayer.curr == $.imgPlayer.count - 1)
            $.imgPlayer.next = 0;
        $.imgPlayer.play($.imgPlayer.next);

        $.imgPlayer.curr++;
        if ($.imgPlayer.curr > $.imgPlayer.count - 1) { $.imgPlayer.curr = 0; $.imgPlayer.next = $.imgPlayer.curr + 1; }
    },
    play: function(_next) {
        $('#{0} li'.format($.imgPlayer.ops.container)).eq($.imgPlayer.curr).css({ 'opacity': '0.5' }).animate({ 'left': '-{0}px'.format($.imgPlayer.ops.width), 'opacity': '1' }, $.imgPlayer.ops.speed, function() {
            $(this).css({ 'left': '{0}px'.format($.imgPlayer.ops.width - 1) });
        }).end().eq(_next).animate({ 'left': '0px', 'opacity': '1' }, $.imgPlayer.ops.speed, function() {
            $('#{0}_ico a'.format($.imgPlayer.ops.container)).siblings('a').removeClass($.imgPlayer.ops.activeClass).end().eq(_next).addClass($.imgPlayer.ops.activeClass);
        });
    },
    init: function(o) {
        for (var a in $.imgPlayer.ops) {
            if (typeof o[a] == 'undefined')
                continue;
            $.imgPlayer.ops[a] = o[a];
        }


        if (typeof o['width'] == 'undefined') {
            $.imgPlayer.ops.width = $('#{0}'.format($.imgPlayer.ops.container)).css('width').toInt();
        }
        $.imgPlayer.count = $('#{0}_ico a'.format($.imgPlayer.ops.container)).size();
        t = setInterval('$.imgPlayer.imgPlay()', $.imgPlayer.ops.tick);

        $('#{0} li, #{0}_ico a'.format($.imgPlayer.ops.container)).hover(function() {
            clearInterval(t);
            $(this.tagName == "a" ? this.parentNode : this).css("color", "#fff").css("backgroundColor", "#e5488c");
        }, function() {
            t = setInterval('$.imgPlayer.imgPlay()', $.imgPlayer.ops.tick);
            $(this.tagName == "a" ? this.parentNode : this).css("color", "#e5488c").css("backgroundColor", "#fff");
        });


        $('#{0}_ico a'.format($.imgPlayer.ops.container)).click(function() {
            var index = $('#{0}_ico a'.format($.imgPlayer.ops.container)).index(this);
            if ($.imgPlayer.curr != index) {
                $.imgPlayer.play(index);
                $.imgPlayer.curr = index;
            };
            return false;
        });
    }
}