/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright (c) 2013-2014 Adobe Systems Incorporated. All rights reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4 */
/*global define: true, window: true, document: true*/

define([
    'jquery',
    'underscore',
    'backbone',
    'ccweb.files.extract/utils/TemplateUtil',
    'text!ccweb.files.extract/views/templates/popupTemplate.html',
    'parfait/BasePopupView'
], function ($, _, Backbone, TemplateUtil, PopupTemplate, BasePopupView) {
    'use strict';
    var DwBasePopupView = BasePopupView.extend({
            remove: function () {
                this.undelegateEvents();
                this.$el.removeData().unbind();
                this.$el.empty();
                this.unbind();
                this.stopListening();

                window.graphite.events.off(null, null, this);

                $(window).off("mousedown", $.proxy(this.handleMouseDown, this));
                $(document).off("keyup", $.proxy(this.handleKeyUp, this));

                return Backbone.View.prototype.remove.apply(this, arguments);
            },
        
            positionPopup: function () {
                var $workArea = $('.extract-plugin');
                if ($workArea.length === 0) {
                    $workArea = $('#main');
                }
                var workAreaOffset = document.documentElement.clientHeight;

                var source = this.source;

                // find y offset
                var yOffset = this.$el.parent().offset().top;
                var sourceHeight = source.height() +
                    parseInt(source.css('padding-top'), 10) +
                    parseInt(source.css('padding-bottom'), 10);
                var popupHeight = parseInt(this.$el.height(), 10) +
                    parseInt(this.$el.css('padding-top'), 10) +
                    parseInt(this.$el.css('padding-bottom'), 10);
                var notch = this.$el.find('.notch');
                var $psdHeader = $('.psd-header');

                notch.show();
                var nY = this.getSourceYOffset() - yOffset + sourceHeight;
                if (nY + popupHeight + notch.height() > workAreaOffset - yOffset) {
                    nY = source.offset().top - yOffset - this.$el.height() -
                        (2 * parseInt(this.$el.css('padding-top'), 10));

                    this.$el.addClass('top');
                    this.$el.removeClass('bottom');
                } else {
                    nY += notch.height();
                    this.$el.addClass('bottom');
                    this.$el.removeClass('top');
                }

                // Constrain to not go into the header area.
                if ($psdHeader.length && $psdHeader.offset()) {
                    var topmostY = $psdHeader.position().top + $psdHeader.outerHeight() + 13;
                    if (nY < topmostY) {
                        //Set the top to the bottom of the header area
                        nY = topmostY;
                        notch.hide();
                    }
                }
                this.$el.css('top', nY);

                //find x offset
                var sourceWidth = source.width() +
                    parseInt(source.css('padding-left'), 10) +
                    parseInt(source.css('padding-right'), 10);
                var popupWidth = parseInt(this.$el.width(), 10) +
                    parseInt(this.$el.css('padding-left'), 10) +
                    parseInt(this.$el.css('padding-right'), 10);
                var nX = this.getSourceXOffset() + ((sourceWidth - popupWidth) / 2);
                if (nX + popupWidth > $(window).width() - 5) {
                    nX = $(window).width() - popupWidth - 5;
                }

                this.$el.css('left', nX);

                // center notch to source button
                var notchx = this.getSourceXOffset() - (nX + parseInt(this.$el.css('border-left-width'), 10)) +
                    parseInt(source.css('margin-left'), 10) + ((sourceWidth - notch.width()) / 2);

                notch.css('left', notchx);
            }
        });
    
    return DwBasePopupView;
});
