﻿///<reference path="../../Lib/Extjs_Intellisense.js" />

Ext.namespace('LUL');

LUL.LinkPanelHandler = function() {
    this.showAddLink = function() {
        if (loginManager.editAllowed()) {
            
            Ext.select('.linkPanel .addIcon').on('click', this.quickAdd, this);
            Ext.select('.linkPanel .addIcon').each(function(panel) {
                panel.dom.style.display = 'block';
            });
        }
    }

    this.hideAddLink = function() {
        Ext.select('.linkPanel .addIcon').un('click', this.quickAdd);
        Ext.select('.linkPanel .addIcon').each(function(panel) {
            panel.dom.style.display = 'none';
        });
    }

    // öpnna redigeringsläge och lägg till nyhet
    this.quickAdd = function(e) {
        var panel = Ext.get(e.target).findParent('div.linkPanel');
        // kan vara för tidigt så data inte är laddat?
        if (panel != null) editorManager.editPanel(panel.id, 'add');
    }

    // sätt igång enskild länkpanel efter uppdatering
    this.initPanel = function(panel) {
        panel.select('.addIcon').on('click', this.quickAdd, this);
        panel.select('.addIcon').setStyle('display', 'block');
    }

    // sätt igång alla länkpaneler vid start
    this.init = function() {
        loginManager.on('loggedIn', this.showAddLink, this);
        loginManager.on('loggedOut', this.hideAddLink, this);
    }
}

var linkPanelHandler = new LUL.LinkPanelHandler();

Ext.onReady(function() {
    linkPanelHandler.init();
});
