(function (jQuery) {
    jQuery.fn.leaveNotice = function (opt) {
        var defaults = {
            siteName: window.location.href,
            exitMessage: "<p><strong>Sorry to see your leaving our CVA page.</strong></p><p>Thank you for visiting.</p>",
            preLinkMessage: "<div class='setoff'><p>You will now be directed to:<br/>{URL}</p></div>",
            linkString: "",
            timeOut: 10000,
            overlayId: "ln-blackout",
            messageBoxId: "ln-messageBox",
            messageHolderId: "ln-messageHolder",
            displayUrlLength: 50,
            overlayAlpha: 0.3
        };
        var options = jQuery.extend(defaults, opt);
        return this.each(function () {
            el = jQuery(this);
            var url = el.attr('href');
            var ulen = options.displayUrlLength;
            //if (url.length >= ulen) {
//                var suffix = "..."
//            } else {
//                var suffix = ""
//            }
           // var shortUrl = url.substr(0, ulen) + suffix;
            var title = el.attr('title');
            if (title != "") {
                var linkText = title
            } else if (title == "") {
               // var linkText = shortUrl
            }
            el.click(function () {
                jQuery('body').append('<div id="' + options.overlayId + '"></div>');
                jQuery('body').append('<div id="' + options.messageHolderId + '"><div id="' + options.messageBoxId + '"></div></div>');
                if (options.overlayAlpha !== false) {
                    jQuery('#' + options.overlayId).css('opacity', options.overlayAlpha)
                }
                preFilteredContent = options.exitMessage + "<br/> <div class='notice'>Could a CVA solve your cash-flow problems, remove creditor press and leave YOU in control? A CVA is the best way to rescue a viable business and write of a proportion of the company's debts. <br/><br/> <a href='http://www.companyrescue.co.uk/documents/ExpertsCVAguide-v1.pdf'> Click here for our free 92 page Experts guide to CVAs</a></div><br/><br/> ";
                msgContent = preFilteredContent.replace(/\{URL\}/g, '<a href="' + url + '" title="' + url + '"' + options.linkString + '>' + linkText + '</a>');
                //msgContent = msgContent.replace(/\{SITENAME\}/g, options.siteName);
                if (options.timeOut > 0) {
                    msgContent += '<p id="ln-cancelMessage"><a href="#close" id="ln-cancelLink">Cancel</a> or press the ESC key.</p>'
                } else {
                    msgContent += '<p id="ln-cancelMessage">Click the link above to continue or <a href="#close" id="ln-cancelLink">Cancel</a></p>'
                }
                jQuery('#' + options.messageBoxId).append(msgContent);
                if (options.timeOut > 0) {
                    leaveIn = setTimeout(function () {
                        jQuery('#ln-cancelMessage').html('<em>Loading...</em>');
                        window.location.href = url
                    }, options.timeOut)
                } else {
                    leaveIn = false
                }
                jQuery('#ln-cancelLink').click(function () {
                    closeDialog(options, leaveIn);
                    return false
                });
                jQuery(document).bind('keyup', function (e) {
                    if (e.which == 27) {
                        closeDialog(options, leaveIn)
                    }
                });
                jQuery(window).unload(function () {
                    closeDialog(options, leaveIn)
                });
                return false
            })
        })
    };

    function closeDialog(options, timer) {
        if (options.timeOut > 0) {
            clearTimeout(timer)
        }
        jQuery('#' + options.overlayId + ', #' + options.messageHolderId).fadeOut('fast', function () {
            jQuery('#' + options.overlayId + ', #' + options.messageHolderId).remove()
        });
        jQuery(document).unbind('keyup')
    }
})(jQuery);
