/*
author:radys
website: http://www.radys.cn
date: 2010-1-7 16:06
plugins name: rady.ui.focusText
version v1.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._ititle = null;
        this._itext = null;
        this._ititle = null;
        this._init();
    };

    rady.ui.focusText.prototype = {
        _init: function() {
            var $this = this;
            $this._ititle = $($this._container + " .title");
            $this._itext = $($this._container + " .qrstjcp_box");
            $this._imain = $($this._container + " li");
            $this._itemCount = $this._imain.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._imain.each(function(i) {
                $(this).hover(function() {
                    $this._index = i;
                    $this._stopAuto();
                    $this._showItems();
                },
                function() {
                    $this._startAuto();
                }
            )
            })
            
            $this._ititle.hover(function() {
                $this._stopAuto();
            },
            function() {
                 $this._startAuto();
            }
            )

            $this._itext.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._ititle.removeClass(this.opts.currentClass).eq($this._index).addClass(this.opts.currentClass);
            $this._itext.hide().eq($this._index).show();
            $this._ititle.show().eq($this._index).hide();
        }
    };

    //设计选项默认值
    rady.ui.focusText.defaults = {
        itemContain: "#proProducts",
        //currentClass: "current",
        auto: 2
    };
})(jQuery)