﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
(function() {
    $.fn.banner = function(options) {
        options = $.extend({
            imgUrl: "",
            miniImgUrl: "",
            width:0,
            height:0
        }, options || {});
        
        
        var timer, currentIndex = 0;
        var arrayImg = options.imgUrl.split(',');
        var imgSize = arrayImg.length;
        var $panel = $(this);
        var $img = $("<img border=\"0\" width=\""+options.width+"\" height=\""+options.height+"\"/>");
        var $nav = $("<span style=\"display:none;\"></span>");
        $panel.css("position", "relative");


        $img.hover(function() { stop(); }, function() { play(); }).attr("src", arrayImg[0]).appendTo($panel);
       if ($.browser.msie) {
            $img.css("filter", "RevealTrans(duration=3，transition=23)");
        }
        //预加载图片+处理导航
        var tempImg=new Image();
        $.each(arrayImg, function(i, item) {
            //$("<img src=\"" + item + "\" style=\"display:none;\"/>").appendTo("body");
            tempImg.src=item;
            var $navItem = $("<a href=\"javascript:void(0);\" style=\"display:inline-block;text-align:center; margin:5px; height:20px;width:20px;border:1px solid #C8E0FB;background-color:#5D6D6E; text-decoration:none; color:white;\"></a>");
            if (i == 0) {
               $navItem.css({ "background-color": "red", "font-weight": "bold", "color": "white" });
            }
            $navItem.html(i + 1).appendTo($nav).click(function() { return jump(i); });
       });
        $nav.appendTo($panel).css({ "position": "absolute", "right": 10, "top": ($panel.height() - $nav.outerHeight() - 10) }).show();

        var play = function() { timer = setTimeout(goNext, 5000); }
        var stop = function() { clearTimeout(timer); }

        var goNext = function() {
            if ((currentIndex + 1) == imgSize) {
                currentIndex = 0;
            } else {
                currentIndex++;
            }
            setCurrent(currentIndex);
        }

        var setCurrent = function(index) {
            if (jQuery.browser.msie) {
                $img.get(0).filters.revealTrans.Transition = Math.floor(Math.random() * 23);
                $img.get(0).filters.revealTrans.apply();
            }
            $img.attr("src", arrayImg[index]);
            if (jQuery.browser.msie) {
                $img.get(0).filters.revealTrans.play();
            }
            navClass(index);
            play();
        }

        var jump = function(index) {
            stop();
            setCurrent(index);

        }
        var navClass = function(index) {
            $nav.find("a").eq(index).css({ "background-color": "#000", "font-weight": "bold"}).siblings().css({ "background-color": "#5D6D6E", "font-weight": "normal"});
        }
        play();
    }
})(jQuery);