﻿///<reference path="Extjs_Intellisense.js" />

Ext.namespace('LUL');

LUL.NewsPanelHandler = function() {
    this.toggle = function(e) {
        var news = Ext.get(e.target).findParent('div.newsPanelNews');
        var body = Ext.get(news).child('div.newsBody');
        if (body.dom.style.display == 'block')
            body.dom.style.display = 'none';
        else
            body.dom.style.display = 'block';
    }

    this.showAddLink = function() {
        if (loginManager.editAllowed()) {
            Ext.select('.newsPanel .addIcon').on('click', this.quickAdd, this);
            Ext.select('.newsPanel .addIcon').each(function(panel) {
                //var el = Ext.get(panel.dom.id);
                //Ext.DomHelper.append(el, 'hej');
                panel.dom.style.display = 'block';
            });
        }
    }

    this.hideAddLink = function() {
        Ext.select('.newsPanel .addIcon').un('click', this.quickAdd);
        Ext.select('.newsPanel .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.newsPanel');
        // kan vara för tidigt så data inte är laddat?
        if (panel != null) editorManager.editPanel(panel.id, 'add');
    }

    // sätt igång enskild nyhetspanel efter uppdatering
    this.initPanel = function(panel) {
        panel.select('.addIcon').on('click', this.quickAdd, this);
        panel.select('.addIcon').setStyle('display', 'block');
        panel.select('div.newsPanelNews').on('click', this.toggle, this);
    }

    // sätt igång alla nyhetspaneler vid start
    this.init = function() {
        Ext.select('div.newsPanelNews').on('click', this.toggle, this);
        loginManager.on('loggedIn', this.showAddLink, this);
        loginManager.on('loggedOut', this.hideAddLink, this);
    }
}

var newsPanelHandler = new LUL.NewsPanelHandler();

Ext.onReady(function() {
    newsPanelHandler.init();
});
