﻿/*
 * Modal Dialog Functions, version 1.0.0
 *
 * Copyright (c) 2010 by www.lh.co.kr. All right reserved.
 *
 * AUTHOR: C.J.LEE (cjlee@lh.co.kr)
 *
 * NOTE: This script requires jQuery to work. Download jQuery at www.jquery.com
 * 
 * USAGE: 
 * 
 * # Initialize & Show
 *
 * $(object).lhDialog({ top: '', left: '', width:'', height:'', opacity: 0.6, zIndex: 1, closeButtonId: 'vcard_confirm_close' });
 * 
 * # Hide Dialog
 *
 * $.lhDialog.Hide();
 * 
 * 
 * Date: 2010-11-24
 * Last Modify: 2010-11-29 13:26
 *
 */

(function($) {

    ////////////////////
    // Private Variables

    var _config;

    var _content;
    var _position;

    var _width;
    var _height;

    var _overlayLayout;
    var _closeButton;

    /////////////////
    // Public Methods

    $.fn.lhDialog = function(settings) {

        var config = {
            top: null,
            left: null,
            width: null,
            height: null,
            opacity: 0.6,
            zIndex: 1000,
            closeButtonId: "",
            bgClickClose: true //배경 레이어 클릭시 닫기 동작 여부 추가. 110906. 김규민
        };

        if (settings) $.extend(config, settings);

        _config = config;

        //alert($(this).html());

        _content = $(this);

        // 기본 설정
        _initialize();

        // 보여주기
        _open();

        return this;
    };

    /////////////////
    // Public Methods

    $.lhDialog = {
        Hide: function() {
            if (_content) _close();
        }
    }

    //////////////////
    // Private Methods

    var _initialize = function() {

        _width = (_config.width) ? parseInt(_config.width) : _content.width();
        _height = (_config.height) ? parseInt(_config.height) : _content.height();

        //alert(_width + ", " + _height);

        _position = _center(_width, _height);

        //alert(_position[0] + ", " + _position[1]);

        $(document).bind('keydown.lhDialog', function(e) {
            if (e.keyCode == 27) _close();
            return true;
        })

    };

    var _open = function() {

        // Overlay Background
        _overlayLayout = $("<div />")
            .css({
                position: 'absolute',
                margin: '0', padding: '0',
                top: '0', left: '0',
                background: '#222',
                width: '100%',//$(window).width(), //배경 레이어 창늘리면 늘어나게 수정. 110916. 김규민.
                height: $(document).height(),
                opacity: _config.opacity,
                zIndex: _config.zIndex,
                display: "none"
            })
            .bind('click', function(){if(_config.bgClickClose){_close()}})
            .appendTo(document.body)
            .fadeIn(1000);

        // Dialog Close Button
        if (_config.closeButtonId != "") {
            _closeButton = $("#" + _config.closeButtonId)
                .bind("click", _close) //배경 레이어 클릭시 닫기 동작 여부 추가. 110906. 김규민
                .css("cursor", "pointer");
        }

        // Dialog Object
        _content.css({
            position: 'absolute',
            left: _config.left + _position[0] + 'px',
            top: _config.top + _position[1] + 'px',
            width: _width + 'px',
            height: _height + 'px',
            zIndex: _config.zIndex + 1000
        })
        .fadeIn(10);

    };

    var _close = function() {
        _content.hide();
        _overlayLayout.remove();
    };

    var _center = function (w, h) {
        var _t = ($(document).height() > $(window).height()) ? $(window).width()/2 - w/2 - 0 : $(window).width()/2 - w/2;
        var _l = $(document).scrollTop() + $(window).height()/2 - h/2;
        //창보다 레이어가 클때 가려지지 않게 수정. 110424. 김규민
        if(_l< 0){_l=0;}
        //if(_l+_height >_t){_l=50;}
        
        return [_t, _l];
    };

})(jQuery);

