﻿/* ===========================================================
 * bootstrap-popover.js v1.3.0
 * http://twitter.github.com/bootstrap/javascript.html#popover
 * ===========================================================
 * Copyright 2011 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================================================== */


!function ($) {

    var Popover = function (element, options) {
        //스타일 변경을 위해 class option 추가
        this.className = options.className || "popover";
        this.callback = options.callback || function (e) { console.log(e.data) };
        this.$element = $(element)
        this.options = options || "";
        this.element_left = options.left || "";
        this.enabled = true
        this.fixTitle()
    }

    /* NOTE: POPOVER EXTENDS BOOTSTRAP-TWIPSY.js
    ========================================= */
    function maybeCall(thing, ctx, args) {
        return typeof thing == 'function' ? thing.apply(ctx, args) : thing
    }
    Popover.prototype = $.extend({}, $.fn.twipsy.Twipsy.prototype, {
        show: function () {
            var pos
        , actualWidth
        , actualHeight
        , placement
        , $tip
        , tp

            if (this.getTitle() && this.enabled) {
                $tip = this.tip()
                this.setContent()

                if (this.options.animate) {
                    $tip.addClass('fade')
                }

                $tip
          .remove()
          .css({ top: 0, left: 0, display: 'block' })
          .prependTo(document.body)

                pos = $.extend({}, this.$element.offset(), {
                    width: this.$element[0].offsetWidth
        , height: this.$element[0].offsetHeight
                })

                actualWidth = $tip[0].offsetWidth
                actualHeight = $tip[0].offsetHeight
                //pop창의 위치를 조정하기 위해서 수정하였음
                placement = maybeCall(this.options.placement, this, [$tip[0], this.$element[0]])
                if (this.element_left != "") {
                    switch (placement) {
                        case 'below':
                            tp = { top: pos.top + pos.height + this.options.offset, left: (pos.left + this.element_left) + pos.width / 2 - actualWidth / 2 }
                            break
                        case 'above':
                            tp = { top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2 }
                            break
                        case 'left':
                            tp = { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset }
                            break
                        case 'right':
                            tp = { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset }
                            break
                    }
                }
                else {
                    switch (placement) {
                        case 'below':
                            tp = { top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2 }
                            break
                        case 'above':
                            tp = { top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2 }
                            break
                        case 'left':
                            tp = { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset }
                            break
                        case 'right':
                            tp = { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset }
                            break
                    }
                }
                $tip.css(tp).addClass(placement).addClass('in');
            }
        }
    ,
        setContent: function () {
            var $tip = this.tip();
            $tip.find('.title')[this.options.html ? 'html' : 'text'](this.getTitle());
            $tip.find('.content p')[this.options.html ? 'html' : 'text'](this.getContent());
            $tip[0].className = this.className;
        }
          , getContent: function () {
              var content
               , $e = this.$element
               , o = this.options

              if (typeof this.options.content == 'string') {
                  content = $e.attr(o.content)
              } else if (typeof this.options.content == 'function') {
                  content = this.options.content.call(this.$element[0])
              }
              return content
          }
          , tip: function () {
              if (!this.$tip) {
                  this.$tip = $("<div class='" + this.className + "' />").html('<div class="arrow"></div><div class="inner"><h3 class="title" style="display:none"></h3><div class="content"><p></p></div></div>')
              }
              if (this.className == "tooltip-anl") {
                  this.$tip.find(".anl_all").click({ flag: 1 }, this.callback);
                  this.$tip.find(".anl_gubun").click({ flag: 2 }, this.callback);
              }
              return this.$tip;
          }

        // 팝업 잔상이 계속 남는 현상 때문에 $.fn.twipsy 안에 hide함수 오버라이딩
        //transitionEnd 변수가 twipsy쪽에만 선언되어 있어 이곳에서 접근 할 수 있게 생성
        , hide: function () {
            var that = this, $tip = this.tip()
            $tip.removeClass('in');

            function removeElement() {
                if (that.className == "tooltip-anl") {
                    $tip.fadeOut(200);
                }
                else {
                    $tip.remove();
                }
            }
            var transitionEnd;
            if ($.support.transition) {
                transitionEnd = "TransitionEnd"
                if ($.browser.webkit) {
                    transitionEnd = "webkitTransitionEnd"
                } else if ($.browser.mozilla) {
                    transitionEnd = "transitionend"
                } else if ($.browser.opera) {
                    transitionEnd = "oTransitionEnd"
                }
            }
            //!$.browser.mozilla 경우 transitionend event가 버그가 있는듯 제거
            $.support.transition && this.$tip.hasClass('fade') && !$.browser.mozilla ?
                $tip.bind(transitionEnd, removeElement) : removeElement()

        }
    });


    /* POPOVER PLUGIN DEFINITION
    * ======================= */

    $.fn.popover = function (options) {
        if (typeof options == 'object') options = $.extend({}, $.fn.popover.defaults, options)
        $.fn.twipsy.initWith.call(this, options, Popover, 'popover');

        return this
    }

    $.fn.popover.defaults = $.extend({}, $.fn.twipsy.defaults, { content: 'data-content', placement: 'right' })

} (window.jQuery || window.ender)
