﻿/*
 *	jquery.msgbox 4.2 - 2009-11-23
 *
 *	All the stuff written by pwwang (pwwang.com)	
 *	Feel free to do whatever you want with this file
 *  Please keep the distribution
 *
 */
(function($) { $.msgbox = function(o) { if (typeof (o) == 'string') { o = { content: o} } var g = o || {}; g.width = o.width || 360; g.height = o.height || 200, g.closeAfter = o.closeAfter || 0; g.title = o.title || '提示', g.wrapperClass = o.wrapperClass || 'msgbox_wrapper'; g.titleClass = o.titleClass || 'msgbox_title'; g.titleWrapperClass = o.titleWrapperClass || 'msgbox_title_wrapper'; g.mainClass = o.mainClass || 'msgbox_main'; g.bgClass = o.bgClass || 'msgbox_bg'; g.buttonClass = o.buttonClass || 'msgbox_button'; g.type = o.type || 'text'; g.content = o.content || 'Hello, world!'; g.closeEvent = o.closeEvent || function() { }; g.closeImg = o.closeImg || 'images/close.gif'; g.bgOpacity = o.bgOpacity || 0.6; g.afterAjaxEvent = o.afterAjaxEvent || function() { }; g.oneBoxMode = typeof (o.oneBoxMode) == 'undefined' ? true : o.oneBoxMode; g.hideScrollBar = typeof (o.hideScrollBar) == 'undefined' ? false : o.hideScrollBar; var h = false; var i = 0; var j = 0; var k = false; var l = $("<div>").css({ 'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '9999' }).addClass(g.bgClass).appendTo('body').animate({ opacity: g.bgOpacity }).dblclick(closeMe).click(function() { flashTitle(0.5, 4, 80) }).mouseup(dragEnd); var m = $("<div>").css({ 'width': g.width + 'px', 'height': g.height + 'px', 'position': 'absolute', 'z-index': '10000' }).addClass(g.wrapperClass).appendTo('body').mouseup(dragEnd); var n = $('<table width=100% cellspacing=0 cellpadding=0><tr><td></td><td><a href="javascript:;">关闭</a></td></tr></table>').css({ 'cursor': 'move', 'vertical-align': 'middle' }).addClass(g.titleWrapperClass).appendTo(m).mousedown(function(e) { dragStart(e) }).mousemove(function(e) { if (h) dragTo(i, j, e) }).mouseout(function(e) { if (h) dragTo(i, j, e) }).mouseup(dragEnd); var p = $("td", n).eq(0).html(g.title).addClass(g.titleClass).mouseup(dragEnd); var q = p.next().attr('align', 'right').find("a").mousedown(closeMe).html("<img src=" + g.closeImg + " border=0 />"); var r = $(document.createElement("div")).addClass(g.mainClass).appendTo(m); r.height(g.height - n.outerHeight(true) - r.outerHeight(true) + r.height()).mouseup(dragEnd); function closeMe() { $('select').css('visibility', 'visible'); if (g.hideScrollBar) $('body').css('overflow', 'auto'); m.fadeOut(); l.fadeOut(); g.closeEvent(k) } function isVisible() { return l.is(":visible") && m.is(":visible") } function autoCloseMe(a) { if (a > 0 && isVisible()) { autoCloseStr = a + " 秒后关闭 ..."; p.html(g.title + " &nbsp; " + autoCloseStr); a--; if (a == 0) closeMe(); setTimeout(function() { autoCloseMe(a) }, 1000) } } function dragStart(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation()) e.stopPropagation(); h = true; var a = m.offset(); i = e.pageX - a.left; j = e.pageY - a.top } function dragTo(x, y, e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation()) e.stopPropagation(); ensureMoveInScreen(e.pageX - x, e.pageY - y) } function ensureMoveInScreen(x, y) { var a = m.offset(); if (x < $(window).scrollLeft()) boxMoveTo($(window).scrollLeft(), y); if (y < $(window).scrollTop()) boxMoveTo(x, $(window).scrollTop()); if (a.left > $(window).scrollLeft() + $(window).width() - g.width) boxMoveTo($(window).scrollLeft() + $(window).width() - g.width, y); if (a.top > $(window).scrollTop() + $(window).height() - g.height) boxMoveTo(x, $(window).scrollTop() + $(window).height() - g.height); if (x >= $(window).scrollLeft() && x <= $(window).scrollLeft() + $(window).width() - g.width && y >= $(window).scrollTop() && y <= $(window).scrollTop() + $(window).height() - g.height) boxMoveTo(x, y) } function boxMoveTo(x, y) { m.css({ 'top': y + 'px', 'left': x + 'px' }) } function dragEnd() { var a = m.offset(); ensureMoveInScreen(a.left, a.top); h = false } function resetPosition() { l.css({ 'width': document.documentElement.scrollWidth + 'px', 'height': Math.max(document.documentElement.scrollHeight, $('body').outerHeight()) + 'px' }); m.css({ 'top': $(window).scrollTop() + ($(window).height() - g.height) / 2 + 'px', 'left': $(window).scrollLeft() + ($(window).width() - g.width) / 2 + 'px' }) } function flashTitle(a, b, c, d) { if (b > 0) { d = !d; op = d ? a : 1; n.css('opacity', op); setTimeout(function() { flashTitle(a, b - 1, c, d) }, c) } } function bindIframeMouseup(a) { try { window.frames[a].document.onmouseup = dragEnd } catch (e) { } setTimeout(function() { bindIframeMouseup(a) }, 200) } function msgbox() { $('select').css('visibility', 'hidden'); if (g.hideScrollBar) $('body').css('overflow', 'hidden'); switch (g.type) { case 'text': r.html(g.content); $('a').click(function() { closeMe() });$('input').click(function() { closeMe() }); break; case 'alert': r.html(g.content); var b = $("<div>").css({ 'text-align': 'center', 'padding': '15px 0' }).appendTo(r); var c = $("<input type=button value=' OK '>").appendTo(b).addClass(g.buttonClass).click(closeMe); break; case 'confirm': r.html(g.content); var b = $("<div>").css({ 'text-align': 'center', 'padding': '15px 0' }).appendTo(r); var d = $("<input type=button value=' Yes '>").appendTo(b).addClass(g.buttonClass).after(" &nbsp; &nbsp; ").click(function() { k = true; closeMe() }); var e = $("<input type=button value=' No '>").appendTo(b).addClass(g.buttonClass).click(function() { k = false; closeMe() }); break; case 'get': case 'ajax': case 'url': r.text("Loading ...").load(g.content, '', function(a) { (g.afterAjaxEvent)(a) }); break; case 'iframe': if (self != top && g.oneBoxMode) { l.remove(); m.remove(); setTimeout(function() { window.location.href = g.content }, 1) } else { var f = $("<iframe frameborder=0 marginheight=0 marginwidth=0></iframe>").appendTo(r).attr({ 'width': '100%', 'height': '100%', 'scrolling': 'auto', 'src': g.content }); f.find(document).ready(function() { var a = $("iframe").index(f); bindIframeMouseup(a) }) } break } } resetPosition(); $(window).load(resetPosition).resize(resetPosition).mouseup(dragEnd); if (g.closeAfter > 0) autoCloseMe(g.closeAfter); msgbox(); return this } })(jQuery);