/*
jQuery Flying Popup, by Damian Szewczyk (netkevin)
damian.szewczyk@gmail.com
Version 1.0
Flying Popup plugin for jQuery. Allows to show floating HTML popups that follow page scroll/resize (also static HTML popups can be shown when desired). Popups can fly in from outside of the screen (from every direction)
See example usage in readme.txt or run index.html for useful creator
*/
(function ($) {
    $.fn.popup = function (settings) {
        var config = 
		{
            starttime: 0,
            selfclose: 0,
            popup_div: 'popup',
            overlay_div: 'overlay',
            close_id: 'baner_close',
            overlay: false,
            opacity_level: 0.7,
            overlay_cc: true,
            centered: true,
            top: 130,
            setcookie: false,
            cookie_name: 'popup',
            cookie_timeout: 0,
            floating: true,
            floating_reaction: 700,
            floating_speed: 12,
            fly_in: true,
            fly_from: 'top',
            popup_appear: 'show',
            popup_appear_time: ''
        };
        if (settings) $.extend(config, settings);
        var timer;
        var timer_anim = 0;
        var goal;
        var goal_left;
        var current_position;
        var current_position_left;
        var last_position;
        var last_position_left;
        var popup_content = '';
        var popup_object;
        var $popup = $('#' + config.popup_div);
        var $overlay = $('#' + config.overlay_div);
        this.each(function () {
            popup_object = $(this);
            $(this).hide();
        });
        if (config.starttime == 0) {
            setTimeout(show_popup, 250);
        } else {
            setTimeout(show_popup, (config.starttime * 1000));
        }
        return this;

        function show_popup() {
            hidePopup();
            popup_content = popup_object.html();
            if (config.setcookie) {
                if (getCookie(config.cookie_name)) return false;
            }
            if (config.overlay) {
                $("body").prepend('<div id="' + config.overlay_div + '"></div>');
                $overlay = $('#' + config.overlay_div);
                $overlay.css({
                    'opacity': config.opacity_level
                });
                if (config.overlay_cc) {
                    $overlay.click(function () {
                        hidePopup();
                    });
                }
                isIE = $.browser.msie && !$.support.opacity;
                isIE6 = isIE && $.browser.version < 7;
                if (isIE6) {
                    $(window).bind('resize scroll', function () {
                        $overlay.css({
                            width: $(window).width(),
                            height: $(window).height(),
                            top: $(window).scrollTop(),
                            left: $(window).scrollLeft()
                        });
                    }).trigger('scroll');
                }
            }
            $("body").prepend('<div id="' + config.popup_div + '">' + popup_content + '</div>');
            $popup = $('#' + config.popup_div);
            if (config.centered) config.top = ($(window).height() - $popup.outerHeight()) / 2;
            config.top = Math.round(config.top);
            var scrollTop = $(window).scrollTop();
            var width = ($(window).width() - $popup.outerWidth()) / 2;
            if (config.fly_in && config.floating) {
                switch (config.fly_from) {
                case 'top-left':
                    $popup.css("top", (-$popup.outerHeight() + scrollTop) + "px");
                    $popup.css("left", -$popup.outerWidth());
                    break;
                case 'top-right':
                    $popup.css("top", (-$popup.outerHeight() + scrollTop) + "px");
                    $popup.css("right", -$popup.outerWidth());
                    break;
                case 'left':
                    $popup.css("top", (config.top + scrollTop) + "px");
                    $popup.css("left", -$popup.outerWidth());
                    break;
                case 'right':
                    $popup.css("top", (config.top + scrollTop) + "px");
                    $popup.css("right", -$popup.outerWidth());
                    break;
                case 'bottom':
                    $popup.css("top", ($(window).height() + $(document).scrollTop()) + "px");
                    $popup.css({
                        'left': Math.round(width)
                    });
                    break;
                case 'bottom-left':
                    $popup.css("top", ($(window).height() + scrollTop) + "px");
                    $popup.css("left", -$popup.outerWidth());
                    break;
                case 'bottom-right':
                    $popup.css("top", ($(window).height() + scrollTop) + "px");
                    $popup.css("right", -$popup.outerWidth());
                    break;
                default:
                    $popup.css("top", (-$popup.outerHeight() + scrollTop) + "px");
                    $popup.css({
                        'left': Math.round(width)
                    });
                }
            } else {
                $popup.css({
                    'left': Math.round(width)
                });
                $popup.css("top", (config.top + scrollTop) + "px");
            }
            $('#' + config.close_id).click(function () {
                hidePopup();
                return false;
            });
            if (config.floating) {
                setFloating();
            }
            switch (config.popup_appear) {
            case 'fadeIn':
                $popup.fadeIn(config.popup_appear_time);
                break;
            case 'slideDown':
                $popup.slideDown(config.popup_appear_time);
                break;
            default:
                $popup.show(config.popup_appear_time);
                break;
            }
            $(window).resize(function () {
                if (!config.floating) {
                    var width = ($(window).width() - $popup.width()) / 2;
                    $popup.css({
                        'left': width
                    });
                    if (config.centered) {
                        var scrollTop = $(window).scrollTop();
                        var height = Math.round(($(window).height() - $popup.outerHeight()) / 2);
                        $popup.css({
                            'top': (height + scrollTop)
                        });
                        config.top = height;
                    }
                } else {
                    if (config.centered) {
                        var height = Math.round(($(window).height() - $popup.outerHeight()) / 2);
                        config.top = height;
                    }
                }
            });
            if (config.selfclose != 0) {
                setTimeout(hidePopup, (config.selfclose * 1000));
            }
            if (config.setcookie) {
                setCookie(config.cookie_name, '1', config.cookie_timeout);
            }
        };

        function hidePopup() {
            $popup.remove();
            if (config.overlay) {
                $overlay.remove();
            }
            if (config.floating) {
                clearInterval(timer);
                clearTimeout(timer_anim);
            }
        }
        function setFloating() {
            setTimeout(movePopup, 10);
            timer = setInterval(movePopup, config.floating_reaction);
        }
        function movePopup() {
            goal = Math.round($(window).scrollTop() + config.top);
            goal_left = Math.round(($(window).width() - $popup.outerWidth()) / 2);
            offset = $popup.offset();
            current_position = offset.top;
            current_position_left = offset.left;
            if ((current_position != goal || current_position_left != goal_left) && timer_anim == 0) animatePopup();
        }
        function animatePopup() {
            current_position = Math.round((config.floating_speed * current_position + goal) / (config.floating_speed + 1));
            previous_position_left = current_position_left;
            current_position_left = Math.round((config.floating_speed * current_position_left + goal_left) / (config.floating_speed + 1));
            if (current_position != last_position || current_position_left != last_position_left) {
                $popup.css("top", Math.round(current_position) + "px");
                $popup.css("left", Math.round(current_position_left) + "px");
                timer_anim = setTimeout(animatePopup, 20);
                last_position = current_position;
                last_position_left = current_position_left;
            } else if (current_position != goal || current_position_left != goal_left) {
                last_position = current_position;
                last_position_left = current_position_left;
                if (current_position > goal) {
                    current_position = current_position - 1;
                } else if (current_position < goal) {
                    current_position = current_position + 1;
                }
                if (current_position_left > goal_left) {
                    current_position_left = current_position_left - 1;
                } else if (current_position_left < goal_left) {
                    current_position_left = current_position_left + 1;
                }
                timer_anim = setTimeout(animatePopup, 20);
            } else {
                last_position = current_position = goal;
                last_position_left = current_position_left = goal_left;
                $popup.css("top", Math.round(goal) + "px");
                $popup.css("left", Math.round(goal_left) + "px");
                timer_anim = 0;
            }
        }
        function setCookie(c_name, value, expiredays) {
            var exdate = new Date();
            if (expiredays == 0) expiredays = null;
            exdate.setDate(exdate.getDate() + expiredays);
            document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
        };

        function getCookie(c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }
    };
})(jQuery);
