/**
 * @fileOverview
 * the Contact List script for www.maxthon.com
 * @author zhangwei <zhangwei@maxthon.net>
 * @version 2010-08-10
 */




(function ($) {

    /**
     * tips
     * @param {string} tips content Object's id or .class
     */
    $.fn.tips = function (obj) {
        
        var obj = $(obj);
        $(this).hover(function () {
            obj.stop(true, true).slideDown("fast");
        }, function (e) {
            obj.stop(true, true).slideUp("fast");
        });
        return this;
    };
    
    
    /**
     * Dropdown menu
     * @param {string} btns  menu buttons
     */
    $.fn.menu = function (btns) {
    
        var btns = btns || '.menu-list>li';
        
        $(btns).each(function (i) {
            // have menu
            if (!!$(this).children("ul").get(0)) {
                $(this).hover(function () {
                    $(this).addClass("hover");
                    $(this).children("ul").stop(true, true).slideDown(100);
                }, function () {
                    $(this).removeClass("hover");
                    $(this).children("ul").stop(true, true).slideUp(100);
                });
                
                $(this).children("ul").mouseleave(function () {
                    $(this).slideUp("fast");
                });
            }
            
        });
    }
    
    
    
    
    /**
     * slide
     * @param {config} options
     * @config {String} contents    slide content
     * @config {String} btns        slide mini pic
     * @config {String} next        button for next item
     * @config {String} prev        button for previous item
     * @config {String} width       
     */
    $.fn.slide = function (options) {
        var defaults = {
            contents: '#slide .product-intro li',
            btns: '#slide-btn-list li',
            next: '#slide-next',
            prev: '#slide-previous',
            width: 120
        };
        var opt = $.extend(defaults, options);
        
        var con = $(opt.contents);
        var btns = $(opt.btns);
        var next = $(opt.next);
        var prev = $(opt.prev);
        var w = opt.width;
        var max = btns.parent().parent().width();
        var maxNum = max/w-1; // contains max items
        var curIndex = 0;
        var index = 0;
        var ul = btns.parent();
        var autoTimer;
        
        // goto current and show it
        var invoke = function (num) {
            var curCon = $(con[curIndex]);
            var curBtn = $(btns[curIndex]);
        
            if (num > maxNum) {
                ul.animate({left: -w*(num-maxNum)+'px'}, "fast");
            }
            else if(num < curIndex && curIndex > maxNum) {
                ul.animate({left: 0}, "fast");
            }
            // set current
            curCon.hide();
            curBtn.removeClass("cur");
            $(con[num]).stop(true, true).animate({opacity: 'toggle'},"slow");
            $(btns[num]).addClass("cur");
            
            curIndex = num;
        }
        
        // initialization
        con.hide();
        invoke(0);
        
        next.click(function (e) {
            if (curIndex < btns.length-1 && curIndex < maxNum+1) {
                index++;
                invoke(index);
            }
            else {
                index = 0;
                invoke(index);
            }
            return false;
        });
        prev.click(function () {
            if (curIndex > 0) {
                index--;
                invoke(index);
            }
            else {
                index = Math.min(btns.length-1,maxNum+1);
                invoke(index);
            }
            return false;
        });
        
        btns.click(function (e) {
            var thisIndex = $(this).index();
            e.preventDefault();
            index = thisIndex;
            invoke(thisIndex);
        });
    }
    
    
    
    /**
     * scroll txt
     * @param {string} btns  menu buttons
     */
    $.fn.scrollTxt = function (options) {
        var defaults = {
            con: ".press-lastest-list",
            height: 34,
            prev: "#scroll-bar .up",
            next: "#scroll-bar .down",
            auto: true
        }
        
        var opt = $.extend(defaults, options);
        var con = $(opt.con);
        var h = opt.height;
        var prev = $(opt.prev);
        var next = $(opt.next);
        var curIndex = 0;
        var maxnum = con.children().length;
        var timer;
        var clicked = false;
        
        var invoke = function (num) {
            con.stop(true, true).animate({marginTop: -num*h},"slow");
        };
        
        var autofn = function () {
            timer = setInterval(function () {
                invoke(curIndex);
                curIndex++;
                if (curIndex > maxnum-1) {
                    curIndex = 0;
                }
            }, 3000);
        }
        
        prev.click(function (e) {
            e.preventDefault();

            clicked = true;
            clearInterval(timer);
            if (curIndex <= 0) {
                curIndex = maxnum-1;
                invoke(curIndex);
            }
            else {
                curIndex--;
                invoke(curIndex);
            }
        }).mouseout(function () {
            if (clicked) {
                autofn();
                clicked = false;
            }
        });
        
        next.click(function (e) {
            e.preventDefault();

            clicked = true;
            clearInterval(timer);
            if (curIndex >= maxnum-1) {
                curIndex = 0;
                invoke(curIndex);
            }
            else {
                curIndex++;
                invoke(curIndex);
            }
        }).mouseout(function () {
            if (clicked) {
                autofn();
                clicked = false;
            }
        });
        
        // auto scroll
        if (opt.auto) {
            autofn();
        }
        
        con.find('li').hover(function () {
            clearInterval(timer);
        }, function () {
            autofn();
        });
        
    }
})(jQuery);
