﻿(function($) {
    $.fn.extend({
        scroll: function(param) {
            var iThis = $(this);
            var speed = 1;              //滚动速度，每单位时间1像素
            var dist = 40;              //滚动多少距离后停顿
            var count = 5;              //滚动多少次后回到起点（默认为10）
            var time = 30;              //滚动时间千分之？秒（0.03秒）
            var stoptime = 3000;        //停顿时间（默认3秒）
            var pause = true;           //是否停顿
            iThis.html(iThis.html() + iThis.html());
            for (var p in param) {
                switch (p) {
                    case "dist":
                        dist = parseInt(param[p]);
                        break;
                    case "count":
                        count = parseInt(param[p]);
                        break;
                    case "time":
                        time = parseInt(param[p]);
                        break;
                    case "stoptime":
                        stoptime = parseInt(param[p]);
                        break;
                    case "pause":
                        pause = param[p];
                }
            }
            iThis.attr({ speed: speed, dist: dist, count: count, time: time, stoptime: stoptime, pause: pause });
            iThis.hover(
                function() {
                    iThis.attr("speed", 0);
                },
                function() {
                    iThis.attr("speed", 1);
                }
            );
            window.setTimeout(
            function() {
                iThis.runScroll();
            },
            parseInt(iThis.attr("stoptime"))
            );
        },
        runScroll: function() {
            var iThis = $(this);
            iThis.attr("scrollTop", parseInt(iThis.attr("scrollTop")) + parseInt(iThis.attr("speed")));
            if (parseInt(iThis.attr("scrollTop")) == parseInt(iThis.attr("dist")) * parseInt(iThis.attr("count"))) {
                iThis.attr("scrollTop", 0);
            }
            if (parseInt(iThis.attr("scrollTop")) % parseInt(iThis.attr("dist")) == 0) {
                window.setTimeout(
                    function() {
                        iThis.runScroll();
                    },
                    parseInt(iThis.attr("pause") == "true" ? iThis.attr("stoptime") : iThis.attr("time"))
                );
            }
            else {
                window.setTimeout(
                    function() {
                        iThis.runScroll();
                    },
                    parseInt(iThis.attr("time"))
                );
            }
        },
        rotation: function(event,selector) {
            var iThis = this;
			var tagName=selector==null?"a":selector;
            var current;
            iThis.find(tagName+"[href^='#tab']").each(function() {
                $(this).bind(event, function() {
                    current = $(this).attr("href").replace("#tab", "");
                    $(this).addClass("selected").siblings(tagName+"[href^='#tab']").removeClass("selected");
                    iThis.siblings("#container" + current).show().siblings("*[id^='container']").hide();
                });
            });
        }
    });
})(jQuery);
