﻿/*
By Jane 2009-10-28
QQ:830390
*/

///<Reference Path="jquery-1.3.2-vsdoc2.js" />
jQuery.fn.extend({
    flowAdv: function(option) {
        var settings = {
            posX: 0, //起始位置x坐标
            posY: 0, //起始位置y坐标
            width: 200, //宽度
            height: 100, //高度
            imageUrl: '', //图片地址
            url: '#', //连接跳转地址
            title: '测试', //鼠标提示
            target: '_blank', //打开窗体方式
            step: 1, //每次移动像素点
            times: 1, //移动的倍率
            delay: 30, //间隔时间
            leftToRight: true,
            topToButton: true,
            closeWidth: 40, //关闭按钮宽度
            closeHeight: 20, //关闭按钮高度
            clickCallBack: function() { return true; }
        };

        /*定义相关中间变量*/
        var interval = -1;

        jQuery.extend(settings, option);
        var container = jQuery(this);

        jQuery(this).css({
            cursor: 'pointer',
            overflow: 'hidden',
            position: 'absolute',
            left: settings.posX,
            top: settings.posY,
            width: settings.width,
            height: settings.height + settings.closeHeight
        });

        //创建图形容器
        var content = jQuery("<div>");
        content.css({
            cursor: 'pointer',
            overflow: 'hidden',
            position: 'absolute',
            left: 0,
            top: 0,
            width: settings.width,
            height: settings.height
        });
        content.appendTo(container);

        //创建连接       
        var href = jQuery("<a></a>");
        href.attr('target', settings.target);
        href.attr('href', settings.url);
        href.attr('title', settings.title);
        href.appendTo(content);
        href.click(function() {
            return settings.clickCallBack();
        })

        //创建图形
        var image = jQuery("<img>");
        image.attr('src', settings.imageUrl);
        image.attr('alt', settings.title);
        image.css({
            left: 0,
            top: 0,
            width: settings.width,
            height: settings.height,
            border: 0,
            zindex: 10000
        });
        image.appendTo(href);


        //创建关闭按钮
        var closeDiv = jQuery("<div>");
        closeDiv.css({
            cursor: 'pointer',
            overflow: 'hidden',
            position: 'absolute',
            left: settings.width - settings.closeWidth,
            top: settings.height,
            width: settings.closeWidth,
            height: settings.closeHeight,
            border: '0px solid blue',
            paddingTop: '2px',
            fontSize: '10pt',
            textAlign: 'right',
            verticalAlign: 'middle'
        });
        closeDiv.html("<span title='关闭'>关闭</span>&nbsp;");
        closeDiv.click(function() {
            container.remove();
        });

        closeDiv.appendTo(container);

        function changePos() {
            var wnd = jQuery(window);
            var doc = jQuery(document);
            var x = container.position().left;
            var y = container.position().top;
            var newX = x;
            var newY = y;

            if (settings.leftToRight) {
                newX = x + settings.step * settings.times;
            }
            else {
                newX = x - settings.step * settings.times;
            }
            if (settings.topToButton) {
                newY = y + settings.step * settings.times;
            }
            else {
                newY = y - settings.step * settings.times;
            }

            if (newX > wnd.width() + doc.scrollLeft() - container.width()) {
                newX = wnd.width() + doc.scrollLeft() - container.width();
                settings.leftToRight = false;
            }
            if (newX < doc.scrollLeft()) {
                newX = doc.scrollLeft();
                settings.leftToRight = true;
            }

            if (newY > wnd.height() + doc.scrollTop() - container.height()) {
                newY = wnd.height() + doc.scrollTop() - container.height();
                settings.topToButton = false;
            }
            if (newY < doc.scrollTop()) {
                newY = doc.scrollTop();
                settings.topToButton = true;
            }
            container.css({ left: newX, top: newY });
        }

        function start() {
            if (interval == -1) {
                interval = setInterval(changePos, settings.delay);
            }
        }

        jQuery(this).mouseover(function() {
            if (interval != -1) {
                clearInterval(interval);
                interval = -1;
            }
        });

        jQuery(this).mouseout(function() {
            start();
        });

        start();

        return jQuery(this);
    }
});