/*
author:radys
website: http://www.radys.cn
date: 2010-1-7 16:06
plugins name: rady.ui.focusText
version v2.0
*/
if(typeof rady === 'undefined')
    var rady = window.rady = {};
if(typeof rady.ui === 'undefined')
    rady.ui = {};
(function($) {
    rady.ui.focusText = function(options) {
        this.opts = $.extend({}, rady.ui.focusText.defaults, options);

        this._container = this.opts.itemContain;
        this._timer = null;

        this._itemCount = 0;
        this._index = 0;

        this._itemsM = null;
        this._items1 = null;
        this._items2 = null;

        this._init();
    };

    rady.ui.focusText.prototype = {
        _init: function() {
            var $this = this;
            $this._itemsM = $($this._container + " " + this.opts.mainItem);

            $this._items1 = $($this._container + " " + this.opts.items1);
            if (this.opts.items2 != null) {
                $this._items2 = $($this._container + " " + this.opts.items2);
            }
            $this._itemCount = $this._itemsM.length

            $this._bindEvent();
            $this._showItems();
            $this._startAuto();
        },
        _startAuto: function(s) {
            if (s != undefined)
                this.opts.auto = s;

            if (this.opts.auto == 0)
                return this._stopAuto();

            if (this._timer != null)
                return;

            var $this = this;
            this._timer = setInterval(function() { $this._moveRight(); }, this.opts.auto * 1000);
        },
        _stopAuto: function() {
            if (this._timer == null)
                return;
            clearTimeout(this._timer);
            this._timer = null;
        },
        _bindEvent: function() {
            var $this = this;
            $this._itemsM.each(function(i) {
                $(this).hover(function() {
                    $this._index = i;
                    $this._stopAuto();
                    $this._showItems();
                },
                function() {
                    $this._startAuto();
                }
            )
            })

            $this._items1.hover(function() {
                $this._stopAuto();
            },
            function() {
                $this._startAuto();
            }
            )
            if ($this._items2 != null) {
                $this._items2.hover(function() {
                    $this._stopAuto();
                },
                function() {
                    $this._startAuto();
                }
                )
            }
        },
        _moveRight: function() {
            var $this = this;
            if ($this._index < $this._itemCount - 1)
            { $this._index += 1; }
            else
            { $this._index = 0; }
            $this._showItems();
        },
        _showItems: function() {
            var $this = this;
            $this._itemsM.removeClass($this.opts.currentClass).eq($this._index).addClass($this.opts.currentClass)
            $this._items1.hide().eq($this._index).show();
            if ($this._items2 != null) {
                if ($this.opts.itemChange == 1)

                    $this._items2.show().eq($this._index).hide();
                else
                    $this._items2.hide().eq($this._index).show(); 
            }
        }
    };

    //设计选项默认值
    rady.ui.focusText.defaults = {
        itemContain: "#proProducts",
        mainItem: "",
        items1: "",
        items2: null,
        itemChange: 1,
        currentClass: "current",
        auto: 2
    };
})(jQuery)