/**
* @class
* 팝업을 호출을 편하고 레이어 팝업 호출과 일반 팝업 호출을 같이 사용하도록 하기 위하여  만듬
*
* @example
* 호출 type : layer 입력시 레이어 호출, 'browser' 일반 팝업창, 기본 browser
* var opts  = {'url' : 'index.html', 'width' : '600px', 'height' : '500px', 'left': '100px' , 'top' : '300px', 'type' : 'layer' };
* $.popup(opts);
*
* $('.layerpopup').popup(opts);
*
* @name jquery.popup.sde.js
* @author JsYang <yakuyaku@gmail.com>
* @since 2010년 2월 19일 금요일
* @version 1.0
*/

(function($) {

    $.fn.popup = function(options) {

        var popup, popup_options;
        var $this = $(this);
        var opts = $.extend({}, $.fn.popup.defaults, options);

        if (opts.center) {
            opts.top = $(window).height() / 2 - opts.height.replace('px', '') / 2;
            opts.left = $(window).width() / 2 - opts.width.replace('px', '') / 2;
        }

        //popup options , layer , borwser
        if (opts.type == 'browser') {
            popup_options = "width=" + opts.width + ",height=" + opts.height;
            popup_options += ",left=" + opts.left + ",top=" + opts.top;
            popup_options += ",scrollbars=" + opts.scrollbars + ",toolbar=" + opts.toolbar + ",menubars=" + opts.menubars;
            popup_options += ",locationbar=" + opts.locationbar + ",statusbar=" + opts.statusbar;
            popup_options += ",resizable=" + opts.resizable;
            popup_options += ",titlebar=" + opts.titlebar;
        } else {


            var len = $('.' + opts.name).length;

            if (len > 1) {
                $('.' + opts.name).show();
                return;
            }

            popup = $("<div class='" + opts.name + "'></div>").css({
                'display': 'none',
                'position': 'absolute',
                'z-index': '1',
                'width': opts.width,
                'height': opts.height,
                'left': opts.left,
                'top': opts.top
            });

            //titleBar
            var titleBar = $("<div class='titleBar'></div>");
            var title = $("<div>" + opts.title + "</div>").css({ 'float': 'left', 'padding-left': '0px', 'padding-top': '0px' });
            var popIframe = $("<iframe src='" + opts.url + "' width='100%' height='80%' marginwidth='0' marginheight='0' frameborder='0' scrolling='auto' ></iframe>");
            //var bottom = $("<div id='popup_bottom' style='text-align:right; padding-right:5px;vertical-align:text-bottom;'><span id='bottom_close1'  style='cursor:pointer;'>닫기</span></div>");
            var bottom = $("<div id='popup_bottom'></div>");
            var bottom_msgArea = $("<div class='msgArea'><input type='checkbox' name='checkbox' value='" + opts.bottom_value + "' id='chkPopupCookie' ><span>" + opts.bottom_message + "</span></div>");
            bottom_msgArea.css({
                'float': 'left',
                'padding-left': '5px'
            });
            var win_close = $("<div id='bottom_close'><span style='vertical-align:bottom;'>닫기</span></div>");
            win_close.css({
                'float': 'right',
                'padding-right': '5px',
                'cursor': 'pointer',
                'vertical-align': 'buttom',
                'padding-bottom': '0'
            });

            //var closeBtn = $("<div><table width='100%' border='0' cellspacing='0' cellpadding='0' style='background-color:Silver; background-repeat:repeat-x'><tr><td style='background-color:Silver'>&nbsp;</td><td width='17px' align='right'><a href='#' class='layer_close'><img src='/images/common/btn/bt_del3.gif'/></a>&nbsp;&nbsp;</td></tr></table><div>");
            popup.append(popIframe);
            bottom.append(bottom_msgArea);
            bottom.append(win_close);
            popup.append(bottom);
            $("body").prepend(popup);
            $('.layer_close').click(function() {
                popup.hide();
            }).css({
                'color': '#ffffff',
                'font-size': '14px',
                'line-height': '14px',
                'font-weight': '700',
                'text-decoration': 'none'
            });

            $("#bottom_close").click(function(e) {
                popup.hide();
            });

            if (opts.bottom_display) {
                bottom.css({
                    'width': '100%',
                    'background-color': '#000',
                    'color': '#9c9c9c',
                    'height': '21px',
                    'padding-top': '0px',
                    'font-size': '12px',
                    'vertical-align': 'bottom'
                });

                $("#chkPopupCookie").click(function(e) {
                    setCookie(opts.name, "done", this.value);
                    popup.hide();
                });

                function setCookie(name, value, expires) {
                    var date = new Date();
                    date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000));
                    document.cookie = name + "=" + escape(value) + "; path=/; expires=" + date.toUTCString() + ";"
                }
            }
        }

        var methods = {
            'open': function() {
                if (opts.type == 'browser') {
                    popup = window.open(opts.url, opts.name, popup_options);
                    if (!popup) {
                        alert(opts.message);
                        return false;
                    } else {
                        popup.focus();
                    }
                } else {
                    popup.show();
                }
            },
            'close': function() {
                if (opts.type == 'browser') {
                    popup.close();
                } else {
                    popup.hide();
                }
            },
            'getPopup': function() {
                return pupup;
            }
        }

        // Click Event .
        if ($this.length > 0) {
            $this.css('cursor', 'pointer');
            $this.click(function(e) {
                methods.open();
                e.preventDefault();
            });
        } else {
            methods.open();
        }

        return methods;
    }


    $.fn.popup.defaults = {
        'url': 'index.html',
        'type': 'browser',
        'name': 'popup',
        'width': '300px',
        'height': '300px',
        'scrollbars': 'no',
        'toolbar': 'no',
        'menubars': 'no',
        'locationbar': 'no',
        'statusbar': 'no',
        'resizable': 'no',
        'titlebar': 'no',
        'left': '0px',
        'top': '0px',
        'message': '팝업차단을 해제해주세요.',
        'title': '팝업 ',
        'center': true,
        'bottom_display': true,
        'bottom_value': 1,
        'bottom_message': ' 오늘 하루 이 창을 보지 않기 '
    };

    $.extend({
        popup: function(options) {
            return $.fn.popup(options);
        }
    });

})(jQuery);
