/*
 * 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, graphite: true*/

/* superclass for layers list items that handles selection and visibility */
define([
    'jquery',
    'underscore',
    'backbone',
    'ccweb.files.extract/controllers/SelectionController',
    'ccweb.files.extract/utils/KeyboardUtils',
    'parfait/LayersListItemView'
], function ($, _, Backbone, SelectionController, KeyboardUtils, LayersListItemView) {
    'use strict';
    var DwLayersListItemView = LayersListItemView.extend({
        removeEventListeners: function () {
            this.model.off(null, null, this);
            window.graphite.events.off(null, null, this);
        },
        
        handleSelectionChange: function () {
            if (this.$el.hasClass('selected')) {
                this.$el.removeClass('selected');
                if (this.parentView) {
                    this.parentView.childSelected(false);
                }
            }

            if (this.model.get('selected') === true) {
                this.$el.addClass('selected');
                if (this.model.canBeExtracted()) {
                    this.$el.find('.layer-group-header').first().addClass('show-export');
                }
                if (this.parentView) {
                    this.parentView.childSelected(true);
                    this.parentView.expand();
                }
                this.scrollIntoView();
            } else {
                this.$el.find('.layer-group-header').first().removeClass('show-export');
            }
        },
        
        remove: function () {
            this.removeEventListeners();
            this.$el.removeData().unbind();
            this.$el.empty();
            Backbone.View.prototype.remove.apply(this, arguments);
        }

    });
    
    return DwLayersListItemView;
});