$.fn.extend({
    TrunAd: function(options) {
        var auto = null;
        var obj = this;
        var curent = 0;
        var settings = { timer: 1000, menu: "#play_menu" };
        options = options || {};
        $.extend(settings, options);
        var ulcontent = "<div class='box'></div><ul>";
        $('a', this).each(function(i) {
            if (i == 0) {
                $(this).mouseout(function() { curent = i + 1; auto = setInterval(showAuto, settings.timer); }).mouseover(function() { clearInterval(auto); current = i; }); ulcontent = ulcontent + "<li style='padding-left:0px;'><a href='" + $(this).attr("href") + "' target='_blank'><img border='0' longDesc='" + $("img", $(this)).attr("src").substring(0, $("img", $(this)).attr("src").lastIndexOf(".")) + "_small_2.jpg' src='" + $("img", $(this)).attr("src").substring(0, $("img", $(this)).attr("src").lastIndexOf(".")) + "_small_1.jpg' class='smallpic'/></a></li>"
            }
            else {
                $(this).mouseout(function() { curent = i + 1; auto = setInterval(showAuto, settings.timer); }).mouseover(function() { clearInterval(auto); current = i; }); ulcontent = ulcontent + "<li><a href='" + $(this).attr("href") + "' target='_blank'><img border='0' longDesc='" + $("img", $(this)).attr("src").substring(0, $("img", $(this)).attr("src").lastIndexOf(".")) + "_small_2.jpg' src='" + $("img", $(this)).attr("src").substring(0, $("img", $(this)).attr("src").lastIndexOf(".")) + "_small_1.jpg' class='smallpic'/></a></li>"
            }
        });
        ulcontent = ulcontent + "</ul>";
        $(settings.menu).html(ulcontent);
        var box = $(".box", settings.menu);
        var box_init = box.position().left;
        box.css("left", box_init + "px");
        $("a:not(:first-child)", obj).hide();
        $("li", $(settings.menu)).each(function(i) {
            $(this).mouseout(function() { curent = i + 1; auto = setInterval(showAuto, settings.timer); }).mouseover(function() {
                clearInterval(auto); current = i;
                movebox(i);
                changeImgSrc($(".smallpic_hover", $(settings.menu))).attr("class", "smallpic");
                changeImgSrc($("img", $(this)).attr("class", "smallpic_hover"));
                //changeImgSrc($("img",$(this)).attr("class","smallpic_hover")).hide().fadeIn(300);
                $("a:visible", obj).fadeOut(100, function() { $($("a", obj).get(i)).fadeIn(200); });
            })
        });
        $("li", $(settings.menu)).eq(curent).trigger("mouseover");
        function showAuto() {

            if (curent >= $("li", $(settings.menu)).length) {
                curent = 0;
            }

            $("li", $(settings.menu)).eq(curent).trigger("mouseover");
            curent++;
            auto = setInterval(showAuto, settings.timer);
        }
        curent = 1;
        auto = setInterval(showAuto, settings.timer);

        function changeImgSrc(obj) {
            var t = obj.attr("src");
            obj.attr("src", obj.attr("longDesc"));
            obj.attr("longDesc", t);
            return obj;
        }
        var moveinterval = null;
        var target_p = box_init;
        function movebox(xpoint) {
            target_p = xpoint * 158 + box_init;
            box.css("left", target_p + "px");
            //            clearInterval(moveinterval);
            //            target_p = xpoint * 158 + box_init;
            //            moveinterval = setInterval(domove, 80);

        }
        //        function domove() {
        //            var offset_p = target_p - box.offset().left;
        //            if (offset_p > 3 || offset_p < 3) {
        //                box.css("left", (box.offset().left + offset_p / 4) + "px");
        //            }
        //            else {
        //                //alert(offset_p);
        //                box.css("left", target_p + "px");
        //                clearInterval(moveinterval);
        //            }
        //        }

    }

});

