﻿/*!
* Time limit JavaScript Library v1.0.0
* http://mrzzcn.diandian.com/
*
* Copyright 2011, Mrzzcn
* Copyright 2011, The Dojo Foundation
* Released under the MIT, BSD, and GPL Licenses.
*
* Date: Fri Oct 14 16:18:33 2011 -9711
*/
(function ($) {
    // 菜单式导航
    $.fn.PictureRotation = function (options) {
        $.OtherFunction.debug(this);
        var settings = new Array();
        $.extend(settings, $.fn.PictureRotation.defaults, options);
        // 显示窗口
        var showWindow = $(this);
        showWindow.width(settings.width + "px").height(settings.height + "px").css("position", "relative").css("overflow", "hidden");
        settings.ShowWindow = showWindow;

        // 显示窗口ID
        if (showWindow.attr("id") == undefined || showWindow.attr("id") == "") {
            var id = $.OtherFunction.getRandomID();
            showWindow.attr("id", id);
        }
        // 显示窗口标识
        // settings.switchExpression = "#" + showWindow.attr("id");
        // 是否正在执行动画---暂未启用
        settings.moving = false;

        // 轮换图片列表
        var switchItmes = showWindow.children().eq(0).children();
        switchItmes.width(settings.width).height(settings.height).css("position", "absolute");
        // 轮换图片数量
        var itemscount = switchItmes.length;
        // 设置轮换图片的方向属性
        for (var i = 0; i < itemscount; i++) {
            if (settings.direction == "left") {
                switchItmes.eq(i).css("left", i * settings.width + "px");
            }
            else if (settings.direction == "top") {
                switchItmes.eq(i).css("top", i * settings.height + "px");
            }

        }
        settings.SwitchItems = switchItmes;

        // 容器
        var picContainer = showWindow.children().eq(0);
        if (settings.direction == "left") {
            picContainer.height(settings.height + "px").width(itemscount * settings.width + "px");
        }
        else if (settings.direction == "top") {
            picContainer.width(settings.width + "px").height(itemscount * settings.height + "px");
        }
        picContainer.css("position", "absolute");
        settings.PicContainer = picContainer;

        // 当前运动方向
        settings.currentDirection = 1;

        // 分页容器ID
        var pageid = $.OtherFunction.getRandomID();
        // 分页容器
        var $pages;

        // 导航
        switch (settings.menutype) {
            case "other":
                // 页面内其他导航方式
                $pages = $(settings.menuExpression);
                break;
            case "lastNext":
                var pageHtml = "<div tag='lastptzz' class=\"" + settings.lastNext.lastClass + "\" >";
                pageHtml += settings.lastNext.lastHtml;
                pageHtml += "</div>";
                pageHtml += "<div tag='nextptzz' class=\"" + settings.lastNext.nextClass + "\" >";
                pageHtml += settings.lastNext.nextHtml;
                pageHtml += "</div>";

                showWindow.append(pageHtml);
                settings.menuExpression = "div[tag*='ptzz']";
                $pages = $(settings.menuExpression);
                break;
            case "num":
                // 数字
                var pageHtml = "<div id=\"" + pageid + "\" class=\"" + settings.numMenu.pageClass + "\" style=\"" + settings.numMenu.pageStyle + "\" >";
                for (var i = 1; i <= itemscount; i++) {
                    var page = "<a href=\"#\" class=\"" + settings.numMenu.itemClass + "\">" + i + "</a>";
                    pageHtml += page;
                }
                pageHtml += "</div>";
                showWindow.append(pageHtml);
                settings.menuExpression = "div[id*='" + pageid + "']>a";
                $pages = $(settings.menuExpression);
                break;
            default:
                // 数字
                var pageHtml = "<div id=\"" + pageid + "\" class=\"" + settings.numMenu.pageClass + "\" style=\"" + settings.numMenu.pageStyle + "\" >";
                for (var i = 1; i <= itemscount; i++) {
                    var page = "<a href=\"#\" class=\"" + settings.numMenu.itemClass + "\">" + i + "</a>";
                    pageHtml += page;
                }
                pageHtml += "</div>";
                showWindow.append(pageHtml);
                settings.menuExpression = "div[id*='" + pageid + "']>a";
                $pages = $(settings.menuExpression);
                break;
        }

        // 初始化导航选中第一项
        $pages.eq(0).addClass(settings.menuActive);
        settings.currentID = 0;
        // 导航事件
        $pages.each(function (index) {
            $(this).hover(
            function () {
                settings.mousein = true;
                var _self = this;
                // 停止事件
                clearTimeout($.fn.PictureRotation.mousemoveEvents[index]);
                // 当鼠标进入超过 0.2 秒, 展开菜单, 并记录到线程 ID 中
                $.fn.PictureRotation.mousemoveEvents[index] = setTimeout(function () {
                    settings.timeToNext = 0;
                    var currentID = $.OtherFunction.getCurrentID(settings);
                    var nextToShow = $(_self).index();
                    if ($(_self).attr("tag") == "lastptzz") {
                        settings.currentDirection = -1;
                        nextToShow = currentID + settings.currentDirection;
                        if (nextToShow < 0) {
                            settings.currentDirection = 1;
                            nextToShow = 1;
                        }
                    }
                    else if ($(_self).attr("tag") == "nextptzz") {
                        settings.currentDirection = 1;
                        nextToShow = currentID + settings.currentDirection;
                        if (nextToShow > settings.SwitchItems.length - 1) {
                            settings.currentDirection = -1;
                            nextToShow = settings.SwitchItems.length - 2;
                        }
                    }

                    $.OtherFunction.active(settings, nextToShow);
                }, 200);
            },
            function () { settings.mousein = false; }
            );
        });

        // 鼠标进入事件
        if (settings.mouseinstop) {
            showWindow.hover(
        function () { settings.mousein = true; },
        function () { settings.mousein = false; }
        );
        }
        // 阻止导航的点击事件
        $(settings.menuExpression).click(function () {
            var currentID = $.OtherFunction.getCurrentID(settings);
            var nextToShow = 1;
            if ($(this).attr("tag") == "lastptzz") {
                settings.timeToNext = 0;
                settings.currentDirection = -1;
                nextToShow = currentID + settings.currentDirection;
                if (nextToShow < 0) {
                    settings.currentDirection = 1;
                    nextToShow = 1;
                }
                $.OtherFunction.active(settings, nextToShow);
            }
            else if ($(this).attr("tag") == "nextptzz") {
                settings.timeToNext = 0;
                settings.currentDirection = 1;
                nextToShow = currentID + settings.currentDirection;
                if (nextToShow > settings.SwitchItems.length - 1) {
                    settings.currentDirection = -1;
                    nextToShow = settings.SwitchItems.length - 2;
                }
                $.OtherFunction.active(settings, nextToShow);
            }
            else {
                return !settings.clearClick;
            }
        });
        // 自动播放
        if (settings.autoPlay) {
            setInterval(function () {
                if ((settings.mouseinstop && (!settings.mousein)) || (!settings.mouseinstop)) {
                    settings.timeToNext = settings.timeToNext + 100;
                    if (settings.timeToNext == (settings.speed + settings.delay)) {
                        settings.timeToNext = 0;
                        var currentIndex = $.OtherFunction.getCurrentID(settings);
                        var next = currentIndex + settings.currentDirection;
                        if (next < 0) {
                            // 到第一个了 设置第二项为下一项  并改变方向
                            next = 1;
                            settings.currentDirection = 1;
                        }
                        if (next > settings.SwitchItems.length - 1) {
                            // 到末一个了 设置倒数第二项为下一项
                            next = 0;
                            // 如果允许倒序则倒回来并改变方向
                            if (settings.cycleback) {
                                next = settings.SwitchItems.length - 2;
                                settings.currentDirection = -1;
                            }
                        }
                        $.OtherFunction.active(settings, next);
                    }
                }
            }, 100);
        }
        return $(this);
    };

    $.OtherFunction = {
        // debugging    
        debug: function ($obj) {
            if (window.console && window.console.log)
                window.console.log('hilight selection count: ' + $obj.size());
        },
        getRandomID: function () {
            var date = new Date();
            var id = date.getHours() + "" + date.getMinutes() + "" + date.getSeconds() + date.getMilliseconds();
            var r = Math.random();
            return id + "" + r;
        },
        clearMoving: function () {
        },
        play: function ($obj, animate, time) {
            $obj.animate(animate, time / 2).animate({}, time / 2);
        },
        autoPaly: function ($items, options) {
        },
        setItemsDefault: function (options) {
        },
        getCurrentID: function (options) {
            /* var $pages = $(options.menuExpression);
            var currentIndex = $pages.filter("." + options.menuActive).index();
            if (currentIndex == undefined) {
            currentIndex = options.SwitchItems.length - 1;
            }
            currentIndex = parseInt(currentIndex);
            return currentIndex;*/
            return options.currentID;
        },
        moveMovie: function (options, value) {
            if (options.direction == "left") {
                options.PicContainer.animate({ left: value + "px" }, options.speed).delay(options.delay).animate({}, options.delay);
            }
            else if (options.direction == "top") {
                options.PicContainer.animate({ top: value + "px" }, options.speed).delay(options.delay).animate({}, options.delay);
            }
        },
        active: function (options, index) {
            var $pages = $(options.menuExpression);
            var $container = options.PicContainer;
            var currentIndex = $.OtherFunction.getCurrentID(options);
            var count = $container.children("div").length;
            if (currentIndex == undefined) {
                currentIndex = count - 2;
            }
            currentIndex = parseInt(currentIndex);
            var next = index;
            if (currentIndex == next) { return false; }
            if (currentIndex <= 0) {
                options.currentDirection = 1;
            }
            if (next > count - 1) {
                next = count - 2;
                options.currentDirection = -1;
            }
            var width = options.width;
            var height = options.height;
            var left = -(index) * width;
            var top = -(index) * height;
            var tdire = "left";
            var tvalue = left;
            if (options.direction == "top") {
                tdire = "top";
                tvalue = top;
            }
            if (options.movieType == "move") {
                $.OtherFunction.moveMovie(options, tvalue);
            }
            else if (options.movieType == "switch") {
                $container.css(tdire, tvalue + "px");
                options.SwitchItems.hide().eq(index).fadeIn(options.speed).delay(options.delay).animate({}, options.delay);
            }
            $pages.removeClass(options.menuActive);
            $pages.eq(next).addClass(options.menuActive);
            options.currentID = next;
        }

    };
    $.fn.PictureRotation.timeToNext = 100;
    $.fn.PictureRotation.moving = false;
    $.fn.PictureRotation.mousemoveEvents = [];
    // 插件的defaults
    $.fn.PictureRotation.defaults = {
        width: 100,
        height: 100,
        hascaption: true,
        menutype: "num",
        menuActive: "active",
        menuExpression: "",
        speed: 1000,
        delay: 1000,
        timeToNext: 100,
        numMenu: { pageClass: "paging", pageStyle: "", itemClass: "" },
        lastNext: { lastClass: "last", lastHtml: "<<<", nextClass: "next", nextHtml: ">>>", currentDirection: 1, currentClass: "" },
        movieType: "move",
        direction: "top",
        mouseinstop: false,
        mousein: false,
        cycleback: true,
        autoPlay: true,
        clearClick: true
    };

})(jQuery);    
