// jQuery Alert Dialogs Plugin
//
// Version 1.0
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 29 December 2008
//
// Visit http://abeautifulsite.net/notebook/87 for more information
//
// Usage:
//		jAlert( message, [title, callback] )
//		jConfirm( message, [title, callback] )
//		jPrompt( message, [value, title, callback] )
// 
// History:
//
//		1.00 - Released (29 December 2008)
//
// License:
// 
//		This plugin is licensed under the GNU General Public License: http://www.gnu.org/licenses/gpl.html
//
(function ($) {

    $.alerts = {

        // These properties can be read/written by accessing $.alerts.propertyName from your scripts at any time

        verticalOffset: -75,                // vertical offset of the dialog from center screen, in pixels
        horizontalOffset: 0,                // horizontal offset of the dialog from center screen, in pixels/
        repositionOnResize: true,           // re-centers the dialog on window resize
        overlayOpacity: .3,                // transparency level of overlay
        overlayColor: '#000',               // base color of overlay
        draggable: true,                    // make the dialogs draggable (requires UI Draggables plugin)
        okButton: '&nbsp;OK&nbsp;',         // text for the OK button
        cancelButton: '&nbsp;Cancel&nbsp;', // text for the Cancel button
        dialogClass: null,                  // if specified, this class will be applied to all dialogs

        // Public methods

        alert: function (type, message, title, callback) {
            if (title == null) title = 'Alert';
            $.alerts._show(title, message, null, type, function (result) {
                if (callback) callback(result);
            });
        },

        confirm: function (message, title, callback) {
            if (title == null) title = 'Confirm';
            $.alerts._show(title, message, null, 'confirm', function (result) {
                if (callback) callback(result);
            });
        },

        prompt: function (message, value, title, callback) {
            if (title == null) title = 'Prompt';
            $.alerts._show(title, message, value, 'prompt', function (result) {
                if (callback) callback(result);
            });
        },

        // Private methods

        _show: function (title, msg, value, type, callback) {

            $.alerts._hide();
            $.alerts._overlay('show');

            if (type == 'naver') {

                $("BODY").append('<div id="popup_container" style="width:320px; margin:0;">' +
	                                '<div id="popup_title" style="width:320px; height:406px; margin:0;">' +
		                                '<img src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup01.jpg">' +
	                                '</div>' +
	                                '<div style="width:320px; height:47px; margin:0;">' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup02.jpg">' +
		                                '</ol>' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img id="popupOk" src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup03.jpg" style="cursor:pointer">' +
		                                '</ol>' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img id="popupCancel" src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup04.jpg" style="cursor:pointer">' +
		                                '</ol>' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup05.jpg">' +
		                                '</ol>' +
	                                '</div>' +
	                                '<div style="width:320px; height:18px; margin:0;">' +
		                                '<img src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup06.jpg" width="320" height="18" />' +
	                                '</div>' +
	                                '<div style="width:320px; height:29px; margin:0;">' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup07.jpg" width="269" height="29" />' +
		                                '</ol>' +
		                                '<ol style="float:left; margin:0;">' +
			                                '<img class="popup_ok" src="http://img.wizwell.co.kr/design/images/naver/naver_check_popup08.jpg" width="51" height="29" border="0" style="cursor:pointer" />' +
		                                '</ol>' +
	                                '</div>' +
                                '</div>');
            }
            else {
                $("BODY").append('<div id="popup_container" style="width:307px; height:196px; margin:0;">' +
                                '<div id="popup_title" style="width:307px; height:30px; margin:0; background-image:url(http://img.wizwell.co.kr/Design/images/alot/errorpopup_top_bg.png);">' +
                                    '<ol style="float:left; width:63px; height:13px; margin:10px 0 0 0;">' +
                                        '<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_message0.png" />' +
                                    '</ol>' +
                                    '<ol style="float:right; margin:10px 12px 0 0;">' +
                                        '<img class="popup_ok" src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_close_btn.jpg" style="cursor:pointer"/>' +
                                    '</ol>' +
                                '</div>' +
                                '<div id="popup_content" style="width:307px; height:155px; margin:0; background-image:url(http://img.wizwell.co.kr/Design/images/alot/errorpopup_middle_bg.png);">' +
                                    '<ol style="float:left; margin:0 0 0 3px;" id="popup_message">' +
                                    '</ol>' +
                                    '<ol style="margin:0 0 0 110px;" id="popup_ok">' +
                                        '<img class="popup_ok" src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_check_btn.jpg" width="77" height="22" style="cursor:pointer" />' +
                                    '</ol>' +
                                '</div>' +
                                '<div style="width:307px; height:11px; margin:0 0 0 0;">' +
                                    '<ol>' +
                                        '<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_bottom_bg.png" />' +
                                    '</ol>' +
                                '</div>' +
                            '</div>');
            }

            if ($.alerts.dialogClass) $("#popup_container").addClass($.alerts.dialogClass);

            // IE6 Fix
            var pos = ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';

            $("#popup_container").css({
                position: pos,
                zIndex: 99999,
                padding: 0,
                margin: 0
            });

            //$("#popup_title").text(title);
            $("#popup_content").addClass(type);
            //$("#popup_message").text(msg);
            //$("#popup_message").html( $("#popup_message").text().replace(/\n/g, '<br />') );

            $("#popup_container").css({
                minWidth: $("#popup_container").outerWidth(),
                maxWidth: $("#popup_container").outerWidth()
            });

            $.alerts._reposition();
            $.alerts._maintainPosition(true);

            switch (type) {
                case 'info':
                case 'warning':
                case 'success':
                case 'name':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_03.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    //                  $("#popup_ok").focus().keypress(function (e) {
                    //                      if (e.keyCode == 13 || e.keyCode == 27) $("#popup_ok").trigger('click');
                    //                  });

                    break;
                case 'socialNo':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_13.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });
                    break;
                case 'id':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_09.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });
                    break;
                case 'password':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_05.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'email':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_10.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'idPwd_inconsistency':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_15.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'nameSocial_inconsistency':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_23.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'getCoupon':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_39.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'get_bookCoupon_empty':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/alot_cupon_05.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(false);
                    });

                    break;
                case 'get_bookCoupon':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_cupon_04.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });

                    break;
                case 'get_bookCoupon2':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_cupon_02.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(false);
                    });

                    break;
                case 'get_bookCoupon3':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_cupon_03.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(false);
                    });

                    break;
                case 'cart':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_37.jpg" width="291" height="113"/>');
                    $("#popup_ok").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_basket_btn.jpg" id="popupOk" style="margin-left:-70px; cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="http://img.wizwell.co.kr/design/images/alot/alot_shopping_btn.jpg" id="popupCancel" style="cursor:pointer;">');
                    $("#popupOk").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popupCancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });

                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                    });
                    break;
                case 'wishlist':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_38.jpg" width="291" height="113"/>');
                    $("#popup_ok").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_wishlist_btn.jpg" id="popupOk" style="margin-left:-70px; cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="http://img.wizwell.co.kr/design/images/alot/alot_shopping_btn.jpg" id="popupCancel" style="cursor:pointer;">');
                    $("#popupOk").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popupCancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });

                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                    });
                    break;
                case 'passwordConfirm':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_28.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'orderNo':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_42.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'nameOrderNo_inconsistency':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_43.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'nameEmail_inconsistency':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_44.jpg" width="291" height="113"/>');
                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                        callback(true);
                    });

                    break;
                case 'checkLogin':
                    $("#popup_message").html('<img src="http://img.wizwell.co.kr/Design/images/alot/errorpopup_content_40.jpg" width="291" height="113"/>');
                    $("#popup_ok").html('<img src="http://img.wizwell.co.kr/design/images/alot/alot_yes_btn.jpg" id="popupOk" style="margin-left:-40px; cursor:pointer;">&nbsp;&nbsp;&nbsp;<img src="http://img.wizwell.co.kr/design/images/alot/alot_no_btn.jpg" id="popupCancel" style="cursor:pointer;">');
                    $("#popupOk").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popupCancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });

                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                    });
                    break;
                case 'confirm':
                    $("#popup_message").after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_ok").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });
                    $("#popup_ok").focus();
                    $("#popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    break;
                case 'prompt':
                    $("#popup_message").append('<br /><input type="text" size="30" id="popup_prompt" />').after('<div id="popup_panel"><input type="button" value="' + $.alerts.okButton + '" id="popup_ok" /> <input type="button" value="' + $.alerts.cancelButton + '" id="popup_cancel" /></div>');
                    $("#popup_prompt").width($("#popup_message").width());
                    $("#popup_ok").click(function () {
                        var val = $("#popup_prompt").val();
                        $.alerts._hide();
                        if (callback) callback(val);
                    });
                    $("#popup_cancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(null);
                    });
                    $("#popup_prompt, #popup_ok, #popup_cancel").keypress(function (e) {
                        if (e.keyCode == 13) $("#popup_ok").trigger('click');
                        if (e.keyCode == 27) $("#popup_cancel").trigger('click');
                    });
                    if (value) $("#popup_prompt").val(value);
                    $("#popup_prompt").focus().select();
                    break;
                case 'naver':
                    $("#popupOk").click(function () {
                        $.alerts._hide();
                        if (callback) callback(true);
                    });
                    $("#popupCancel").click(function () {
                        $.alerts._hide();
                        if (callback) callback(false);
                    });

                    $(".popup_ok").click(function () {
                        $.alerts._hide();
                    });
                    break;
            }

            // Make draggable
            if ($.alerts.draggable) {
                try {
                    $("#popup_container").draggable({ handle: $("#popup_title") });
                    $("#popup_title").css({ cursor: 'move' });
                } catch (e) { /* requires jQuery UI draggables */ }
            }
        },

        _hide: function () {
            $("#popup_container").remove();
            $.alerts._overlay('hide');
            $.alerts._maintainPosition(false);
        },

        _overlay: function (status) {
            switch (status) {
                case 'show':
                    $.alerts._overlay('hide');
                    $("BODY").append('<div id="popup_overlay"></div>');
                    $("#popup_overlay").css({
                        position: 'absolute',
                        zIndex: 99998,
                        top: '0px',
                        left: '0px',
                        width: '100%',
                        height: $(window).height() + 'px',
                        background: $.alerts.overlayColor,
                        opacity: $.alerts.overlayOpacity
                    });
                    break;
                case 'hide':
                    $("#popup_overlay").remove();
                    break;
            }
        },

        _reposition: function () {
            var top = ((($(window).height() + 100) / 2) - ($("#popup_container").outerHeight() / 2)) + $.alerts.verticalOffset;
            var left = (($(window).width() / 2) - ($("#popup_container").outerWidth() / 2)) + $.alerts.horizontalOffset;
            if (top < 0) top = 0;
            if (left < 0) left = 0;

            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#popup_container").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#popup_overlay").height($(document).height());
        },

        _maintainPosition: function (status) {
            if ($.alerts.repositionOnResize) {
                switch (status) {
                    case true:
                        $(window).bind('resize', function () {
                            $.alerts._reposition();
                        });
                        break;
                    case false:
                        $(window).unbind('resize');
                        break;
                }
            }
        }

    }

    // Shortuct functions
    jAlert = function (type, message, title, callback) {
        $.alerts.alert(type, message, title, callback);
    }

    jConfirm = function (message, title, callback) {
        $.alerts.confirm(message, title, callback);
    };

    jPrompt = function (message, value, title, callback) {
        $.alerts.prompt(message, value, title, callback);
    };

})(jQuery);
