/**
* @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' : '1000' ,
            '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 closeBtn  = $("<div><a href='javascript:;' class='layer_close'>x</a></div>").css({'float':'right','padding-right' : '5px', 'padding-top' : '3px'});
        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'></div>");        
        var closeBtn  = $("<div><table width='100%' border='0' cellspacing='0' cellpadding='0' style='background-image:url(/images/popup/popup_title_bg.gif); background-repeat:repeat-x'><tr><td><img src='/images/popup/popup_title_logo.gif'/></td><td width='17px' align='right'><a href='#' class='layer_close'><img src='/images/popup/popup_close.gif'/></a>&nbsp;&nbsp;</td></tr></table><div>");
        


        //titleBar.append(title).append(closeBtn);
        //closeBtn.append("<div style='clear:both;'></div>");
        popup.append(closeBtn);
        popup.append(popIframe);
        //popup.append(bottom);
        $("body").prepend(popup);

/*
        //popupTitle
        $('.titleBar').css({  'cursor' : 'move', 'width' : opts.width , 'height' : '25px','background': '#F7B64A none repeat scroll 0 0','border' : '1px solid #E78F08', 'color' : '#FFFFFF'  });
        popup.draggable();
*/        
        $('.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' : 'white' ,
//                'height' : '27px' ,
//                'padding-top' : '5px',
//                'font-size' : '12px'
//            });

//            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 bottom_btnArea = $("<div class='btnArea'><input type='button' value='닫기' id='bottom_close' /></div>");
//            bottom_btnArea.css({
//                'float' : 'right',
//                'padding-right' : '10px'
//            });



//            $("#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' : 'yes' ,
    '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' : ' window '
};

$.extend({
    popup : function (options) {
        return $.fn.popup(options);
    }
});

})(jQuery);